decode method

({int channels, Float32List pcm, int sampleRate}) decode(
  1. Uint8List ogg
)

Decode a complete Ogg-Vorbis stream. Returns the interleaved float PCM with its sample rate and channel count. Throws StateError on a non-Vorbis or corrupt buffer.

Implementation

({int sampleRate, int channels, Float32List pcm}) decode(Uint8List ogg) {
  if (ogg.isEmpty) throw StateError('empty input');
  final inPtr = calloc<Uint8>(ogg.length);
  inPtr.asTypedList(ogg.length).setAll(0, ogg);
  final sr = calloc<Int32>();
  final ch = calloc<Int32>();
  final fr = calloc<Int32>();
  try {
    final ptr = _decode(inPtr, ogg.length, sr, ch, fr);
    if (ptr == nullptr || ch.value <= 0) {
      throw StateError('vorbis decode failed (not Vorbis or corrupt)');
    }
    final total = fr.value * ch.value;
    final pcm = Float32List.fromList(ptr.asTypedList(total));
    _free(ptr.cast<Void>());
    return (sampleRate: sr.value, channels: ch.value, pcm: pcm);
  } finally {
    calloc.free(inPtr);
    calloc.free(sr);
    calloc.free(ch);
    calloc.free(fr);
  }
}