Audio setup & debugging

asktoby edited this page Nov 12, 2017 · 42 revisions

Setting up your devices

If you want to change your playback / recording device (also because the default setting doesn't work for you), you can do that in the configuration file. If you installed into /opt as recommended, it is /etc/opt/AlexaPi/config.yaml. If you installed elsewhere, the file config.yaml is in your AlexaPi/src directory.

You may need to adjust the volume and/or input gain for the microphone, you can do this with alsamixer. Once the adjustments have been made, you can save the settings using alsactl store.

Debugging your setup

If there is an error in your log file complaining about audio you may need to tinker your audio setup. These errors might be these:

  • Invalid input_device in config:

    If you use an incorrect / unlisted device name, AlexaPi will tell you and exit with the list of correct options.

  • Another application is using your device:

    IOError: [Errno -9985] Device unavailable

    If you are running a desktop OS, it might be PulseAudio in user-session mode. You have to run PA in system-wide mode in order to run AlexaPi. Scroll down for more info.

Checking if your system audio works

  1. To test playback, run aplay /usr/share/sounds/alsa/Front_Center.wav
  2. To test recording, run
    1. arecord -f S16_LE -r 16000 -D your_input_device test.wav - exit with CTRL+C and
    2. aplay -f S16_LE -r 16000 test.wav to play

Audio playback issues

  1. First check that you are on the latest version of your OS, VLC and the python-vlc package.
  2. Try SoX instead of VLC. Change sound/playback_handler to sox in your config. SoX doesn't handle some playlists correctly for now though. A lot of things might not work.

Checking AlexaPi's recordings

If you get null responses, you might wanna check the sound AlexaPi records. First, run in the Debug mode and then check the directory /tmp/AlexaPi-runtime-xxx (where xxx is some random string) for the file recording.wav. This has to be played with special aplay settings:

aplay -r 16000 -f S16_LE recording.wav

Audio debugging script

To help in debugging and tracking down audio issues, please use the following command as a standard form of data collection:

cd ~ && wget https://raw.githubusercontent.com/alexa-pi/AlexaPi-misc/master/alsa-info.sh && bash alsa-info.sh

This site has some good information regarding alsa: http://www.volkerschatz.com/noise/alsa.html

If you can't figure it out yourself, be sure to post the output of those commands when asking for help

Raspberry Pi-specific

HDMI Audio

If you are using HDMI out and are losing the first part of audio playback try running vcgencmd force_audio hdmi 1. See the Raspberry Pi section in Devices for details.

3.5mm jack

Raspbian defaults to the HDMI audio out. If you want to use the 3.5mm jack you can use

sudo raspi-config 

and choose "Advanced Options" to force 3.5mm audio output.

Arch Linux

The internal audio is not enabled by default in Arch Linux see the Raspberry Pi/Audio section in Arch wiki on how to do that manually.

External DAC Support

Some external DACs might have issues with the default Device Platform configuration. This may or may not include an unresponsive audio system with no sign of normal ALSA audio errors. The HiFiBerry DAC+ for example needs GPIO pins of its' own to receive audio from your pi. The default platform config uses PIN 18 as an optional button, which is one of the same pins used by the DAC to communicate.

Make these pin numbers don't overlap with your DAC's pinout:

  raspberrypi:
    button: 7
    plb_light: 24
    rec_light: 25

Alternatively, if you don't need a button, nor LEDs, you can just use the dummy device platform and it won't touch your GPIO at all.

PulseAudio

You might get errors regarding PulseAudio. This is very probably due to the fact that you're running a GUI installation of your OS with PA in session mode and you want to run AlexaPi as a daemon under another user. Unfortunately, things don't work like this, because this way PA is there only for the user pi.

You have these options:

Disabling PA

Note: You might not be able to run other apps that use audio output without some ALSA tweaking.

All under root (or sudo before every command):

mkdir -p /var/lib/AlexaPi/.config/pulse
cp /etc/pulse/client.conf /var/lib/AlexaPi/.config/pulse/
# edit /var/lib/AlexaPi/.config/pulse/client.conf and set "autospawn = no"

Running PA in system-wide mode (recommended)

This is the preferred (and possibly the only sensible) way to run PA with AlexaPi.

-- In addition, I would call it experimental for now.

Note that there are some warnings about unexpected behavior and the like, but you can safely ignore it. Read more about system-wide PA on their wiki.

Setup and Configure Pulseaudio:

  • Install PulseAudio and optionally uninstall the deprecated modules as recommended on freedesktop.org. Have a look at that page to understand how these things will work - there may also be things there to try if this setup doesn't work for you.

    sudo apt install pulseaudio pavucontrol
    sudo apt remove pavumeter paman padevchooser
    
  • Run these commands:

    mkdir -p /var/lib/AlexaPi/.config/pulse
    sudo cp /etc/pulse/client.conf /var/lib/AlexaPi/.config/pulse/
    

    Edit /var/lib/AlexaPi/.config/pulse/client.conf and set autospawn=no.

    Also if you see issues with permissions in the log file for /var/lib/AlexaPi (if you have an older install), you may need to run these commands:

    # create and set a home directory for the user alexapi
    chown -R alexapi:alexapi /var/lib/AlexaPi/
    usermod --home /var/lib/AlexaPi alexapi
    
  • Next, we add the pi (or your user) and alexapi users to the pulse-access group, and just for good measure add the pulse user to the audio group:

    sudo adduser pulse audio
    sudo adduser <username or pi> pulse-access
    sudo adduser alexapi pulse-access
    

    If you are running the root override (like on a CHIP or Orange Pi), you also need to add the user root into the pulse-access group: sudo adduser root pulse-access.

  • Next we need to create the systemd service so PulseAudio will start on boot in system wide mode: Create the file in you text editor, i used nano:

    sudo nano /etc/systemd/system/pulseaudio.service
    

    in the new blank file that opens, place the following text:

    [Unit]
    Description=PulseAudio Daemon
    
    [Install]
    WantedBy=multi-user.target
    
    [Service]
    Type=simple
    PrivateTmp=true
    ExecStart=/usr/bin/pulseaudio --system --realtime --disallow-exit --no-cpu-limit
    

    exit and save the file in the editor (for nano [Ctrl] + x, then y, then [Enter])

  • Now we need to enable the systemd service so it starts automatically:

    sudo systemctl enable pulseaudio.service
    
  • Last we need to configure config.yaml for AlexaPi to use PulseAudio: (If you didn't install AlexaPi in /opt, the file config.yaml is in your AlexaPi/src directory.)

    sudo nano /etc/opt/AlexaPi/config.yaml 
    

    in this file, in the sound section, if you're using VLC handler set

    input_device: "pulse"
    output: "pulse"
    output_device: ""
    

    Or if you're using SoX set

    input_device: "pulse"
    output: "pulseaudio"
    output_device: ""
    

    and install the libsox-fmt-pulse package

    sudo apt-get install libsox-fmt-pulse
    
  • Reboot your system and check to see that everything is working. If the audio playback is choppy, you can try installing VLC manually with sudo apt-get install vlc.

User session mode (AlexaPi as the only audio app)

TODO

PulseAudio via dmix

Alternatively, you can force PA to use dmix, so other apps can use ALSA directly. See https://wiki.archlinux.org/index.php/PulseAudio#ALSA.2Fdmix_without_grabbing_hardware_device Warning: This has not been tested. Feedback is welcome.

Using other user's (pi for example) PA instance

This is crazy and not supported (nor will ever be). You'd have to setup up some permissions, the actual socket (unix or TCP) to communicate and you'd have to make sure the user pi is always (and first) logged in before starting AlexaPi.