<pre name="code" class="cpp">snd_pcm_t *pcm_handle; bool device_open(int mode) { <span style="white-space:pre"> </span>if (snd_pcm_open (&pcm_handle, “default” , mode , 0) < 0) <span style="white-space:pre"> </span>return false; <span style="white-space:pre"> </span>return true; }
</pre><pre name="code" class="cpp">int bit_per_sample; //样本长度(bit)
int period_size; //周期长度(桢数) <span style="font-family: 'Microsoft YaHei', 'Helvetica Neue', SimSun; text-indent: 2em;"> int chunk_byte; //周期长度(字节数)</span>
<span style="text-indent: 2em; font-family: 'Microsoft YaHei', 'Helvetica Neue', SimSun;">snd_pcm_hw_params_t *params; //定义参数变量 </span>
<span style="text-indent: 2em; font-family: 'Microsoft YaHei', 'Helvetica Neue', SimSun;">bool device_setparams(){ snd_pcm_hw_params_t *hw_params;</span>
</pre><pre name="code" class="cpp">snd_pcm_hw_params_malloc (&hw_params); //为参数变量分配空间
snd_pcm_hw_params_malloc (¶ms);
snd_pcm_hw_params_any ( pcm_handle, hw_params ); //参数初始化
snd_pcm_hw_params_set_access ( pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); //设置为交错模式
snd_pcm_hw_params_set_format( pcm_handle, hw_params, SND_FORMAT_S16_LE); //使用用16位样本
snd_pcm_hw_params_set_rate_near( pcm_handle, hw_params, 44100, 0); //设置采样率为44.1KHz
snd_pcm_hw_params_set_channels( pcm_handle, hw_params, 2); //设置为立体声
snd_pcm_hw_params_get_period_size( hw_params, &period_size); //获取周期长度
bit_per_sample = snd_pcm_hw_format_physical_width( hw_params.format ); //获取样本长度
chunk_byte = period_size * bit_per_sample * hw_params.channels / 8; //计算周期长度(字节数(bytes) = 每周期的桢数 * 样本长度(bit) * 通道数 / 8 )
snd_pcm_hw_params( pcm_handle, hw_params); //设置参数 params = hw_params; //保存参数,方便以后使用
snd_pcm_hw_params_free( hw_params); //释放参数变量空间
return true;
}
转载:http://blog.csdn.net/sepnic/article/details/6140824
原作者blog地址:http://blog.csdn.net/sepnic/