BehaviourContext.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 version of the command estimator was added to address timing changes caused by the legacy effects changes.
  85. /// </summary>
  86. /// <remarks>This was added in system update 14.0.0</remarks>
  87. public const int Revision11 = 11 << 24;
  88. /// <summary>
  89. /// Last revision supported by the implementation.
  90. /// </summary>
  91. public const int LastRevision = Revision11;
  92. /// <summary>
  93. /// Target revision magic supported by the implementation.
  94. /// </summary>
  95. public const int ProcessRevision = BaseRevisionMagic + LastRevision;
  96. /// <summary>
  97. /// Get the revision number from the revision magic.
  98. /// </summary>
  99. /// <param name="revision">The revision magic.</param>
  100. /// <returns>The revision number.</returns>
  101. public static int GetRevisionNumber(int revision) => (revision - BaseRevisionMagic) >> 24;
  102. /// <summary>
  103. /// Current active revision.
  104. /// </summary>
  105. public int UserRevision { get; private set; }
  106. /// <summary>
  107. /// Error storage.
  108. /// </summary>
  109. private ErrorInfo[] _errorInfos;
  110. /// <summary>
  111. /// Current position in the <see cref="_errorInfos"/> array.
  112. /// </summary>
  113. private uint _errorIndex;
  114. /// <summary>
  115. /// Current flags of the <see cref="BehaviourContext"/>.
  116. /// </summary>
  117. private ulong _flags;
  118. /// <summary>
  119. /// Create a new instance of <see cref="BehaviourContext"/>.
  120. /// </summary>
  121. public BehaviourContext()
  122. {
  123. UserRevision = 0;
  124. _errorInfos = new ErrorInfo[Constants.MaxErrorInfos];
  125. _errorIndex = 0;
  126. }
  127. /// <summary>
  128. /// Set the active revision.
  129. /// </summary>
  130. /// <param name="userRevision">The active revision.</param>
  131. public void SetUserRevision(int userRevision)
  132. {
  133. UserRevision = userRevision;
  134. }
  135. /// <summary>
  136. /// Update flags of the <see cref="BehaviourContext"/>.
  137. /// </summary>
  138. /// <param name="flags">The new flags.</param>
  139. public void UpdateFlags(ulong flags)
  140. {
  141. _flags = flags;
  142. }
  143. /// <summary>
  144. /// Check if a given revision is valid/supported.
  145. /// </summary>
  146. /// <param name="revision">The revision magic to check.</param>
  147. /// <returns>Returns true if the given revision is valid/supported</returns>
  148. public static bool CheckValidRevision(int revision)
  149. {
  150. return GetRevisionNumber(revision) <= GetRevisionNumber(ProcessRevision);
  151. }
  152. /// <summary>
  153. /// Check if the given revision is greater than or equal the supported revision.
  154. /// </summary>
  155. /// <param name="revision">The revision magic to check.</param>
  156. /// <param name="supportedRevision">The revision magic of the supported revision.</param>
  157. /// <returns>Returns true if the given revision is greater than or equal the supported revision.</returns>
  158. public static bool CheckFeatureSupported(int revision, int supportedRevision)
  159. {
  160. int revA = GetRevisionNumber(revision);
  161. int revB = GetRevisionNumber(supportedRevision);
  162. if (revA > LastRevision)
  163. {
  164. revA = 1;
  165. }
  166. if (revB > LastRevision)
  167. {
  168. revB = 1;
  169. }
  170. return revA >= revB;
  171. }
  172. /// <summary>
  173. /// Check if the memory pool mapping bypass flag is active.
  174. /// </summary>
  175. /// <returns>True if the memory pool mapping bypass flag is active.</returns>
  176. public bool IsMemoryPoolForceMappingEnabled()
  177. {
  178. return (_flags & 1) != 0;
  179. }
  180. /// <summary>
  181. /// Check if the audio renderer should fix the GC-ADPCM context not being provided to the DSP.
  182. /// </summary>
  183. /// <returns>True if if the audio renderer should fix it.</returns>
  184. public bool IsAdpcmLoopContextBugFixed()
  185. {
  186. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision2);
  187. }
  188. /// <summary>
  189. /// Check if the audio renderer should accept splitters.
  190. /// </summary>
  191. /// <returns>True if the audio renderer should accept splitters.</returns>
  192. public bool IsSplitterSupported()
  193. {
  194. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision2);
  195. }
  196. /// <summary>
  197. /// Check if the audio renderer should use a max pre-delay of 350 instead of 150.
  198. /// </summary>
  199. /// <returns>True if the max pre-delay must be 350.</returns>
  200. public bool IsLongSizePreDelaySupported()
  201. {
  202. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision3);
  203. }
  204. /// <summary>
  205. /// Check if the audio renderer should expose USB audio device.
  206. /// </summary>
  207. /// <returns>True if the audio renderer should expose USB audio device.</returns>
  208. public bool IsAudioUsbDeviceOutputSupported()
  209. {
  210. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision4);
  211. }
  212. /// <summary>
  213. /// Get the percentage allocated to the audio renderer on the DSP for processing.
  214. /// </summary>
  215. /// <returns>The percentage allocated to the audio renderer on the DSP for processing.</returns>
  216. public float GetAudioRendererProcessingTimeLimit()
  217. {
  218. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  219. {
  220. return 0.80f;
  221. }
  222. else if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision4))
  223. {
  224. return 0.75f;
  225. }
  226. return 0.70f;
  227. }
  228. /// <summary>
  229. /// Check if the audio render should support voice flushing.
  230. /// </summary>
  231. /// <returns>True if the audio render should support voice flushing.</returns>
  232. public bool IsFlushVoiceWaveBuffersSupported()
  233. {
  234. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  235. }
  236. /// <summary>
  237. /// Check if the audio renderer should trust the user destination count in <see cref="Splitter.SplitterState.Update(Splitter.SplitterContext, ref Parameter.SplitterInParameter, ReadOnlySpan{byte})"/>.
  238. /// </summary>
  239. /// <returns>True if the audio renderer should trust the user destination count.</returns>
  240. public bool IsSplitterBugFixed()
  241. {
  242. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  243. }
  244. /// <summary>
  245. /// Check if the audio renderer should supply the elapsed frame count to the user when updating.
  246. /// </summary>
  247. /// <returns>True if the audio renderer should supply the elapsed frame count to the user when updating.</returns>
  248. public bool IsElapsedFrameCountSupported()
  249. {
  250. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  251. }
  252. /// <summary>
  253. /// Get the performance metric data format version.
  254. /// </summary>
  255. /// <returns>The performance metric data format version.</returns>
  256. public uint GetPerformanceMetricsDataFormat()
  257. {
  258. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  259. {
  260. return 2;
  261. }
  262. else
  263. {
  264. return 1;
  265. }
  266. }
  267. /// <summary>
  268. /// Check if the audio renderer should support <see cref="Parameter.VoiceInParameter.DecodingBehaviour"/>.
  269. /// </summary>
  270. /// <returns>True if the audio renderer should support <see cref="Parameter.VoiceInParameter.DecodingBehaviour"/>.</returns>
  271. public bool IsDecodingBehaviourFlagSupported()
  272. {
  273. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
  274. }
  275. /// <summary>
  276. /// 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.
  277. /// </summary>
  278. /// <returns>True if the biquad filter state should be cleared.</returns>
  279. public bool IsBiquadFilterEffectStateClearBugFixed()
  280. {
  281. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision6);
  282. }
  283. /// <summary>
  284. /// Check if the audio renderer should accept partial mix updates.
  285. /// </summary>
  286. /// <returns>True if the audio renderer should accept partial mix updates.</returns>
  287. public bool IsMixInParameterDirtyOnlyUpdateSupported()
  288. {
  289. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision7);
  290. }
  291. /// <summary>
  292. /// Check if the audio renderer should use the new wavebuffer format.
  293. /// </summary>
  294. /// <returns>True if the audio renderer should use the new wavebuffer format.</returns>
  295. public bool IsWaveBufferVersion2Supported()
  296. {
  297. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8);
  298. }
  299. /// <summary>
  300. /// Check if the audio renderer should use the new effect info format.
  301. /// </summary>
  302. /// <returns>True if the audio renderer should use the new effect info format.</returns>
  303. public bool IsEffectInfoVersion2Supported()
  304. {
  305. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision9);
  306. }
  307. /// <summary>
  308. /// 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.
  309. /// </summary>
  310. /// <returns>True if the audio renderer should use the optimization.</returns>
  311. public bool IsBiquadFilterGroupedOptimizationSupported()
  312. {
  313. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision10);
  314. }
  315. /// <summary>
  316. /// Check if the audio renderer should support new channel resource mapping for 5.1 on Delay, Reverb and Reverb 3D effects.
  317. /// </summary>
  318. /// <returns>True if the audio renderer support new channel resource mapping for 5.1.</returns>
  319. public bool IsNewEffectChannelMappingSupported()
  320. {
  321. return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision11);
  322. }
  323. /// <summary>
  324. /// Get the version of the <see cref="ICommandProcessingTimeEstimator"/>.
  325. /// </summary>
  326. /// <returns>The version of the <see cref="ICommandProcessingTimeEstimator"/>.</returns>
  327. public int GetCommandProcessingTimeEstimatorVersion()
  328. {
  329. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision11))
  330. {
  331. return 5;
  332. }
  333. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision10))
  334. {
  335. return 4;
  336. }
  337. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8))
  338. {
  339. return 3;
  340. }
  341. if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
  342. {
  343. return 2;
  344. }
  345. return 1;
  346. }
  347. /// <summary>
  348. /// Append a new <see cref="ErrorInfo"/> to the error array.
  349. /// </summary>
  350. /// <param name="errorInfo">The new <see cref="ErrorInfo"/> to add.</param>
  351. public void AppendError(ref ErrorInfo errorInfo)
  352. {
  353. Debug.Assert(errorInfo.ErrorCode == ResultCode.Success);
  354. if (_errorIndex <= Constants.MaxErrorInfos - 1)
  355. {
  356. _errorInfos[_errorIndex++] = errorInfo;
  357. }
  358. }
  359. /// <summary>
  360. /// Copy the internal <see cref="ErrorInfo"/> array to the given <see cref="Span{ErrorInfo}"/> and output the count copied.
  361. /// </summary>
  362. /// <param name="errorInfos">The output <see cref="Span{ErrorInfo}"/>.</param>
  363. /// <param name="errorCount">The output error count containing the count of <see cref="ErrorInfo"/> copied.</param>
  364. public void CopyErrorInfo(Span<ErrorInfo> errorInfos, out uint errorCount)
  365. {
  366. if (errorInfos.Length != Constants.MaxErrorInfos)
  367. {
  368. throw new ArgumentException("Invalid size of errorInfos span!");
  369. }
  370. errorCount = Math.Min(_errorIndex, Constants.MaxErrorInfos);
  371. for (int i = 0; i < Constants.MaxErrorInfos; i++)
  372. {
  373. if (i < errorCount)
  374. {
  375. errorInfos[i] = _errorInfos[i];
  376. }
  377. else
  378. {
  379. errorInfos[i] = new ErrorInfo
  380. {
  381. ErrorCode = 0,
  382. ExtraErrorInfo = 0
  383. };
  384. }
  385. }
  386. }
  387. /// <summary>
  388. /// Clear the <see cref="ErrorInfo"/> array.
  389. /// </summary>
  390. public void ClearError()
  391. {
  392. _errorIndex = 0;
  393. }
  394. }
  395. }