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)
32 bit float
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.
x bit unsigned
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
x bit signed
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:
- If the number was positive you can just pad with 0
- If the number was negative (highest bit of the most significant byte is set) then you pad with with \xFF bytes.
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.