Table of Contents

Struct OpusDecoder

Namespace
DSharpPlus.VoiceLink.Opus
Assembly
DSharpPlus.VoiceLink.dll
public readonly struct OpusDecoder : IDisposable
Implements
Inherited Members

Constructors

public OpusDecoder(nint state)

Parameters

state nint

Methods

Perform a CTL function on an Opus decoder.

public void Control(OpusControlRequest control, out int value)

Parameters

control OpusControlRequest
value int

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.

Allocates and initializes a decoder state.

public static OpusDecoder Create(OpusSampleRate sampleRate, int channels)

Parameters

sampleRate OpusSampleRate

Sample rate to decode at (Hz).

channels int

Number of channels (1 or 2) to decode.

Returns

OpusDecoder

A new decoder instance.

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 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 int

Number 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), then frameSize 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 bool

Flag (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

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 int

Number 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), then frameSize 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 bool

Flag (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

Frees an OpusDecoder allocated by DecoderCreate(OpusSampleRate, int, out OpusErrorCode*).

public void Destroy()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

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.

Gets the size of an OpusDecoder structure.

public static int GetSize(int channels)

Parameters

channels int

Number of channels. This must be 1 or 2.

Returns

int

The size in bytes.

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 OpusSampleRate

Sampling rate to decode to (Hz).

channels int

Number of channels (1 or 2) to decode.