glintDecodeAudio function
Decode a whole encoded stream (MP3 / AAC-LC / Ogg-Opus, format auto-
detected, Opus surround included) to interleaved float PCM. rate
resamples the output (0 = keep native). Throws on unrecognized input.
Implementation
GlintDecodedAudio glintDecodeAudio(Uint8List data, {int rate = 0}) {
if (data.isEmpty) throw StateError('empty input');
final lib = _loadLibrary();
final fn =
lib.lookupFunction<_DecodeExNative, _DecodeEx>('glint_decode_audio_ex');
final free = lib.lookupFunction<_FreeNative, _Free>('glint_free');
final inPtr = calloc<Uint8>(data.length);
inPtr.asTypedList(data.length).setAll(0, data);
final sr = calloc<Int32>();
final ch = calloc<Int32>();
final fr = calloc<Int32>();
try {
final ptr = fn(inPtr, data.length, rate, 0, sr, ch, fr);
if (ptr == nullptr || ch.value <= 0) {
throw StateError('decode failed (unrecognized or corrupt input)');
}
final total = fr.value * ch.value;
final pcm = Float32List.fromList(ptr.cast<Float>().asTypedList(total));
free(ptr);
return GlintDecodedAudio(pcm, sr.value, ch.value);
} finally {
calloc.free(inPtr);
calloc.free(sr);
calloc.free(ch);
calloc.free(fr);
}
}