SplitterDestination.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.Parameter;
  18. using Ryujinx.Common.Utilities;
  19. using System;
  20. using System.Diagnostics;
  21. using System.Runtime.InteropServices;
  22. namespace Ryujinx.Audio.Renderer.Server.Splitter
  23. {
  24. /// <summary>
  25. /// Server state for a splitter destination.
  26. /// </summary>
  27. [StructLayout(LayoutKind.Sequential, Size = 0xE0, Pack = Alignment)]
  28. public struct SplitterDestination
  29. {
  30. public const int Alignment = 0x10;
  31. /// <summary>
  32. /// The unique id of this <see cref="SplitterDestination"/>.
  33. /// </summary>
  34. public int Id;
  35. /// <summary>
  36. /// The mix to output the result of the splitter.
  37. /// </summary>
  38. public int DestinationId;
  39. /// <summary>
  40. /// Mix buffer volumes storage.
  41. /// </summary>
  42. private MixArray _mix;
  43. private MixArray _previousMix;
  44. /// <summary>
  45. /// Pointer to the next linked element.
  46. /// </summary>
  47. private unsafe SplitterDestination* _next;
  48. /// <summary>
  49. /// Set to true if in use.
  50. /// </summary>
  51. [MarshalAs(UnmanagedType.I1)]
  52. public bool IsUsed;
  53. /// <summary>
  54. /// Set to true if the internal state need to be updated.
  55. /// </summary>
  56. [MarshalAs(UnmanagedType.I1)]
  57. public bool NeedToUpdateInternalState;
  58. [StructLayout(LayoutKind.Sequential, Size = 4 * Constants.MixBufferCountMax, Pack = 1)]
  59. private struct MixArray { }
  60. /// <summary>
  61. /// Mix buffer volumes.
  62. /// </summary>
  63. /// <remarks>Used when a splitter id is specified in the mix.</remarks>
  64. public Span<float> MixBufferVolume => SpanHelpers.AsSpan<MixArray, float>(ref _mix);
  65. /// <summary>
  66. /// Previous mix buffer volumes.
  67. /// </summary>
  68. /// <remarks>Used when a splitter id is specified in the mix.</remarks>
  69. public Span<float> PreviousMixBufferVolume => SpanHelpers.AsSpan<MixArray, float>(ref _previousMix);
  70. /// <summary>
  71. /// Get the <see cref="Span{SplitterDestination}"/> of the next element or <see cref="Span{SplitterDestination}.Empty"/> if not present.
  72. /// </summary>
  73. public Span<SplitterDestination> Next
  74. {
  75. get
  76. {
  77. unsafe
  78. {
  79. return _next != null ? new Span<SplitterDestination>(_next, 1) : Span<SplitterDestination>.Empty;
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Create a new <see cref="SplitterDestination"/>.
  85. /// </summary>
  86. /// <param name="id">The unique id of this <see cref="SplitterDestination"/>.</param>
  87. public SplitterDestination(int id) : this()
  88. {
  89. Id = id;
  90. DestinationId = Constants.UnusedMixId;
  91. ClearVolumes();
  92. }
  93. /// <summary>
  94. /// Update the <see cref="SplitterDestination"/> from user parameter.
  95. /// </summary>
  96. /// <param name="parameter">The user parameter.</param>
  97. public void Update(SplitterDestinationInParameter parameter)
  98. {
  99. Debug.Assert(Id == parameter.Id);
  100. if (parameter.IsMagicValid() && Id == parameter.Id)
  101. {
  102. DestinationId = parameter.DestinationId;
  103. parameter.MixBufferVolume.CopyTo(MixBufferVolume);
  104. if (!IsUsed && parameter.IsUsed)
  105. {
  106. MixBufferVolume.CopyTo(PreviousMixBufferVolume);
  107. NeedToUpdateInternalState = false;
  108. }
  109. IsUsed = parameter.IsUsed;
  110. }
  111. }
  112. /// <summary>
  113. /// Update the internal state of the instance.
  114. /// </summary>
  115. public void UpdateInternalState()
  116. {
  117. if (IsUsed && NeedToUpdateInternalState)
  118. {
  119. MixBufferVolume.CopyTo(PreviousMixBufferVolume);
  120. }
  121. NeedToUpdateInternalState = false;
  122. }
  123. /// <summary>
  124. /// Set the update internal state marker.
  125. /// </summary>
  126. public void MarkAsNeedToUpdateInternalState()
  127. {
  128. NeedToUpdateInternalState = true;
  129. }
  130. /// <summary>
  131. /// Return true if the <see cref="SplitterDestination"/> is used and has a destination.
  132. /// </summary>
  133. /// <returns>True if the <see cref="SplitterDestination"/> is used and has a destination.</returns>
  134. public bool IsConfigured()
  135. {
  136. return IsUsed && DestinationId != Constants.UnusedMixId;
  137. }
  138. /// <summary>
  139. /// Get the volume for a given destination.
  140. /// </summary>
  141. /// <param name="destinationIndex">The destination index to use.</param>
  142. /// <returns>The volume for the given destination.</returns>
  143. public float GetMixVolume(int destinationIndex)
  144. {
  145. Debug.Assert(destinationIndex >= 0 && destinationIndex < Constants.MixBufferCountMax);
  146. return MixBufferVolume[destinationIndex];
  147. }
  148. /// <summary>
  149. /// Clear the volumes.
  150. /// </summary>
  151. public void ClearVolumes()
  152. {
  153. MixBufferVolume.Fill(0);
  154. PreviousMixBufferVolume.Fill(0);
  155. }
  156. /// <summary>
  157. /// Link the next element to the given <see cref="SplitterDestination"/>.
  158. /// </summary>
  159. /// <param name="next">The given <see cref="SplitterDestination"/> to link.</param>
  160. public void Link(ref SplitterDestination next)
  161. {
  162. unsafe
  163. {
  164. fixed (SplitterDestination *nextPtr = &next)
  165. {
  166. _next = nextPtr;
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// Remove the link to the next element.
  172. /// </summary>
  173. public void Unlink()
  174. {
  175. unsafe
  176. {
  177. _next = null;
  178. }
  179. }
  180. }
  181. }