newmsrd
newmsrd

Reputation: 43

Dependency Issues with ALSA (libao and sndfile)

I have written a small program to play PCM audio files using a reference from github with slight modifications for my application. The program is compiling fine, but I am having a few runtime issues regarding dependencies and ALSA devices.

The errors I am receiving are:

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

I have been researching many different solutions and none seem to be working, including adding a line to the alsa.conf file in /etc/modprobe.d (however, these solutions suggest adding a line in reference to intel when I am using an AMD setup.

Here is the full code:

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

I hope someone else has had the same trouble and can shed light on the solution. Thank you.

Upvotes: 2

Views: 705

Answers (2)

user11718766
user11718766

Reputation: 275

To summarise the discussion:

The problem does not seem to lie within the code per se but in the libao configuration.

According to the libao documentation, the library tries to determine a default driver as follows:

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.

The error messages indicate that several drivers including nas, sndio and alsa are tried. Choosing the driver manually with ao_driver_id(...) instead of using ao_default_driver_id() fixes the issue.

Further problems with opening the device with ao_open_live(...) can be investigated by getting the corresponding error number with printf("errno %d\n", errno);. The output can be interpreted as follows:

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.

Besides that debugging can be enabled in the ~/.libao configuration file to get further information.

Upvotes: 2

newmsrd
newmsrd

Reputation: 43

Here is the lsmod output:

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

and here is the aplay -L output:

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

Upvotes: 0

Related Questions