linux – how to gracefully cope with Alsa card moving slots across reboot

I have a Raspberry Pi 4 Model B Rev 1.1 running Raspbian with USB card/speaker connected. The problem is that it moves slots across reboot. Here, it shows up in slot #3:

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
  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: vc4hdmi0 [vc4-hdmi-0], device 0: MAI PCM i2s-hifi-0 [MAI PCM i2s-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: vc4hdmi1 [vc4-hdmi-1], device 0: MAI PCM i2s-hifi-0 [MAI PCM i2s-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 3: UACDemoV10 [UACDemoV1.0], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

however previous reboot it was reported in slot #2.

This is a problem, because /usr/share/alsa/alsa.conf contains these lines:

defaults.ctl.card 2
defaults.pcm.card 2

which means playing e.g. mp3 files with mpg123 currently fails, until the configuration is changed.

I guess there could be a systemd service introduced that would check the Alsa slots after reboot and change alsa.conf accordingly, however I wonder
if there is more elegant way how to cope with the card moving (Alsa) slots across reboots ?

Read more here: Source link