livecd-sound 6.9 KB

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