SplitterState.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using Ryujinx.Audio.Renderer.Parameter;
  2. using System;
  3. using System.Buffers;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Audio.Renderer.Server.Splitter
  7. {
  8. /// <summary>
  9. /// Server state for a splitter.
  10. /// </summary>
  11. [StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = Alignment)]
  12. public struct SplitterState
  13. {
  14. public const int Alignment = 0x10;
  15. /// <summary>
  16. /// The unique id of this <see cref="SplitterState"/>.
  17. /// </summary>
  18. public int Id;
  19. /// <summary>
  20. /// Target sample rate to use on the splitter.
  21. /// </summary>
  22. public uint SampleRate;
  23. /// <summary>
  24. /// Count of splitter destinations (<see cref="SplitterDestination"/>).
  25. /// </summary>
  26. public int DestinationCount;
  27. /// <summary>
  28. /// Set to true if the splitter has a new connection.
  29. /// </summary>
  30. [MarshalAs(UnmanagedType.I1)]
  31. public bool HasNewConnection;
  32. /// <summary>
  33. /// Linked list of <see cref="SplitterDestination"/>.
  34. /// </summary>
  35. private unsafe SplitterDestination* _destinationsData;
  36. /// <summary>
  37. /// Span to the first element of the linked list of <see cref="SplitterDestination"/>.
  38. /// </summary>
  39. public Span<SplitterDestination> Destinations
  40. {
  41. get
  42. {
  43. unsafe
  44. {
  45. return (IntPtr)_destinationsData != IntPtr.Zero ? new Span<SplitterDestination>(_destinationsData, 1) : Span<SplitterDestination>.Empty;
  46. }
  47. }
  48. }
  49. /// <summary>
  50. /// Create a new <see cref="SplitterState"/>.
  51. /// </summary>
  52. /// <param name="id">The unique id of this <see cref="SplitterState"/>.</param>
  53. public SplitterState(int id) : this()
  54. {
  55. Id = id;
  56. }
  57. public Span<SplitterDestination> GetData(int index)
  58. {
  59. int i = 0;
  60. Span<SplitterDestination> result = Destinations;
  61. while (i < index)
  62. {
  63. if (result.IsEmpty)
  64. {
  65. break;
  66. }
  67. result = result[0].Next;
  68. i++;
  69. }
  70. return result;
  71. }
  72. /// <summary>
  73. /// Clear the new connection flag.
  74. /// </summary>
  75. public void ClearNewConnectionFlag()
  76. {
  77. HasNewConnection = false;
  78. }
  79. /// <summary>
  80. /// Utility function to apply a given <see cref="SpanAction{T, TArg}"/> to all <see cref="Destinations"/>.
  81. /// </summary>
  82. /// <param name="action">The action to execute on each elements.</param>
  83. private void ForEachDestination(SpanAction<SplitterDestination, int> action)
  84. {
  85. Span<SplitterDestination> temp = Destinations;
  86. int i = 0;
  87. while (true)
  88. {
  89. if (temp.IsEmpty)
  90. {
  91. break;
  92. }
  93. Span<SplitterDestination> next = temp[0].Next;
  94. action.Invoke(temp, i++);
  95. temp = next;
  96. }
  97. }
  98. /// <summary>
  99. /// Update the <see cref="SplitterState"/> from user parameter.
  100. /// </summary>
  101. /// <param name="context">The splitter context.</param>
  102. /// <param name="parameter">The user parameter.</param>
  103. /// <param name="input">The raw input data after the <paramref name="parameter"/>.</param>
  104. public void Update(SplitterContext context, ref SplitterInParameter parameter, ReadOnlySpan<byte> input)
  105. {
  106. ClearLinks();
  107. int destinationCount;
  108. if (context.IsBugFixed)
  109. {
  110. destinationCount = parameter.DestinationCount;
  111. }
  112. else
  113. {
  114. destinationCount = Math.Min(context.GetDestinationCountPerStateForCompatibility(), parameter.DestinationCount);
  115. }
  116. if (destinationCount > 0)
  117. {
  118. ReadOnlySpan<int> destinationIds = MemoryMarshal.Cast<byte, int>(input);
  119. Memory<SplitterDestination> destination = context.GetDestinationMemory(destinationIds[0]);
  120. SetDestination(ref destination.Span[0]);
  121. DestinationCount = destinationCount;
  122. for (int i = 1; i < destinationCount; i++)
  123. {
  124. Memory<SplitterDestination> nextDestination = context.GetDestinationMemory(destinationIds[i]);
  125. destination.Span[0].Link(ref nextDestination.Span[0]);
  126. destination = nextDestination;
  127. }
  128. }
  129. Debug.Assert(parameter.Id == Id);
  130. if (parameter.Id == Id)
  131. {
  132. SampleRate = parameter.SampleRate;
  133. HasNewConnection = true;
  134. }
  135. }
  136. /// <summary>
  137. /// Set the head of the linked list of <see cref="Destinations"/>.
  138. /// </summary>
  139. /// <param name="newValue">A reference to a <see cref="SplitterDestination"/>.</param>
  140. public void SetDestination(ref SplitterDestination newValue)
  141. {
  142. unsafe
  143. {
  144. fixed (SplitterDestination* newValuePtr = &newValue)
  145. {
  146. _destinationsData = newValuePtr;
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// Update the internal state of this instance.
  152. /// </summary>
  153. public void UpdateInternalState()
  154. {
  155. ForEachDestination((destination, _) => destination[0].UpdateInternalState());
  156. }
  157. /// <summary>
  158. /// Clear all links from the <see cref="Destinations"/>.
  159. /// </summary>
  160. public void ClearLinks()
  161. {
  162. ForEachDestination((destination, _) => destination[0].Unlink());
  163. unsafe
  164. {
  165. _destinationsData = (SplitterDestination*)IntPtr.Zero;
  166. }
  167. }
  168. /// <summary>
  169. /// Initialize a given <see cref="Span{SplitterState}"/>.
  170. /// </summary>
  171. /// <param name="splitters">All the <see cref="SplitterState"/> to initialize.</param>
  172. public static void InitializeSplitters(Span<SplitterState> splitters)
  173. {
  174. foreach (ref SplitterState splitter in splitters)
  175. {
  176. unsafe
  177. {
  178. splitter._destinationsData = (SplitterDestination*)IntPtr.Zero;
  179. }
  180. splitter.DestinationCount = 0;
  181. }
  182. }
  183. }
  184. }