Reusable alsa-lib part

If you don’t have pulseaudio on your host then you want a different setup than my alsa via pulseaudio scheme.

Regarding the missing libraries, you need to ensure you’ve staged libasound2-plugins.

Yes, below is what I have in stage-packages:
stage-packages:
- libasound2
- libasound2-plugins
- alsa-utils # uncomment this to add alsa-utils

It is in my snap
ls /snap/dkentry-python3/x1/usr/lib/arm-linux-gnueabihf/alsa-lib/libasound_module_pcm_pulse.so

and the layout is putting it in
ls /usr/lib/arm-linux-gnueabihf/alsa-lib/libasound_module_pcm_pulse.so

A lot has changed on Snapcraft world, and I am trying to understand… What’s the status of this remote part as of snapcraft version 3.8 ? Should I still use or not? How do I include this in the current snapcraft.yaml scheme? @lucyllewy

Right now I have:

Failed to get part information: Cannot find the definition for part 'alsa'.

I’ve written documentation at https://snapcraft-alsa.readthedocs.io/en/latest/. You have two options:

  1. Copy the required yaml into your snapcraft.yaml manually
  2. Convert to using jsonnet to build your snapcraft.yaml and use the alsa-lib jsonnet extension. Compile your jsonnet to yaml using my sc-jsonnet utility.

Manually copying (alternative 1) is the only way to keep using the cloud build provided by snapcraft.io right?

I would like to avoid dependency on an extra tool and writing the code using non yaml code.

That’s correct. If you want to continue with yaml then alternative 1 is the appropriate direction to go :slight_smile:

My jsonnet method is just sugar on top so is completely ignorable; there are some specific advantages, but they are at the expense of requiring a separate step to convert it to yaml and learning a json-like markup.

1 Like

Hi @lucyllewy

I have tried inserting alsa acording to option one but without luck.
Still getting this error when running the snap:

