|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +usage() { |
| 6 | + cat <<- _EOF_ |
| 7 | + live cd sound helper script. |
| 8 | + Usage: livecdsound [OPTION] |
| 9 | + OPTIONS |
| 10 | + -u, --unmute unmute all sound cards |
| 11 | + -p, --pick select a card for speetch output |
| 12 | + -h, --help Show this usage message |
| 13 | +
|
| 14 | +_EOF_ |
| 15 | +} |
| 16 | + |
| 17 | +bugout () { |
| 18 | + printf "/usr/local/bin/livecdsound: programming error" |
| 19 | + stat_fail |
| 20 | +} |
| 21 | + |
| 22 | +echo_card_indices() |
| 23 | +{ |
| 24 | + if [ -f /proc/asound/cards ] ; then |
| 25 | + sed -n -e's/^[[:space:]]*\([0-7]\)[[:space:]].*/\1/p' /proc/asound/cards |
| 26 | + fi |
| 27 | +} |
| 28 | + |
| 29 | +# The following functions try to set many controls. |
| 30 | +# No card has all the controls and so some of the attempts are bound to fail. |
| 31 | +# Because of this, the functions can't return useful status values. |
| 32 | + |
| 33 | +# $1 <card id> |
| 34 | +# $2 <control> |
| 35 | +# $3 <level> |
| 36 | +unmute_and_set_level(){ |
| 37 | + { [ "$3" ] &&[ "$2" ] && [ "$1" ] ; } || bugout |
| 38 | + systemd-cat -t "livecdsound" printf "Setting: %s on card: %s to %s\n" "$2" "$1" "$3" |
| 39 | + systemd-cat -t "livecdsound" amixer -c "$1" set "$2" "$3" unmute |
| 40 | + return 0 |
| 41 | +} |
| 42 | + |
| 43 | +# $1 <card id> |
| 44 | +# $2 <control> |
| 45 | +mute_and_zero_level() |
| 46 | +{ |
| 47 | + { [ "$1" ] && [ "$2" ] ; } || bugout |
| 48 | + systemd-cat -t "livecdsound" printf "Muting control: %s on card: %s\n" "$2" "$1" |
| 49 | + systemd-cat -t "livecdsound" amixer -c "$1" set "$2" "0%" mute |
| 50 | + return 0 |
| 51 | +} |
| 52 | + |
| 53 | +# $1 <card ID> |
| 54 | +# $2 <control> |
| 55 | +# $3 "on" | "off" |
| 56 | +switch_control() |
| 57 | +{ |
| 58 | + { [ "$3" ] && [ "$1" ] ; } || bugout |
| 59 | + systemd-cat -t "livecdsound" printf "Switching control: %s on card: %s to %s\n" "$2" "$1" "$3" |
| 60 | + systemd-cat -t "livecdsound" amixer -c "$1" set "$2" "$3" |
| 61 | + return 0 |
| 62 | +} |
| 63 | + |
| 64 | +# $1 <card ID> |
| 65 | +sanify_levels_on_card() |
| 66 | +{ |
| 67 | + unmute_and_set_level "$1" "Front" "80%" |
| 68 | + unmute_and_set_level "$1" "Master" "80%" |
| 69 | + unmute_and_set_level "$1" "Master Mono" "80%" |
| 70 | + unmute_and_set_level "$1" "Master Digital" "80%" # E.g., cs4237B |
| 71 | + unmute_and_set_level "$1" "Playback" "80%" |
| 72 | + unmute_and_set_level "$1" "Headphone" "100%" |
| 73 | + unmute_and_set_level "$1" "PCM" "80%" |
| 74 | + unmute_and_set_level "$1" "PCM,1" "80%" # E.g., ess1969 |
| 75 | + unmute_and_set_level "$1" "DAC" "80%" # E.g., envy24, cs46xx |
| 76 | + unmute_and_set_level "$1" "DAC,0" "80%" # E.g., envy24 |
| 77 | + unmute_and_set_level "$1" "DAC,1" "80%" # E.g., envy24 |
| 78 | + unmute_and_set_level "$1" "Synth" "80%" |
| 79 | + unmute_and_set_level "$1" "CD" "80%" |
| 80 | + unmute_and_set_level "$1" "PC Speaker" "100%" |
| 81 | + |
| 82 | + mute_and_zero_level "$1" "Mic" |
| 83 | + mute_and_zero_level "$1" "IEC958" # Ubuntu #19648 |
| 84 | + |
| 85 | + # Intel P4P800-MX |
| 86 | + switch_control "$1" "Master Playback Switch" on |
| 87 | + switch_control "$1" "Master Surround" on |
| 88 | + |
| 89 | + # Trident/YMFPCI/emu10k1: |
| 90 | + unmute_and_set_level "$1" "Wave" "80%" |
| 91 | + unmute_and_set_level "$1" "Music" "80%" |
| 92 | + unmute_and_set_level "$1" "AC97" "80%" |
| 93 | + |
| 94 | + # DRC: |
| 95 | + unmute_and_set_level "$1" "Dynamic Range Compression" "80%" |
| 96 | + |
| 97 | + # Required for HDA Intel (hda-intel): |
| 98 | + unmute_and_set_level "$1" "Front" "80%" |
| 99 | + |
| 100 | + # Required for SB Live 7.1/24-bit (ca0106): |
| 101 | + unmute_and_set_level "$1" "Analog Front" "80%" |
| 102 | + |
| 103 | + # Required at least for Via 823x hardware on DFI K8M800-MLVF Motherboard |
| 104 | + switch_control "$1" "IEC958 Capture Monitor" off |
| 105 | + |
| 106 | + # Required for hardware allowing toggles for AC97 through IEC958, |
| 107 | + # valid values are 0, 1, 2, 3. Needs to be set to 0 for PCM1. |
| 108 | + unmute_and_set_level "$1" "IEC958 Playback AC97-SPSA" "0" |
| 109 | + |
| 110 | + # Required for newer Via hardware |
| 111 | + unmute_and_set_level "$1" "VIA DXS,0" "80%" |
| 112 | + unmute_and_set_level "$1" "VIA DXS,1" "80%" |
| 113 | + unmute_and_set_level "$1" "VIA DXS,2" "80%" |
| 114 | + unmute_and_set_level "$1" "VIA DXS,3" "80%" |
| 115 | + |
| 116 | + # Required on some notebooks with ICH4: |
| 117 | + switch_control "$1" "Headphone Jack Sense" off |
| 118 | + switch_control "$1" "Line Jack Sense" off |
| 119 | + |
| 120 | + # Some machines need one or more of these to be on; |
| 121 | + # others need one or more of these to be off: |
| 122 | + |
| 123 | + switch_control "$1" "Audigy Analog/Digital Output Jack" on |
| 124 | + switch_control "$1" "SB Live Analog/Digital Output Jack" on |
| 125 | + |
| 126 | + # D1984 -- Thinkpad T61/X61 |
| 127 | + switch_control "$1" "Speaker" on |
| 128 | + switch_control "$1" "Headphone" on |
| 129 | + |
| 130 | + # HDA-Intel w/ "Digital" capture mixer (See Ubuntu #193823) |
| 131 | + unmute_and_set_level "$1" "Digital" "80%" |
| 132 | + |
| 133 | + return 0 |
| 134 | +} |
| 135 | + |
| 136 | +# $1 <card ID> | "all" |
| 137 | +sanify_levels() |
| 138 | +{ |
| 139 | + local ttsdml_returnstatus=0 |
| 140 | + local card |
| 141 | + case "$1" in |
| 142 | + all) |
| 143 | + for card in $(echo_card_indices) ; do |
| 144 | + sanify_levels_on_card "$card" || ttsdml_returnstatus=1 |
| 145 | + done |
| 146 | + ;; |
| 147 | + *) |
| 148 | + sanify_levels_on_card "$1" || ttsdml_returnstatus=1 |
| 149 | + ;; |
| 150 | + esac |
| 151 | + return $ttsdml_returnstatus |
| 152 | +} |
| 153 | + |
| 154 | +# List all cards that *should* be usable for PCM audio. In my experience, |
| 155 | +# the console speaker (handled by the pcsp driver) isn't a suitable playback |
| 156 | +# device, so we'll exclude it. |
| 157 | +list_non_pcsp_cards() |
| 158 | +{ |
| 159 | + for card in $(echo_card_indices); do |
| 160 | + local cardfile="/proc/asound/card${card}/id" |
| 161 | + if [ -r "$cardfile" ] && [ -f "$cardfile" ] && \ |
| 162 | + [ "$(cat "$cardfile")" != pcsp ]; then |
| 163 | + echo "$card" |
| 164 | + fi |
| 165 | + done |
| 166 | +} |
| 167 | + |
| 168 | +# Properly initialize the sound card so that we have audio at boot. |
| 169 | +unmute_all_cards() |
| 170 | +{ |
| 171 | + sanify_levels all |
| 172 | +} |
| 173 | + |
| 174 | +is_numeric() { |
| 175 | + local str=$1 |
| 176 | + [[ "$str" =~ ^[0-9]+$ ]] |
| 177 | +} |
| 178 | + |
| 179 | +set_default_card() { |
| 180 | + local card=$1 |
| 181 | + sed -e "s/%card%/$card/g" < /usr/local/share/livecd-sound/asound.conf.in \ |
| 182 | + > /etc/asound.conf |
| 183 | +} |
| 184 | + |
| 185 | +play_on_card() { |
| 186 | + local card=$1 file=$2 |
| 187 | + aplay -q "-Dplughw:$card,0" "$file" |
| 188 | +} |
| 189 | + |
| 190 | +# If there are multiple usable sound cards, prompt the user to choose one, |
| 191 | +# using auditory feedback. |
| 192 | +pick_a_card() |
| 193 | +{ |
| 194 | + set -f |
| 195 | + usable_cards="$(list_non_pcsp_cards)" |
| 196 | + num_usable_cards="$(wc -w <<< "$usable_cards")" |
| 197 | + |
| 198 | + if [ "$num_usable_cards" -eq 1 ]; then |
| 199 | + systemd-cat -t "livecdsound" printf "Only one sound card is detected\n" |
| 200 | + exit 0 |
| 201 | + fi |
| 202 | + systemd-cat -t "livecdsound" printf "multiple sound cards detected\n" |
| 203 | + for card in $usable_cards; do |
| 204 | + if ! is_numeric "$card"; then |
| 205 | + continue |
| 206 | + fi |
| 207 | + play_on_card "$card" /usr/share/livecd-sounds/pick-a-card.wav& |
| 208 | + done |
| 209 | + wait |
| 210 | + sleep 1 |
| 211 | + for card in $usable_cards; do |
| 212 | + if ! is_numeric "$card"; then |
| 213 | + continue |
| 214 | + fi |
| 215 | + play_on_card "$card" /usr/share/livecd-sounds/beep.wav |
| 216 | + if read -r -t 10; then |
| 217 | + systemd-cat -t "livecdsound" printf "Selecting %s sound card as default\n" "$card" |
| 218 | + set_default_card "$card" |
| 219 | + break |
| 220 | + fi |
| 221 | +done |
| 222 | +} |
| 223 | + |
| 224 | +if [[ $# -eq 0 ]]; then |
| 225 | + echo "error: No argument passed." |
| 226 | + exit 1 |
| 227 | +fi |
| 228 | +while [[ "${1}" != "" ]]; do |
| 229 | + case ${1} in |
| 230 | + -h|--help) |
| 231 | + usage |
| 232 | + exit |
| 233 | + ;; |
| 234 | + -u|--unmute) |
| 235 | + systemd-cat -t "livecdsound" printf "Unmuting all cards" |
| 236 | + unmute_all_cards |
| 237 | + ;; |
| 238 | + -p|--pick) |
| 239 | + pick_a_card |
| 240 | + ;; |
| 241 | + *) |
| 242 | + echo "error: Unsupported argument" |
| 243 | + usage |
| 244 | + exit 1 |
| 245 | + ;; |
| 246 | + esac |
| 247 | + shift |
| 248 | +done |
0 commit comments