encodeFloat method

Uint8List encodeFloat(
  1. Float32List pcm
)

Encode one frame of interleaved 32-bit float PCM samples.

Samples should be in the range -1.0, 1.0.

Implementation

Uint8List encodeFloat(Float32List pcm) {
  _checkNotDisposed();

  final channelPtrs = calloc<Pointer<Float>>(channels);
  final buffers = <Pointer<Float>>[];

  for (var ch = 0; ch < channels; ch++) {
    final buf = calloc<Float>(samplesPerFrame);
    buffers.add(buf);
    channelPtrs[ch] = buf;

    for (var s = 0;
        s < samplesPerFrame && (s * channels + ch) < pcm.length;
        s++) {
      buf[s] = pcm[s * channels + ch];
    }
  }

  final outSizePtr = calloc<Int32>();
  final result = _glintEncodeFloat(_handle, channelPtrs, outSizePtr);
  final outSize = outSizePtr.value;

  Uint8List output;
  if (result == nullptr || outSize <= 0) {
    output = Uint8List(0);
  } else {
    output = Uint8List.fromList(result.asTypedList(outSize));
  }

  for (final buf in buffers) {
    calloc.free(buf);
  }
  calloc.free(channelPtrs);
  calloc.free(outSizePtr);

  return output;
}