BufferedOpusEncoder class

An implementation of OpusEncoder 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 encoded, the buffers are allocated at initalization. Then, pcm samples is directly written into the inputBuffer, the inputBufferIndex is updated, based on how many data where written, and one of the encode methods is called. The encoded opus packet can then be accessed using the outputBuffer getter.

BufferedOpusEncoder encoder;

void example() {
  // Get some pcm samples in s16le format
  Int16List s16lePcm=getSomeData();
  // Interpret the samples as bytes
  Uint8List asBytes = s16lePcm.buffer
      .asUint8List(s16lePcm.offsetInBytes, s16lePcm.lengthInBytes);
  // Set the bytes to the input buffer
  encoder.inputBuffer.setAll(0,asBytes);
  // Update the inputBufferIndex with amount of bytes (not samples!) written
  encoder.inputBufferIndex = asBytes.length;
  // encode
  encoder.encode();
  // Do something with the result
  doSomething(encoder.outputBuffer);
}
Inheritance

Constructors

BufferedOpusEncoder({required int sampleRate, required int channels, required Application application, int? maxInputBufferSizeBytes, int? maxOutputBufferSizeBytes})
Creates an new BufferedOpusEncoder based on the sampleRate, channels and application type. The native allocated buffer size is determined by maxInputBufferSizeBytes and maxOutputBufferSizeBytes.
factory

Properties

application Application
The kind of application for which this encoders output is used. Setting the right application type can increase quality of the encoded frames.
final
channels int
Number of channels, must be 1 for mono or 2 for stereo.
final
destroyed bool
Wheter this encoder 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
maxInputBufferSizeBytes int
The size of the allocated the input buffer in bytes (not sampels).
final
maxOutputBufferSizeBytes int
The size of the allocated the output buffer. It can be used to impose an instant upper limit on the bitrate, but must not be to small to hold the encoded data. Otherwise, the enocde methods might throw an exception. The default value of maxDataBytes ensures that there is enough space.
final
outputBuffer Uint8List
The portion of the allocated output buffer that is currently filled with data. The data represents an opus packet in bytes.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sampleRate int
The sample rate in Hz for this encoder. Opus supports sample rates from 8kHz to 48kHz so this value must be between 8000 and 48000.
final

Methods

destroy() → void
Destroys this encoder by releasing all native resources. After this, it is no longer possible to encode using this encoder, so any further method call will throw an OpusDestroyedError.
override
encode() Uint8List
Interpets inputBufferIndex bytes of the inputBuffer as s16le pcm data, and encodes them to the outputBuffer. This means, that this method encodes [inputBufferIndex]/2 samples, since inputBufferIndex is in bytes, and s16le uses two bytes per sample.
encodeFloat() Uint8List
Interpets inputBufferIndex bytes of the inputBuffer as float pcm data, and encodes them to the outputBuffer. This means, that this method encodes [inputBufferIndex]/4 samples, since inputBufferIndex is in bytes, and the float represntation uses two bytes per sample.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited