summaryrefslogtreecommitdiff
path: root/audio-autoAdjust.sh
blob: 12a66f75db4a39daac756332387f49f2b4883586 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/bash

renice -n 19 $$
ionice -c 3 -p $$

# sys-def vars
# DO NOT TOUCH
VOL=0
STATE=1

# usr-def vars
OTHERPLAYER_MUSIC_PERCENTAGE=55
NORMAL_MUSIC_PLAYER_PERCENTAGE=80

function lower_music() {
    for ((i=$NORMAL_MUSIC_PLAYER_PERCENTAGE; i>=$OTHERPLAYER_MUSIC_PERCENTAGE; i--)); do
        playerctl -p spotify volume 0.$i
        sleep 0.01
    done
    playerctl -p spotify volume 0.$OTHERPLAYER_MUSIC_PERCENTAGE
}

function normal_music() {
    for ((i=$OTHERPLAYER_MUSIC_PERCENTAGE; i<=$NORMAL_MUSIC_PLAYER_PERCENTAGE; i++)); do
        playerctl -p spotify volume 0.$i
        sleep 0.01
    done
    playerctl -p spotify volume 0.$NORMAL_MUSIC_PLAYER_PERCENTAGE
}

if [ "$1" ]; then
    if [[ "$1" == '--help' || "$1" == '-h' ]]; then
        /sbin/echo -e "Usage: $0 [OPTION]\n\nOptions:\n\t-l|--lower\tTo lower music volume\n\t-n|--normal\tTo set music volume to normal"
        exit 0
    elif [[ "$1" == '--daemon' || "$1" == '-d' ]]; then
        while true; do
            if [[ "$(systemctl is-system-running)" == "running" || "$(systemctl is-system-running)" == "degraded" ]]; then
                if [[ $(playerctl status -p firefox) == "Playing" ]]; then
                    if [ $STATE -eq 0 ]; then
                        STATE=1
                        echo VideoDetected
                        lower_music
                    fi
                elif [[ $(playerctl status -p spotify) == "Playing" ]]; then
                    if [ $STATE -eq 1 ]; then
                        STATE=0
                        echo VideoGone
                        normal_music
                    fi
                else
                    sleep 5
                fi
                sleep 1
            fi
        done
        exit 0
    elif [[ "$1" == '--lower' || "$1" == '-l' ]]; then
        lower_music
        exit 0
    elif [[ "$1" == '--normal' || "$1" == '-n' ]]; then
        normal_music
        exit 0
    fi
fi

/sbin/echo -e "Un-recornized option: '$@', Please use options:\n\t-l|--lower\tTo lower music volume\n\t-n|--normal\tTo set music volume to normal"
exit 1