pcmToWave method

Future<void> pcmToWave({
  1. required String inputFile,
  2. required String outputFile,
  3. int numChannels = 1,
  4. int sampleRate = 16000,
})

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,

  /// Stereophony is not yet implemented
  int numChannels = 1,
  int sampleRate = 16000,
}) async {
  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,
    numChannels = numChannels, //
    sampleRate = 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();
}