MixContext.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using Ryujinx.Audio.Renderer.Common;
  2. using Ryujinx.Audio.Renderer.Server.Splitter;
  3. using Ryujinx.Audio.Renderer.Utils;
  4. using System;
  5. using System.Diagnostics;
  6. namespace Ryujinx.Audio.Renderer.Server.Mix
  7. {
  8. /// <summary>
  9. /// Mix context.
  10. /// </summary>
  11. public class MixContext
  12. {
  13. /// <summary>
  14. /// The total mix count.
  15. /// </summary>
  16. private uint _mixesCount;
  17. /// <summary>
  18. /// Storage for <see cref="MixState"/>.
  19. /// </summary>
  20. private Memory<MixState> _mixes;
  21. /// <summary>
  22. /// Storage of the sorted indices to <see cref="MixState"/>.
  23. /// </summary>
  24. private Memory<int> _sortedMixes;
  25. /// <summary>
  26. /// Graph state.
  27. /// </summary>
  28. public NodeStates NodeStates { get; }
  29. /// <summary>
  30. /// The instance of the adjacent matrix.
  31. /// </summary>
  32. public EdgeMatrix EdgeMatrix { get; }
  33. /// <summary>
  34. /// Create a new instance of <see cref="MixContext"/>.
  35. /// </summary>
  36. public MixContext()
  37. {
  38. NodeStates = new NodeStates();
  39. EdgeMatrix = new EdgeMatrix();
  40. }
  41. /// <summary>
  42. /// Initialize the <see cref="MixContext"/>.
  43. /// </summary>
  44. /// <param name="sortedMixes">The storage for sorted indices.</param>
  45. /// <param name="mixes">The storage of <see cref="MixState"/>.</param>
  46. /// <param name="nodeStatesWorkBuffer">The storage used for the <see cref="NodeStates"/>.</param>
  47. /// <param name="edgeMatrixWorkBuffer">The storage used for the <see cref="EdgeMatrix"/>.</param>
  48. public void Initialize(Memory<int> sortedMixes, Memory<MixState> mixes, Memory<byte> nodeStatesWorkBuffer, Memory<byte> edgeMatrixWorkBuffer)
  49. {
  50. _mixesCount = (uint)mixes.Length;
  51. _mixes = mixes;
  52. _sortedMixes = sortedMixes;
  53. if (!nodeStatesWorkBuffer.IsEmpty && !edgeMatrixWorkBuffer.IsEmpty)
  54. {
  55. NodeStates.Initialize(nodeStatesWorkBuffer, mixes.Length);
  56. EdgeMatrix.Initialize(edgeMatrixWorkBuffer, mixes.Length);
  57. }
  58. int sortedId = 0;
  59. for (int i = 0; i < _mixes.Length; i++)
  60. {
  61. SetSortedState(sortedId++, i);
  62. }
  63. }
  64. /// <summary>
  65. /// Associate the given <paramref name="targetIndex"/> to a given <paramref cref="id"/>.
  66. /// </summary>
  67. /// <param name="id">The sorted id.</param>
  68. /// <param name="targetIndex">The index to associate.</param>
  69. private void SetSortedState(int id, int targetIndex)
  70. {
  71. _sortedMixes.Span[id] = targetIndex;
  72. }
  73. /// <summary>
  74. /// Get a reference to the final <see cref="MixState"/>.
  75. /// </summary>
  76. /// <returns>A reference to the final <see cref="MixState"/>.</returns>
  77. public ref MixState GetFinalState()
  78. {
  79. return ref GetState(Constants.FinalMixId);
  80. }
  81. /// <summary>
  82. /// Get a reference to a <see cref="MixState"/> at the given <paramref name="id"/>.
  83. /// </summary>
  84. /// <param name="id">The index to use.</param>
  85. /// <returns>A reference to a <see cref="MixState"/> at the given <paramref name="id"/>.</returns>
  86. public ref MixState GetState(int id)
  87. {
  88. return ref SpanIOHelper.GetFromMemory(_mixes, id, _mixesCount);
  89. }
  90. /// <summary>
  91. /// Get a reference to a <see cref="MixState"/> at the given <paramref name="id"/> of the sorted mix info.
  92. /// </summary>
  93. /// <param name="id">The index to use.</param>
  94. /// <returns>A reference to a <see cref="MixState"/> at the given <paramref name="id"/>.</returns>
  95. public ref MixState GetSortedState(int id)
  96. {
  97. Debug.Assert(id >= 0 && id < _mixesCount);
  98. return ref GetState(_sortedMixes.Span[id]);
  99. }
  100. /// <summary>
  101. /// Get the total mix count.
  102. /// </summary>
  103. /// <returns>The total mix count.</returns>
  104. public uint GetCount()
  105. {
  106. return _mixesCount;
  107. }
  108. /// <summary>
  109. /// Update the internal distance from the final mix value of every <see cref="MixState"/>.
  110. /// </summary>
  111. private void UpdateDistancesFromFinalMix()
  112. {
  113. foreach (ref MixState mix in _mixes.Span)
  114. {
  115. mix.ClearDistanceFromFinalMix();
  116. }
  117. for (int i = 0; i < GetCount(); i++)
  118. {
  119. ref MixState mix = ref GetState(i);
  120. SetSortedState(i, i);
  121. if (mix.IsUsed)
  122. {
  123. uint distance;
  124. if (mix.MixId != Constants.FinalMixId)
  125. {
  126. int mixId = mix.MixId;
  127. for (distance = 0; distance < GetCount(); distance++)
  128. {
  129. if (mixId == Constants.UnusedMixId)
  130. {
  131. distance = MixState.InvalidDistanceFromFinalMix;
  132. break;
  133. }
  134. ref MixState distanceMix = ref GetState(mixId);
  135. if (distanceMix.DistanceFromFinalMix != MixState.InvalidDistanceFromFinalMix)
  136. {
  137. distance = distanceMix.DistanceFromFinalMix + 1;
  138. break;
  139. }
  140. mixId = distanceMix.DestinationMixId;
  141. if (mixId == Constants.FinalMixId)
  142. {
  143. break;
  144. }
  145. }
  146. if (distance > GetCount())
  147. {
  148. distance = MixState.InvalidDistanceFromFinalMix;
  149. }
  150. }
  151. else
  152. {
  153. distance = MixState.InvalidDistanceFromFinalMix;
  154. }
  155. mix.DistanceFromFinalMix = distance;
  156. }
  157. }
  158. }
  159. /// <summary>
  160. /// Update the internal mix buffer offset of all <see cref="MixState"/>.
  161. /// </summary>
  162. private void UpdateMixBufferOffset()
  163. {
  164. uint offset = 0;
  165. foreach (ref MixState mix in _mixes.Span)
  166. {
  167. mix.BufferOffset = offset;
  168. offset += mix.BufferCount;
  169. }
  170. }
  171. /// <summary>
  172. /// Sort the mixes using distance from the final mix.
  173. /// </summary>
  174. public void Sort()
  175. {
  176. UpdateDistancesFromFinalMix();
  177. int[] sortedMixesTemp = _sortedMixes.Slice(0, (int)GetCount()).ToArray();
  178. Array.Sort(sortedMixesTemp, (a, b) =>
  179. {
  180. ref MixState stateA = ref GetState(a);
  181. ref MixState stateB = ref GetState(b);
  182. return stateB.DistanceFromFinalMix.CompareTo(stateA.DistanceFromFinalMix);
  183. });
  184. sortedMixesTemp.AsSpan().CopyTo(_sortedMixes.Span);
  185. UpdateMixBufferOffset();
  186. }
  187. /// <summary>
  188. /// Sort the mixes and splitters using an adjacency matrix.
  189. /// </summary>
  190. /// <param name="splitterContext">The <see cref="SplitterContext"/> used.</param>
  191. /// <returns>Return true, if no errors in the graph were detected.</returns>
  192. public bool Sort(SplitterContext splitterContext)
  193. {
  194. if (splitterContext.UsingSplitter())
  195. {
  196. bool isValid = NodeStates.Sort(EdgeMatrix);
  197. if (isValid)
  198. {
  199. ReadOnlySpan<int> sortedMixesIndex = NodeStates.GetTsortResult();
  200. int id = 0;
  201. for (int i = sortedMixesIndex.Length - 1; i >= 0; i--)
  202. {
  203. SetSortedState(id++, sortedMixesIndex[i]);
  204. }
  205. UpdateMixBufferOffset();
  206. }
  207. return isValid;
  208. }
  209. else
  210. {
  211. UpdateMixBufferOffset();
  212. return true;
  213. }
  214. }
  215. }
  216. }