Skip to content

Commit

Permalink
Fix reading of stereo WAV files
Browse files Browse the repository at this point in the history
  • Loading branch information
ggerganov committed Oct 1, 2022
1 parent 62897e8 commit 6d654d1
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2140,20 +2140,22 @@ int main(int argc, char ** argv) {
return 5;
}

int n = wav.totalPCMFrameCount;

std::vector<int16_t> pcm16;
pcm16.resize(wav.totalPCMFrameCount);
drwav_read_pcm_frames_s16(&wav, wav.totalPCMFrameCount, pcm16.data());
pcm16.resize(n*wav.channels);
drwav_read_pcm_frames_s16(&wav, n, pcm16.data());
drwav_uninit(&wav);

// convert to float
pcmf32.resize(pcm16.size());
// convert to mono, float
pcmf32.resize(n);
if (wav.channels == 1) {
for (size_t i = 0; i < pcm16.size(); i++) {
for (size_t i = 0; i < n; i++) {
pcmf32[i] = float(pcm16[i])/32768.0f;
}
} else {
for (size_t i = 0; i < pcm16.size(); i++) {
pcmf32[i] = float(pcm16[i*2 + 0] + pcm16[i*2 + 1])/32768.0f/2.0f;
for (size_t i = 0; i < n; i++) {
pcmf32[i] = float(pcm16[2*i] + pcm16[2*i + 1])/65536.0f;
}
}
}
Expand Down

0 comments on commit 6d654d1

Please sign in to comment.