using System;
using System.Diagnostics;
using static Ryujinx.Audio.Renderer.Common.BehaviourParameter;
namespace Ryujinx.Audio.Renderer.Server
{
///
/// Behaviour context.
///
/// This handles features based on the audio renderer revision provided by the user.
public class BehaviourContext
{
///
/// The base magic of the Audio Renderer revision.
///
public const int BaseRevisionMagic = ('R' << 0) | ('E' << 8) | ('V' << 16) | ('0' << 24);
///
/// REV1: first revision.
///
public const int Revision1 = 1 << 24;
///
/// REV2: Added support for splitter and fix GC-ADPCM context not being provided to the DSP.
///
/// This was added in system update 2.0.0
public const int Revision2 = 2 << 24;
///
/// REV3: Incremented the max pre-delay from 150 to 350 for the reverb command and removed the (unused) codec system.
///
/// This was added in system update 3.0.0
public const int Revision3 = 3 << 24;
///
/// REV4: Added USB audio device support and incremented the rendering limit percent to 75%.
///
/// This was added in system update 4.0.0
public const int Revision4 = 4 << 24;
///
/// REV5: , were added to voice.
/// A new performance frame format (version 2) was added with support for more information about DSP timing.
/// was added to supply the count of update done sent to the DSP.
/// A new version of the command estimator was added to address timing changes caused by the voice changes.
/// Additionally, the rendering limit percent was incremented to 80%.
///
///
/// This was added in system update 6.0.0
public const int Revision5 = 5 << 24;
///
/// REV6: This fixed a bug in the biquad filter command not clearing up with usage state.
///
/// This was added in system update 6.1.0
public const int Revision6 = 6 << 24;
///
/// REV7: Client side (finally) doesn't send all the mix client state to the server and can do partial updates.
///
/// This was added in system update 8.0.0
public const int Revision7 = 7 << 24;
///
/// REV8:
/// Wavebuffer was changed to support more control over loop (you can now specify where to start and end a loop, and how many times to loop).
/// was added (see for more info).
/// Final leftovers of the codec system were removed.
/// support was added.
/// A new version of the command estimator was added to address timing changes caused by the voice and command changes.
///
/// This was added in system update 9.0.0
public const int Revision8 = 8 << 24;
///
/// REV9:
/// EffectInfo parameters were revisited with a new revision (version 2) allowing more data control between the client and server.
/// A new effect was added: Limiter. This effect is effectively implemented with a DRC while providing statistics on the processing on .
///
/// This was added in system update 12.0.0
public const int Revision9 = 9 << 24;
///
/// REV10:
/// Added Bluetooth audio device support and removed the unused "GetAudioSystemMasterVolumeSetting" audio device API.
/// A new effect was added: Capture. This effect allows the client side to capture audio buffers of a mix.
/// A new command was added for double biquad filters on voices. This is implemented using a direct form 1 (instead of the usual direct form 2).
/// A new version of the command estimator was added to support the new commands.
///
/// This was added in system update 13.0.0
public const int Revision10 = 10 << 24;
///
/// REV11:
/// The "legacy" effects (Delay, Reverb and Reverb 3D) were updated to match the standard channel mapping used by the audio renderer.
/// A new effect was added: Compressor. This effect is effectively implemented with a DRC.
/// A new version of the command estimator was added to address timing changes caused by the legacy effects changes.
/// A voice drop parameter was added in 15.0.0: This allows an application to amplify or attenuate the estimated time of DSP commands.
///
/// This was added in system update 14.0.0 but some changes were made in 15.0.0
public const int Revision11 = 11 << 24;
///
/// Last revision supported by the implementation.
///
public const int LastRevision = Revision11;
///
/// Target revision magic supported by the implementation.
///
public const int ProcessRevision = BaseRevisionMagic + LastRevision;
///
/// Get the revision number from the revision magic.
///
/// The revision magic.
/// The revision number.
public static int GetRevisionNumber(int revision) => (revision - BaseRevisionMagic) >> 24;
///
/// Current active revision.
///
public int UserRevision { get; private set; }
///
/// Error storage.
///
private ErrorInfo[] _errorInfos;
///
/// Current position in the array.
///
private uint _errorIndex;
///
/// Current flags of the .
///
private ulong _flags;
///
/// Create a new instance of .
///
public BehaviourContext()
{
UserRevision = 0;
_errorInfos = new ErrorInfo[Constants.MaxErrorInfos];
_errorIndex = 0;
}
///
/// Set the active revision.
///
/// The active revision.
public void SetUserRevision(int userRevision)
{
UserRevision = userRevision;
}
///
/// Update flags of the .
///
/// The new flags.
public void UpdateFlags(ulong flags)
{
_flags = flags;
}
///
/// Check if a given revision is valid/supported.
///
/// The revision magic to check.
/// Returns true if the given revision is valid/supported
public static bool CheckValidRevision(int revision)
{
return GetRevisionNumber(revision) <= GetRevisionNumber(ProcessRevision);
}
///
/// Check if the given revision is greater than or equal the supported revision.
///
/// The revision magic to check.
/// The revision magic of the supported revision.
/// Returns true if the given revision is greater than or equal the supported revision.
public static bool CheckFeatureSupported(int revision, int supportedRevision)
{
int revA = GetRevisionNumber(revision);
int revB = GetRevisionNumber(supportedRevision);
if (revA > LastRevision)
{
revA = 1;
}
if (revB > LastRevision)
{
revB = 1;
}
return revA >= revB;
}
///
/// Check if the memory pool mapping bypass flag is active.
///
/// True if the memory pool mapping bypass flag is active.
public bool IsMemoryPoolForceMappingEnabled()
{
return (_flags & 1) != 0;
}
///
/// Check if the audio renderer should fix the GC-ADPCM context not being provided to the DSP.
///
/// True if if the audio renderer should fix it.
public bool IsAdpcmLoopContextBugFixed()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision2);
}
///
/// Check if the audio renderer should accept splitters.
///
/// True if the audio renderer should accept splitters.
public bool IsSplitterSupported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision2);
}
///
/// Check if the audio renderer should use a max pre-delay of 350 instead of 150.
///
/// True if the max pre-delay must be 350.
public bool IsLongSizePreDelaySupported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision3);
}
///
/// Check if the audio renderer should expose USB audio device.
///
/// True if the audio renderer should expose USB audio device.
public bool IsAudioUsbDeviceOutputSupported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision4);
}
///
/// Get the percentage allocated to the audio renderer on the DSP for processing.
///
/// The percentage allocated to the audio renderer on the DSP for processing.
public float GetAudioRendererProcessingTimeLimit()
{
if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
{
return 0.80f;
}
else if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision4))
{
return 0.75f;
}
return 0.70f;
}
///
/// Check if the audio render should support voice flushing.
///
/// True if the audio render should support voice flushing.
public bool IsFlushVoiceWaveBuffersSupported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
}
///
/// Check if the audio renderer should trust the user destination count in .
///
/// True if the audio renderer should trust the user destination count.
public bool IsSplitterBugFixed()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
}
///
/// Check if the audio renderer should supply the elapsed frame count to the user when updating.
///
/// True if the audio renderer should supply the elapsed frame count to the user when updating.
public bool IsElapsedFrameCountSupported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
}
///
/// Get the performance metric data format version.
///
/// The performance metric data format version.
public uint GetPerformanceMetricsDataFormat()
{
if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
{
return 2;
}
else
{
return 1;
}
}
///
/// Check if the audio renderer should support .
///
/// True if the audio renderer should support .
public bool IsDecodingBehaviourFlagSupported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5);
}
///
/// Check if the audio renderer should fix the biquad filter command not clearing up with usage state.
///
/// True if the biquad filter state should be cleared.
public bool IsBiquadFilterEffectStateClearBugFixed()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision6);
}
///
/// Check if the audio renderer should accept partial mix updates.
///
/// True if the audio renderer should accept partial mix updates.
public bool IsMixInParameterDirtyOnlyUpdateSupported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision7);
}
///
/// Check if the audio renderer should use the new wavebuffer format.
///
/// True if the audio renderer should use the new wavebuffer format.
public bool IsWaveBufferVersion2Supported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8);
}
///
/// Check if the audio renderer should use the new effect info format.
///
/// True if the audio renderer should use the new effect info format.
public bool IsEffectInfoVersion2Supported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision9);
}
///
/// Check if the audio renderer should use an optimized Biquad Filter (Direct Form 1) in case of two biquad filters are defined on a voice.
///
/// True if the audio renderer should use the optimization.
public bool IsBiquadFilterGroupedOptimizationSupported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision10);
}
///
/// Check if the audio renderer should support new channel resource mapping for 5.1 on Delay, Reverb and Reverb 3D effects.
///
/// True if the audio renderer support new channel resource mapping for 5.1.
public bool IsNewEffectChannelMappingSupported()
{
return CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision11);
}
///
/// Get the version of the .
///
/// The version of the .
public int GetCommandProcessingTimeEstimatorVersion()
{
if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision11))
{
return 5;
}
if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision10))
{
return 4;
}
if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision8))
{
return 3;
}
if (CheckFeatureSupported(UserRevision, BaseRevisionMagic + Revision5))
{
return 2;
}
return 1;
}
///
/// Append a new to the error array.
///
/// The new to add.
public void AppendError(ref ErrorInfo errorInfo)
{
Debug.Assert(errorInfo.ErrorCode == ResultCode.Success);
if (_errorIndex <= Constants.MaxErrorInfos - 1)
{
_errorInfos[_errorIndex++] = errorInfo;
}
}
///
/// Copy the internal array to the given and output the count copied.
///
/// The output .
/// The output error count containing the count of copied.
public void CopyErrorInfo(Span errorInfos, out uint errorCount)
{
if (errorInfos.Length != Constants.MaxErrorInfos)
{
throw new ArgumentException("Invalid size of errorInfos span!");
}
errorCount = Math.Min(_errorIndex, Constants.MaxErrorInfos);
for (int i = 0; i < Constants.MaxErrorInfos; i++)
{
if (i < errorCount)
{
errorInfos[i] = _errorInfos[i];
}
else
{
errorInfos[i] = new ErrorInfo
{
ErrorCode = 0,
ExtraErrorInfo = 0
};
}
}
}
///
/// Clear the array.
///
public void ClearError()
{
_errorIndex = 0;
}
}
}