BufferedOpusDecoder class
An implementation of OpusDecoder that uses preallocated buffers. Don't forget to call destroy once you are done with it.
The idea behind this implementation is to reduce the amount of memory allocation calls. Instead of allocating new buffers everytime something is decoded, the buffers are allocated at initalization. Then, an opus packet is directly written into the inputBuffer, the inputBufferIndex is updated, based on how many bytes where written, and one of the deocde methods is called. The decoded pcm samples can then be accessed using the outputBuffer getter (or one of the outputBufferAsInt16List or outputBufferAsFloat32List convenience getters).
BufferedOpusDecoder decoder;
void example() {
// Get an opus packet
Uint8List packet = receivePacket();
// Set the bytes to the input buffer
decoder.inputBuffer.setAll(0, packet);
// Update the inputBufferIndex with amount of bytes written
decoder.inputBufferIndex = packet.length;
// decode
decoder.decode();
// Interpret the output as s16le
Int16List pcm = decoder.outputBufferAsInt16List;
doSomething(pcm);
}
- Inheritance
-
- Object
- OpusDecoder
- BufferedOpusDecoder
Constructors
- BufferedOpusDecoder({required int sampleRate, required int channels, int? maxInputBufferSizeBytes, int? maxOutputBufferSizeBytes})
-
Creates an new BufferedOpusDecoder based on the
sampleRate
andchannels
. The native allocated buffer size is determined bymaxInputBufferSizeBytes
andmaxOutputBufferSizeBytes
.factory
Properties
- channels → int
-
Number of channels, must be 1 for mono or 2 for stereo.
final
- destroyed → bool
-
Wheter this decoder was already destroyed by calling destroy.
If so, calling any method will result in an OpusDestroyedError.
no setteroverride
- hashCode → int
-
The hash code for this object.
no setterinherited
- inputBuffer → Uint8List
-
Returns the native input buffer backed by native memory.
no setter
- inputBufferIndex ↔ int
-
Indicates, how many bytes of data are currently stored in the inputBuffer.
getter/setter pair
- lastPacketDurationMs → int?
-
The duration of the last decoded packet in ms or null if there was no packet yet.
no setteroverride
- maxInputBufferSizeBytes → int
-
The size of the allocated the input buffer in bytes.
Should be choosen big enough to hold a maximal opus packet
with size of maxDataBytes bytes.
final
- maxOutputBufferSizeBytes → int
-
The size of the allocated the output buffer. If this value is choosen
to small, this decoder will not be capable of decoding some packets.
final
- outputBuffer → Uint8List
-
The portion of the allocated output buffer that is currently filled with data.
The data are pcm samples, either encoded as s16le or floats, depending on
what method was used to decode the input packet.
no setter
- outputBufferAsFloat32List → Float32List
-
Convenience method to get the current output buffer as floats.
no setter
- outputBufferAsInt16List → Int16List
-
Convenience method to get the current output buffer as s16le.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- sampleRate → int
-
The sample rate in Hz for this decoder.
Opus supports sample rates from 8kHz to 48kHz so this value must be between 8000 and 48000.
final
Methods
-
decode(
{bool fec = false, int? loss}) → Int16List -
Interpretes inputBufferIndex bytes from the inputBuffer as a whole
opus packet and decodes them to s16le samples, stored in the outputBuffer.
Set inputBufferIndex to
0
to indicate packet loss.override -
decodeFloat(
{bool autoSoftClip = false, bool fec = false, int? loss}) → Float32List -
Interpretes inputBufferIndex bytes from the inputBuffer as a whole
opus packet and decodes them to float samples, stored in the outputBuffer.
Set inputBufferIndex to
0
to indicate packet loss.override -
destroy(
) → void -
Destroys this decoder by releasing all native resources.
After this, it is no longer possible to decode using this decoder, so any further method call will throw an OpusDestroyedError.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
pcmSoftClipOutputBuffer(
) → Float32List - Performs soft clipping on the outputBuffer.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited