writeRawAudio function

Uint8List writeRawAudio(
  1. List<Float64List> channels,
  2. WavFormat format
)

Writes raw audio samples to a byte buffer.

Raw audio files are essentially Wav files without a format header. So this function will not write any metadata to the buffer (bits per sample, number of channels or sample rate).

Implementation

Uint8List writeRawAudio(List<Float64List> channels, WavFormat format) {
  // Calculate sizes etc.
  final numChannels = channels.length;
  int numSamples = 0;
  for (final channel in channels) {
    if (channel.length > numSamples) numSamples = channel.length;
  }

  // Write samples.
  final bytes = BytesWriter();
  final writeSample = bytes.getSampleWriter(format);
  for (int i = 0; i < numSamples; ++i) {
    for (int j = 0; j < numChannels; ++j) {
      writeSample(i < channels[j].length ? channels[j][i] : 0);
    }
  }

  return bytes.takeBytes();
}