read method
Reads up to numSamples next audio sample frames from the WAV file.
Returns an IWavSamplesStorage containing the read samples, or null if EOF is reached.
Implementation
IWavSamplesStorage? read(int numSamples) {
if (_isClosed) throw StateError("WavReader is closed");
if (_currentSample >= totalSamples) return null;
final int samplesToRead = min(numSamples, totalSamples - _currentSample);
if (samplesToRead <= 0) return null;
final int bytesToRead = samplesToRead * format.blockAlign;
final bytes = file.readSync(bytesToRead);
if (bytes.isEmpty) return null;
final int actualSamplesRead = bytes.length ~/ format.blockAlign;
if (actualSamplesRead == 0) return null;
final ByteData chunkData = ByteData.sublistView(bytes);
late final IWavSamplesStorage storage;
switch (storageType) {
case StorageType.uint8:
storage = Uint8Storage.fromBytes(format.numChannels, chunkData, numEndianness);
break;
case StorageType.int16:
storage = Int16Storage.fromBytes(format.numChannels, chunkData, numEndianness);
break;
case StorageType.int32:
if (format.containerBitsPerSample == 24) {
storage = Int32Storage.fromBytes24(format.numChannels, chunkData, numEndianness);
} else {
storage = Int32Storage.fromBytes32(format.numChannels, chunkData, numEndianness);
}
break;
case StorageType.float32:
storage = Float32Storage.fromBytes(format.numChannels, chunkData, numEndianness);
break;
case StorageType.float64:
storage = Float64Storage.fromBytes(format.numChannels, chunkData, numEndianness);
break;
}
_currentSample += actualSamplesRead;
return storage;
}