tags:

views:

703

answers:

4

I need a library for MS VC6 which encodes sampled data which is in the form of a float array, to an audio file format preferably wav Also is there a library that can encode the samples into pcm form and play it directly through the sound card without saving a wav file first??

A: 

A wav file is rather simple. It has a 44 byte header, followed by the sound data, in integer format.

The header is described in detail here.

Shmoopty
A: 

As noted, the WAV file format is very simple. To just play the samples, use the waveOut functions; they're documented.

To convert from a float to a signed 16-bit PCM sample, just convert the sample into the 16 bit range. For example, assuming a sample in the range -1.0 to +1.0 multiply by 32767.0 and convert to an integer:

int16_t sample = static_cast<int16_t>(32767.0 * float_sample);

Once you have those, just use the waveOut* functions to play the samples.

janm
+1  A: 

It looks like libsndfile (www.mega-nerd.com/libsndfile/) and SndLib (ccrma.stanford.edu/software/snd/sndlib/) will do what you want.

Larry Engholm
A: 

Here's a simple class that reads/writes wav files: http://www.thisisnotalabel.com/wavio/wavIO.cpp I can confirm that it works well. libsndfile is great, but overkill if you just want to write a simple wav file.

mazbox