From 1c31cceae40b010a01adbc4b7425fc06b96d4d7f Mon Sep 17 00:00:00 2001 From: Haelnorr Date: Mon, 23 Jun 2025 15:31:45 +1000 Subject: [PATCH] added audio output toggle --- .config/polybar/audio_output_status.sh | 33 ++++++++++++++++++ .config/polybar/audio_output_switch.sh | 48 ++++++++++++++++++++++++++ .config/polybar/config.ini | 11 ++++-- 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100755 .config/polybar/audio_output_status.sh create mode 100755 .config/polybar/audio_output_switch.sh diff --git a/.config/polybar/audio_output_status.sh b/.config/polybar/audio_output_status.sh new file mode 100755 index 0000000..b72756e --- /dev/null +++ b/.config/polybar/audio_output_status.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Get the ID of the default sink (output device) +default_sink=$(wpctl status 2>/dev/null | awk '/Sinks:/,/Sources:/' | awk '/\*/ {print $3}' | tr -d '.') + +if [ -z "$default_sink" ]; then + echo "No default audio output device found" + exit 1 +fi + +# Get the description or fallback to node.name +device_name=$(wpctl inspect "$default_sink" 2>/dev/null | awk -F '"' '/node.description/ {print $2}') + +if [ -z "$device_name" ]; then + device_name=$(wpctl inspect "$default_sink" 2>/dev/null | awk -F '"' '/node.name/ {print $2}') +fi + +# Map known device names to labels +case "$device_name" in + "Starship/Matisse HD Audio Controller Analog Stereo") + label="Speakers" + ;; + "Yeti Stereo Microphone Analog Stereo") + label="Earphones" + ;; + *) + label="$device_name" + ;; +esac + +# Output the label +echo "$label" + diff --git a/.config/polybar/audio_output_switch.sh b/.config/polybar/audio_output_switch.sh new file mode 100755 index 0000000..7e49e19 --- /dev/null +++ b/.config/polybar/audio_output_switch.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Function to get the ID of the default sink +get_default_sink_id() { + wpctl status | awk '/Sinks:/,/Sources:/' | awk '/\*/ {print $3}' | tr -d '.' +} + +# Function to get the description of a sink by ID +get_sink_description() { + wpctl inspect "$1" | awk -F '"' '/node.description/ {print $2}' +} + +# Get current default sink ID +current_sink_id=$(get_default_sink_id) + +# If no sink found, exit +if [ -z "$current_sink_id" ]; then + echo "No default audio output device found" + exit 1 +fi + +# Get its description +current_description=$(get_sink_description "$current_sink_id") + +# Decide target device name based on current device +case "$current_description" in + "Starship/Matisse HD Audio Controller Analog Stereo") + target_description="Yeti Stereo Microphone Analog Stereo" + ;; + "Yeti Stereo Microphone Analog Stereo") + target_description="Starship/Matisse HD Audio Controller Analog Stereo" + ;; + *) + target_description="Yeti Stereo Microphone Analog Stereo" + ;; +esac + +# Find the sink ID of the target device +target_sink_id=$(wpctl status | awk '/Sinks:/,/Sources:/' | grep "$target_description" | awk '{print $2}' | tr -d '.') + +# If target sink found, switch to it +if [ -n "$target_sink_id" ]; then + wpctl set-default "$target_sink_id" + echo "Switched audio output to: $target_description" +else + echo "Target device '$target_description' not found." + exit 1 +fi diff --git a/.config/polybar/config.ini b/.config/polybar/config.ini index b0fc4cf..db0930c 100644 --- a/.config/polybar/config.ini +++ b/.config/polybar/config.ini @@ -83,7 +83,7 @@ separator-foreground = ${colors.red} font-0 = JetBrains Mono Nerd Font:size=12;4 modules-left = xworkspaces xwindow -modules-right = picom-status pulseaudio battery memory cpu wlan eth date +modules-right = picom-status audio audio_device battery memory cpu wlan eth date cursor-click = pointer cursor-scroll = ns-resize @@ -136,7 +136,7 @@ label-mounted = %{F#F0C674}%mountpoint%%{F-} %percentage_used%% label-unmounted = %mountpoint% not mounted label-unmounted-foreground = ${colors.disabled} -[module/pulseaudio] +[module/audio] type = internal/pulseaudio format-volume-prefix = " " @@ -149,6 +149,13 @@ label-volume-foreground = ${colors.green} label-muted = muted label-muted-foreground = ${colors.disabled} +[module/audio_device] +type = custom/script +exec = ~/.config/polybar/audio_output_status.sh +hook-0 = ~/.config/polybar/audio_output_status.sh +interval = 0 +click-left = ~/.config/polybar/audio_output_switch.sh + [module/xkeyboard] type = internal/xkeyboard blacklist-0 = num lock