static int snd_pcm_init(snd_para_t *snd_para,snd_pcm_t *pcm)
{
snd_pcm_hw_params_t *hwparams = NULL;
int ret;
if(snd_para->alsa_pcm_dev_name!=NULL)
// ret = snd_pcm_open(&(pcm), snd_para->alsa_pcm_dev_name, snd_para->stream, 0);
ret = snd_pcm_open(&(pcm), ALSA_PCM_PLAYBACK_DEV, snd_para->stream, 0);
else{
puts(" pcm dev name is null\n");
return -1;
}
if (0 != ret) {
fprintf(stderr, "snd_pcm_open error: %s: %s\n",
snd_para->alsa_pcm_dev_name, snd_strerror(ret));
return -1;
}
snd_pcm_hw_params_malloc(&hwparams);
ret = snd_pcm_hw_params_any(pcm, hwparams);
if (0 != ret) {
fprintf(stderr, "snd_pcm_hw_params_any error: %s\n", snd_strerror(ret));
goto err2;
}
ret = snd_pcm_hw_params_set_access(pcm, hwparams, snd_para->_access);
if (0 != ret) {
fprintf(stderr, "snd_pcm_hw_params_set_access error: %s\n", snd_strerror(ret));
goto err2;
}
ret = snd_pcm_hw_params_set_format(pcm, hwparams, snd_para->fmt);
if (0 != ret) {
fprintf(stderr, "snd_pcm_hw_params_set_format error: %s\n", snd_strerror(ret));
goto err2;
}
ret = snd_pcm_hw_params_set_rate(pcm, hwparams, snd_para->sample_rate, 0);
if (0 != ret) {
fprintf(stderr, "snd_pcm_hw_params_set_rate error: %s\n", snd_strerror(ret));
goto err2;
}
ret = snd_pcm_hw_params_set_channels(pcm, hwparams, snd_para->channels);
if (0!= ret) {
fprintf(stderr, "snd_pcm_hw_params_set_channels error: %s\n", snd_strerror(ret));
goto err2;
}
ret = snd_pcm_hw_params_set_period_size(pcm, hwparams, snd_para->period_size, 0);
if (0 != ret) {
fprintf(stderr, "snd_pcm_hw_params_set_period_size error: %s\n", snd_strerror(ret));
goto err2;
}
ret = snd_pcm_hw_params_set_periods(pcm, hwparams, snd_para->periods, 0);
if (0!= ret) {
fprintf(stderr, "snd_pcm_hw_params_set_periods error: %s\n", snd_strerror(ret));
goto err2;
}
ret = snd_pcm_hw_params(pcm, hwparams);
snd_pcm_hw_params_free(hwparams);
if (0 != ret) {
fprintf(stderr, "snd_pcm_hw_params error: %s\n", snd_strerror(ret));
goto err1;
}
return 0;
err2:
snd_pcm_hw_params_free(hwparams); //释放内存
err1:
snd_pcm_close(pcm); //关闭 pcm 设备
return -1;
}