pcmToWave method
Converts a raw PCM file to a WAVE file.
Add a WAVE header in front of the PCM data
This verb is usefull to convert a Raw PCM file to a Wave file.
It adds a Wave
envelop to the PCM file, so that the file can be played back with startPlayer()
.
Note: the parameters numChannels
and sampleRate
are mandatory, and must match the actual PCM data.
See here a discussion about Raw PCM
and WAVE
file format.
Implementation
Future<void> pcmToWave({
required String inputFile,
required String outputFile,
required Pcm codec,
}) async {
if (codec.audioFormat != AudioFormat.raw || codec.sampleRate == null) {
throw Exception('Codec must be raw PCM');
}
var filIn = File(inputFile);
var filOut = File(outputFile);
var size = filIn.lengthSync();
logger.i(
'pcmToWave() : input = $inputFile, output = $outputFile, size = $size');
var sink = filOut.openWrite();
var header = WaveHeader(
WaveHeader.formatPCM,
codec.nbrChannels(), //
codec.sampleRate!,
16, // 16 bits per byte
size, // total number of bytes
);
header.write(sink);
await filIn.open();
var buffer = filIn.readAsBytesSync();
sink.add(buffer.toList());
await sink.close();
}