MixContext.cs 9.0 KB

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