SplitterDestinationVersion2.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using Ryujinx.Audio.Renderer.Parameter;
  2. using Ryujinx.Common.Memory;
  3. using Ryujinx.Common.Utilities;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.InteropServices;
  8. namespace Ryujinx.Audio.Renderer.Server.Splitter
  9. {
  10. /// <summary>
  11. /// Server state for a splitter destination (version 2).
  12. /// </summary>
  13. [StructLayout(LayoutKind.Sequential, Size = 0x110, Pack = Alignment)]
  14. public struct SplitterDestinationVersion2
  15. {
  16. public const int Alignment = 0x10;
  17. /// <summary>
  18. /// The unique id of this <see cref="SplitterDestinationVersion2"/>.
  19. /// </summary>
  20. public int Id;
  21. /// <summary>
  22. /// The mix to output the result of the splitter.
  23. /// </summary>
  24. public int DestinationId;
  25. /// <summary>
  26. /// Mix buffer volumes storage.
  27. /// </summary>
  28. private MixArray _mix;
  29. private MixArray _previousMix;
  30. /// <summary>
  31. /// Pointer to the next linked element.
  32. /// </summary>
  33. private unsafe SplitterDestinationVersion2* _next;
  34. /// <summary>
  35. /// Set to true if in use.
  36. /// </summary>
  37. [MarshalAs(UnmanagedType.I1)]
  38. public bool IsUsed;
  39. /// <summary>
  40. /// Set to true if the internal state need to be updated.
  41. /// </summary>
  42. [MarshalAs(UnmanagedType.I1)]
  43. public bool NeedToUpdateInternalState;
  44. [StructLayout(LayoutKind.Sequential, Size = sizeof(float) * Constants.MixBufferCountMax, Pack = 1)]
  45. private struct MixArray { }
  46. /// <summary>
  47. /// Mix buffer volumes.
  48. /// </summary>
  49. /// <remarks>Used when a splitter id is specified in the mix.</remarks>
  50. public Span<float> MixBufferVolume => SpanHelpers.AsSpan<MixArray, float>(ref _mix);
  51. /// <summary>
  52. /// Previous mix buffer volumes.
  53. /// </summary>
  54. /// <remarks>Used when a splitter id is specified in the mix.</remarks>
  55. public Span<float> PreviousMixBufferVolume => SpanHelpers.AsSpan<MixArray, float>(ref _previousMix);
  56. /// <summary>
  57. /// Get the reference of the next element or null if not present.
  58. /// </summary>
  59. public readonly ref SplitterDestinationVersion2 Next
  60. {
  61. get
  62. {
  63. unsafe
  64. {
  65. return ref Unsafe.AsRef<SplitterDestinationVersion2>(_next);
  66. }
  67. }
  68. }
  69. private Array2<BiquadFilterParameter> _biquadFilters;
  70. private Array2<bool> _isPreviousBiquadFilterEnabled;
  71. /// <summary>
  72. /// Create a new <see cref="SplitterDestinationVersion2"/>.
  73. /// </summary>
  74. /// <param name="id">The unique id of this <see cref="SplitterDestinationVersion2"/>.</param>
  75. public SplitterDestinationVersion2(int id) : this()
  76. {
  77. Id = id;
  78. DestinationId = Constants.UnusedMixId;
  79. ClearVolumes();
  80. }
  81. /// <summary>
  82. /// Update the <see cref="SplitterDestinationVersion2"/> from user parameter.
  83. /// </summary>
  84. /// <param name="parameter">The user parameter.</param>
  85. public void Update<T>(in T parameter) where T : ISplitterDestinationInParameter
  86. {
  87. Debug.Assert(Id == parameter.Id);
  88. if (parameter.IsMagicValid() && Id == parameter.Id)
  89. {
  90. DestinationId = parameter.DestinationId;
  91. parameter.MixBufferVolume.CopyTo(MixBufferVolume);
  92. _biquadFilters = parameter.BiquadFilters;
  93. if (!IsUsed && parameter.IsUsed)
  94. {
  95. MixBufferVolume.CopyTo(PreviousMixBufferVolume);
  96. NeedToUpdateInternalState = false;
  97. }
  98. IsUsed = parameter.IsUsed;
  99. }
  100. }
  101. /// <summary>
  102. /// Update the internal state of the instance.
  103. /// </summary>
  104. public void UpdateInternalState()
  105. {
  106. if (IsUsed && NeedToUpdateInternalState)
  107. {
  108. MixBufferVolume.CopyTo(PreviousMixBufferVolume);
  109. }
  110. NeedToUpdateInternalState = false;
  111. }
  112. /// <summary>
  113. /// Set the update internal state marker.
  114. /// </summary>
  115. public void MarkAsNeedToUpdateInternalState()
  116. {
  117. NeedToUpdateInternalState = true;
  118. }
  119. /// <summary>
  120. /// Return true if the <see cref="SplitterDestinationVersion2"/> is used and has a destination.
  121. /// </summary>
  122. /// <returns>True if the <see cref="SplitterDestinationVersion2"/> is used and has a destination.</returns>
  123. public readonly bool IsConfigured()
  124. {
  125. return IsUsed && DestinationId != Constants.UnusedMixId;
  126. }
  127. /// <summary>
  128. /// Get the volume for a given destination.
  129. /// </summary>
  130. /// <param name="destinationIndex">The destination index to use.</param>
  131. /// <returns>The volume for the given destination.</returns>
  132. public float GetMixVolume(int destinationIndex)
  133. {
  134. Debug.Assert(destinationIndex >= 0 && destinationIndex < Constants.MixBufferCountMax);
  135. return MixBufferVolume[destinationIndex];
  136. }
  137. /// <summary>
  138. /// Get the previous volume for a given destination.
  139. /// </summary>
  140. /// <param name="destinationIndex">The destination index to use.</param>
  141. /// <returns>The volume for the given destination.</returns>
  142. public float GetMixVolumePrev(int destinationIndex)
  143. {
  144. Debug.Assert(destinationIndex >= 0 && destinationIndex < Constants.MixBufferCountMax);
  145. return PreviousMixBufferVolume[destinationIndex];
  146. }
  147. /// <summary>
  148. /// Clear the volumes.
  149. /// </summary>
  150. public void ClearVolumes()
  151. {
  152. MixBufferVolume.Clear();
  153. PreviousMixBufferVolume.Clear();
  154. }
  155. /// <summary>
  156. /// Link the next element to the given <see cref="SplitterDestinationVersion2"/>.
  157. /// </summary>
  158. /// <param name="next">The given <see cref="SplitterDestinationVersion2"/> to link.</param>
  159. public void Link(ref SplitterDestinationVersion2 next)
  160. {
  161. unsafe
  162. {
  163. fixed (SplitterDestinationVersion2* nextPtr = &next)
  164. {
  165. _next = nextPtr;
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// Remove the link to the next element.
  171. /// </summary>
  172. public void Unlink()
  173. {
  174. unsafe
  175. {
  176. _next = null;
  177. }
  178. }
  179. /// <summary>
  180. /// Checks if any biquad filter is enabled.
  181. /// </summary>
  182. /// <returns>True if any biquad filter is enabled.</returns>
  183. public bool IsBiquadFilterEnabled()
  184. {
  185. return _biquadFilters[0].Enable || _biquadFilters[1].Enable;
  186. }
  187. /// <summary>
  188. /// Checks if any biquad filter was previously enabled.
  189. /// </summary>
  190. /// <returns>True if any biquad filter was previously enabled.</returns>
  191. public bool IsBiquadFilterEnabledPrev()
  192. {
  193. return _isPreviousBiquadFilterEnabled[0];
  194. }
  195. /// <summary>
  196. /// Gets the biquad filter parameters.
  197. /// </summary>
  198. /// <param name="index">Biquad filter index (0 or 1).</param>
  199. /// <returns>Biquad filter parameters.</returns>
  200. public ref BiquadFilterParameter GetBiquadFilterParameter(int index)
  201. {
  202. return ref _biquadFilters[index];
  203. }
  204. /// <summary>
  205. /// Checks if any biquad filter was previously enabled.
  206. /// </summary>
  207. /// <param name="index">Biquad filter index (0 or 1).</param>
  208. public void UpdateBiquadFilterEnabledPrev(int index)
  209. {
  210. _isPreviousBiquadFilterEnabled[index] = _biquadFilters[index].Enable;
  211. }
  212. }
  213. }