site  contact  subhomenews

Improved hardware profiling for audio

January 16, 2020 — BarryK

I posted about the introduction of hardware-profiling for audio:

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

This has evolved. Still just the two scripts /etc/init.d/10alsa and /usr/sbin/mscw. Here is the relevant code from 10alsa:

   #200113 hardware-profile... 200115 improve... (see also /usr/sbin/mscw)
FNDflg=0
[ ! -f /etc/asound.conf ] && touch /etc/asound.conf #200116
echo -n '' > /tmp/10alsa-AudioCardHardwareProfiles
CURR_SND_HW_PROFILE="$(grep '^#SND_HW_PROFILE' /etc/asound.conf | cut -f 2 -d "'")" #ex: 0+8086:1c20 (card#+PCIID)
[ ! "$CURR_SND_HW_PROFILE" ] && CURR_SND_HW_PROFILE='0+' #precaution
AUDIO_IDS="$(lspci -d ::0403 -n | cut -f 1,3 -d ' ' | tr ' ' '|' | tr '\n' ' ')" #ex: 00:1b.0|8086:1c20
readlink /sys/class/sound/card[0-9] > /tmp/10alsa-CARD_PATHS #ex: ../../devices/pci0000:00/0000:00:1b.0/sound/card0
for aAID in $AUDIO_IDS
do
aPTH=":${aAID/|*/}/sound" #ex: :00:1b.0/sound
aID="${aAID/*|/}" #ex: 8086:1c20
aCDS="$(grep "${aPTH}" /tmp/10alsa-CARD_PATHS | rev | cut -c 1 | rev)" #ex: 0 (card#)
for aCARD in $aCDS
do
echo "${aCARD}+${aID}" >> /tmp/10alsa-AudioCardHardwareProfiles
done
if [ "$CURR_SND_HW_PROFILE" == "0+" ];then #try fix profile...
CURR_CARD="$(grep '^defaults.pcm.card' /etc/asound.conf | tr -s ' ' | cut -f 2 -d ' ')"
[ ! "$CURR_CARD" ] && CURR_CARD='0'
CURR_PROF="${CURR_CARD}+${aID}"
if grep -q -x "${CURR_PROF}" /tmp/10alsa-AudioCardHardwareProfiles;then
grep -v '^#SND_HW_PROFILE' /etc/asound.conf > /tmp/10alsa-asound.conf-temp
echo "#SND_HW_PROFILE='${CURR_PROF}'" >> /tmp/10alsa-asound.conf-temp
mv -f /tmp/10alsa-asound.conf-temp /etc/asound.conf
cp -f /etc/asound.conf /etc/asound.conf.${CURR_PROF}
CURR_SND_HW_PROFILE="${CURR_PROF}"
fi
fi
if ! grep -q -x "$CURR_SND_HW_PROFILE" /tmp/10alsa-AudioCardHardwareProfiles;then
for aCONF in /etc/asound.conf.*
do
[ ! "$aCONF" ] && continue
aPROF="${aCONF/*./}" #ex: 0+8086:1c20
if grep -q -x "$aPROF" /tmp/10alsa-AudioCardHardwareProfiles;then
if ! grep -q '^#SND_HW_PROFILE' ${aCONF};then #precaution.
echo "#SND_HW_PROFILE='${aPROF}'" >> ${aCONF}
fi
cp -af ${aCONF} /etc/asound.conf #use backup.
SND_HW_PROFILE="$aPROF"
#use backup state...
[ -f /etc/asound.state.${SND_HW_PROFILE} ] && cp -f /etc/asound.state.${SND_HW_PROFILE} /etc/asound.state
FNDflg=1
break 2
fi
done
else
if [ ! -f /etc/asound.conf.${CURR_SND_HW_PROFILE} ];then #create backup if not exist.
if ! grep -q '^#SND_HW_PROFILE' /etc/asound.conf;then #precaution.
echo "#SND_HW_PROFILE='${CURR_SND_HW_PROFILE}'" >> /etc/asound.conf
fi
cp -f /etc/asound.conf /etc/asound.conf.${CURR_SND_HW_PROFILE}
fi
SND_HW_PROFILE="$CURR_SND_HW_PROFILE"
FNDflg=1
break
fi
done
if [ $FNDflg -eq 0 ];then
echo -n '' > /etc/asound.conf #go back to automatic detection.
fi
#200115 backup the state also...
if [ "$SND_HW_PROFILE" == "$CURR_SND_HW_PROFILE" ];then
if [ -f /etc/asound.state ];then
cp -f /etc/asound.state /etc/asound.state.${SND_HW_PROFILE} #update every bootup.
fi
fi

...see the highlighted line. It is currently only profiling audio cards on the PCI bus. I intend to add profiling of USB audio "cards" after the couple ordered from eBay arrive. Bluetooth audio will also need some consideration.

Here is the code from /usr/sbin/mscw, in the write_asound_card() function:

 #200113 hardware-profile... 200115 improve...
SND_HW_PROFILE="${card}+"
AUDIO_IDS="$(lspci -d ::0403 -n | cut -f 1,3 -d ' ' | tr ' ' '|')" #ex: 00:1b.0|8086:1c20
CARD_PATH="$(readlink /sys/class/sound/card${card})"
#...ex: ../../devices/pci0000:00/0000:00:1b.0/sound/card0
echo "$CARD_PATH" > /tmp/10alsa-CARD_PATH
for aAID in $AUDIO_IDS
do
aPTH=":${aAID/|*/}/sound" #ex: :00:1b.0/sound
aID="${aAID/*|/}" #ex: 8086:1c20
if grep -q "${aPTH}" /tmp/10alsa-CARD_PATH;then
SND_HW_PROFILE="${card}+${aID}"
break
fi
done
echo "#SND_HW_PROFILE='${SND_HW_PROFILE}'" >> /etc/asound.conf
cp -a -f /etc/asound.conf /etc/asound.conf.${SND_HW_PROFILE}

I have done some basic testing, and it looks good. Booted Easy 2.2.4 on my HP midi-tower PC, and got the backups. Then booted on my Acer Aspire1 laptop, and got appropriate backups. Booting up on either computer, the appropriate backup files load.

Here is a snapshot, in folder /etc:

img1

...this was after booting on the laptop, and the bold text shows the appropriate profiles for the laptop. I then booted up on the HP PC, and confirmed that asound.conf loaded from asound.conf.0+8086"1c20 and asound.state from asound.state.0+80886:ic20. In other words, the profile appropriate to the PC got loaded.

A simple test is to set the sound level differently on each computer, and see it get set appropriately, when bootup on either computer.

One extra note: this profiling would also work if change the sound card on the same computer.  

Tags: easy