Below is the code I have used to read 16-bit and 32-bit per sample wavs which work just fine.
My question is, how can I read the remaining 8-bit unsigned, 24-bit signed, and 32-bit float wavs?
Read one sample of a 16-bit signed wav:
short buffer;
file.read( ( char * ) &readbuffer, 2 );
Read one sample of a 32-bit signed wav:
int buffer;
file.read( ( char * ) &readbuffer, 4 );
You're making a few assumptions about the target machine. According to the Microsoft WAV format, All sample data is little-endian. You're also expecting the various data types to be the size that you want, which may not always be the case.
But as your current routines work for you, we won't focus on that for the moment (but you probably should fix these things at some point)
If we forget about the scary endian-ness and non-standard type sizes, the 32-bit float case becomes relatively straightforward using your other code as a template:
float buffer;
file.read( ( char * ) &buffer, 4 );
This question covers reading floats from a binary source in a lot more detail.
Since we know that your machine is correctly interpreting the 16 and 32 bit cases, we can assume it is little endian. Which means you can just read everything into an unsigned int that has been initialized to zero and the remaining bytes are already correctly padded for you:
unsigned int buffer = 0;
file.read( ( char * ) &buffer, 1 ); // 8bit unsigned integer
buffer = 0;
file.read( ( char * ) &buffer, 3 ); // 24bit unsigned integer
Finally, if you're reading a signed integer, you need to pad the remaining bytes of your buffer variable depending on the value of the number you just read:
This code works on a 24 bit signed integer:
long buffer;
int number_of_bytes = 3; // 24 bit signed integer
file.read( (char *) &buffer, number_of_bytes);
// Determine the padding byte
unsigned char padding_byte = 0;
if ( ((char*) &buffer)[number_of_bytes - 1] & 128) {
padding_byte = 255;
}
// Pad the data
for (int i = number_of_bytes; i < sizeof(buffer); i++) {
((char*) &buffer)[i] = padding_byte;
}
Again, I feel I should point out that this code will fail on some machines because you're not checking endian-ness. But all you need to do to fix that is check the endian-ness of the machine that's running the code and reverse the order of the bytes if you're on a big-endian machine.