-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-switch-audio-port.sh
52 lines (39 loc) · 1.68 KB
/
install-switch-audio-port.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# Define the content of the switch-audio-port script
read -r -d '' SWITCH_AUDIO_PORT_SCRIPT <<'EOF'
#!/bin/bash
#!/bin/bash
# Update these descriptions to match your device names
SPEAKER_DESC="Starship/Matisse HD Audio Controller Analog Stereo"
HEADPHONE_DESC="HyperX 7.1 Audio Analog Stereo"
# Get sinks dynamically
SINK_SPEAKERS=$(pactl list sinks | grep -B 4 -i "$SPEAKER_DESC" | grep 'Name:' | awk '{print $2}')
SINK_HEADPHONES=$(pactl list sinks | grep -B 4 -i "$HEADPHONE_DESC" | grep 'Name:' | awk '{print $2}')
# Get active ports dynamically
PORT_SPEAKERS="analog-output-lineout"
PORT_HEADPHONES="analog-output-headphones"
# Print debugging information
echo "SINK_SPEAKERS: $SINK_SPEAKERS"
echo "SINK_HEADPHONES: $SINK_HEADPHONES"
echo "PORT_SPEAKERS: $PORT_SPEAKERS"
echo "PORT_HEADPHONES: $PORT_HEADPHONES"
current_sink=$(pactl list short sinks | grep RUNNING | awk '{print $2}')
if [ "$current_sink" == "$SINK_SPEAKERS" ]; then
new_sink="$SINK_HEADPHONES"
pactl set-sink-port "$SINK_HEADPHONES" "$PORT_HEADPHONES"
else
new_sink="$SINK_SPEAKERS"
pactl set-sink-port "$SINK_SPEAKERS" "$PORT_SPEAKERS"
fi
# Set new default sink and move all audio streams to the new sink
pactl set-default-sink "$new_sink"
pactl list short sink-inputs | while read -r stream; do
stream_id=$(echo "$stream" | awk '{print $1}')
pactl move-sink-input "$stream_id" "$new_sink"
done
EOF
# Create the switch-audio-port file in /usr/local/bin
echo "$SWITCH_AUDIO_PORT_SCRIPT" | sudo tee /usr/local/bin/switch-audio-port > /dev/null
# Set the file permissions to make it executable
sudo chmod +x /usr/local/bin/switch-audio-port
echo "switch-audio-port script has been created and installed."