IAudioRendererManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using Ryujinx.Audio;
  2. using Ryujinx.HLE.Logging;
  3. using Ryujinx.HLE.OsHle.Ipc;
  4. using Ryujinx.HLE.OsHle.Services.Aud.AudioRenderer;
  5. using Ryujinx.HLE.OsHle.Utilities;
  6. using System.Collections.Generic;
  7. using static Ryujinx.HLE.OsHle.ErrorCode;
  8. namespace Ryujinx.HLE.OsHle.Services.Aud
  9. {
  10. class IAudioRendererManager : IpcService
  11. {
  12. private const int Rev0Magic = ('R' << 0) |
  13. ('E' << 8) |
  14. ('V' << 16) |
  15. ('0' << 24);
  16. private const int Rev = 4;
  17. public const int RevMagic = Rev0Magic + (Rev << 24);
  18. private Dictionary<int, ServiceProcessRequest> m_Commands;
  19. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  20. public IAudioRendererManager()
  21. {
  22. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  23. {
  24. { 0, OpenAudioRenderer },
  25. { 1, GetAudioRendererWorkBufferSize },
  26. { 2, GetAudioDevice }
  27. };
  28. }
  29. public long OpenAudioRenderer(ServiceCtx Context)
  30. {
  31. IAalOutput AudioOut = Context.Ns.AudioOut;
  32. AudioRendererParameter Params = GetAudioRendererParameter(Context);
  33. MakeObject(Context, new IAudioRenderer(Context.Memory, AudioOut, Params));
  34. return 0;
  35. }
  36. public long GetAudioRendererWorkBufferSize(ServiceCtx Context)
  37. {
  38. AudioRendererParameter Params = GetAudioRendererParameter(Context);
  39. int Revision = (Params.Revision - Rev0Magic) >> 24;
  40. if (Revision <= Rev)
  41. {
  42. bool IsSplitterSupported = Revision >= 3;
  43. long Size;
  44. Size = IntUtils.AlignUp(Params.Unknown8 * 4, 64);
  45. Size += Params.MixCount * 0x400;
  46. Size += (Params.MixCount + 1) * 0x940;
  47. Size += Params.VoiceCount * 0x3F0;
  48. Size += IntUtils.AlignUp((Params.MixCount + 1) * 8, 16);
  49. Size += IntUtils.AlignUp(Params.VoiceCount * 8, 16);
  50. Size += IntUtils.AlignUp(
  51. ((Params.SinkCount + Params.MixCount) * 0x3C0 + Params.SampleCount * 4) *
  52. (Params.Unknown8 + 6), 64);
  53. Size += (Params.SinkCount + Params.MixCount) * 0x2C0;
  54. Size += (Params.EffectCount + Params.VoiceCount * 4) * 0x30 + 0x50;
  55. if (IsSplitterSupported)
  56. {
  57. Size += IntUtils.AlignUp((
  58. NodeStatesGetWorkBufferSize(Params.MixCount + 1) +
  59. EdgeMatrixGetWorkBufferSize(Params.MixCount + 1)), 16);
  60. Size += Params.SplitterDestinationDataCount * 0xE0;
  61. Size += Params.SplitterCount * 0x20;
  62. Size += IntUtils.AlignUp(Params.SplitterDestinationDataCount * 4, 16);
  63. }
  64. Size = Params.EffectCount * 0x4C0 +
  65. Params.SinkCount * 0x170 +
  66. Params.VoiceCount * 0x100 +
  67. IntUtils.AlignUp(Size, 64) + 0x40;
  68. if (Params.PerformanceManagerCount >= 1)
  69. {
  70. Size += (((Params.EffectCount +
  71. Params.SinkCount +
  72. Params.VoiceCount +
  73. Params.MixCount + 1) * 16 + 0x658) *
  74. (Params.PerformanceManagerCount + 1) + 0x13F) & ~0x3FL;
  75. }
  76. Size = (Size + 0x1907D) & ~0xFFFL;
  77. Context.ResponseData.Write(Size);
  78. Context.Ns.Log.PrintDebug(LogClass.ServiceAudio, $"WorkBufferSize is 0x{Size:x16}.");
  79. return 0;
  80. }
  81. else
  82. {
  83. Context.ResponseData.Write(0L);
  84. Context.Ns.Log.PrintWarning(LogClass.ServiceAudio, $"Library Revision 0x{Params.Revision:x8} is not supported!");
  85. return MakeError(ErrorModule.Audio, AudErr.UnsupportedRevision);
  86. }
  87. }
  88. private AudioRendererParameter GetAudioRendererParameter(ServiceCtx Context)
  89. {
  90. AudioRendererParameter Params = new AudioRendererParameter();
  91. Params.SampleRate = Context.RequestData.ReadInt32();
  92. Params.SampleCount = Context.RequestData.ReadInt32();
  93. Params.Unknown8 = Context.RequestData.ReadInt32();
  94. Params.MixCount = Context.RequestData.ReadInt32();
  95. Params.VoiceCount = Context.RequestData.ReadInt32();
  96. Params.SinkCount = Context.RequestData.ReadInt32();
  97. Params.EffectCount = Context.RequestData.ReadInt32();
  98. Params.PerformanceManagerCount = Context.RequestData.ReadInt32();
  99. Params.VoiceDropEnable = Context.RequestData.ReadInt32();
  100. Params.SplitterCount = Context.RequestData.ReadInt32();
  101. Params.SplitterDestinationDataCount = Context.RequestData.ReadInt32();
  102. Params.Unknown2C = Context.RequestData.ReadInt32();
  103. Params.Revision = Context.RequestData.ReadInt32();
  104. return Params;
  105. }
  106. private static int NodeStatesGetWorkBufferSize(int Value)
  107. {
  108. int Result = IntUtils.AlignUp(Value, 64);
  109. if (Result < 0)
  110. {
  111. Result |= 7;
  112. }
  113. return 4 * (Value * Value) + 0x12 * Value + 2 * (Result / 8);
  114. }
  115. private static int EdgeMatrixGetWorkBufferSize(int Value)
  116. {
  117. int Result = IntUtils.AlignUp(Value * Value, 64);
  118. if (Result < 0)
  119. {
  120. Result |= 7;
  121. }
  122. return Result / 8;
  123. }
  124. public long GetAudioDevice(ServiceCtx Context)
  125. {
  126. long UserId = Context.RequestData.ReadInt64();
  127. MakeObject(Context, new IAudioDevice());
  128. return 0;
  129. }
  130. }
  131. }