ALSA Programming HOWTO
根据ALSA写一简单的PCM应用程序,我们首先需要为PCM设备打开一个句柄(Handle),然后指定PCM流的方向是播放或者是捕获(playback还是capture),我们也可以配置一些我们想要的参数,比如,buffer size, sample rate, pcm数据格式等.因此我们就有了一个大体的框架了,简单高效,如下:
/* Handle for the PCM device */ 声明一个设备句柄. snd_pcm_t *pcm_handle; /* Playback stream */ 设置音频流的方向为播放 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK; /* This structure contains information about */声明一个硬件配置结构,用来配置相应的参数. /* the hardware and can be used to specify the */ /* configuration to be used for the PCM stream. */ snd_pcm_hw_params_t *hwparams;PCM设备在ALSA的接口有"plughw"和"hw",接口,不过我们这里使用默认的接口"default",
char *pcm_name;//定义设备名称然后我们分配一个硬件配置结构体pcm_name = strdup("default");
/* Allocate the snd_pcm_hw_params_t structure on the stack. */ snd_pcm_hw_params_alloca(&hwparams);现在我们可以打开设备了,
/* Open PCM. The last parameter of this function is the mode. */ /* If this is set to 0, the standard mode is used. Possible */ /* other values are SND_PCM_NONBLOCK and SND_PCM_ASYNC. */ /* If SND_PCM_NONBLOCK is used, read / write access to the */ /* PCM device will return immediately. If SND_PCM_ASYNC is */ /* specified, SIGIO will be emitted whenever a period has *