IAudioOut.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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(9)] // 4.0.0+
  118. // GetAudioOutBufferCount() -> u32
  119. public ResultCode GetAudioOutBufferCount(ServiceCtx context)
  120. {
  121. uint bufferCount = _audioOut.GetBufferCount(_track);
  122. context.ResponseData.Write(bufferCount);
  123. return ResultCode.Success;
  124. }
  125. [Command(10)] // 4.0.0+
  126. // GetAudioOutPlayedSampleCount() -> u64
  127. public ResultCode GetAudioOutPlayedSampleCount(ServiceCtx context)
  128. {
  129. ulong playedSampleCount = _audioOut.GetPlayedSampleCount(_track);
  130. context.ResponseData.Write(playedSampleCount);
  131. return ResultCode.Success;
  132. }
  133. [Command(11)] // 4.0.0+
  134. // FlushAudioOutBuffers() -> b8
  135. public ResultCode FlushAudioOutBuffers(ServiceCtx context)
  136. {
  137. bool heldBuffers = _audioOut.FlushBuffers(_track);
  138. context.ResponseData.Write(heldBuffers);
  139. return ResultCode.Success;
  140. }
  141. [Command(12)] // 6.0.0+
  142. // SetAudioOutVolume(s32)
  143. public ResultCode SetAudioOutVolume(ServiceCtx context)
  144. {
  145. float volume = context.RequestData.ReadSingle();
  146. _audioOut.SetVolume(_track, volume);
  147. return ResultCode.Success;
  148. }
  149. [Command(13)] // 6.0.0+
  150. // GetAudioOutVolume() -> s32
  151. public ResultCode GetAudioOutVolume(ServiceCtx context)
  152. {
  153. float volume = _audioOut.GetVolume(_track);
  154. context.ResponseData.Write(volume);
  155. return ResultCode.Success;
  156. }
  157. public void Dispose()
  158. {
  159. Dispose(true);
  160. }
  161. protected virtual void Dispose(bool disposing)
  162. {
  163. if (disposing)
  164. {
  165. _audioOut.CloseTrack(_track);
  166. }
  167. }
  168. }
  169. }