gpt4 book ai didi

c - ALSA 的依赖问题(libao 和 sndfile)

转载 作者:行者123 更新时间:2023-12-04 07:21:56 33 4
gpt4 key购买 nike

我编写了一个小程序来播放 PCM 音频文件,使用来自 github 的引用,并对我的应用程序稍作修改。该程序编译良好,但我在依赖项和 ALSA 设备方面遇到了一些运行时问题。
我收到的错误是:

ERROR: Failed to load plugin /usr/lib/x86_64-linux-gnu/ao/plugins-4/libsndio.so => dlopen() failed
ERROR: Failed to load plugin /usr/lib/x86_64-linux-gnu/ao/plugins-4/libnas.so => dlopen() failed
ao_alsa WARNING: Unable to open surround playback. Trying default device...
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
ao_alsa ERROR: Unable to open ALSA device 'default' for playback => No such file or directory
我一直在研究许多不同的解决方案,但似乎都没有奏效,包括在/etc/modprobe.d 中的 alsa.conf 文件中添加一行(但是,这些解决方案建议在我使用 AMD 时添加一行引用 intel设置。
这是完整的代码:
/*
*
* ao_example.c
*
* Written by Stan Seibert - July 2001
*
* Legal Terms:
*
* This source file is released into the public domain. It is
* distributed without any warranty; without even the implied
* warranty * of merchantability or fitness for a particular
* purpose.
*
* Function:
*
* This program opens the default driver and plays a 440 Hz tone for
* one second.
*
* Compilation command line (for Linux systems):
*
* gcc -o ao_example ao_example.c -lao -ldl -lm -lsndfile
*
*/

#include <stdio.h>
#include <string.h>
#include <ao/ao.h>
#include <math.h>
#include <sndfile.h>

#define BUF_SIZE 4096

static void clean(ao_device *device, SNDFILE *file){
ao_close(device);
sf_close(file);
ao_shutdown();
}

int main(int argc, char **argv)
{
ao_device *device;
ao_sample_format format;
unsigned long count;
int default_driver;
short *buffer;
int buf_size;
int sample;
int i;
//FILE *fp;
SF_INFO sfinfo;

if (argc != 2){
printf("usage: %s <filename>\n", argv[0]);
exit(1);
}

/* -- Initialize -- */

fprintf(stderr, "libao example program\n");

SNDFILE *fp = sf_open(argv[1], SFM_READ, &sfinfo);

// fp = fopen(argv[1], "rb");
if (fp == NULL){
printf("Cannot open %s.\n", argv[1]);
exit(1);
}

printf("samples: %d\n", sfinfo.frames);
printf("sample rate: %d\n", sfinfo.samplerate);
printf("Channels: %d\n", sfinfo.channels);

ao_initialize();

/* -- Setup for default driver -- */

default_driver = ao_default_driver_id();

switch(sfinfo.format & SF_FORMAT_SUBMASK){
case SF_FORMAT_PCM_16:
format.bits = 16;
break;
case SF_FORMAT_PCM_24:
format.bits = 24;
break;
case SF_FORMAT_PCM_32:
format.bits = 32;
break;
case SF_FORMAT_PCM_S8:
format.bits = 8;
break;
case SF_FORMAT_PCM_U8:
format.bits = 8;
break;
default:
format.bits = 24;
break;
}

format.channels = sfinfo.channels;
format.rate = sfinfo.samplerate;
format.byte_format = AO_FMT_LITTLE;
// format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;

// memset(&format, 0, sizeof(format));
// format.bits = 24;
// format.channels = 16;
// format.rate = 48000;
// format.byte_format = AO_FMT_LITTLE;

/* -- Open driver -- */
device = ao_open_live(default_driver, &format, NULL /* no options */);

if (device == NULL) {
fprintf(stderr, "Error opening device.\n");
return 1;
}

// fseek(fp, 0, SEEK_END);
// count = ftell(fp);
// fseek(fp, 0, SEEK_SET);

// // printf("count: %ld\n", count);

buffer = calloc(BUF_SIZE, sizeof(short));

while(1){
int read = sf_read_short(fp, buffer, BUF_SIZE);

if (ao_play(device, (char *) buffer, (uint_32)(read * sizeof(short))) == 0){
printf("ao_play: failed\n");
clean(device, fp);
break;
}
}

clean(device, fp);

return 0;
}
我希望其他人也遇到了同样的问题,并能阐明解决方案。谢谢你。

最佳答案

总结讨论:
问题似乎不在于代码本身,而在于 libao配置。
根据libao documentation ,库尝试确定默认驱动程序如下:

In the absence of configuration files to explicit identify a defaultdriver, the library will try to detect a suitable default driver. Itdoes this by testing every available live output driver (usingao_plugin_test()) and finding the driver with the highest priority(see the ao_info struct) that works. Drivers with priority 0, such asthe null and file output drivers, are never selected as the default.


错误消息表明包括 nas 在内的几个驱动程序, sndioalsa都试过了。使用 ao_driver_id(...) 手动选择驱动程序而不是使用 ao_default_driver_id()解决了这个问题。
使用 ao_open_live(...) 打开设备的其他问题可以通过 printf("errno %d\n", errno); 获取相应的错误号进行调查.输出可以解释如下:
AO_ENODRIVER (1) - No driver corresponds to driver_id.
AO_ENOTLIVE (3) - This driver is not a live output device.
AO_EBADOPTION (4) - A valid option key has an invalid value.
AO_EOPENDEVICE (5) - Cannot open the device (for example, if /dev/dsp cannot be opened for writing).
AO_EFAIL (100) - Any other cause of failure.
除此之外,可以在 ~/.libao 中启用调试。配置文件以获取更多信息。

关于c - ALSA 的依赖问题(libao 和 sndfile),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68445412/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com