-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from smarkoco/main
bug fix when having multiple audio sinks to not confuse processes
- Loading branch information
Showing
4 changed files
with
45 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/bin/bash | ||
tmux new-session -d -s adfree | ||
tmux send-keys '~/Spotify-Ads-Muter-Linux/mute_spotify_timer.sh' C-m # C-m sends ENTER keystroke | ||
tmux send-keys "~/Spotify-Ads-Muter-Linux/mute_spotify_timer.sh" C-m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,53 @@ | ||
#!/bin/bash | ||
# Script to mute an application using PulseAudio, depending solely on | ||
# process name, constructed as answer on askubuntu.com:. | ||
# http://askubuntu.com/questions/180612/script-to-mute-an-application | ||
|
||
# It works as: mute_application.sh vlc mute OR mute_application.sh vlc unmute | ||
# Script to respawn or mute an application using PulseAudio | ||
|
||
# it works as mute_app.sh <app_name> <action>, such as mute_app.sh spotify mute | ||
if [ -z "$1" ]; then | ||
echo "Please provide me with an application name" | ||
echo "Please provide an application name." | ||
exit 1 | ||
fi | ||
|
||
if [ -z "$2" ]; then | ||
echo "Please provide me with an action mute/unmute after the application name" | ||
echo "Please provide an action: mute, unmute, or respawn." | ||
exit 1 | ||
fi | ||
|
||
if ! [[ "$2" == "mute" || "$2" == "unmute" ]]; then | ||
echo "The 2nd argument must be mute/unmute" | ||
if [[ "$2" != "mute" && "$2" != "unmute" && "$2" != "respawn" ]]; then | ||
echo "The action must be mute, unmute, or respawn." | ||
exit 1 | ||
fi | ||
|
||
process_id=$(pidof "$1") | ||
# Get the sink input index for the sink with matching application name, and use grep to find all matching indexes | ||
sink_input_indexes=$(pacmd list-sink-inputs | grep -B 20 "application.name = \"$1\"" | awk '/index:/{print $2}') | ||
# this second command catches Chromium commercials | ||
sink_input_indexes_alt=$(pacmd list-sink-inputs | grep -B 27 "application.process.binary = \"$1\"" | awk '/index:/{print $2}') | ||
# Now combine the sink input indexes from both commands to remove duplicates | ||
sink_input_indexes=$(echo -e "$sink_input_indexes\n$sink_input_indexes_alt" | sort -n | uniq) | ||
|
||
if [ $? -ne 0 ]; then | ||
echo "There is no such process as "$1"" | ||
if [ -z "$sink_input_indexes" ]; then | ||
echo "Could not find sink input index for $1." | ||
exit 1 | ||
fi | ||
|
||
temp=$(mktemp) | ||
|
||
pacmd list-sink-inputs > $temp | ||
|
||
inputs_found=0; | ||
current_index=-1; | ||
|
||
while read line; do | ||
if [ $inputs_found -eq 0 ]; then | ||
inputs=$(echo -ne "$line" | awk '{print $2}') | ||
if [[ "$inputs" == "to" ]]; then | ||
continue | ||
fi | ||
inputs_found=1 | ||
else | ||
if [[ "${line:0:6}" == "index:" ]]; then | ||
current_index="${line:7}" | ||
elif [[ "${line:0:25}" == "application.process.id = " ]]; then | ||
if [[ "${line:25}" == "\"$process_id\"" ]]; then | ||
# index found... | ||
break; | ||
fi | ||
fi | ||
# Perform the specified action on each sink input index | ||
for sink_input_index in $sink_input_indexes; do | ||
if [ "$2" == "mute" ]; then | ||
pacmd set-sink-input-mute "$sink_input_index" 1 > /dev/null 2>&1 | ||
echo "Muted $1 on sink input index $sink_input_index." | ||
elif [ "$2" == "unmute" ]; then | ||
pacmd set-sink-input-mute "$sink_input_index" 0 > /dev/null 2>&1 | ||
echo "Unmuted $1 on sink input index $sink_input_index." | ||
elif [ "$2" == "respawn" ]; then | ||
# Kill the application | ||
pkill -x "$1" | ||
# Wait for the application to close | ||
while pgrep -x "$1" > /dev/null; do sleep 1; done | ||
# Restart the application | ||
"$1" > /dev/null 2>&1 & | ||
echo "Respawned $1." | ||
# break out of the loop after respawning the application because this is only needed once | ||
break | ||
fi | ||
done < $temp | ||
|
||
rm -f $temp | ||
|
||
if [ $current_index -eq -1 ]; then | ||
echo "Could not find "$1" in the processes that output sound." | ||
exit 1 | ||
fi | ||
|
||
# muting... | ||
if [[ "$2" == "mute" ]]; then | ||
pacmd set-sink-input-mute "$current_index" 1 > /dev/null 2>&1 | ||
else | ||
pacmd set-sink-input-mute "$current_index" 0 > /dev/null 2>&1 | ||
fi | ||
done | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters