Alsa异步回调
我正在尝试使用 ALSA 的异步回调功能,以便我可以在应用程序级别提供暂停和恢复功能。但函数 async_add_pcm_handler()
返回一个错误(更具体地说,返回 -38)。
rc = snd_pcm_open(&handle, (char*)"default",SND_PCM_STREAM_PLAYBACK, 0);
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(handle, params);
snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(handle, params, 1);
val = 22050;
snd_pcm_hw_params_set_rate_near(handle, params,&val, &dir);
frames=128;
snd_pcm_hw_params_set_period_size_near(handle,params, &frames, &dir);
snd_pcm_hw_params(handle, params);
snd_pcm_hw_params_get_period_size(params, &frames,&dir);
size = frames * 2;
pcmfile=fopen("output.pcm","rb");
fseek(pcmfile,0,SEEK_SET);
buffer=(char*)malloc(size);
memset(buffer,0,size);
if(snd_async_add_pcm_handler(&pcm_callback, handle, MyCallback, NULL) != 0) {
printf("handler not successful\n");
}
while(!feof(pcmfile)){
returnvalue=fread(buffer,sizeof(char),size,pcmfile);
snd_pcm_writei(handle, buffer, frames);
}
上面是我正在使用的代码段,并定义了函数 MyCallback。你能指出我可能的错误吗?
I am trying to use the asynchronous callback functionality of ALSA so that I can provide the pause and resume functionality at the application level. But the function async_add_pcm_handler()
returns an error (returns -38 to be more specific).
rc = snd_pcm_open(&handle, (char*)"default",SND_PCM_STREAM_PLAYBACK, 0);
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(handle, params);
snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(handle, params, 1);
val = 22050;
snd_pcm_hw_params_set_rate_near(handle, params,&val, &dir);
frames=128;
snd_pcm_hw_params_set_period_size_near(handle,params, &frames, &dir);
snd_pcm_hw_params(handle, params);
snd_pcm_hw_params_get_period_size(params, &frames,&dir);
size = frames * 2;
pcmfile=fopen("output.pcm","rb");
fseek(pcmfile,0,SEEK_SET);
buffer=(char*)malloc(size);
memset(buffer,0,size);
if(snd_async_add_pcm_handler(&pcm_callback, handle, MyCallback, NULL) != 0) {
printf("handler not successful\n");
}
while(!feof(pcmfile)){
returnvalue=fread(buffer,sizeof(char),size,pcmfile);
snd_pcm_writei(handle, buffer, frames);
}
The above is the piece of code that I am using and the function MyCallback is defined. Could you point to me the possible mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了与您的症状完全相同的问题,所以我希望解决方案是相同的。
我的解决方案是直接打开音频设备,而不是打开“默认”设备。这是名称类似于“hw:someName”的音频设备。
我相信最初的问题是“默认”设备是一个与pulseAudio声音服务器连接的虚拟设备——在我的Ubuntu linux上无论如何都是如此。由于pulseAudio声音服务器不支持完整的ALSA API,因此您会收到“功能未实现”错误。
为了澄清解决方案,替换
为
I had a problem with exactly the same symptoms as yours so I hope the solution is the same.
My solution was that instead of opening the "default" device you open your audio device directly. That is the audio device with a name like "hw:someName".
I believe the original problem is that the "default" device is a virtual device that is interfaced with the pulseAudio Sound server - on my Ubuntu linux it is anyway. Since the pulseAudio sound server does not support the full ALSA API you get the "function not implemented" error.
To clarify the solution, replace
with