BehaviourContext.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using System;
  2. using System.Diagnostics;
  3. using static Ryujinx.Audio.Renderer.Common.BehaviourParameter;
  4. namespace Ryujinx.Audio.Renderer.Server
  5. {
  6. /// <summary>
  7. /// Behaviour context.
  8. /// </summary>
  9. /// <remarks>This handles features based on the audio renderer revision provided by the user.</remarks>
  10. public class BehaviourContext
  11. {
  12. /// <summary>
  13. /// The base magic of the Audio Renderer revision.
  14. /// </summary>
  15. public const int BaseRevisionMagic = ('R' << 0) | ('E' << 8) | ('V' << 16) | ('0' << 24);
  16. /// <summary>
  17. /// REV1: first revision.
  18. /// </summary>
  19. public const int Revision1 = 1 << 24;
  20. /// <summary>
  21. /// REV2: Added support for splitter and fix GC-ADPCM context not being provided to the DSP.
  22. /// </summary>
  23. /// <remarks>This was added in system update 2.0.0</remarks>
  24. public const int Revision2 = 2 << 24;
  25. /// <summary>
  26. /// REV3: Incremented the max pre-delay from 150 to 350 for the reverb command and removed the (unused) codec system.
  27. /// </summary>
  28. /// <remarks>This was added in system update 3.0.0</remarks>
  29. public const int Revision3 = 3 << 24;
  30. /// <summary>
  31. /// REV4: Added USB audio device support and incremented the rendering limit percent to 75%.
  32. /// </summary>
  33. /// <remarks>This was added in system update 4.0.0</remarks>
  34. public const int Revision4 = 4 << 24;
  35. /// <summary>
  36. /// REV5: <see cref="Parameter.VoiceInParameter.DecodingBehaviour"/>, <see cref="Parameter.VoiceInParameter.FlushWaveBufferCount"/> were added to voice.
  37. /// A new performance frame format (version 2) was added with support for more information about DSP timing.
  38. /// <see cref="Parameter.RendererInfoOutStatus"/> was added to supply the count of update done sent to the DSP.
  39. /// A new version of the command estimator was added to address timing changes caused by the voice changes.
  40. /// Additionally, the rendering limit percent was incremented to 80%.
  41. ///
  42. /// </summary>
  43. /// <remarks>This was added in system update 6.0.0</remarks>
  44. public const int Revision5 = 5 << 24;
  45. /// <summary>
  46. /// REV6: This fixed a bug in the biquad filter command not clearing up <see cref="Dsp.State.BiquadFilterState"/> with <see cref="Effect.UsageState.New"/> usage state.
  47. /// </summary>
  48. /// <remarks>This was added in system update 6.1.0</remarks>
  49. public const int Revision6 = 6 << 24;
  50. /// <summary>
  51. /// REV7: Client side (finally) doesn't send all the mix client state to the server and can do partial updates.
  52. /// </summary>
  53. /// <remarks>This was added in system update 8.0.0</remarks>
  54. public const int Revision7 = 7 << 24;
  55. /// <summary>
  56. /// REV8:
  57. /// Wavebuffer was changed to support more control over loop (you can now specify where to start and end a loop, and how many times to loop).
  58. /// <see cref="Parameter.VoiceInParameter.SrcQuality"/> was added (see <see cref="Parameter.VoiceInParameter.SampleRateConversionQuality"/> for more info).
  59. /// Final leftovers of the codec system were removed.
  60. /// <see cref="Common.SampleFormat.PcmFloat"/> support was added.
  61. /// A new version of the command estimator was added to address timing changes caused by the voice and command changes.
  62. /// </summary>
  63. /// <remarks>This was added in system update 9.0.0</remarks>
  64. public const int Revision8 = 8 << 24;
  65. /// <summary>
  66. /// REV9:
  67. /// EffectInfo parameters were revisited with a new revision (version 2) allowing more data control between the client and server.
  68. /// A new effect was added: Limiter. This effect is effectively implemented with a DRC while providing statistics on the processing on <see cref="Parameter.EffectOutStatusVersion2"/>.
  69. /// </summary>
  70. /// <remarks>This was added in system update 12.0.0</remarks>
  71. public const int Revision9 = 9 << 24;
  72. /// <summary>
  73. /// REV10:
  74. /// Added Bluetooth audio device support and removed the unused "GetAudioSystemMasterVolumeSetting" audio device API.
  75. /// A new effect was added: Capture. This effect allows the client side to capture audio buffers of a mix.
  76. /// A new command was added for double biquad filters on voices. This is implemented using a direct form 1 (instead of the usual direct form 2).
  77. /// A new version of the command estimator was added to support the new commands.
  78. /// </summary>
  79. /// <remarks>This was added in system update 13.0.0</remarks>
  80. public const int Revision10 = 10 << 24;
  81. /// <summary>
  82. /// REV11:
  83. /// The "legacy" effects (Delay, Reverb and Reverb 3D) were updated to match the standard channel mapping used by the audio renderer.
  84. /// A new effect was added: Compressor. This effect is effectively implemented with a DRC.
  85. /// A new version of the command estimator was added to address timing changes caused by the legacy effects changes.
  86. /// A voice drop parameter was added in 15.0.0: This allows an application to amplify or attenuate the estimated time of DSP commands.
  87. /// </summary>
  88. /// <remarks>This was added in system update 14.0.0 but some changes were made in 15.0.0</remarks>
  89. public const int Revision11 = 11 << 24;
  90. /// <summary>
  91. /// Last revision supported by the implementation.
  92. /// </summary>
  93. public const int LastRevision = Revision11;
  94. /// <summary>
  95. /// Target revision magic supported by the implementation.
  96. /// </summary>
  97. public const int ProcessRevision = BaseRevisionMagic + LastRevision;
  98. /// <summary>
  99. /// Get the revision number from the revision magic.
  100. /// </summary>
  101. /// <param name="revision">The revision magic.</param>
  102. /// <returns>The revision number.</returns>
  103. public static int GetRevisionNumber(int revision) => (revision - BaseRevisionMagic) >> 24;
  104. /// <summary>
  105. /// Current active revision.
  106. /// </summary>
  107. public int UserRevision { get; private set; }
  108. /// <summary>
  109. /// Error storage.
  110. /// </summary>
  111. private ErrorInfo[] _errorInfos;
  112. /// <summary>
  113. /// Current position in the <see cref="_errorInfos"/> array.
  114. /// </summary>
  115. private uint _errorIndex;
  116. /// <summary>
  117. /// Current flags of the <see cref="BehaviourContext"/>.
  118. /// </summary>
  119. private ulong _flags;
  120. /// <summary>
  121. /// Create a new instance of <see cref="BehaviourContext"/>.
  122. /// </summary>
  123. public BehaviourContext()
  124. {
  125. UserRevision = 0;
  126. _errorInfos = new ErrorInfo[Constants.MaxErrorInfos];
  127. _errorIndex = 0;
  128. }
  129. /// <summary>
  130. /// Set the active revision.
  131. /// </summary>
  132. /// <param name="userRevision">The active revision.</param>
  133. public void SetUserRevision(int userRevision)
  134. {
  135. UserRevision = userRevision;
  136. }
  137. /// <summary>
  138. /// Update flags of the <see cref="BehaviourContext"/>.
  139. /// </summary>
  140. /// <param name="flags">The new flags.</param>
  141. public void UpdateFlags(ulong flags)
  142. {
  143. _flags = flags;
  144. }
  145. /// <summary>
  146. /// Check if a given revision is valid/supported.
  147. /// </summary>
  148. /// <param name="revision">The revision magic to check.</param>
  149. /// <returns>Returns true if the given revision is valid/supported</returns>
  150. public static bool CheckValidRevision(int revision)
  151. {
  152. return GetRevisionNumber(revision) <= GetRevisionNumber(ProcessRevision);
  153. }
  154. /// <summary>
  155. /// Check if the given revision is greater than or equal the supported revision.
  156. /// </summary>
  157. /// <param name="revision">The revision magic to check.</param>
  158. /// <param name="supportedRevision">The revision magic of the supported revision.</param>
  159. /// <returns>Returns true if the given revision is greater than or equal the supported revision.</returns>
  160. public static bool CheckFeatureSupported(int revision, int supportedRevision)
  161. {
  162. int revA = GetRevisionNumber(revision);
  163. int revB = GetRevisionNumber(supportedRevision);
  164. if (revA > LastRevision)
  165. {
  166. revA = 1;
  167. }
  168. if (revB > LastRevision)
  169. {
  170. revB = 1;
  171. }
  172. return revA >= revB;
  173. }
  174. /// <summary>
  175. /// Check if the memory pool mapping bypass flag is active.
  176. /// </summary>
  177. /// <returns>True if the memory pool mapping bypass flag is active.</returns>
  178. public bool IsMemoryPoolForceMappingEnabled()
  179. {
  180. return (_flags & 1) != 0;
  181. }
  182. /// <summary>
  183. /// Check if the audio renderer should fix the GC-ADPCM context not being provided to the DSP.
  184. /// </summary>
  185. /// <returns>True if if the audio renderer should fix it.</returns>
  186. public bool IsAdpcmLoopContextBugFixed()
  187. {
  188. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision2);
  189. }
  190. /// <summary>
  191. /// Check if the audio renderer should accept splitters.
  192. /// </summary>
  193. /// <returns>True if the audio renderer should accept splitters.</returns>
  194. public bool IsSplitterSupported()
  195. {
  196. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision2);
  197. }
  198. /// <summary>
  199. /// Check if the audio renderer should use a max pre-delay of 350 instead of 150.
  200. /// </summary>
  201. /// <returns>True if the max pre-delay must be 350.</returns>
  202. public bool IsLongSizePreDelaySupported()
  203. {
  204. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision3);
  205. }
  206. /// <summary>
  207. /// Check if the audio renderer should expose USB audio device.
  208. /// </summary>
  209. /// <returns>True if the audio renderer should expose USB audio device.</returns>
  210. public bool IsAudioUsbDeviceOutputSupported()
  211. {
  212. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision4);
  213. }
  214. /// <summary>
  215. /// Get the percentage allocated to the audio renderer on the DSP for processing.
  216. /// </summary>
  217. /// <returns>The percentage allocated to the audio renderer on the DSP for processing.</returns>
  218. public float GetAudioRendererProcessingTimeLimit()
  219. {
  220. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  221. {
  222. return 0.80f;
  223. }
  224. else if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision4))
  225. {
  226. return 0.75f;
  227. }
  228. return 0.70f;
  229. }
  230. /// <summary>
  231. /// Check if the audio render should support voice flushing.
  232. /// </summary>
  233. /// <returns>True if the audio render should support voice flushing.</returns>
  234. public bool IsFlushVoiceWaveBuffersSupported()
  235. {
  236. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  237. }
  238. /// <summary>
  239. /// Check if the audio renderer should trust the user destination count in <see cref="Splitter.SplitterState.Update(Splitter.SplitterContext, ref Parameter.SplitterInParameter, ReadOnlySpan{byte})"/>.
  240. /// </summary>
  241. /// <returns>True if the audio renderer should trust the user destination count.</returns>
  242. public bool IsSplitterBugFixed()
  243. {
  244. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  245. }
  246. /// <summary>
  247. /// Check if the audio renderer should supply the elapsed frame count to the user when updating.
  248. /// </summary>
  249. /// <returns>True if the audio renderer should supply the elapsed frame count to the user when updating.</returns>
  250. public bool IsElapsedFrameCountSupported()
  251. {
  252. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  253. }
  254. /// <summary>
  255. /// Get the performance metric data format version.
  256. /// </summary>
  257. /// <returns>The performance metric data format version.</returns>
  258. public uint GetPerformanceMetricsDataFormat()
  259. {
  260. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  261. {
  262. return 2;
  263. }
  264. else
  265. {
  266. return 1;
  267. }
  268. }
  269. /// <summary>
  270. /// Check if the audio renderer should support <see cref="Parameter.VoiceInParameter.DecodingBehaviour"/>.
  271. /// </summary>
  272. /// <returns>True if the audio renderer should support <see cref="Parameter.VoiceInParameter.DecodingBehaviour"/>.</returns>
  273. public bool IsDecodingBehaviourFlagSupported()
  274. {
  275. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  276. }
  277. /// <summary>
  278. /// Check if the audio renderer should fix the biquad filter command not clearing up <see cref="Dsp.State.BiquadFilterState"/> with <see cref="Effect.UsageState.New"/> usage state.
  279. /// </summary>
  280. /// <returns>True if the biquad filter state should be cleared.</returns>
  281. public bool IsBiquadFilterEffectStateClearBugFixed()
  282. {
  283. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision6);
  284. }
  285. /// <summary>
  286. /// Check if the audio renderer should accept partial mix updates.
  287. /// </summary>
  288. /// <returns>True if the audio renderer should accept partial mix updates.</returns>
  289. public bool IsMixInParameterDirtyOnlyUpdateSupported()
  290. {
  291. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision7);
  292. }
  293. /// <summary>
  294. /// Check if the audio renderer should use the new wavebuffer format.
  295. /// </summary>
  296. /// <returns>True if the audio renderer should use the new wavebuffer format.</returns>
  297. public bool IsWaveBufferVersion2Supported()
  298. {
  299. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8);
  300. }
  301. /// <summary>
  302. /// Check if the audio renderer should use the new effect info format.
  303. /// </summary>
  304. /// <returns>True if the audio renderer should use the new effect info format.</returns>
  305. public bool IsEffectInfoVersion2Supported()
  306. {
  307. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision9);
  308. }
  309. /// <summary>
  310. /// Check if the audio renderer should use an optimized Biquad Filter (Direct Form 1) in case of two biquad filters are defined on a voice.
  311. /// </summary>
  312. /// <returns>True if the audio renderer should use the optimization.</returns>
  313. public bool IsBiquadFilterGroupedOptimizationSupported()
  314. {
  315. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision10);
  316. }
  317. /// <summary>
  318. /// Check if the audio renderer should support new channel resource mapping for 5.1 on Delay, Reverb and Reverb 3D effects.
  319. /// </summary>
  320. /// <returns>True if the audio renderer support new channel resource mapping for 5.1.</returns>
  321. public bool IsNewEffectChannelMappingSupported()
  322. {
  323. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision11);
  324. }
  325. /// <summary>
  326. /// Get the version of the <see cref="ICommandProcessingTimeEstimator"/>.
  327. /// </summary>
  328. /// <returns>The version of the <see cref="ICommandProcessingTimeEstimator"/>.</returns>
  329. public int GetCommandProcessingTimeEstimatorVersion()
  330. {
  331. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision11))
  332. {
  333. return 5;
  334. }
  335. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision10))
  336. {
  337. return 4;
  338. }
  339. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8))
  340. {
  341. return 3;
  342. }
  343. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  344. {
  345. return 2;
  346. }
  347. return 1;
  348. }
  349. /// <summary>
  350. /// Append a new <see cref="ErrorInfo"/> to the error array.
  351. /// </summary>
  352. /// <param name="errorInfo">The new <see cref="ErrorInfo"/> to add.</param>
  353. public void AppendError(ref ErrorInfo errorInfo)
  354. {
  355. Debug.Assert(errorInfo.ErrorCode == ResultCode.Success);
  356. if (_errorIndex <= Constants.MaxErrorInfos - 1)
  357. {
  358. _errorInfos[_errorIndex++] = errorInfo;
  359. }
  360. }
  361. /// <summary>
  362. /// Copy the internal <see cref="ErrorInfo"/> array to the given <see cref="Span{ErrorInfo}"/> and output the count copied.
  363. /// </summary>
  364. /// <param name="errorInfos">The output <see cref="Span{ErrorInfo}"/>.</param>
  365. /// <param name="errorCount">The output error count containing the count of <see cref="ErrorInfo"/> copied.</param>
  366. public void CopyErrorInfo(Span<ErrorInfo> errorInfos, out uint errorCount)
  367. {
  368. if (errorInfos.Length != Constants.MaxErrorInfos)
  369. {
  370. throw new ArgumentException("Invalid size of errorInfos span!");
  371. }
  372. errorCount = Math.Min(_errorIndex, Constants.MaxErrorInfos);
  373. for (int i = 0; i < Constants.MaxErrorInfos; i++)
  374. {
  375. if (i < errorCount)
  376. {
  377. errorInfos[i] = _errorInfos[i];
  378. }
  379. else
  380. {
  381. errorInfos[i] = new ErrorInfo
  382. {
  383. ErrorCode = 0,
  384. ExtraErrorInfo = 0
  385. };
  386. }
  387. }
  388. }
  389. /// <summary>
  390. /// Clear the <see cref="ErrorInfo"/> array.
  391. /// </summary>
  392. public void ClearError()
  393. {
  394. _errorIndex = 0;
  395. }
  396. }
  397. }