Troubleshooting Audio

In this section, you will get an overview on common issues when dealing with the audio setup on a device with a Linux OS. The examples used in the troubleshooting have been tested on a Raspberry Pi running Raspbian Stretch.

The troubleshooting assumes that, if you are in a Raspberry Pi, you have already tried unsuccessfully to configure your audio setup using Sam as outlined in the Quick Start Raspberry Pi. That is the easier and preferred way to setup your audio configuration but if you have more advance needs you may find here some solutions.

Please try to go through all sections below in order.

All the instructions below are run on a Raspberry Pi. To do so you need to either connect remotely to your Pi using ssh or connecting the Pi to a screen and opening a terminal.

Check if you have an audio configuration

Run the following command in your terminal:

cat /etc/asound.conf

You can find detailed documentation about the asound.conf configuration file and related functionalities in the official documentation.

If you have the default audio configuration, the command will output something very close to this:

pcm.!default {
type asym
playback.pcm {
type plug
slave.pcm "hw:0,0"
}
capture.pcm {
type plug
slave.pcm "hw:1,0"
}
}

Use the right card and device

In the asound.conf file you may notice the attribute playback and capture. playback stands for the configuration related to speaker(s) and capture for the configuration related to the microphone(s) device. For both, speakers and microphone, you have to ensure that you are using the right card and device interfaces in the hw:<card, device> field. To do so, type in the terminal:

  • aplay -l to show speakers card and device. With our hardware, using the default audio devices in the Raspberry Pi, we get:

**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
Subdevices: 7/7
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
Subdevices: 1/1
Subdevice #0: subdevice #0

Through the output terminal above we can see that we have two devices in card 0 that we could use to output sound. The device 0 uses the mini-jack output and the device 1 the HDMI output. For instance, if you would like to set HDMI as your audio output, you would replace the playback configuration in the asound.conf with hw:0,1

  • arecord -l to show microphones card and device. With out hardware setup, using an USB microphone, we get:

**** List of CAPTURE Hardware Devices ****
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

The output in the terminal shows us that the card 1 has the device 0 available. Similarly as what we did before, we could set the capture device configuration to hw:1,0 if we would like to use this device.

Hopefully, by now, you have set the devices you want to use with your hardware setup. This is the final asound.conf after the changes proposed above.

pcm.!default {
type asym
playback.pcm {
type plug
slave.pcm "hw:0,1"
}
capture.pcm {
type plug
slave.pcm "hw:1,0"
}
}

Check if my microphone and speaker work

To check if your speakers and microphone are working, run the following instructions for each device:

Microphone

See if you can record audio with your microphone using arecord. The following command will record audio for 5 seconds using your default recording device:

arecord -d 5 -f S16_LE -r 44100 /tmp/test-mic.wav

In the previous command we use S16_LE sample format and 44100 Hz sampling rate. This configuration may not work for your microphone. Please check the specifications of your microphone to make sure you are using the right parameters. Check the arecord documentation

If you want to specify a specific card and device to record, you can do so at the beginning of the command as follows:

arecord hw:1,0 -d 5 -f S16_LE -r 44100 /tmp/test-mic.wav

Speaker

See if you can play audio through your speakers using aplay followed by an audio file to be played. We use the file we created in the previous step:

aplay /tmp/test-mic.wav

As with arecord you can specify the card and device where you want the audio file to play:

aplay hw:1,0 /tmp/test-mic.wav

If you want to learn more about the arecord and aplay commands in the Linux documentation here.

My microphone is not recognized

If you use a specific microphone (such a microphone array or something fancy) check with the manufacturer if you need to install specific drivers for it to be able to work.

To check if your microphone is recognized try first if it is listed using arecord -l in your terminal. If it is listed, you may want to check the card and device interface specified and set them in your asound.conf as shown in Use the right card and device. To make sure it is working try to record audio using arecord as shown in the section Check if my microphone and speaker work.

More than one application using audio

If you want to have multiple applications running input or audio processes at the same time you would need to use dmix and dsnoop in your asound.conf file. There is no configuration working as a one-fit-all solution which means you will need to tinker with and understand well the asound.conf configuration file to set it up properly.

ReSpeaker 2-Mics Pi HAT

Assuming that the ReSpeaker 2-Mics Pi Hat drivers are correctly installed.

In order to have it working with Snips on your Raspberry Pi make sure that:

1. The audio server uses Alsa.

Edit the file /etc/snips.toml and add to [snips-audio-server] section the following:

alsa_capture = "default"
alsa_playback = "default"

The goal is to avoid this portaudio issue.

Alsa will be used as default instead of portaudio in snips next releases.

2. Verify your asound.conf

Replace the /etc/asound.conf file with this working example inspired from the official Seed repository:

# Dmix / Dsnoop configuration
# Allows to share capture and playback interfaces accross multiple programs.
# Do not use with raspberry pi bcm2835 sound card (dmix known issue)
pcm.!default {
type asym
playback.pcm "playback"
capture.pcm "capture"
}
pcm.playback {
type plug
slave.pcm "dmixed"
}
pcm.capture {
type plug
slave.pcm "array"
}
pcm.dmixed {
type dmix
ipc_key 555555
ipc_key_add_uid yes
ipc_perm 0666
slave.pcm "hw:seeed2micvoicec"
}
pcm.array {
type dsnoop
ipc_key 666666
ipc_key_add_uid yes
ipc_perm 0666
slave.pcm "hw:seeed2micvoicec"
}

With the latest version of the Linux kernel and the latest ReSpeaker driver, you may be prompted with this issue: EACCES: Permission denied. If that's the case, make sure you set the ipc_perm to 0666 to share the interface across users.

3. Restart the Snips audio server

Finally, restart the Snips audio server to apply the changes:

sudo systemctl restart snips-audio-server

After you have restart the Snips audio server it is worth to make sure that has been able to initialized without any issues. You can run the following command to display the logs in your terminal:

journalctl -f -u snips-audio-server

Errors may occur because of various reasons and hopefully the steps and solutions proposed int this troubleshooting will help you solve them. Anyhow we want to point you out to two common scenarios where snips-audio-server will fail to restart if your asound.conf is incorrect: 1. The logs will show an invalidSampleRate error 2. The snips-audio-server crashes after a "Hey Snips, [some command]"

If any of these two happen, please double check your asound.conf and the steps proposed in the sections of this article.

The respeaker PiHAT 2 input and output sampling rate cannot be set independently. The first interface used will constrain the other one.

e.g. If the capture interface is set at 16000 samples per seconds, the playback interface will inherit the same setting.