Alsa异步回调

发布于 2025-01-04 01:17:04 字数 1119 浏览 0 评论 0原文

我正在尝试使用 ALSA 的异步回调功能,以便我可以在应用程序级别提供暂停和恢复功能。但函数 async_add_pcm_handler() 返回一个错误(更具体地说,返回 -38)。

rc = snd_pcm_open(&handle, (char*)"default",SND_PCM_STREAM_PLAYBACK, 0);
snd_pcm_hw_params_alloca(&params);
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2025-01-11 01:17:04

我遇到了与您的症状完全相同的问题,所以我希望解决方案是相同的。

我的解决方案是直接打开音频设备,而不是打开“默认”设备。这是名称类似于“hw:someName”的音频设备。
我相信最初的问题是“默认”设备是一个与pulseAudio声音服务器连接的虚拟设备——在我的Ubuntu linux上无论如何都是如此。由于pulseAudio声音服务器不支持完整的ALSA API,因此您会收到“功能未实现”错误。

为了澄清解决方案,替换

rc = snd_pcm_open(&handle, (char*)"default",SND_PCM_STREAM_PLAYBACK, 0);

rc = snd_pcm_open(&handle, (char*)"hw:someName",SND_PCM_STREAM_PLAYBACK, 0);

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

rc = snd_pcm_open(&handle, (char*)"default",SND_PCM_STREAM_PLAYBACK, 0);

with

rc = snd_pcm_open(&handle, (char*)"hw:someName",SND_PCM_STREAM_PLAYBACK, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文