alsa接口
Posted renhl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了alsa接口相关的知识,希望对你有一定的参考价值。
■ 基本流程
打开设备
设置参数
获取fd
poll
写入数据/读取数据
关闭
■ 写入数据
snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)
snd_pcm_hw_params_malloc(&hwparams)
snd_pcm_hw_params_any(pcm_handle, hwparams)
snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)
snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[ss->format])
snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &ss->rate, NULL)
snd_pcm_hw_params_set_channels(pcm_handle, hwparams, ss->channels)
snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, periods, NULL)
snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, buffer_size)
snd_pcm_hw_params(pcm_handle, hwparams)
snd_pcm_hw_params_free(hwparams)
*n_io_sources = snd_pcm_poll_descriptors_count(pcm_handle)
snd_pcm_poll_descriptors(pcm_handle, pfds, *n_io_sources)
poll
snd_pcm_prepare(u->pcm_handle)
snd_pcm_writei(u->pcm_handle, memchunk->memblock->data + memchunk->index, memchunk->length / u->frame_size)
snd_pcm_drop(u->pcm_handle)
snd_pcm_close(u->pcm_handle)
■ 读取数据
snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK) < 0) {
snd_pcm_hw_params_malloc(&hwparams)
snd_pcm_hw_params_any(pcm_handle, hwparams)
snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)
snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[ss->format])
snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &ss->rate, NULL)
snd_pcm_hw_params_set_channels(pcm_handle, hwparams, ss->channels)
snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, periods, NULL)
snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, buffer_size)
snd_pcm_hw_params(pcm_handle, hwparams)
snd_pcm_hw_params_free(hwparams)
snd_pcm_poll_descriptors_count(pcm_handle)
snd_pcm_poll_descriptors(pcm_handle, pfds, *n_io_sources)
poll
snd_pcm_state(u->pcm_handle)
snd_pcm_prepare(u->pcm_handle)
snd_pcm_readi(u->pcm_handle, u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length / u->frame_size))
snd_pcm_drop(u->pcm_handle);
snd_pcm_close(u->pcm_handle);
以上是关于alsa接口的主要内容,如果未能解决你的问题,请参考以下文章
Linux ALSA驱动之四:Control设备创建流程源码分析(5.18)