Skip to content

Instantly share code, notes, and snippets.

@rnagarajanmca
Last active August 26, 2024 18:40
Show Gist options
  • Save rnagarajanmca/63badce0fe0e2ad126041c7c139970ea to your computer and use it in GitHub Desktop.
Save rnagarajanmca/63badce0fe0e2ad126041c7c139970ea to your computer and use it in GitHub Desktop.
Configure Raspberry pi with USB Audio

Device specifications

List USB Device

Once you connected the USB Audio device with Raspberry pi use following command to see the list of connected USB device

lsusb

Above command will respond with list of USB devices the response will be similar like

Bus 001 Device 004: ID 1b3f:2008 Generalplus Technology Inc.
Bus 001 Device 003: ID 148f:7601 Ralink Technology, Corp.
Bus 001 Device 002: ID 05e3:0606 Genesys Logic, Inc. USB 2.0 Hub / D-Link DUB-H4 USB 2.0 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Use following command to check whether USB audio device identified by Raspberry pi

pi@raspberrypi:~$ aplay -l

**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  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
  Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

From the above response we can understand card 1 is USB audio device

Configure USB Audio device as default device

We can configure default audio devices using Alsa.
Advanced Linux Sound Architecture (ALSA) is a software framework is part of the linux so we should be able to modify the conf file and set the default audio device Use Alsa Configuration file will be available under /etc/modprobe.d/alsa-base.conf. If the file is not available create one and put the following lines to reorder the audio devices

# This sets the index value of the cards but doesn't reorder.
options snd_usb_audio index=0
options snd_bcm2835 index=1

# Does the reordering.
options snd slots=snd_usb_audio,snd_bcm2835
pi@raspberrypi:~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Device [USB Audio Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  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
  Subdevice #7: subdevice #7
card 1: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Now if you check the hardware order using aplay -l, Audio devices will be displayed as following order Then you should set the external device as default device by editing the file ~/.asoundrc. type following lines in the asoundrc file

pcm.!default plughw:Device

ctl.!default plughw:Device

Test the Audio

Use following command to test the audio output

# -D - Device 
# -c Channels 
pi@raspberrypi:~$ speaker-test -t wav -c 2 -D hw:0

speaker-test 1.0.28

Playback device is hw:0
Stream parameters are 48000Hz, S16_LE, 2 channels
WAV file(s)
Rate set to 48000Hz (requested 48000Hz)
Buffer size range from 96 to 262144
Period size range from 48 to 131072
Using max buffer size 262144
Periods = 4
was set period_size = 65536
was set buffer_size = 262144
 0 - Front Left
 1 - Front Right
Time per period = 5.545710
 0 - Front Left
 1 - Front Right
Time per period = 5.539851

You can play the songs by usiong following command

pi@raspberrypi:~$ aplay  /usr/share/scratch/Media/Sounds/Animal/Bird.wav
Playing WAVE '/usr/share/scratch/Media/Sounds/Animal/Bird.wav' : Signed 16 bit Little Endian, Rate 11025 Hz, Mono

Troubleshoot

  • When you find following error when playing audio
     pi@raspberrypi:~$ aplay /usr/share/scratch/Media/Sounds/Animal/Bird.wav
     Playing WAVE '/usr/share/scratch/Media/Sounds/Animal/Bird.wav' : Signed 16 bit Little Endian, Rate 11025 Hz, Mono
     aplay: set_params:1239: Channels count non available
    Do the following: Change the file ~/.asoundrc and set default device
  • If you are planning to configure different hardware for mic and speaker change ~/.asoundrc as mentioned below
     pcm.!default {
     type asym
     	playback.pcm{
     		type plug
     		slave.pcm "hw:0,0"
     	}
     	capture.pcm{
     		type plug
     		slave.pcm "hw:0,0"
     	}
     }
    
    In my above configuration my both device connected in hardware card 0,
  • Configuration parameters in asoundrc file changes when rebooting RPi If you face this issue create a file under /etc/asound.conf and put the following lines into it and reboot
     pcm.!default {
       type asym
        playback.pcm {
          type plug
          slave.pcm "hw:0,0"
        }
        capture.pcm {
          type plug
          slave.pcm "hw:1,0"
        }
     }
@Kayakbabe
Copy link

This helped me a lot. I did figure out that to run the command and test if the sound works on a different channel without changing any config files, it was a matter of telling the command which card to use.
The default or whatever is in your config files as the default device
speaker-test -t wav -c 2 -D hw:0
versus for card 2
speaker-test -t wav -c 2 -D hw:2
knowing this, you could have output on any compatible audio output. Say clicks and beeps with the default buzzer on the pi and then real music, voice, etc on an external usb sound bar on card 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment