IAudioOut.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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));
  73. return ResultCode.Success;
  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, _) = 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>(context.Memory, position);
  86. // NOTE: Assume PCM16 all the time, change if new format are found.
  87. short[] buffer = new short[data.SampleBufferSize / sizeof(short)];
  88. context.Memory.Read((ulong)data.SampleBufferPtr, MemoryMarshal.Cast<short, byte>(buffer));
  89. _audioOut.AppendBuffer(_track, tag, buffer);
  90. return ResultCode.Success;
  91. }
  92. [Command(8)] // 3.0.0+
  93. // GetReleasedAudioOutBufferAuto() -> (u32 count, buffer<nn::audio::AudioOutBuffer, 0x22>)
  94. public ResultCode GetReleasedAudioOutBufferAuto(ServiceCtx context)
  95. {
  96. (long position, long size) = context.Request.GetBufferType0x22();
  97. return GetReleasedAudioOutBufferImpl(context, position, size);
  98. }
  99. public ResultCode GetReleasedAudioOutBufferImpl(ServiceCtx context, long position, long size)
  100. {
  101. uint count = (uint)((ulong)size >> 3);
  102. long[] releasedBuffers = _audioOut.GetReleasedBuffers(_track, (int)count);
  103. for (uint index = 0; index < count; index++)
  104. {
  105. long tag = 0;
  106. if (index < releasedBuffers.Length)
  107. {
  108. tag = releasedBuffers[index];
  109. }
  110. context.Memory.Write((ulong)(position + index * 8), tag);
  111. }
  112. context.ResponseData.Write(releasedBuffers.Length);
  113. return ResultCode.Success;
  114. }
  115. [Command(9)] // 4.0.0+
  116. // GetAudioOutBufferCount() -> u32
  117. public ResultCode GetAudioOutBufferCount(ServiceCtx context)
  118. {
  119. uint bufferCount = _audioOut.GetBufferCount(_track);
  120. context.ResponseData.Write(bufferCount);
  121. return ResultCode.Success;
  122. }
  123. [Command(10)] // 4.0.0+
  124. // GetAudioOutPlayedSampleCount() -> u64
  125. public ResultCode GetAudioOutPlayedSampleCount(ServiceCtx context)
  126. {
  127. ulong playedSampleCount = _audioOut.GetPlayedSampleCount(_track);
  128. context.ResponseData.Write(playedSampleCount);
  129. return ResultCode.Success;
  130. }
  131. [Command(11)] // 4.0.0+
  132. // FlushAudioOutBuffers() -> b8
  133. public ResultCode FlushAudioOutBuffers(ServiceCtx context)
  134. {
  135. bool heldBuffers = _audioOut.FlushBuffers(_track);
  136. context.ResponseData.Write(heldBuffers);
  137. return ResultCode.Success;
  138. }
  139. [Command(12)] // 6.0.0+
  140. // SetAudioOutVolume(s32)
  141. public ResultCode SetAudioOutVolume(ServiceCtx context)
  142. {
  143. float volume = context.RequestData.ReadSingle();
  144. _audioOut.SetVolume(_track, volume);
  145. return ResultCode.Success;
  146. }
  147. [Command(13)] // 6.0.0+
  148. // GetAudioOutVolume() -> s32
  149. public ResultCode GetAudioOutVolume(ServiceCtx context)
  150. {
  151. float volume = _audioOut.GetVolume(_track);
  152. context.ResponseData.Write(volume);
  153. return ResultCode.Success;
  154. }
  155. public void Dispose()
  156. {
  157. Dispose(true);
  158. }
  159. protected virtual void Dispose(bool disposing)
  160. {
  161. if (disposing)
  162. {
  163. _audioOut.CloseTrack(_track);
  164. }
  165. }
  166. }
  167. }