read static method

Wav read(
  1. Uint8List bytes
)

Read a Wav from a byte buffer.

Not all formats are supported. See WavFormat for a canonical list. Unrecognized metadata will be ignored.

Implementation

static Wav read(Uint8List bytes) {
  // Utils for reading.
  var byteReader = BytesReader(bytes)
    ..assertString(_kStrRiff)
    ..readUint32() // File size.
    ..assertString(_kStrWave)
    ..findChunk(_kStrFmt);
  final fmtSize = roundUpToEven(byteReader.readUint32());
  final formatCode = byteReader.readUint16();
  final numChannels = byteReader.readUint16();
  final samplesPerSecond = byteReader.readUint32();
  byteReader.readUint32(); // Bytes per second.
  final bytesPerSampleAllChannels = byteReader.readUint16();
  final bitsPerSample = byteReader.readUint16();
  if (fmtSize > _kFormatSize) byteReader.skip(fmtSize - _kFormatSize);

  byteReader.findChunk(_kStrData);
  final dataSize = byteReader.readUint32();
  final numSamples = dataSize ~/ bytesPerSampleAllChannels;
  final channels = <Float64List>[];
  for (int i = 0; i < numChannels; ++i) {
    channels.add(Float64List(numSamples));
  }
  final format = _getFormat(formatCode, bitsPerSample);

  // Read samples.
  final readSample = byteReader.getSampleReader(format);
  for (int i = 0; i < numSamples; ++i) {
    for (int j = 0; j < numChannels; ++j) {
      channels[j][i] = readSample();
    }
  }
  return Wav(channels, samplesPerSecond, format);
}