Skip to content

Commit

Permalink
fix #273135: incorrect stereo samples playback
Browse files Browse the repository at this point in the history
Fix 'for' loop which converts floats to shorts
  • Loading branch information
anatoly-os committed Jun 8, 2018
1 parent 964c301 commit b5893a0
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions audiofile/audiofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,18 @@ sf_count_t AudioFile::readData(short* data, sf_count_t frames)
resFrames = sf_readf_short(sf, data, frames);
else {
//read native float values
float* dataF = new float[frames * channels()];
int totalFrames = frames * channels();
float* dataF = new float[totalFrames];
resFrames = sf_readf_float(sf, dataF, frames);
//find the maximum signal value
float maxSignal = 0.f;
for (int i = 0; i < resFrames; ++i) {
for (int i = 0; i < totalFrames; ++i) {
if (fabs(dataF[i]) > maxSignal)
maxSignal = dataF[i] > 0 ? dataF[i] : -dataF[i];
}
//convert normilized floats to signed short values
float adjScale = 1.f/maxSignal;
for (int i = 0; i < resFrames; ++i)
for (int i = 0; i < totalFrames; ++i)
data[i] = adjScale * lrintf(dataF[i] * (dataF[i] > 0 ? SHRT_MAX : -SHRT_MIN));
}

Expand Down

0 comments on commit b5893a0

Please sign in to comment.