Fixed WAVE file reader position calculation

The bug only showed through massive seek times.
This commit is contained in:
Daniel Wolf 2016-06-14 19:46:30 +02:00
parent 522f6c2019
commit c4b054176c
2 changed files with 13 additions and 15 deletions

View File

@ -28,7 +28,7 @@ enum class Codec {
WaveFileReader::WaveFileReader(boost::filesystem::path filePath) : WaveFileReader::WaveFileReader(boost::filesystem::path filePath) :
filePath(filePath), filePath(filePath),
file(), file(),
sampleIndex(0) frameIndex(0)
{ {
openFile(); openFile();
@ -113,7 +113,7 @@ WaveFileReader::WaveFileReader(boost::filesystem::path filePath) :
case fourcc('d', 'a', 't', 'a'): { case fourcc('d', 'a', 't', 'a'): {
reachedDataChunk = true; reachedDataChunk = true;
dataOffset = file.tellg(); dataOffset = file.tellg();
sampleCount = chunkSize / bytesPerSample; int sampleCount = chunkSize / bytesPerSample;
frameCount = sampleCount / channelCount; frameCount = sampleCount / channelCount;
break; break;
} }
@ -127,7 +127,7 @@ WaveFileReader::WaveFileReader(boost::filesystem::path filePath) :
if (!reachedDataChunk) { if (!reachedDataChunk) {
dataOffset = file.tellg(); dataOffset = file.tellg();
sampleCount = frameCount = 0; frameCount = 0;
} }
} }
@ -139,12 +139,11 @@ WaveFileReader::WaveFileReader(const WaveFileReader& rhs, bool reset) :
frameRate(rhs.frameRate), frameRate(rhs.frameRate),
frameCount(rhs.frameCount), frameCount(rhs.frameCount),
channelCount(rhs.channelCount), channelCount(rhs.channelCount),
sampleCount(rhs.sampleCount),
dataOffset(rhs.dataOffset), dataOffset(rhs.dataOffset),
sampleIndex(-1) frameIndex(-1)
{ {
openFile(); openFile();
seek(reset ? 0 : rhs.sampleIndex); seek(reset ? 0 : rhs.frameIndex);
} }
std::unique_ptr<AudioStream> WaveFileReader::clone(bool reset) const { std::unique_ptr<AudioStream> WaveFileReader::clone(bool reset) const {
@ -178,19 +177,19 @@ int64_t WaveFileReader::getSampleCount() const {
} }
int64_t WaveFileReader::getSampleIndex() const { int64_t WaveFileReader::getSampleIndex() const {
return sampleIndex; return frameIndex;
} }
void WaveFileReader::seek(int64_t sampleIndex) { void WaveFileReader::seek(int64_t frameIndex) {
if (sampleIndex < 0 || sampleIndex > sampleCount) throw std::invalid_argument("sampleIndex out of range."); if (frameIndex < 0 || frameIndex > frameCount) throw std::invalid_argument("frameIndex out of range.");
file.seekg(dataOffset + static_cast<std::streamoff>(sampleIndex * channelCount * bytesPerSample)); file.seekg(dataOffset + static_cast<std::streamoff>(frameIndex * channelCount * bytesPerSample));
this->sampleIndex = sampleIndex; this->frameIndex = frameIndex;
} }
float WaveFileReader::readSample() { float WaveFileReader::readSample() {
if (sampleIndex + channelCount > sampleCount) throw std::out_of_range("End of stream."); if (frameIndex + channelCount > frameCount) throw std::out_of_range("End of stream.");
sampleIndex += channelCount; ++frameIndex;
float sum = 0; float sum = 0;
for (int channelIndex = 0; channelIndex < channelCount; channelIndex++) { for (int channelIndex = 0; channelIndex < channelCount; channelIndex++) {

View File

@ -33,7 +33,6 @@ private:
int frameRate; int frameRate;
int64_t frameCount; int64_t frameCount;
int channelCount; int channelCount;
int64_t sampleCount;
std::streampos dataOffset; std::streampos dataOffset;
int64_t sampleIndex; int64_t frameIndex;
}; };