encode method

Uint8List encode(
  1. Int16List pcm
)

Encode one frame of interleaved 16-bit PCM samples.

Implementation

Uint8List encode(Int16List pcm) {
  _checkNotDisposed();

  final channelPtrs = calloc<Pointer<Int16>>(channels);
  final buffers = <Pointer<Int16>>[];
  for (var ch = 0; ch < channels; ch++) {
    final buf = calloc<Int16>(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 = _aacEncode(_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;
}