Sound Selector MacOS
Page content
Sound Selector for MacOS
on MacOS, you can switch the input and output source on “system setting/sound”. i’d like todo this on the cli.
SwitchAudio
there is a litte tool called switchaudio. it can list, and also set the input/output device. let’s build a small wrapper around.
brew install switchaudio-osx
Usage
List Sound Devices
stoege@mac ~ % sound.sh
1: Externe Kopfhörer
2: Externes Mikrofon
3: Jabra Link 400
4: Mac mini-Lautsprecher
5: SRS-XB33
6: USB Audio Device
Set Sound Device
stoege@mace ~ % sound.sh 5
input audio device set to "SRS-XB33"
output audio device set to "SRS-XB33"
Script
#!/usr/bin/env bash
# Audio Switcher on MAC, Stöge, June 2024, https://blog.stoege.net/posts/sound/
# Vars
f="${HOME}/.sound_devices"
# Function to list audio sources with numbers
list_devices() {
count=1
rm -f "${f}" # Clear the temporary file if it exists
SwitchAudioSource -a | sort | uniq | while read -r line; do
echo "$count: $line" | tee -a "${f}"
count=$((count + 1))
done
}
# Function to set audio source/target
showset() {
local device="$1"
echo
if [[ "$device" == "Jabra Link 400" || "$device" == "SRS-XB33" ]]; then
SwitchAudioSource -t input -s "$device"
fi
SwitchAudioSource -s "$device"
echo
}
# No arguments given, list devices
if [ $# -eq 0 ]; then
echo; list_devices; echo
exit 1
fi
# Check if the provided argument is a valid number
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please provide a valid number."
exit 1
fi
# Check if the given number corresponds to a device
device_line=$(grep "^$1: " ${f})
if [ -z "$device_line" ]; then
echo "Device number $1 not found in the list."
exit 1
fi
# Extract the device name
device_name=$(echo "$device_line" | cut -d' ' -f2-)
# Set the audio source to the selected device
showset "${device_name}"
# Done
exit
Any Comments ?
sha256: c3677383b02dc3b1dd46ebc31064ccc8dec6a41024c8dbc1642a596f3271d92c