From 7fefbaec6be4e73e5f33486df3bd7a4a8b03c7a9 Mon Sep 17 00:00:00 2001 From: luc Date: Wed, 13 Aug 2025 15:00:08 +0200 Subject: [PATCH] src/audio: add --- src/audio | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/audio diff --git a/src/audio b/src/audio new file mode 100644 index 0000000..3a7c8d7 --- /dev/null +++ b/src/audio @@ -0,0 +1,124 @@ +#!/bin/sh + +# Function to notify about current audio sink/source state +function notify_state { + case "$type" in + "sink") # Handle notifications for audio sink (output) + # Read the last audio sink notification ID from state, or set default if it does not exist + sink_last_notify_id="$(cat $HOME/.local/state/audio/sink || echo "1024")" + + # Get the volume level and mute status of the default audio sink + local volume="$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk '{print $2 * 100}')" + local mute="$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk '{print $3}')" + + # Send a different notification based on the mute status + if [ -z $mute ]; then + + # Send a different notification based on the volume level + if [ $volume -gt 67 ]; then + sink_last_notify_id="$(notify-send "Volume sink: ${volume}%" -h int:value:${volume} -h string:synchronous: -i $sink_high -t 2000 -r $sink_last_notify_id -p)" + elif [ $volume -gt 33 ]; then + sink_last_notify_id="$(notify-send "Volume sink: ${volume}%" -h int:value:${volume} -h string:synchronous: -i $sink_medium -t 2000 -r $sink_last_notify_id -p)" + else + sink_last_notify_id="$(notify-send "Volume sink: ${volume}%" -h int:value:${volume} -h string:synchronous: -i $sink_low -t 2000 -r $sink_last_notify_id -p)" + fi + else + sink_last_notify_id="$(notify-send "Volume sink: muted" -h int:value:${volume} -h string:synchronous: -i $sink_muted -t 2000 -r $sink_last_notify_id -p)" + fi + + # Write the last audio sink notification ID to state + echo $sink_last_notify_id > $HOME/.local/state/audio/sink + ;; + "source") # Handle notifications for audio source (input) + # Read the last audio source notification from state, or set default if it does not exist + source_last_notify_id="$(cat $HOME/.local/state/audio/source || echo "1024")" + + # Get the volume level and mute status of the default audio source + local volume="$(wpctl get-volume @DEFAULT_AUDIO_SOURCE@ | awk '{print $2 * 100}')" + local mute="$(wpctl get-volume @DEFAULT_AUDIO_SOURCE@ | awk '{print $3}')" + + # Send a different notification based on the mute status + if [ -z $mute ]; then + + # Send a different notification based on the volume level + if [ $volume -gt 67 ]; then + source_last_notify_id="$(notify-send "Volume source: ${volume}%" -h int:value:${volume} -h string:synchronous: -i $source_high -t 2000 -r $source_last_notify_id -p)" + elif [ $volume -gt 33 ]; then + source_last_notify_id="$(notify-send "Volume source: ${volume}%" -h int:value:${volume} -h string:synchronous: -i $source_medium -t 2000 -r $source_last_notify_id -p)" + else + source_last_notify_id="$(notify-send "Volume source: ${volume}%" -h int:value:${volume} -h string:synchronous: -i $source_low -t 2000 -r $source_last_notify_id -p)" + fi + else + source_last_notify_id="$(notify-send "Volume source: muted" -h int:value:${volume} -h string:synchronous: -i $source_muted -t 2000 -r $source_last_notify_id -p)" + fi + + # Write the last audio source notification ID to state + echo $source_last_notify_id > $HOME/.local/state/audio/source + ;; + esac +} + +# Get type and action from args +type="$1" +action="$2" + +# Define icon theme +icon_theme="tela-circle-black" + +# Define paths to icon files for different audio sink states +sink_high="/usr/share/icons/${icon_theme}/24/panel/audio-volume-high.svg" +sink_medium="/usr/share/icons/${icon_theme}/24/panel/audio-volume-medium.svg" +sink_low="/usr/share/icons/${icon_theme}/24/panel/audio-volume-low.svg" +sink_muted="/usr/share/icons/${icon_theme}/24/panel/audio-volume-muted.svg" + +# Define paths to icon files for different audio source states +source_high="/usr/share/icons/${icon_theme}/24/panel/mic-volume-high.svg" +source_medium="/usr/share/icons/${icon_theme}/24/panel/mic-volume-medium.svg" +source_low="/usr/share/icons/${icon_theme}/24/panel/mic-volume-low.svg" +source_muted="/usr/share/icons/${icon_theme}/24/panel/mic-volume-muted.svg" + +# Determine action to take based on type +case "$type" in + "sink") # Actions for audio sink + case "$action" in + "volup") # Increase audio sink volume by 10% + wpctl set-volume @DEFAULT_AUDIO_SINK@ 10%+ + ;; + "voldown") # Decrease audio sink volume by 10% + wpctl set-volume @DEFAULT_AUDIO_SINK@ 10%- + ;; + "toggle") # Toggle audio sink mute + wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle + ;; + *) # Handle invalid actions for audio sink + echo "Invalid action for sink. Use audio sink ." + exit 1 + ;; + esac + ;; + "source") # Actions for audio source + case "$action" in + "volup") # Increase audio source volume by 10% + wpctl set-volume @DEFAULT_AUDIO_SOURCE@ 10%+ + ;; + "voldown") # Decrease audio source volume by 10% + wpctl set-volume @DEFAULT_AUDIO_SOURCE@ 10%- + ;; + "toggle") # Toggle audio source mute + wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle + ;; + *) # Handle invalid actions for audio source + echo "Invalid action for source. Use audio source ." + exit 1 + ;; + esac + ;; + + *) # Handle invalid types + echo "Invalid type. Use audio ." + exit 1 + ;; +esac + +# Notify the current audio state after action +notify_state