Something went wrong. Try again.
Mirroring: https://github.com/MunifTanjim/scripts.sh
Something went wrong. Try again.
scripts.sh audio-device
4.5 kB · 190 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191#!/usr/bin/env bash
set -euo pipefail
assert_command() { if ! type "${1}" >/dev/null 2>&1; then echo "${2:-"command not found: ${1}"}" exit 1 fi}
assert_val() { if [[ -z "${1}" ]]; then echo "${2}" exit 1 fi}
assert_func() { if ! declare -F "${1}" >/dev/null; then echo "${2}" exit 1 fi}
__linux_device_type() { local device_type="${1}" case "${1}" in i*|so*) echo "source";; o*|si*) echo "sink";; *) echo "invalid device type: ${1}" exit 1 esac}
__linux_describe_device() { local -r device_type="$(__linux_device_type "${1}")" local -r index=$(( ${2} )) local -r key="${3}"
local delim=":" if [[ "${key}" = *"."* ]]; then delim='"' fi
pacmd list-"${device_type}s" \ | sed -n "/index: ${index}/,/index: /{/index: /!p;}" \ | grep -e "${key}" | cut -d "${delim}" -f2 | head -1 \ | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' \ || echo ""}
__linux_device_name() { local -r device_type="$(__linux_device_type "${1}")" local -r index="${2}"
local device_name for key in "device.product.name" "bluez.alias" "device.description"; do device_name="$(__linux_describe_device "${device_type}" "${index}" "${key}")" if test -n "${device_name}"; then echo "${device_name}" return fi done}
__linux_current_device_index() { local -r device_type="$(__linux_device_type "${1}")" pacmd list-"${device_type}s" | grep "\* index: " | sed -e 's/ *\* *index: *//'}
__linux_list_devices() { local -r device_type="$(__linux_device_type "${1}")"
local -a devices mapfile -t devices < <(pactl list short "${device_type}s")
local index for device in "${devices[@]}"; do index="$(echo "${device}" | awk '{print $1}')" if [[ "$(__linux_describe_device "${device_type}" "${index}" "flags")" = *HARDWARE* ]]; then echo "${index} $(__linux_device_name "${device_type}" "${index}")" fi done}
_linux_current() { local -r input_device="$(__linux_device_name input "$(__linux_current_device_index input)")" local -r output_device="$(__linux_device_name output "$(__linux_current_device_index output)")"
local -n variable="${1:-}_device" if test -z "${variable:-}"; then echo " Input: ${input_device}" echo "Output: ${output_device}" else echo "${variable}" fi}
_linux_input() { local -r device_type="$(__linux_device_type input)"
local selected_index selected_index="$(__linux_list_devices "${device_type}" | fzf | cut -d' ' -f1)"
pactl set-default-source "${selected_index}"}
_linux_output() { local -r device_type="$(__linux_device_type output)"
local selected_index selected_index="$(__linux_list_devices "${device_type}" | fzf | cut -d' ' -f1)"
pactl set-default-sink "${selected_index}"
if [[ "$(__linux_describe_device "${device_type}" "${selected_index}" "device.bus")" = "bluetooth" ]]; then local -A profile_map=(["Headset Head Unit (w/ Microphone)"]=headset_head_unit ["High Fidelity Playback (w/o Micrphone)"]=a2dp_sink) local -r profile="${profile_map[$(printf "%s\n" "${!profile_map[@]}" | fzf --header='Bluetooth Sound Profile')]}" pacmd set-card-profile "$(__linux_describe_device "${device_type}" "${selected_index}" "card" | awk '{ print $1 }')" "${profile}" fi}
_darwin_current() { local -r input_device="$(SwitchAudioSource -t input -c)" local -r output_device="$(SwitchAudioSource -t output -c)"
local -n variable="${1:-}_device"
if test -z "${variable:-}"; then echo " Input: ${input_device}" echo "Output: ${output_device}" else echo "${variable}" fi}
_darwin_input() { local -r device="$(SwitchAudioSource -t input -a | fzf --header='Input Device')" SwitchAudioSource -t input -s "${device}"}
_darwin_output() { local -r device="$(SwitchAudioSource -t output -a | fzf --header='Output Device')" SwitchAudioSource -t output -s "${device}"}
assert_command fzf
declare OS=""case "${OSTYPE}" in darwin*) assert_command SwitchAudioSource OS="darwin" ;; linux*) assert_command pacmd assert_command pactl OS="linux" ;; *) echo "Unsupported OS!" exit 1;;esac
declare -r cmd="${1:-"help"}"shift || true
case "${cmd}" in source) cmd="input";; sink) cmd="output";; help) cat << HELPUSAGE: audio-device current [input|output] audio-device input audio-device outputHELP exit 0esac
declare -r func="_${OS}_${cmd}"assert_func "${func}" "invalid command: ${cmd}"
$func $@