Read audio wav samples

Hello everyone!
What is the easiest and most efficient way to read a wav file in C ++ and have the float samples of the audio track?
I'd like to have something like the Matlab audioread() function.
Thanks in advance.
I assume you're using windows.
I would start by making a class or struct that represents the WAVE RIFF header format, which is described here: http://soundfile.sapp.org/doc/WaveFormat/

Then I would write a utility function which reads your sound file using an std::ifstream stream object, populating your header struct.

Using the information in your header struct enables you to determine if samples are mono or stereo, and what bit depth they have. That information will be necessary when creating an array or container of samples.

NOTE: One thing to watch out for are non-canonical wave files, as a naive implementation of your header class or struct will probably not support them. These types of wave files can have variable amounts of additional "Chunks" in their meta-data, which makes them more difficult to parse. The simplest solution would use the subchunk1size field to assert what the rest of the size (that follows this number) of the subchunk is (which should be 16 for PCM). Additionally, the data chunk doesn't necessarily immediately follow the fmt chunk, so it is wise to skip potential chunks until you find one whose ID field is "data".
Also, if your RIFF chunk descriptor starts with "RIFF", you're dealing with little-endianess.
Last edited on
Don't do it yourself; use a library.
Here's a decent one: http://soundfile.sapp.org/
Topic archived. No new replies allowed.