BehaviourContext.cs 18 KB

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