site  contact  subhomenews

Hardware profiling for audio

January 13, 2020 — BarryK

This morning I posted about hardware-profiling for Xorg:

https://bkhome.org/news/202001/improved-hardware-profiling-for-xorg.html

The same concept can be applied to audio. Any special setting, usually done via the Multiple Sound Card Wizard, can be hardware-profiled, so that we won't have an inappropriate audio device selected when boot the USB-stick on different PCs.

I have made a start on this. There are two files edited, /usr/sbin/mscw and /etc/init.d/10alsa. This is the new code in 10alsa:

   #200113 hardware-profile...
REAL_SND_HW_PROFILE="$(lspci -d ::0403 -n | cut -f 3 -d ' ' | tr '\n' '_')" #ex: 8086:1c20_
echo "$REAL_SND_HW_PROFILE" > /tmp/10alsa-REAL_SND_HW_PROFILE #note, may be more than one card, "_" delimiter.
if [ -f /etc/asound.conf ];then
#this file is created by Multiple Sound Card Wizard.
SND_HW_PROFILE="$(grep '^#SND_HW_PROFILE' /etc/asound.conf | cut -f 2 -d "'")"
if [ "$SND_HW_PROFILE" ];then #precaution.
if ! grep -q "$SND_HW_PROFILE" /tmp/10alsa-REAL_SND_HW_PROFILE;then
if [ -f /etc/asound.conf.${SND_HW_PROFILE} ];then
cp -f /etc/asound.conf.${SND_HW_PROFILE} /etc/asound.conf
else
mv -f /etc/asound.conf /etc/asound.conf.${SND_HW_PROFILE}
fi
fi
fi

There is a backup file, /etc/asound.conf.${SND_HW_PROFILE}, and the hardware-profile variable is also stored inside the file. It is pretty simple, if the current sound hardware does not match the profile variable inside asound.conf, the backup is used.

The MSCW has this code:

 #200113 hardware-profile...
REAL_SND_HW_PROFILE="$(lspci -d ::0403 -n | cut -f 3 -d ' ' | tr '\n' '_')" #ex: 8086:1c20_
#do the lines returned by lspci (if more than one card) match card #'s from "aplay -l"?...
OFF=$(($card+1))
SND_HW_PROFILE="$(echo "$REAL_SND_HW_PROFILE" | tr '_' '\n' | head -n $OFF | tail -n 1)_"
echo "#SND_HW_PROFILE='${SND_HW_PROFILE}'" >> /etc/asound.conf
cp -a -f /etc/asound.conf /etc/asound.conf.${SND_HW_PROFILE}

...but this is where I am uncertain. $REAL_SND_HW_PROFILE may find more than one audio interface, that is, more than one card. But, is the order of cards found by 'lspci' the same as the cards reported by 'aplay -l'?

I have assumed yes, but someone with more than one card plugged in can confirm. Run these:

# lspci -d ::0403
# aplay -l

...does the card numbering reported by the second command match the order listed by the first? 

Tags: easy