I'm trying to export a single channel WAV file as a 2-channel WAV file (basically duplicate the original channel); however, my program is doing this strange behavior after I add numChannels = channels
. I have no clue as to why it behaves like this. I also tried modifying the line with &
, *
, and bitwise operations, but it still results in those weird output. My only lead [since hours ago] is the problematic line above.
Disclaimer: Sorry, I don't know how to explain the problem without showing the entire source. I did try putting it all here, but I figured this would be more concise (and I'm at my wit's end here). Here's the link to the source code containing the problematic line. The code snippet containing the line is below the output.
Without numChannels = channels
5249 4646 f052 0200 5741 5645 666d 7420
1000 0000 0100 0100 112b 0000 112b 0000
0100 0800 6461 7461 cb52 0200 8080 8080
8080 8080 8180 8080 8080 8080 8080 8080
8080 8080 8080 8080 8080 8180 8180 8180
8180 8180 8180 8180 8180 8180 8180 8180
8180 8180 8180 8180 8180 8180 8180 8180
8180 8180 8180 8180 8180 8180 8180 8180
With numChannels = channels
5249 4646 f052 0200 5741 5645 666d 7420
1000 0000 0100 0200 112b 0000 2256 0000
0200 0800 6461 7461 94a5 0400 8080 8080
8080 8080 8080 8080 8080 8080 8181 8080
8080 8080 8080 8080 8080 8080 8080 8080
8080 8080 8080 8080 8080 8080 8080 8080
8080 8080 8080 8080 8181 8080 8181 8080
8181 8080 8181 8080 8181 8080 8181 8080
8181 8080 8181 8080 8181 8080 8181 8080
8181 8080 8181 8080 8181 8080 8181 8080
8181 8080 8181 8080 8181 8080 8181 8080
8181 8080 8181 8080 8181 8080 8181 8080
8181 8080 8181 8080 8181 8080 8181 8080
Pseudo-code
- Create the WAVFile object, read (using fread) and store the opened wav file contents.
- Convert the wav file in to 2 channels (Problematic line here. WAVFile.cpp, WAVFile::changeNumChannels).
- Export (using fwrite) the modified contents as a new wav file.
Code Snippet
// FILE * audio;
// uint16_t numChannels;
// audio = fopen("11k8bitpcm.wav", "rb");
// fread(...);
// fread(&numChannels, sizeof(uint16_t), 1, );
bool WAVFile::changeNumChannels(uint16_t channels) {
if(numChannels == channels) return false;
else if(numChannels < channels) {
audioData8.insert(audioData8.end(), channels - numChannels, vector<uint8_t>());
for(uint16_t i = numChannels; i < channels; i++)
audioData8.at(i) = audioData8.at(i - numChannels);
}
numChannels = channels;
byteRate = channels * sampleRate * (bitsPerSample/8);
blockAlign = channels * (bitsPerSample/8);
uint32_t numSamples = subchunk2Size / (channels * (bitsPerSample/8));
subchunk2Size = blockAlign * numSamples * (channels * (bitsPerSample/8));
return true;
}