IAudioOut.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using Ryujinx.Audio;
  2. using Ryujinx.Cpu;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Kernel.Common;
  5. using Ryujinx.HLE.HOS.Kernel.Threading;
  6. using System;
  7. using System.Runtime.InteropServices;
  8. namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager
  9. {
  10. class IAudioOut : IpcService, IDisposable
  11. {
  12. private IAalOutput _audioOut;
  13. private KEvent _releaseEvent;
  14. private int _track;
  15. public IAudioOut(IAalOutput audioOut, KEvent releaseEvent, int track)
  16. {
  17. _audioOut = audioOut;
  18. _releaseEvent = releaseEvent;
  19. _track = track;
  20. }
  21. [Command(0)]
  22. // GetAudioOutState() -> u32 state
  23. public ResultCode GetAudioOutState(ServiceCtx context)
  24. {
  25. context.ResponseData.Write((int)_audioOut.GetState(_track));
  26. return ResultCode.Success;
  27. }
  28. [Command(1)]
  29. // StartAudioOut()
  30. public ResultCode StartAudioOut(ServiceCtx context)
  31. {
  32. _audioOut.Start(_track);
  33. return ResultCode.Success;
  34. }
  35. [Command(2)]
  36. // StopAudioOut()
  37. public ResultCode StopAudioOut(ServiceCtx context)
  38. {
  39. _audioOut.Stop(_track);
  40. return ResultCode.Success;
  41. }
  42. [Command(3)]
  43. // AppendAudioOutBuffer(u64 tag, buffer<nn::audio::AudioOutBuffer, 5>)
  44. public ResultCode AppendAudioOutBuffer(ServiceCtx context)
  45. {
  46. return AppendAudioOutBufferImpl(context, context.Request.SendBuff[0].Position);
  47. }
  48. [Command(4)]
  49. // RegisterBufferEvent() -> handle<copy>
  50. public ResultCode RegisterBufferEvent(ServiceCtx context)
  51. {
  52. if (context.Process.HandleTable.GenerateHandle(_releaseEvent.ReadableEvent, out int handle) != KernelResult.Success)
  53. {
  54. throw new InvalidOperationException("Out of handles!");
  55. }
  56. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  57. return ResultCode.Success;
  58. }
  59. [Command(5)]
  60. // GetReleasedAudioOutBuffer() -> (u32 count, buffer<nn::audio::AudioOutBuffer, 6>)
  61. public ResultCode GetReleasedAudioOutBuffer(ServiceCtx context)
  62. {
  63. long position = context.Request.ReceiveBuff[0].Position;
  64. long size = context.Request.ReceiveBuff[0].Size;
  65. return GetReleasedAudioOutBufferImpl(context, position, size);
  66. }
  67. [Command(6)]
  68. // ContainsAudioOutBuffer(u64 tag) -> b8
  69. public ResultCode ContainsAudioOutBuffer(ServiceCtx context)
  70. {
  71. long tag = context.RequestData.ReadInt64();
  72. context.ResponseData.Write(_audioOut.ContainsBuffer(_track, tag) ? 1 : 0);
  73. return 0;
  74. }
  75. [Command(7)] // 3.0.0+
  76. // AppendAudioOutBufferAuto(u64 tag, buffer<nn::audio::AudioOutBuffer, 0x21>)
  77. public ResultCode AppendAudioOutBufferAuto(ServiceCtx context)
  78. {
  79. (long position, long size) = context.Request.GetBufferType0x21();
  80. return AppendAudioOutBufferImpl(context, position);
  81. }
  82. public ResultCode AppendAudioOutBufferImpl(ServiceCtx context, long position)
  83. {
  84. long tag = context.RequestData.ReadInt64();
  85. AudioOutData data = MemoryHelper.Read<AudioOutData>(
  86. context.Memory,
  87. position);
  88. // NOTE: Assume PCM16 all the time, change if new format are found.
  89. short[] buffer = new short[data.SampleBufferSize / sizeof(short)];
  90. context.Memory.Read((ulong)data.SampleBufferPtr, MemoryMarshal.Cast<short, byte>(buffer));
  91. _audioOut.AppendBuffer(_track, tag, buffer);
  92. return ResultCode.Success;
  93. }
  94. [Command(8)] // 3.0.0+
  95. // GetReleasedAudioOutBufferAuto() -> (u32 count, buffer<nn::audio::AudioOutBuffer, 0x22>)
  96. public ResultCode GetReleasedAudioOutBufferAuto(ServiceCtx context)
  97. {
  98. (long position, long size) = context.Request.GetBufferType0x22();
  99. return GetReleasedAudioOutBufferImpl(context, position, size);
  100. }
  101. public ResultCode GetReleasedAudioOutBufferImpl(ServiceCtx context, long position, long size)
  102. {
  103. uint count = (uint)((ulong)size >> 3);
  104. long[] releasedBuffers = _audioOut.GetReleasedBuffers(_track, (int)count);
  105. for (uint index = 0; index < count; index++)
  106. {
  107. long tag = 0;
  108. if (index < releasedBuffers.Length)
  109. {
  110. tag = releasedBuffers[index];
  111. }
  112. context.Memory.Write((ulong)(position + index * 8), tag);
  113. }
  114. context.ResponseData.Write(releasedBuffers.Length);
  115. return ResultCode.Success;
  116. }
  117. [Command(12)] // 6.0.0+
  118. // SetAudioOutVolume(s32)
  119. public ResultCode SetAudioOutVolume(ServiceCtx context)
  120. {
  121. // Games send a gain value here, so we need to apply it on the current volume value.
  122. float gain = context.RequestData.ReadSingle();
  123. float currentVolume = _audioOut.GetVolume();
  124. float newVolume = Math.Clamp(currentVolume + gain, 0.0f, 1.0f);
  125. _audioOut.SetVolume(newVolume);
  126. return ResultCode.Success;
  127. }
  128. [Command(13)] // 6.0.0+
  129. // GetAudioOutVolume() -> s32
  130. public ResultCode GetAudioOutVolume(ServiceCtx context)
  131. {
  132. float volume = _audioOut.GetVolume();
  133. context.ResponseData.Write(volume);
  134. return ResultCode.Success;
  135. }
  136. public void Dispose()
  137. {
  138. Dispose(true);
  139. }
  140. protected virtual void Dispose(bool disposing)
  141. {
  142. if (disposing)
  143. {
  144. _audioOut.CloseTrack(_track);
  145. }
  146. }
  147. }
  148. }