writeWavBytes function
Serialises decoded FLAC samples as a RIFF/WAVE byte buffer.
frames are the decoded audio frames, sampleRate and channels come
from STREAMINFO, and bitsPerSample is the FLAC stream's bit depth.
The WAV output uses a sample width rounded up to the nearest whole
byte: 1–8 → 8-bit unsigned, 9–16 → 16-bit signed LE, 17–24 → 24-bit
signed LE, 25–32 → 32-bit signed LE.
Implementation
Uint8List writeWavBytes({
required Iterable<FlacFrame> frames,
required int sampleRate,
required int channels,
required int bitsPerSample,
}) {
final outBps = ((bitsPerSample + 7) ~/ 8) * 8;
final frameList = frames is List<FlacFrame> ? frames : frames.toList();
final builder = BytesBuilder(copy: false);
for (final f in frameList) {
builder.add(frameToWavPcmBytes(f, outBps));
}
final pcm = builder.takeBytes();
final dataSize = pcm.length;
final header = writeWavHeaderBytes(
dataSize: dataSize,
sampleRate: sampleRate,
channels: channels,
bitsPerSample: outBps,
);
final out = Uint8List(header.length + dataSize);
out.setRange(0, header.length, header);
out.setRange(header.length, header.length + dataSize, pcm);
return out;
}