using Ryujinx.Audio.Renderer.Parameter; using Ryujinx.Common.Utilities; using System; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Ryujinx.Audio.Renderer.Server.Splitter { /// /// Server state for a splitter destination (version 1). /// [StructLayout(LayoutKind.Sequential, Size = 0xE0, Pack = Alignment)] public struct SplitterDestinationVersion1 { public const int Alignment = 0x10; /// /// The unique id of this . /// public int Id; /// /// The mix to output the result of the splitter. /// public int DestinationId; /// /// Mix buffer volumes storage. /// private MixArray _mix; private MixArray _previousMix; /// /// Pointer to the next linked element. /// private unsafe SplitterDestinationVersion1* _next; /// /// Set to true if in use. /// [MarshalAs(UnmanagedType.I1)] public bool IsUsed; /// /// Set to true if the internal state need to be updated. /// [MarshalAs(UnmanagedType.I1)] public bool NeedToUpdateInternalState; [StructLayout(LayoutKind.Sequential, Size = sizeof(float) * Constants.MixBufferCountMax, Pack = 1)] private struct MixArray { } /// /// Mix buffer volumes. /// /// Used when a splitter id is specified in the mix. public Span MixBufferVolume => SpanHelpers.AsSpan(ref _mix); /// /// Previous mix buffer volumes. /// /// Used when a splitter id is specified in the mix. public Span PreviousMixBufferVolume => SpanHelpers.AsSpan(ref _previousMix); /// /// Get the reference of the next element or null if not present. /// public readonly ref SplitterDestinationVersion1 Next { get { unsafe { return ref Unsafe.AsRef(_next); } } } /// /// Create a new . /// /// The unique id of this . public SplitterDestinationVersion1(int id) : this() { Id = id; DestinationId = Constants.UnusedMixId; ClearVolumes(); } /// /// Update the from user parameter. /// /// The user parameter. /// Indicates that the audio renderer revision in use supports explicitly resetting the volume. public void Update(in T parameter, bool isPrevVolumeResetSupported) where T : ISplitterDestinationInParameter { Debug.Assert(Id == parameter.Id); if (parameter.IsMagicValid() && Id == parameter.Id) { DestinationId = parameter.DestinationId; parameter.MixBufferVolume.CopyTo(MixBufferVolume); bool resetPrevVolume = isPrevVolumeResetSupported ? parameter.ResetPrevVolume : !IsUsed && parameter.IsUsed; if (resetPrevVolume) { MixBufferVolume.CopyTo(PreviousMixBufferVolume); NeedToUpdateInternalState = false; } IsUsed = parameter.IsUsed; } } /// /// Update the internal state of the instance. /// public void UpdateInternalState() { if (IsUsed && NeedToUpdateInternalState) { MixBufferVolume.CopyTo(PreviousMixBufferVolume); } NeedToUpdateInternalState = false; } /// /// Set the update internal state marker. /// public void MarkAsNeedToUpdateInternalState() { NeedToUpdateInternalState = true; } /// /// Return true if the is used and has a destination. /// /// True if the is used and has a destination. public readonly bool IsConfigured() { return IsUsed && DestinationId != Constants.UnusedMixId; } /// /// Get the volume for a given destination. /// /// The destination index to use. /// The volume for the given destination. public float GetMixVolume(int destinationIndex) { Debug.Assert(destinationIndex >= 0 && destinationIndex < Constants.MixBufferCountMax); return MixBufferVolume[destinationIndex]; } /// /// Get the previous volume for a given destination. /// /// The destination index to use. /// The volume for the given destination. public float GetMixVolumePrev(int destinationIndex) { Debug.Assert(destinationIndex >= 0 && destinationIndex < Constants.MixBufferCountMax); return PreviousMixBufferVolume[destinationIndex]; } /// /// Clear the volumes. /// public void ClearVolumes() { MixBufferVolume.Clear(); PreviousMixBufferVolume.Clear(); } /// /// Link the next element to the given . /// /// The given to link. public void Link(ref SplitterDestinationVersion1 next) { unsafe { fixed (SplitterDestinationVersion1* nextPtr = &next) { _next = nextPtr; } } } /// /// Remove the link to the next element. /// public void Unlink() { unsafe { _next = null; } } } }