mylinuxforwork/dotfiles

[FEATURE] Quick link to switch audio output device in waybar

Opened this issue · 0 comments

Is your feature request related to a problem? Please describe.
I often change audio output device between speakers and headphones. Currently there is no quick way in waybar to do this. It would be handy to utilize mouse right click and midclick actions for such tasks.

Describe the solution you'd like
I have modified the included Pulseaudio module settings by adding a mouse right-click function and added a script that uses pactl to change between audio outputs ("default sink"). My script is configured to my individual setup by using my device names. I am a beginner in linux scripting so I'm not sure how to implement it so that it would read all device names with pactl and configure itself accordingly. If this script is implented as is, user needs to manually change speaker and headphone device names for it to work. Maybe this kind of function could be implemented with wpctl?

Additionally, mute audio could be easily added with mouse middle-click using pactl.

Below is the script that I'm using (toggleaudio.sh). It is modified from this reddit post.

#!/bin/bash

newSink=""

Headphones="alsa_output.usb-Logitech_PRO_X_Wireless_Gaming_Headset-00.analog-stereo" #Change this device's to headphone pactl name
Speaker="alsa_output.pci-0000_0b_00.4.analog-stereo" #Change this to device's speaker pactl name
currentSink=$(pactl info | sed -n 's/Default Sink: //p')

if [ "$Speaker" = "$currentSink" ]; then
    newSink="$Headphones"
else
    newSink="$Speaker"
fi

pactl set-default-sink "$newSink"
pactl list short sink-inputs|while read stream; do
    streamId=$(echo $stream|cut '-d ' -f1)
    echo "moving stream $streamId"
    pactl move-sink-input "$streamId" "$newSink"
done

Pulseaudio module (in waybar/modules.json) with right-click function calling toggleaudio.sh script:

  // Pulseaudio
  "pulseaudio": {
    // "scroll-step": 1, // %, can be a float
    "format": "{icon}  {volume}%",
    "format-bluetooth": "{volume}% {icon} {format_source}",
    "format-bluetooth-muted": " {icon} {format_source}",
    "format-muted": " {format_source}",
    "format-source": "{volume}% ",
    "format-source-muted": "",
    "format-icons": {
      "headphone": " ",
      "hands-free": " ",
      "headset": " ",
      "phone": " ",
      "portable": " ",
      "car": " ",
      "default": ["", "", ""]
    },
    "on-click": "pavucontrol",
    "on-click-right": "~/.config/waybar/toggleaudio.sh"
  },