//
// Copyright (c) 2019-2021 Ryujinx
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see .
//
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 * RendererConstants.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 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 = RendererConstants.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 bool IsConfigured()
{
return IsUsed && DestinationId != RendererConstants.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 < RendererConstants.MixBufferCountMax);
return MixBufferVolume[destinationIndex];
}
///
/// Clear the volumes.
///
public void ClearVolumes()
{
MixBufferVolume.Fill(0);
PreviousMixBufferVolume.Fill(0);
}
///
/// 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;
}
}
}
}