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

Dependency Issues with ALSA (libao and sndfile)

我使用 github 中的参考编写了一个播放 PCM 音频文件的小程序,对我的应用程序稍作修改。该程序编译正常,但我遇到了一些有关依赖项和 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

我一直在研究许多不同的解决方案并且 none 似乎有效,包括在 /etc/modprobe.d 中的 alsa.conf 文件中添加一行(但是,这些解决方案建议添加当我使用 AMD 设置时参考英特尔的一行。

完整代码如下:

/*
 *
 * 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;
}

我希望其他人也遇到过同样的麻烦,并且可以阐明解决方案。谢谢。

这是 lsmod 的输出:

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 输出:

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

总结讨论:

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

根据 libao documentation,库尝试确定默认驱动程序如下:

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

错误消息表明尝试了多个驱动程序,包括nassndioalsa。使用 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 配置文件中启用调试以获取更多信息。