dtcooper/raspotify

Card number changes with reboot

jmayday opened this issue · 11 comments

Due Diligence

  • I have done my due diligence

What can we help you with?

How to configure /etc/asound.conf so that it's always pointing to correct card number?
After reboot card number changes, basically here is my setup:

jmayday@raspberrypi:~ $ 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: DAC51090305 [ADI-2 DAC (51090305)], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0

I want to use card 3, so I created /etc/asound.conf:

jmayday@raspberrypi:~ $ cat /etc/asound.conf 
defaults.ctl.card 3
defaults.pcm.card 3
defaults.pcm.dmix.rate 768000
defaults.pcm.dmix.format S32_LE

Unfortunately after reboot it might happen, that my external DAC won't be detected as card 3 but card 1. So I have to go to asound.conf and reconfigure it. Is it possible to prevent such situation?

Then you want to reference your card by name then not index.

Use aplay -L to get your DAC's name.

My DAC is a Topping D10s so the part I care about in my output is:

hw:CARD=D10s,DEV=0
    D10s, USB Audio
    Direct hardware device without any conversions

My card's name is D10s. So I would use that in this:

# /etc/asound.conf

pcm.!default {
    type plug
    slave.pcm {
        type dmix
        ipc_key {
            @func refer
            name defaults.pcm.ipc_key
        }
        ipc_gid {
            @func refer
            name defaults.pcm.ipc_gid
        }
        ipc_perm {
            @func refer
            name defaults.pcm.ipc_perm
        }
        slave {
            pcm {
                type hw
                card D10s
                nonblock {
                    @func refer
                    name defaults.pcm.nonblock
                }
            }
            channels 2
            rate 44100
            format S32_LE
        }
        bindings {
            0 0
            1 1
        }
    }
}

ctl.!default {
    type hw
    card D10s
}

When you find your cards name just replace D10s with whatever it is. As a side note there is absolutely no point to setting your rate to 768000 unless your goal is wasting CPU cycles and degrading the sound quality. Set the rate to the highest sampling rate of the audio that you typically play. I use 44100 personally because that's the sampling rate librespot outputs.

If Raspotify is the only thing on you computer that will ever be using the DAC and it supports 44100 you can skip /etc/asound.conf and tell librespot to just always use the DAC directly bypassing all software mixing by editing /etc/raspotify/conf and setting LIBRESPOT_DEVICE= to your DAC:

Using my example output from above for me that would mean:

LIBRESPOT_DEVICE="hw:CARD=D10s,DEV=0"

OK, I'm closing this as solved. I'm going to assume you figured it out.

Thanks! I don't understand structure of /etc/asound.conf you've given but it works (just replaced D10s with my card name).

BTW is there a way to generate that file (passing the card name as param)?

I'm sure you could write a quick bash script that accepted the card name as an arg and generated the file with cat or echo?

I meant script provided with raspotify, so that after installing raspotify on new raspberry installation I can easily set it up (the asound.conf file) without having to remember how the file looks like or coming back to this GitHub issue. I would be nice addition I think.

I meant script provided with raspotify, so that after installing raspotify on new raspberry installation I can easily set it up (the asound.conf file) without having to remember how the file looks like or coming back to this GitHub issue. I would be nice addition I think.

That's beyond the scope of the project. This really isn't a general Linux sound help service. Using Raspotify may require you to roll up your sleeves, get your hands dirty and learn something.

If you need or want a script, write a script.

Plus it's not a good idea to just clobber /etc/asound.conf with a script unless you know what you're doing or at least have some help. Writing an automated script that would suit everyone's needs is basically impossible because there are a million and one different configurations and user needs. It's better if you wrote your own script for your own needs.

@jmayday I spend way to much time on a script as you suggested because I was bored.

Instructions can be found Here.