alsa 编程入门

来源:互联网 发布:黑客linux入门 编辑:程序博客网 时间:2024/06/13 23:24


        ALSA是Advanced Linux Sound Architecture,高级Linux声音架构的简称,它在Linux操作系统上提供了音频和MIDI(Musical Instrument Digital Interface,音乐设备数字化接口)的支持。在2.6系列内核中,ALSA已经成为默认的声音子系统,用来替换2.4系列内核中的OSS(Open Sound System,开放声音系统)。

因此学习alsa迫在眉睫。参考文档:http://users.suse.com/~mana/alsa090_howto.html

使用alsa播放、录音的基本步骤:

1. 打开设备(open pcm device)

/* Playback stream */

snd_pcm_stream_t stream = SND_PCM_STREAM_PLLAYBACK;

/* Capture stream */
snd_pcm_stream_t stream_capture = SND_PCM_STREAM_CAPTURE;

int snd_pcm_open(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);

2. 为配置参数分配空间并初始化(Then we initialize the variables and allocate a hwparams structure)

/* Allocate the snd_pcm_hw_params_t structure on the stack  */

snd_pcm_hw_params_alloca 

/* Fill it in with default values. */

int snd_pcm_hw_params_any(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);

3. 设置存取方式(set access type)

/* Set access type. This can be either    */
/* SND_PCM_ACCESS_RW_INTERLEAVED or       */
/* SND_PCM_ACCESS_RW_NONINTERLEAVED.      */

int snd_pcm_hw_params_set_access(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t _access);

4. 设置采样格式(set sample format)

/* Signed 16 bit Little Endian   SND_PCM_FORMAT_S16_LE*/

int snd_pcm_hw_params_set_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val);

5. 设置采样频率(set sample rate)

int snd_pcm_hw_params_set_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);

6. 设置通道数(set number of channels)

int snd_pcm_hw_params_set_channels(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);

7. 设置帧的周期大小(Set period size of frames.

/* frame as the unit of period*/

int snd_pcm_hw_params_set_period_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val, int dir);

8. 设置缓冲区大小(Set buffer size (in frames). )

/* periods = period_size * n */ /*  用足够大的缓冲区()存储帧数据  */

int snd_pcm_hw_params_set_buffer_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val);

9. 应用设置(apply to configuration)

int snd_pcm_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);

10. 读设备或写设备

/* playback stream */

snd_pcm_sframes_t snd_pcm_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);

/* capture stream */

snd_pcm_sframes_t snd_pcm_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size);


note: 不同的设备,所支持的存取类型、缓冲区大小、通道数、采样格式、采样频率和同期数(periods),在不清楚设备特性的情况下,可以使用一系列的测试函数,测试出支持的参数,再使用一系列的设置函数设置参数, 或使用以near结尾的设置函数,系统根据所给的参数选择相近的参数来设置参数。

如:

/* dir 控制选择参数值与所给参数值的大小关系 */

int rate = 44100; /* Sample rate */
int exact_rate;   /* Sample rate returned by */

/* exact_rate == rate --> dir = 0 */
/* exact_rate < rate  --> dir = -1 */
/* exact_rate > rate  --> dir = 1 */

/* 测试采样频率的函数 */

int snd_pcm_hw_params_test_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);

/* 以near结尾的采样频率设置函数 */

int snd_pcm_hw_params_set_rate_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);


注:防止在录放音时出现UI无响应的情况,读取或写入的帧数不要过少,读取或写入的帧数多少决定着下一次读取或写入的间隔时间,如果操作的帧数较多,则有更多的空闲时间来响应UI。


欢迎转载!出处:http://blog.csdn.net/xiarong715/article/details/24305753



0 0