using Ryujinx.Audio.Renderer.Parameter;
using Ryujinx.Common.Utilities;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Ryujinx.Audio.Renderer.Server.Splitter
{
///
/// Server state for a splitter destination.
///
[StructLayout(LayoutKind.Sequential, Size = 0xE0, Pack = Alignment)]
public struct SplitterDestination
{
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 SplitterDestination* _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 = 4 * 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 of the next element or if not present.
///
public readonly Span Next
{
get
{
unsafe
{
return _next != null ? new Span(_next, 1) : Span.Empty;
}
}
}
///
/// Create a new .
///
/// The unique id of this .
public SplitterDestination(int id) : this()
{
Id = id;
DestinationId = Constants.UnusedMixId;
ClearVolumes();
}
///
/// Update the from user parameter.
///
/// The user parameter.
public void Update(SplitterDestinationInParameter parameter)
{
Debug.Assert(Id == parameter.Id);
if (parameter.IsMagicValid() && Id == parameter.Id)
{
DestinationId = parameter.DestinationId;
parameter.MixBufferVolume.CopyTo(MixBufferVolume);
if (!IsUsed && parameter.IsUsed)
{
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];
}
///
/// 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 SplitterDestination next)
{
unsafe
{
fixed (SplitterDestination* nextPtr = &next)
{
_next = nextPtr;
}
}
}
///
/// Remove the link to the next element.
///
public void Unlink()
{
unsafe
{
_next = null;
}
}
}
}