Digital Sound & Music: Concepts, Applications, & Science, Chapter 5, last updated 6/25/2013
42
installation of the sound libraries into the Linux environment and show you how to program with
the libraries. (See the.websites cited in the references at the end of the chapter.) The ALSA
version of Program 5.1 is given in Program 5.2.
/*This program demonstrates how to read in a raw
data file and write it to the sound device to be played.
The program uses the ALSA library.
Use option -lasound on compile line.*/
#include /usr/include/alsa/asoundlib.h
#include math.h
#include iostream
using namespace std;
static char *device = "default"; /*default playback device */
int main(int argc, char* argv[])
{
int err, numSamples;
snd_pcm_t *handle;
snd_pcm_sframes_t frames;
numSamples = atoi(argv[1]);
char* samples = (char*) malloc((size_t) numSamples);
FILE *inFile = fopen(argv[2], "rb");
int numRead = fread(samples, 1, numSamples, inFile);
fclose(inFile);
if ((err=snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) 0){
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err =
snd_pcm_set_params(handle,SND_PCM_FORMAT_U8,
SND_PCM_ACCESS_RW_INTERLEAVED,1,44100, 1, 100000) ) 0 ){
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
frames = snd_pcm_writei(handle, samples, numSamples);
if (frames 0)
frames = snd_pcm_recover(handle, frames, 0);
if (frames 0) {
printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
}
snd_pcm_close(handle);
return 0;
}
Program 5.2 Reading raw data in a C++ program, ALSA library
5.3.3 Reading and Writing Formatted Audio Files in C++
So far, we've worked only with raw audio data. However, most sound is handled in file types
that store not only the raw data but also a header containing information about how the data is
formatted. Two of the commonly-used file formats are WAV and AIFF. If you want to read or
write a WAV file without the help of an existing library of functions, you need to know the
required format.
Previous Page Next Page