首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ALSA的依赖问题(libao和sndfile)

ALSA的依赖问题(libao和sndfile)
EN

Stack Overflow用户
提问于 2021-07-19 18:33:16
回答 2查看 240关注 0票数 2

我已经编写了一个小程序来播放PCM音频文件,使用来自github的引用,并对我的应用程序做了一些修改。这个程序编译得很好,但是我遇到了一些关于依赖项和ALSA设备的运行时问题。

我所收到的错误如下:

代码语言:javascript
复制
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设置时添加一条引用英特尔的行。

以下是完整的代码:

代码语言:javascript
复制
/*
 *
 * 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;
}

我希望其他人也有同样的麻烦,并能为解决方案提供线索。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-19 22:32:20

总结讨论:

问题似乎不在于代码本身,而在于libao配置。

根据libao 文档,库试图确定默认驱动程序如下:

如果没有配置文件来显式标识默认驱动程序,库将尝试检测到合适的默认驱动程序。它通过测试每个可用的实时输出驱动程序(使用ao_plugin_test())和找到具有最高优先级的驱动程序(参见ao_info结构)来实现这一点。优先级为0的驱动程序(如null和文件输出驱动程序)从未被选中为默认值。

错误消息表明,尝试了几个驱动程序,包括nassndioalsa。使用ao_driver_id(...)手动选择驱动程序,而不是使用ao_default_driver_id()修复问题。

通过用ao_open_live(...)得到相应的错误号,可以进一步研究用printf("errno %d\n", errno);打开设备的问题。输出可以解释如下:

代码语言:javascript
复制
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配置文件中启用调试,以获得更多信息。

票数 2
EN

Stack Overflow用户

发布于 2021-07-19 19:44:21

下面是lsmod输出:

代码语言:javascript
复制
Module                  Size  Used by
rfcomm                 81920  4
cmac                   16384  2
algif_hash             16384  1
algif_skcipher         16384  1
af_alg                 28672  6 algif_hash,algif_skcipher
bnep                   24576  2
nls_iso8859_1          16384  1
hid_logitech_hidpp     45056  0
input_leds             16384  0
joydev                 24576  0
hid_logitech_dj        28672  0
hid_generic            16384  0
snd_hda_codec_realtek   131072  1
snd_hda_codec_generic    81920  1 snd_hda_codec_realtek
ledtrig_audio          16384  1 snd_hda_codec_generic
snd_hda_codec_hdmi     61440  1
snd_hda_intel          53248  4
edac_mce_amd           32768  0
snd_intel_dspcfg       28672  1 snd_hda_intel
iwlmvm                393216  0
snd_hda_codec         139264  4 snd_hda_codec_generic,snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec_realtek
snd_hda_core           94208  5 snd_hda_codec_generic,snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec,snd_hda_codec_realtek
mac80211              905216  1 iwlmvm
snd_hwdep              20480  1 snd_hda_codec
snd_pcm               114688  4 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec,snd_hda_core
amdgpu               5218304  11
libarc4                16384  1 mac80211
snd_seq_midi           20480  0
snd_seq_midi_event     16384  1 snd_seq_midi
kvm                   712704  0
snd_rawmidi            36864  1 snd_seq_midi
snd_seq                69632  2 snd_seq_midi,snd_seq_midi_event
crct10dif_pclmul       16384  1
snd_seq_device         16384  3 snd_seq,snd_seq_midi,snd_rawmidi
ghash_clmulni_intel    16384  0
iwlwifi               352256  1 iwlmvm
snd_timer              40960  2 snd_seq,snd_pcm
aesni_intel           372736  3
iommu_v2               20480  1 amdgpu
crypto_simd            16384  1 aesni_intel
gpu_sched              36864  1 amdgpu
cryptd                 24576  3 crypto_simd,ghash_clmulni_intel
glue_helper            16384  1 aesni_intel
usbhid                 57344  1 hid_logitech_dj
ttm                   102400  1 amdgpu
wmi_bmof               16384  0
hid                   135168  4 usbhid,hid_generic,hid_logitech_dj,hid_logitech_hidpp
rapl                   20480  0
drm_kms_helper        217088  1 amdgpu
efi_pstore             16384  0
cfg80211              778240  3 iwlmvm,iwlwifi,mac80211
cec                    53248  1 drm_kms_helper
rc_core                61440  1 cec
snd                    94208  19 snd_hda_codec_generic,snd_seq,snd_seq_device,snd_hda_codec_hdmi,snd_hwdep,snd_hda_intel,snd_hda_codec,snd_hda_codec_realtek,snd_timer,snd_pcm,snd_rawmidi
fb_sys_fops            16384  1 drm_kms_helper
syscopyarea            16384  1 drm_kms_helper
sysfillrect            16384  1 drm_kms_helper
sysimgblt              16384  1 drm_kms_helper
k10temp                16384  0
ccp                    98304  0
soundcore              16384  1 snd
btusb                  57344  0
btrtl                  24576  1 btusb
btbcm                  16384  1 btusb
btintel                28672  1 btusb
mac_hid                16384  0
bluetooth             581632  31 btrtl,btintel,btbcm,bnep,btusb,rfcomm
ecdh_generic           16384  1 bluetooth
ecc                    32768  1 ecdh_generic
sch_fq_codel           20480  3
hwmon_vid              16384  0
parport_pc             45056  0
ppdev                  24576  0
lp                     20480  0
drm                   552960  9 gpu_sched,drm_kms_helper,amdgpu,ttm
parport                65536  3 parport_pc,lp,ppdev
ip_tables              32768  0
x_tables               49152  1 ip_tables
autofs4                45056  2
crc32_pclmul           16384  0
xhci_pci               20480  0
nvme                   49152  2
i2c_piix4              28672  0
igb                   221184  0
ahci                   40960  0
libahci                36864  1 ahci
xhci_pci_renesas       20480  1 xhci_pci
i2c_algo_bit           16384  2 igb,amdgpu
nvme_core             114688  4 nvme
dca                    16384  1 igb
wmi                    32768  1 wmi_bmof
video                  49152  0

下面是aplay -L输出:

代码语言:javascript
复制
default
    Playback/recording through the PulseAudio sound server
surround21
    2.1 Surround output to Front and Subwoofer speakers
surround40
    4.0 Surround output to Front and Rear speakers
surround41
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50
    5.0 Surround output to Front, Center and Rear speakers
surround51
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
null
    Discard all samples (playback) or generate zero samples (capture)
samplerate
    Rate Converter Plugin Using Samplerate Library
speexrate
    Rate Converter Plugin Using Speex Resampler
jack
    JACK Audio Connection Kit
oss
    Open Sound System
pulse
    PulseAudio Sound Server
upmix
    Plugin for channel upmix (4,6,8)
vdownmix
    Plugin for channel downmix (stereo) with a simple spacialization
hdmi:CARD=Generic,DEV=0
    HD-Audio Generic, HDMI 0
    HDMI Audio Output
hdmi:CARD=Generic,DEV=1
    HD-Audio Generic, HDMI 1
    HDMI Audio Output
hdmi:CARD=Generic,DEV=2
    HD-Audio Generic, HDMI 2
    HDMI Audio Output
dmix:CARD=Generic,DEV=3
    HD-Audio Generic, HDMI 0
    Direct sample mixing device
dmix:CARD=Generic,DEV=7
    HD-Audio Generic, HDMI 1
    Direct sample mixing device
dmix:CARD=Generic,DEV=8
    HD-Audio Generic, HDMI 2
    Direct sample mixing device
dsnoop:CARD=Generic,DEV=3
    HD-Audio Generic, HDMI 0
    Direct sample snooping device
dsnoop:CARD=Generic,DEV=7
    HD-Audio Generic, HDMI 1
    Direct sample snooping device
dsnoop:CARD=Generic,DEV=8
    HD-Audio Generic, HDMI 2
    Direct sample snooping device
hw:CARD=Generic,DEV=3
    HD-Audio Generic, HDMI 0
    Direct hardware device without any conversions
hw:CARD=Generic,DEV=7
    HD-Audio Generic, HDMI 1
    Direct hardware device without any conversions
hw:CARD=Generic,DEV=8
    HD-Audio Generic, HDMI 2
    Direct hardware device without any conversions
plughw:CARD=Generic,DEV=3
    HD-Audio Generic, HDMI 0
    Hardware device with all software conversions
plughw:CARD=Generic,DEV=7
    HD-Audio Generic, HDMI 1
    Hardware device with all software conversions
plughw:CARD=Generic,DEV=8
    HD-Audio Generic, HDMI 2
    Hardware device with all software conversions
usbstream:CARD=Generic
    HD-Audio Generic
    USB Stream Output
sysdefault:CARD=Generic_1
    HD-Audio Generic, ALC1220 Analog
    Default Audio Device
front:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    Front speakers
surround21:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    4.0 Surround output to Front and Rear speakers
surround41:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
dmix:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    Direct sample mixing device
dsnoop:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    Direct sample snooping device
hw:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    Direct hardware device without any conversions
plughw:CARD=Generic_1,DEV=0
    HD-Audio Generic, ALC1220 Analog
    Hardware device with all software conversions
usbstream:CARD=Generic_1
    HD-Audio Generic
    USB Stream Output
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68445412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档