The first thing you need to do in order to play back or capture sound is open a connection to a PCM playback or capture device.
The API calls for opening a PCM device are:
Using this function makes your application more flexible, because you don't need to know the card and device numbers; the function can pass back to you the card and device that it opened.
All these API calls set a PCM connection handle that you'll use as an argument to all other PCM API calls. This handle is very analogous to a file stream handle. It's a pointer to a snd_pcm_t structure, which is an opaque data type.
These functions, like others in the QSA API, work for both capture and playback channels. They take as an argument a channel direction, which is one of:
This code fragment from the wave.c example in the appendix uses these functions to open a playback device:
if (card == -1) { if ((rtn = snd_pcm_open_preferred (&pcm_handle, &card, &dev, SND_PCM_OPEN_PLAYBACK)) < 0) return err ("device open"); } else { if ((rtn = snd_pcm_open (&pcm_handle, card, dev, SND_PCM_OPEN_PLAYBACK)) < 0) return err ("device open"); }
If the user specifies a card and a device number on the command line, this code opens a connection to that specific PCM playback device. If the user doesn't specify a card, the code creates a connection to the preferred PCM playback device, and snd_pcm_open_preferred() stores the card and device numbers in the given variables.