user2530847
user2530847

Reputation: 49

Using the libsndfile library to read a WAV file in C++

I am using libsndfile in C++ to read a WAV file. There are two points I don't understand:

  1. How can I get the "Bits per sample" value for the WAV file in question? I read the documentation at the website http://www.mega-nerd.com/libsndfile/api.html, but I didn't find a member for "Bits per sample" in the SF_INFO struct.
  2. Using the WAV file, how can I create the data to use to draw vectors for describing the sound data, read by function sf_readf_float() in library sndfile.h? Is there any method to do this?

Upvotes: 1

Views: 3206

Answers (1)

Mauro H. Leggieri
Mauro H. Leggieri

Reputation: 1104

The format field will give you the BPS. For e.g.: SF_FORMAT_PCM_16.

The sf_readf_float will convert the sample into the -1.0 to 1.0 range no matter the bps of the input sound. You only have to take care that about the audio channels. If the audio has 2 channels and you read 4 floats, you will have:

sample-1 of left channel
sample-1 of right channel
sample-2 of left channel
sample-2 of right channel

Then, to draw the point you must convert the [-1.0;1.0] to the viewport height. For e.g. if viewport is at Y=20 and the height is 300px, the formula is:

PY = (int)(20.0 + (sample_value / 2.0 + 0.5) * 300.0);

Upvotes: 3

Related Questions