IAudioOut.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 readonly IAalOutput _audioOut;
  13. private readonly KEvent _releaseEvent;
  14. private int _releaseEventHandle;
  15. private readonly int _track;
  16. private readonly int _clientHandle;
  17. public IAudioOut(IAalOutput audioOut, KEvent releaseEvent, int track, int clientHandle)
  18. {
  19. _audioOut = audioOut;
  20. _releaseEvent = releaseEvent;
  21. _track = track;
  22. _clientHandle = clientHandle;
  23. }
  24. [Command(0)]
  25. // GetAudioOutState() -> u32 state
  26. public ResultCode GetAudioOutState(ServiceCtx context)
  27. {
  28. context.ResponseData.Write((int)_audioOut.GetState(_track));
  29. return ResultCode.Success;
  30. }
  31. [Command(1)]
  32. // StartAudioOut()
  33. public ResultCode StartAudioOut(ServiceCtx context)
  34. {
  35. _audioOut.Start(_track);
  36. return ResultCode.Success;
  37. }
  38. [Command(2)]
  39. // StopAudioOut()
  40. public ResultCode StopAudioOut(ServiceCtx context)
  41. {
  42. _audioOut.Stop(_track);
  43. return ResultCode.Success;
  44. }
  45. [Command(3)]
  46. // AppendAudioOutBuffer(u64 tag, buffer<nn::audio::AudioOutBuffer, 5>)
  47. public ResultCode AppendAudioOutBuffer(ServiceCtx context)
  48. {
  49. return AppendAudioOutBufferImpl(context, context.Request.SendBuff[0].Position);
  50. }
  51. [Command(4)]
  52. // RegisterBufferEvent() -> handle<copy>
  53. public ResultCode RegisterBufferEvent(ServiceCtx context)
  54. {
  55. if (_releaseEventHandle == 0)
  56. {
  57. if (context.Process.HandleTable.GenerateHandle(_releaseEvent.ReadableEvent, out _releaseEventHandle) != KernelResult.Success)
  58. {
  59. throw new InvalidOperationException("Out of handles!");
  60. }
  61. }
  62. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_releaseEventHandle);
  63. return ResultCode.Success;
  64. }
  65. [Command(5)]
  66. // GetReleasedAudioOutBuffer() -> (u32 count, buffer<nn::audio::AudioOutBuffer, 6>)
  67. public ResultCode GetReleasedAudioOutBuffer(ServiceCtx context)
  68. {
  69. long position = context.Request.ReceiveBuff[0].Position;
  70. long size = context.Request.ReceiveBuff[0].Size;
  71. return GetReleasedAudioOutBufferImpl(context, position, size);
  72. }
  73. [Command(6)]
  74. // ContainsAudioOutBuffer(u64 tag) -> b8
  75. public ResultCode ContainsAudioOutBuffer(ServiceCtx context)
  76. {
  77. long tag = context.RequestData.ReadInt64();
  78. context.ResponseData.Write(_audioOut.ContainsBuffer(_track, tag));
  79. return ResultCode.Success;
  80. }
  81. [Command(7)] // 3.0.0+
  82. // AppendAudioOutBufferAuto(u64 tag, buffer<nn::audio::AudioOutBuffer, 0x21>)
  83. public ResultCode AppendAudioOutBufferAuto(ServiceCtx context)
  84. {
  85. (long position, _) = context.Request.GetBufferType0x21();
  86. return AppendAudioOutBufferImpl(context, position);
  87. }
  88. public ResultCode AppendAudioOutBufferImpl(ServiceCtx context, long position)
  89. {
  90. long tag = context.RequestData.ReadInt64();
  91. AudioOutData data = MemoryHelper.Read<AudioOutData>(context.Memory, position);
  92. // NOTE: Assume PCM16 all the time, change if new format are found.
  93. short[] buffer = new short[data.SampleBufferSize / sizeof(short)];
  94. context.Process.HandleTable.GetKProcess(_clientHandle).CpuMemory.Read((ulong)data.SampleBufferPtr, MemoryMarshal.Cast<short, byte>(buffer));
  95. _audioOut.AppendBuffer(_track, tag, buffer);
  96. return ResultCode.Success;
  97. }
  98. [Command(8)] // 3.0.0+
  99. // GetReleasedAudioOutBufferAuto() -> (u32 count, buffer<nn::audio::AudioOutBuffer, 0x22>)
  100. public ResultCode GetReleasedAudioOutBufferAuto(ServiceCtx context)
  101. {
  102. (long position, long size) = context.Request.GetBufferType0x22();
  103. return GetReleasedAudioOutBufferImpl(context, position, size);
  104. }
  105. public ResultCode GetReleasedAudioOutBufferImpl(ServiceCtx context, long position, long size)
  106. {
  107. uint count = (uint)((ulong)size >> 3);
  108. long[] releasedBuffers = _audioOut.GetReleasedBuffers(_track, (int)count);
  109. for (uint index = 0; index < count; index++)
  110. {
  111. long tag = 0;
  112. if (index < releasedBuffers.Length)
  113. {
  114. tag = releasedBuffers[index];
  115. }
  116. context.Memory.Write((ulong)(position + index * 8), tag);
  117. }
  118. context.ResponseData.Write(releasedBuffers.Length);
  119. return ResultCode.Success;
  120. }
  121. [Command(9)] // 4.0.0+
  122. // GetAudioOutBufferCount() -> u32
  123. public ResultCode GetAudioOutBufferCount(ServiceCtx context)
  124. {
  125. uint bufferCount = _audioOut.GetBufferCount(_track);
  126. context.ResponseData.Write(bufferCount);
  127. return ResultCode.Success;
  128. }
  129. [Command(10)] // 4.0.0+
  130. // GetAudioOutPlayedSampleCount() -> u64
  131. public ResultCode GetAudioOutPlayedSampleCount(ServiceCtx context)
  132. {
  133. ulong playedSampleCount = _audioOut.GetPlayedSampleCount(_track);
  134. context.ResponseData.Write(playedSampleCount);
  135. return ResultCode.Success;
  136. }
  137. [Command(11)] // 4.0.0+
  138. // FlushAudioOutBuffers() -> b8
  139. public ResultCode FlushAudioOutBuffers(ServiceCtx context)
  140. {
  141. bool heldBuffers = _audioOut.FlushBuffers(_track);
  142. context.ResponseData.Write(heldBuffers);
  143. return ResultCode.Success;
  144. }
  145. [Command(12)] // 6.0.0+
  146. // SetAudioOutVolume(s32)
  147. public ResultCode SetAudioOutVolume(ServiceCtx context)
  148. {
  149. float volume = context.RequestData.ReadSingle();
  150. _audioOut.SetVolume(_track, volume);
  151. return ResultCode.Success;
  152. }
  153. [Command(13)] // 6.0.0+
  154. // GetAudioOutVolume() -> s32
  155. public ResultCode GetAudioOutVolume(ServiceCtx context)
  156. {
  157. float volume = _audioOut.GetVolume(_track);
  158. context.ResponseData.Write(volume);
  159. return ResultCode.Success;
  160. }
  161. public void Dispose()
  162. {
  163. Dispose(true);
  164. }
  165. protected virtual void Dispose(bool disposing)
  166. {
  167. if (disposing)
  168. {
  169. _audioOut.CloseTrack(_track);
  170. }
  171. }
  172. }
  173. }