BehaviourContext.cs 19 KB

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