BehaviourContext.cs 18 KB

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