IAudioRenderer.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Audio;
  3. using Ryujinx.Audio.Adpcm;
  4. using Ryujinx.HLE.HOS.Ipc;
  5. using Ryujinx.HLE.HOS.Kernel;
  6. using Ryujinx.HLE.Logging;
  7. using Ryujinx.HLE.Utilities;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Runtime.InteropServices;
  11. namespace Ryujinx.HLE.HOS.Services.Aud.AudioRenderer
  12. {
  13. class IAudioRenderer : IpcService, IDisposable
  14. {
  15. //This is the amount of samples that are going to be appended
  16. //each time that RequestUpdateAudioRenderer is called. Ideally,
  17. //this value shouldn't be neither too small (to avoid the player
  18. //starving due to running out of samples) or too large (to avoid
  19. //high latency).
  20. private const int MixBufferSamplesCount = 960;
  21. private Dictionary<int, ServiceProcessRequest> m_Commands;
  22. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  23. private KEvent UpdateEvent;
  24. private AMemory Memory;
  25. private IAalOutput AudioOut;
  26. private AudioRendererParameter Params;
  27. private MemoryPoolContext[] MemoryPools;
  28. private VoiceContext[] Voices;
  29. private int Track;
  30. public IAudioRenderer(
  31. Horizon System,
  32. AMemory Memory,
  33. IAalOutput AudioOut,
  34. AudioRendererParameter Params)
  35. {
  36. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  37. {
  38. { 4, RequestUpdateAudioRenderer },
  39. { 5, StartAudioRenderer },
  40. { 6, StopAudioRenderer },
  41. { 7, QuerySystemEvent }
  42. };
  43. UpdateEvent = new KEvent(System);
  44. this.Memory = Memory;
  45. this.AudioOut = AudioOut;
  46. this.Params = Params;
  47. Track = AudioOut.OpenTrack(
  48. AudioConsts.HostSampleRate,
  49. AudioConsts.HostChannelsCount,
  50. AudioCallback);
  51. MemoryPools = CreateArray<MemoryPoolContext>(Params.EffectCount + Params.VoiceCount * 4);
  52. Voices = CreateArray<VoiceContext>(Params.VoiceCount);
  53. InitializeAudioOut();
  54. }
  55. private void AudioCallback()
  56. {
  57. UpdateEvent.ReadableEvent.Signal();
  58. }
  59. private static T[] CreateArray<T>(int Size) where T : new()
  60. {
  61. T[] Output = new T[Size];
  62. for (int Index = 0; Index < Size; Index++)
  63. {
  64. Output[Index] = new T();
  65. }
  66. return Output;
  67. }
  68. private void InitializeAudioOut()
  69. {
  70. AppendMixedBuffer(0);
  71. AppendMixedBuffer(1);
  72. AppendMixedBuffer(2);
  73. AudioOut.Start(Track);
  74. }
  75. public long RequestUpdateAudioRenderer(ServiceCtx Context)
  76. {
  77. long OutputPosition = Context.Request.ReceiveBuff[0].Position;
  78. long OutputSize = Context.Request.ReceiveBuff[0].Size;
  79. AMemoryHelper.FillWithZeros(Context.Memory, OutputPosition, (int)OutputSize);
  80. long InputPosition = Context.Request.SendBuff[0].Position;
  81. StructReader Reader = new StructReader(Context.Memory, InputPosition);
  82. StructWriter Writer = new StructWriter(Context.Memory, OutputPosition);
  83. UpdateDataHeader InputHeader = Reader.Read<UpdateDataHeader>();
  84. Reader.Read<BehaviorIn>(InputHeader.BehaviorSize);
  85. MemoryPoolIn[] MemoryPoolsIn = Reader.Read<MemoryPoolIn>(InputHeader.MemoryPoolSize);
  86. for (int Index = 0; Index < MemoryPoolsIn.Length; Index++)
  87. {
  88. MemoryPoolIn MemoryPool = MemoryPoolsIn[Index];
  89. if (MemoryPool.State == MemoryPoolState.RequestAttach)
  90. {
  91. MemoryPools[Index].OutStatus.State = MemoryPoolState.Attached;
  92. }
  93. else if (MemoryPool.State == MemoryPoolState.RequestDetach)
  94. {
  95. MemoryPools[Index].OutStatus.State = MemoryPoolState.Detached;
  96. }
  97. }
  98. Reader.Read<VoiceChannelResourceIn>(InputHeader.VoiceResourceSize);
  99. VoiceIn[] VoicesIn = Reader.Read<VoiceIn>(InputHeader.VoiceSize);
  100. for (int Index = 0; Index < VoicesIn.Length; Index++)
  101. {
  102. VoiceIn Voice = VoicesIn[Index];
  103. VoiceContext VoiceCtx = Voices[Index];
  104. VoiceCtx.SetAcquireState(Voice.Acquired != 0);
  105. if (Voice.Acquired == 0)
  106. {
  107. continue;
  108. }
  109. if (Voice.FirstUpdate != 0)
  110. {
  111. VoiceCtx.AdpcmCtx = GetAdpcmDecoderContext(
  112. Voice.AdpcmCoeffsPosition,
  113. Voice.AdpcmCoeffsSize);
  114. VoiceCtx.SampleFormat = Voice.SampleFormat;
  115. VoiceCtx.SampleRate = Voice.SampleRate;
  116. VoiceCtx.ChannelsCount = Voice.ChannelsCount;
  117. VoiceCtx.SetBufferIndex(Voice.BaseWaveBufferIndex);
  118. }
  119. VoiceCtx.WaveBuffers[0] = Voice.WaveBuffer0;
  120. VoiceCtx.WaveBuffers[1] = Voice.WaveBuffer1;
  121. VoiceCtx.WaveBuffers[2] = Voice.WaveBuffer2;
  122. VoiceCtx.WaveBuffers[3] = Voice.WaveBuffer3;
  123. VoiceCtx.Volume = Voice.Volume;
  124. VoiceCtx.PlayState = Voice.PlayState;
  125. }
  126. UpdateAudio();
  127. UpdateDataHeader OutputHeader = new UpdateDataHeader();
  128. int UpdateHeaderSize = Marshal.SizeOf<UpdateDataHeader>();
  129. OutputHeader.Revision = IAudioRendererManager.RevMagic;
  130. OutputHeader.BehaviorSize = 0xb0;
  131. OutputHeader.MemoryPoolSize = (Params.EffectCount + Params.VoiceCount * 4) * 0x10;
  132. OutputHeader.VoiceSize = Params.VoiceCount * 0x10;
  133. OutputHeader.EffectSize = Params.EffectCount * 0x10;
  134. OutputHeader.SinkSize = Params.SinkCount * 0x20;
  135. OutputHeader.PerformanceManagerSize = 0x10;
  136. OutputHeader.TotalSize = UpdateHeaderSize +
  137. OutputHeader.BehaviorSize +
  138. OutputHeader.MemoryPoolSize +
  139. OutputHeader.VoiceSize +
  140. OutputHeader.EffectSize +
  141. OutputHeader.SinkSize +
  142. OutputHeader.PerformanceManagerSize;
  143. Writer.Write(OutputHeader);
  144. foreach (MemoryPoolContext MemoryPool in MemoryPools)
  145. {
  146. Writer.Write(MemoryPool.OutStatus);
  147. }
  148. foreach (VoiceContext Voice in Voices)
  149. {
  150. Writer.Write(Voice.OutStatus);
  151. }
  152. return 0;
  153. }
  154. public long StartAudioRenderer(ServiceCtx Context)
  155. {
  156. Context.Device.Log.PrintStub(LogClass.ServiceAudio, "Stubbed.");
  157. return 0;
  158. }
  159. public long StopAudioRenderer(ServiceCtx Context)
  160. {
  161. Context.Device.Log.PrintStub(LogClass.ServiceAudio, "Stubbed.");
  162. return 0;
  163. }
  164. public long QuerySystemEvent(ServiceCtx Context)
  165. {
  166. if (Context.Process.HandleTable.GenerateHandle(UpdateEvent.ReadableEvent, out int Handle) != KernelResult.Success)
  167. {
  168. throw new InvalidOperationException("Out of handles!");
  169. }
  170. Context.Response.HandleDesc = IpcHandleDesc.MakeCopy(Handle);
  171. return 0;
  172. }
  173. private AdpcmDecoderContext GetAdpcmDecoderContext(long Position, long Size)
  174. {
  175. if (Size == 0)
  176. {
  177. return null;
  178. }
  179. AdpcmDecoderContext Context = new AdpcmDecoderContext();
  180. Context.Coefficients = new short[Size >> 1];
  181. for (int Offset = 0; Offset < Size; Offset += 2)
  182. {
  183. Context.Coefficients[Offset >> 1] = Memory.ReadInt16(Position + Offset);
  184. }
  185. return Context;
  186. }
  187. private void UpdateAudio()
  188. {
  189. long[] Released = AudioOut.GetReleasedBuffers(Track, 2);
  190. for (int Index = 0; Index < Released.Length; Index++)
  191. {
  192. AppendMixedBuffer(Released[Index]);
  193. }
  194. }
  195. private void AppendMixedBuffer(long Tag)
  196. {
  197. int[] MixBuffer = new int[MixBufferSamplesCount * AudioConsts.HostChannelsCount];
  198. foreach (VoiceContext Voice in Voices)
  199. {
  200. if (!Voice.Playing)
  201. {
  202. continue;
  203. }
  204. int OutOffset = 0;
  205. int PendingSamples = MixBufferSamplesCount;
  206. while (PendingSamples > 0)
  207. {
  208. int[] Samples = Voice.GetBufferData(Memory, PendingSamples, out int ReturnedSamples);
  209. if (ReturnedSamples == 0)
  210. {
  211. break;
  212. }
  213. PendingSamples -= ReturnedSamples;
  214. for (int Offset = 0; Offset < Samples.Length; Offset++)
  215. {
  216. int Sample = (int)(Samples[Offset] * Voice.Volume);
  217. MixBuffer[OutOffset++] += Sample;
  218. }
  219. }
  220. }
  221. AudioOut.AppendBuffer(Track, Tag, GetFinalBuffer(MixBuffer));
  222. }
  223. private static short[] GetFinalBuffer(int[] Buffer)
  224. {
  225. short[] Output = new short[Buffer.Length];
  226. for (int Offset = 0; Offset < Buffer.Length; Offset++)
  227. {
  228. Output[Offset] = DspUtils.Saturate(Buffer[Offset]);
  229. }
  230. return Output;
  231. }
  232. public void Dispose()
  233. {
  234. Dispose(true);
  235. }
  236. protected virtual void Dispose(bool Disposing)
  237. {
  238. if (Disposing)
  239. {
  240. AudioOut.CloseTrack(Track);
  241. }
  242. }
  243. }
  244. }