aports

Custom Alpine Linux aports

git clone git://git.lin.moe/aports.git

 1#!/bin/sh
 2
 3# When controlC* device are added, restore the card's settings, or set Master and PCM to 10%.
 4# Some devices may have absolute bizzare defaults that may hurt the user in the process. *cough* Dragonfly Red 1.07 *cough*
 5
 6# mdev -s does not expose $DEVNAME but $MDEV
 7if ! [ "${DEVNAME}" ] && [ "${MDEV}" ]; then
 8    DEVNAME="${MDEV}"
 9fi
10
11mute_mixers() {
12    card_id="$1"
13    for control in PCM Master; do
14        amixer -q -M -c "${card_id}" set "${control}" '0%'
15    done
16
17    return 0
18}
19
20restore_mixers() {
21    # Running restore twice, with mute in-between, otherwise volume control may not be flushed to device.
22    # Either it could be edge case with USB DAC firmware or issue with cross-loading state of one USB DAC
23    # onto another one, as all we have is ID.
24
25    card_id="$1"
26    alsactl restore "${card_id}" && mute_mixers "${card_id}" && alsactl restore "${card_id}"
27}
28
29case "${DEVNAME}" in
30    *'controlC'*)
31        control_name="${DEVNAME##*/}"
32        card_id="${control_name#controlC}"
33        # Use NAME instead of numeric ID
34        card_id="$(cat /proc/asound/card${card_id}/id)"
35        if [ "${card_id}" ]; then
36            # Now, we can heavly timeout here, because although we have the control device, the hardware may not yet be ready
37            # so the query about mixers like PCM or Master may fail hard, better to wait for it to become ready
38            #     + amixer -q -c 2 scontrols
39            #     amixer: Mixer hw:2 load error: Connection timed out
40
41            i=1
42            while [ "$i" -lt 5 ]; do
43                amixer -q -c "${card_id}" scontrols >/dev/null && break
44                i="$(( i + 1))"
45            done
46
47            # Always set PCM and Master to 0% prior to running `alsactl restore`.
48            # It appears that *sometimes* the `restore` does not 'flushes' volume level
49            # resulting in at least Dragonfly Red 1.07 blasting at full volume.
50            mute_mixers "${card_id}"
51
52            # If there was no state for this device, set volume of Master and PCM to 10%.
53            # It seems that loading mixer state not always reset volume (Dragonfly Cobalt).
54            if ! restore_mixers "${card_id}"; then
55                for control in PCM Master; do
56                    amixer -q -M -c "${card_id}" set "${control}" '10%'
57                done
58            fi
59        fi
60    ;;
61esac