Skip to content

Instantly share code, notes, and snippets.

@jneen
Created April 16, 2020 17:58
Show Gist options
  • Save jneen/9c7c0c0807929ce8c8b259af8857e4f1 to your computer and use it in GitHub Desktop.
Save jneen/9c7c0c0807929ce8c8b259af8857e4f1 to your computer and use it in GitHub Desktop.
THE ONE TRUE SETUP
#!/bin/bash
export AUDIO_DEFAULT_CAPTURE=hw:PCH
export AUDIO_DEFAULT_PLAYBACK=hw:PCH
execd() {
echo "$@" >&2
"$@"
}
audio-cleanup() {
jack_control ds dummy
jack_control status || (killall jackd && jack_control start)
jack_control sm || return $?
pactl info || pulseaudio --start
pulse-connect sink pulse-sink jack-sink
pulse-connect source pulse-source jack-source
pulse-connect source pulse-monitor jack-monitor
}
pulse-connect() {
local dir="$1"; shift
local jack_name="$1"; shift
local pulse_name="$1"; shift
jack_lsp | grep -q "$jack_name" && return
pactl list "$dir"s | grep -q "Name: $pulse_name" && {
pactl unload-module "$pulse_name"
}
pactl load-module module-jack-"$dir" \
channels=2 client_name="$jack_name" \
"$dir"_name="$pulse_name" \
"$@"
}
audio-setup() {
audio-cleanup
jack_control ds alsa
jack_control dps playback "$AUDIO_DEFAULT_PLAYBACK"
jack_control dps capture "$AUDIO_DEFAULT_CAPTURE"
local playback
while [[ $# -gt 0 ]]; do
local a="$1"; shift
case "$a" in
-o|--option)
local opt_name="$1"; shift
local opt_val="$1"; shift
jack_control dps "$opt_name" "$opt_val"
;;
*)
if [[ -z "$playback" ]]; then
playback=1
jack_control dps playback "$a"
else
jack_control dps capture "$a"
fi
;;
esac
done
jack_control dp
jack_control sm || return $?
patch_pulse
}
jack_patch() {
local a b
paste <(jack_lsp | fgrep "$1" | sort) <(jack_lsp | fgrep "$2" | sort) \
| while IFS="$TAB" read a b; do
[[ -z "$a" ]] && break
[[ -z "$b" ]] && break
is-jack-connected "$a" "$b" || execd jack_connect "$a" "$b"
done
}
is-jack-connected() {
local source="$1"; shift
local sink="$1"; shift
jack_lsp -c "$source" | tail -n+2 | fgrep -q "$sink"
}
patch_pulse() {
jack_patch system:capture pulse-source
jack_patch system:playback pulse-sink
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment