using Ryujinx.Audio.Renderer.Parameter;
using Ryujinx.Common.Memory;
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 2).
///
[StructLayout(LayoutKind.Sequential, Size = 0x110, Pack = Alignment)]
public struct SplitterDestinationVersion2
{
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 SplitterDestinationVersion2* _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 SplitterDestinationVersion2 Next
{
get
{
unsafe
{
return ref Unsafe.AsRef(_next);
}
}
}
private Array2 _biquadFilters;
private Array2 _isPreviousBiquadFilterEnabled;
///
/// Create a new .
///
/// The unique id of this .
public SplitterDestinationVersion2(int id) : this()
{
Id = id;
DestinationId = Constants.UnusedMixId;
ClearVolumes();
}
///
/// Update the from user parameter.
///
/// The user parameter.
public void Update(in T parameter) where T : ISplitterDestinationInParameter
{
Debug.Assert(Id == parameter.Id);
if (parameter.IsMagicValid() && Id == parameter.Id)
{
DestinationId = parameter.DestinationId;
parameter.MixBufferVolume.CopyTo(MixBufferVolume);
_biquadFilters = parameter.BiquadFilters;
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];
}
///
/// 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 SplitterDestinationVersion2 next)
{
unsafe
{
fixed (SplitterDestinationVersion2* nextPtr = &next)
{
_next = nextPtr;
}
}
}
///
/// Remove the link to the next element.
///
public void Unlink()
{
unsafe
{
_next = null;
}
}
///
/// Checks if any biquad filter is enabled.
///
/// True if any biquad filter is enabled.
public bool IsBiquadFilterEnabled()
{
return _biquadFilters[0].Enable || _biquadFilters[1].Enable;
}
///
/// Checks if any biquad filter was previously enabled.
///
/// True if any biquad filter was previously enabled.
public bool IsBiquadFilterEnabledPrev()
{
return _isPreviousBiquadFilterEnabled[0];
}
///
/// Gets the biquad filter parameters.
///
/// Biquad filter index (0 or 1).
/// Biquad filter parameters.
public ref BiquadFilterParameter GetBiquadFilterParameter(int index)
{
return ref _biquadFilters[index];
}
///
/// Checks if any biquad filter was previously enabled.
///
/// Biquad filter index (0 or 1).
public void UpdateBiquadFilterEnabledPrev(int index)
{
_isPreviousBiquadFilterEnabled[index] = _biquadFilters[index].Enable;
}
}
}