IAudioRenderer.cs 10.0 KB

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