Struct OpusDecoder
- Namespace
- DSharpPlus.VoiceLink.Opus
- Assembly
- DSharpPlus.VoiceLink.dll
public readonly struct OpusDecoder : IDisposable
- Implements
- Inherited Members
Constructors
OpusDecoder(nint)
public OpusDecoder(nint state)
Parameters
state
nint
Methods
Control(OpusControlRequest, out int)
Perform a CTL function on an Opus decoder.
public void Control(OpusControlRequest control, out int value)
Parameters
control
OpusControlRequestvalue
int
Remarks
VoiceLink: This method is a varargs method, and as such, it is not supported by the C# language. There are no known alternatives at the time of writing.
Create(OpusSampleRate, int)
Allocates and initializes a decoder state.
public static OpusDecoder Create(OpusSampleRate sampleRate, int channels)
Parameters
sampleRate
OpusSampleRateSample rate to decode at (Hz).
channels
intNumber of channels (1 or 2) to decode.
Returns
- OpusDecoder
A new decoder instance.
Remarks
Internally Opus stores data at 48000 Hz, so that should be the default value for Fs. However, the decoder can efficiently decode to buffers at 8, 12, 16, and 24 kHz so if for some reason the caller cannot use data at the full sample rate, or knows the compressed data doesn't use the full frequency range, it can request decoding at a reduced rate. Likewise, the decoder is capable of filling in either mono or interleaved stereo pcm buffers, at the caller's request.
Exceptions
- OpusException
The Opus library has thrown an exception.
Decode(ReadOnlySpan<byte>, Span<byte>, int, bool)
Decode an Opus packet.
public int Decode(ReadOnlySpan<byte> data, Span<byte> pcm, int frameSize, bool decodeFec)
Parameters
data
ReadOnlySpan<byte>Input payload. Use a Zero (null) pointer to indicate packet loss.
pcm
Span<byte>Output signal (interleaved if 2 channels). Length is frame_size * channels * sizeof(short)
frameSize
intNumber of samples per channel of available space in
pcm
. If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will not be capable of decoding some packets. In the case of PLC (data
is null) or FEC (decodeFec
is true), thenframeSize
needs to be exactly the duration of audio that is missing, otherwise the decoder will not be in the optimal state to decode the next incoming packet. For the PLC and FEC cases,frameSize
must be a multiple of 2.5 ms.decodeFec
boolFlag (0 or 1) to request that any in-band forward error correction data be decoded. If no such data is available, the frame is decoded as if it were lost.
Returns
- int
Number of decoded samples or an OpusErrorCode
DecodeFloat(ReadOnlySpan<byte>, Span<byte>, int, bool)
Decode an Opus packet.
public int DecodeFloat(ReadOnlySpan<byte> data, Span<byte> pcm, int frameSize, bool decodeFec)
Parameters
data
ReadOnlySpan<byte>Input payload. Use a Zero (null) pointer to indicate packet loss.
pcm
Span<byte>Output signal (interleaved if 2 channels). Length is frame_size * channels * sizeof(short)
frameSize
intNumber of samples per channel of available space in
pcm
. If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will not be capable of decoding some packets. In the case of PLC (data
is null) or FEC (decodeFec
is true), thenframeSize
needs to be exactly the duration of audio that is missing, otherwise the decoder will not be in the optimal state to decode the next incoming packet. For the PLC and FEC cases,frameSize
must be a multiple of 2.5 ms.decodeFec
boolFlag (0 or 1) to request that any in-band forward error correction data be decoded. If no such data is available, the frame is decoded as if it were lost.
Returns
- int
Number of decoded samples or an OpusErrorCode
Destroy()
Frees an OpusDecoder allocated by DecoderCreate(OpusSampleRate, int, out OpusErrorCode*).
public void Destroy()
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
GetSampleCount(ReadOnlySpan<byte>)
Gets the number of samples per channel of a packet.
public int GetSampleCount(ReadOnlySpan<byte> data)
Parameters
data
ReadOnlySpan<byte>Opus packet.
Returns
- int
The number of samples per channel of a packet.
Exceptions
- ArgumentException
Invalid argument passed to the decoder.
- InvalidOperationException
The compressed data passed is corrupted or of an unsupported type or an unknown error occured.
GetSize(int)
Gets the size of an OpusDecoder structure.
public static int GetSize(int channels)
Parameters
channels
intNumber of channels. This must be 1 or 2.
Returns
- int
The size in bytes.
Init(OpusSampleRate, int)
Initializes a previously allocated decoder state. The state must be at least the size returned by DecoderGetSize(int). This is intended for applications which use their own allocator instead of malloc.
public void Init(OpusSampleRate sampleRate, int channels)
Parameters
sampleRate
OpusSampleRateSampling rate to decode to (Hz).
channels
intNumber of channels (1 or 2) to decode.