ALSA lib conf.c:3916:(snd_config_update_r) Cannot access file /usr/share/alsa/alsa.conf
ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:0
ALSA lib conf.c:3916:(snd_config_update_r) Cannot access file /usr/share/alsa/alsa.conf
ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:1
ALSA lib conf.c:3916:(snd_config_update_r) Cannot access file /usr/share/alsa/alsa.conf
ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:2
demo: src/hostapi/alsa/pa_linux_alsa.c:1453: BuildDeviceList: Assertion `devIdx < numDeviceNames' failed.
Aborted

I am installing the snap with --dangerous --devmode

snapcraft.yaml :

name: myApp
base: core18
version: '0.2'  # Just for humans, typically '1.2+git' or '1.3.2'
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
description: |
  This is my-snap's description. You have a paragraph or two to tell the

grade: devel  # Must be 'stable' to release into candidate/stable channels
confinement: devmode
#architectures: [all]

parts:
  alsa-mixin:
    plugin: nil
    source: https://github.com/diddlesnaps/snapcraft-alsa.git
    override-pull: |
      cat > asound.conf <<EOF
      pcm.!default {
          type pulse
          fallback "sysdefault"
          hint {
              show on
              description "Default ALSA Output (currently PulseAudio Sound Server)"
          }
      }
      ctl.!default {
          type pulse
          fallback "sysdefault"
      }
      EOF
      cat > alsa-launch <<EOF
      #!/bin/bash
      export ALSA_CONFIG_PATH="\$SNAP/etc/asound.conf"

      if [ -d "\$SNAP/usr/lib/alsa-lib" ]; then
          export LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:\$SNAP/usr/lib/alsa-lib"
      elif [ -d "\$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib" ]; then
          export LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:\$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib"
      fi
      export LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:\$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio"

      # Make PulseAudio socket available inside the snap-specific $XDG_RUNTIME_DIR
      if [ -n "\$XDG_RUNTIME_DIR" ]; then
          pulsenative="pulse/native"
          pulseaudio_sockpath="\$XDG_RUNTIME_DIR/../\$pulsenative"
          if [ -S "\$pulseaudio_sockpath" ]; then
              export PULSE_SERVER="unix:\${pulseaudio_sockpath}"
          fi
      fi

      exec "\$@"
      EOF
      chmod +x alsa-launch
    override-build: |
      snapcraftctl build
      install -m644 -D -t $SNAPCRAFT_PART_INSTALL/etc asound.conf
      install -m755 -D -t $SNAPCRAFT_PART_INSTALL/snap/command-chain alsa-launch
    build-packages:
      - libasound2-dev
    stage-packages:
      - libasound2
      - libasound2-plugins

  myApp:
    plugin: dump
    source: .
    override-build: |
      # logic
      snapcraftctl build

    build-packages:
      - wget
      - unzip
      - git
      - gcc
      - make
      - python3-pyaudio
      - python3-pip
      - libevent-dev
      - python-dev
      - libpcre3
      - libpcre3-dev
      - libperl-dev
      - libasound2
      - libasound2-dev
      - libatlas-base-dev
      - libasound2-plugins
      - alsa-utils
    stage-packages:
      - python3-pyaudio
      - sox
      - python-setuptools
      - libatlas-base-dev
      - libevent-dev
      - python-dev
      - libpcre3
      - libpcre3-dev
      - libperl-dev
      - libasound2
      - libasound2-dev
      - libasound2-plugins
      - alsa-utils

apps:
  myApp:
  demo:
    command: scripts/cScripts/demo $SNAP
    plugs: [network, network-bind, gpio, hardware-observe, audio-playback, audio-record, pulseaudio, alsa]

Extra data, when running my executable inside snapcraft (with --shell-after) I get this error:

snapcraft-myApp ../stage# ./scripts/cScripts/demo .
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM default
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM default
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM dmix
Fail to open PortAudio stream, error message is Device unavailable.

I have found out that option one documentation was changed from my last comment and tried it again.
Now I get different error

ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:0
ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:1
ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:2
ALSA lib dlmisc.c:254:(snd1_dlobj_cache_get) Cannot open shared library /usr/lib/x86_64-linux-gnu/alsa-lib/libasound_module_pcm_pulse.so
ALSA lib dlmisc.c:254:(snd1_dlobj_cache_get) Cannot open shared library /usr/lib/x86_64-linux-gnu/alsa-lib/libasound_module_pcm_pulse.so
Fail to open PortAudio stream, error message is Device unavailable.

@Wimpress @lucyllewy @kyrofa I have created bug for it in launchpad and created public project with only the ALSA part that I use ready to snap and for you to inspect.
Am I missing something in my snapcraft.yaml?

I’ve updated the instructions with a simpler integration. The old instructions were missing the layout definition, which is a requirement:

layout:
  /usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib:
    bind: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib

You can either copy just the layout into what you already have, which is the easiest option because it should only be missing the layout; or, you can redo the alsa configuration with the updated code on https://snapcraft-alsa.readthedocs.io/en/latest/snapcraft_usage.html

After adding the layout and redo the alsa configuration with the updated code I am getting new error, my updated code can be found here.

Error:

ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:0
ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:1
ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:2
ALSA lib pcm_pulse.c:752:(pulse_prepare) PulseAudio: Unable to create stream: Access denied

Expression 'r' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2092
Expression 'PaAlsaStreamComponent_FinishConfigure( &self->capture, hwParamsCapture, inParams, self->primeBuffers, realSr, inputLatency )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2726
Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2838
Fail to open PortAudio stream, error message is Unanticipated host error.

Hi,

It looks like you’re running on a system that doesn’t have pulseaudio running, possibly Ubuntu Core?

The snapcraft-alsa part I wrote as intended for desktop applications to work around the alsa plug’s required review step or manual connection requirement when audio playback is required by hijacking and forwarding to a running pulseaudio system in the user’s desktop session.

As your app appears to be an interactive voice assistant-like system, I suspect you’ll need to use the alsa plug and not use the snapcraft-alsa part that I wrote. This will unfortunately require you to either document that users need to manually connect the alsa interface or you will need to get a store assertion for automatic connection of the alsa interface by posting a request in this forum to the “Store Requests” topic.

1 Like

I have just tried it on another computer and It is working (without connecting to alsa interface).

The one that it didn’t worked on is running kubuntu and running pulseaudio in the terminal return

E: [pulseaudio] pid.c: Daemon already running.
E: [pulseaudio] main.c: pa_pid_file_create() failed.

So it may indeed be error in pulseaudio that created the error.
Thanks for all the help, adding the layout solved it for me.

Does it works with super user permissions?, I am getting error when running with sudo.

ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:0
ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL hw:1
No protocol specified
xcb_connection_has_error() returned true
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM sysdefault
No protocol specified
xcb_connection_has_error() returned true
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM sysdefault
Fail to open PortAudio stream, error message is Device unavailable.

The part that use alsa is working for me without super user but my app need to run with super user permission or as a daemon (daemon: simple).

With at least Ubuntu, the pulseaudio system is started per user session at login. This requires that the session be a desktop (gui) session. The pulseaudio system is restricted from being accessed by other users, including root, while it runs in a user session. What this means is that you cannot use the pulseaudio system from a daemon or system service or anything run through sudo.

Does connecting alsa manually snap connect snapName:alsa will solve alsa not working inside daemon snap?.
If so do I need to change something in my snapcraft.yaml?

Opened bug for running Alsa inside daemon snap