glintWriteWav function

Uint8List glintWriteWav(
  1. Float32List pcm,
  2. int channels,
  3. int sampleRate, {
  4. int bits = 16,
  5. bool floatFmt = false,
})

Encode interleaved float PCM (±1.0) to a WAV file buffer. bits: 8/16/24/32 integer PCM, or 32/64 with floatFmt for IEEE float (invalid combos fall back to 16-bit).

Implementation

Uint8List glintWriteWav(Float32List pcm, int channels, int sampleRate,
    {int bits = 16, bool floatFmt = false}) {
  if (channels < 1) throw StateError('bad channels');
  final lib = _loadLibrary();
  final fn = lib.lookupFunction<_WavWriteNative, _WavWrite>('glint_wav_write');
  final free = lib.lookupFunction<_FreeNative, _Free>('glint_free');
  final inPtr = calloc<Float>(pcm.length);
  inPtr.asTypedList(pcm.length).setAll(0, pcm);
  final outSize = calloc<Int32>();
  try {
    final frames = pcm.length ~/ channels;
    final ptr = fn(
        inPtr, frames, channels, sampleRate, bits, floatFmt ? 1 : 0, outSize);
    if (ptr == nullptr || outSize.value <= 0) {
      throw StateError('WAV write failed');
    }
    final out = Uint8List.fromList(ptr.asTypedList(outSize.value));
    free(ptr.cast<Void>());
    return out;
  } finally {
    calloc.free(inPtr);
    calloc.free(outSize);
  }
}