Skip to content

Commit 4b996ee

Browse files
committed
audio_common.sh: backend/mic discovery & capture utilities
- Introduce ALSA capture utilities: - alsa_pick_capture(): pick a valid hw:<card>,<device> using with fallbacks to /proc/asound/{pcm,devices} Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent 7e821eb commit 4b996ee

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

Runner/utils/audio_common.sh

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ pa_default_source() {
262262
printf '%s\n' "$s"
263263
}
264264

265-
pa_set_default_source() { [ -n "$1" ] && pactl set-default-source "$1" >/dev/null 2>&1 || true; }
265+
pa_set_default_source() {
266+
if [ -n "$1" ]; then
267+
pactl set-default-source "$1" >/dev/null 2>&1 || true
268+
fi
269+
}
266270

267271
pa_source_name() {
268272
id="$1"; [ -n "$id" ] || return 1
@@ -596,4 +600,64 @@ audio_exec_with_timeout() {
596600
# no timeout
597601
"$@"
598602
}
599-
603+
604+
# --------------------------------------------------------------------
605+
# Backend chain + minimal ALSA capture picker (for fallback in run.sh)
606+
# --------------------------------------------------------------------
607+
608+
# Prefer: currently selected (or detected) backend, then pipewire, pulseaudio, alsa.
609+
# We keep it simple: we don't filter by daemon state here; the caller tries each.
610+
build_backend_chain() {
611+
preferred="${AUDIO_BACKEND:-$(detect_audio_backend)}"
612+
chain=""
613+
add_unique() {
614+
case " $chain " in
615+
*" $1 "*) : ;;
616+
*) chain="${chain:+$chain }$1" ;;
617+
esac
618+
}
619+
[ -n "$preferred" ] && add_unique "$preferred"
620+
for b in pipewire pulseaudio alsa; do
621+
add_unique "$b"
622+
done
623+
printf '%s\n' "$chain"
624+
}
625+
626+
# Pick a plausible ALSA capture device.
627+
# Returns something like hw:0,0 if available, else "default".
628+
alsa_pick_capture() {
629+
line="$(arecord -l 2>/dev/null | sed -n 's/^card \([0-9][0-9]*\):.*device \([0-9][0-9]*\):.*/\1 \2/p' | head -n1)"
630+
if [ -n "$line" ]; then
631+
set -- "$line"
632+
printf 'hw:%s,%s\n' "$1" "$2"
633+
return 0
634+
fi
635+
printf '%s\n' "default"
636+
return 0
637+
}
638+
639+
alsa_pick_capture() {
640+
command -v arecord >/dev/null 2>&1 || return 1
641+
# Prefer the first real capture device from `arecord -l`
642+
arecord -l 2>/dev/null | awk '
643+
/card [0-9]+: .*device [0-9]+:/ {
644+
if (match($0, /card ([0-9]+):/, c) && match($0, /device ([0-9]+):/, d)) {
645+
printf("hw:%s,%s\n", c[1], d[1]);
646+
exit 0;
647+
}
648+
}
649+
'
650+
}
651+
652+
# Prefer virtual capture PCMs (PipeWire/Pulse) over raw hw: when a sound server is present
653+
alsa_pick_virtual_pcm() {
654+
command -v arecord >/dev/null 2>&1 || return 1
655+
pcs="$(arecord -L 2>/dev/null | sed -n 's/^[[:space:]]*\([[:alnum:]_]\+\)[[:space:]]*$/\1/p')"
656+
for pcm in pipewire pulse default; do
657+
if printf '%s\n' "$pcs" | grep -m1 -x "$pcm" >/dev/null 2>&1; then
658+
printf '%s\n' "$pcm"
659+
return 0
660+
fi
661+
done
662+
return 1
663+
}

0 commit comments

Comments
 (0)