0

I realized it is bad for me to neglect this thought, because I haven't read anything about number of channels and bits per sample in this light. My reason is that I'm not sure how the samples of 2-channel 8-bit PCM files will look like.

Is it 1 sample = 1 channel? or 1 sample = 4 bits (left) + 4 bits (right)

Context: I am writing a program that reads WAV files, and it occurred to me that if I come across 8-bit PCM WAV files, and my code reads this way (see below), then my program is unable to properly read multi-channel 8-bit PCM WAV files.


// read actual audio data after obtaining
// the headers
// audioData is a vector of vectors (1 vector per channel)
uint32_t temp;
while( !feof(wavFile) ) {
    for(uint16_t i = 0; i < numChannels; i++) {
        temp = 0;
        fread(&temp,sizeof(uint8_t),1,wavFile);
        audioData.at(i).push_back(temp); 
    }
}
Nogurenn
  • 858
  • 3
  • 11
  • 28
  • 1
    Your program will also be unable to properly read single-channel WAV files because you read 1-octet data to `uint32_t` and remain some bits uninitialized. Use `uint8_t` to read data having type `uint8_t` in. – MikeCAT Mar 06 '16 at 10:44
  • 1
    2-channel 8-bit WAV contain 16-bit blocks because it ls 8-bit * 2-channel. – MikeCAT Mar 06 '16 at 10:45
  • @MikeCAT Just confirming, this means 1-channel 8-bit WAV contains 8-bit blocks? – Nogurenn Mar 06 '16 at 15:06

1 Answers1

1

The structure, which typically describes format of WAV audio data, is described in MSDN here: WAVEFORMATEX structure:

  • "sample" for PCM audio is a block of data, which includes all channels
  • nBlockAlign value is size, in bytes, of such block corresponding to sample
  • samples go at specific fixed rate, defined by nSamplesPerSec value
  • each sample block consists of nChannels values, each one of wBitsPerSample

That is, two channel file with 8 bits per sample has nSamplesPerSec pairs for each second of audio data, each pair includes two 8-bit values for every channel of the two.

(here is an example of where this structure exists in the WAV file - though this is a more complicated case with 24-bits/sample, but you should get the idea).

Roman R.
  • 68,205
  • 6
  • 94
  • 158