forked from sm-Fifteen/systemd-alarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarmclock
executable file
·50 lines (43 loc) · 1.63 KB
/
alarmclock
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
#!/bin/bash
soundClipPath=${1:?"\$soundClipPath not set, check start-alarmclock."}
sink=${2:?"\$sink not set, check start-alarmclock."}
initalVolume=${3:?"\$initialVolume not set, check start-alarmclock."}
pipe=/tmp/pactl-output$$
volumeStepIncrease=${4:-"+0%"}
volumeStepDuration=${5:-"0s"}
volumeSteps=${6:-"0"}
#Subscribe a custom pipe to pactl
mkfifo "$pipe"
pactl subscribe > "$pipe"&
pactl set-sink-mute "$sink" 0
pactl set-sink-volume "$sink" "$initalVolume"
echo "Volume at $initalVolume"
ffplay "$soundClipPath" -loop 0 -nodisp -loglevel quiet&
ffplayPID=$!
#Wait for ffplay to register (2 events for the creation and the deletion of a
#first ffmpeg stream plus another one, the real one, which closes the grep)
cat "$pipe" | grep -m2 sink-input
#Switch all sources to our sink once it's done
#Can't use the id from the pipe, as ffplay somehow changes stream right at the start
echo "FFplay has registered."
pactl list short sink-inputs | while read sinkInput; do
sourceId=`echo "$sinkInput" | cut -d $'\t' -f1`
echo "Moving #$sourceId to sink #$sink"
pactl move-sink-input "$sourceId" "$sink"
done
#The pipe can now be deleted
if [ -p "$pipe" ];then
rm "$pipe"
fi
#Increase the volume over time
if [ $volumeSteps != "0" ] && [ $volumeStepIncrease != "+0%" ] && [ $volumeStepDuration != "0s" ]; then
#Fuck me, bash does bracket expansion BEFORE variable expansion?
for i in $(eval echo {0..$volumeSteps}); do
sleep $volumeStepDuration
pactl set-sink-volume "$sink" $volumeStepIncrease
echo "Volume up by $volumeStepIncrease"
done && echo "Volume at maximum"&
fi
wait $ffplayPID
#Stop fiddling with the volume if/when ffplay dies
kill $(jobs -p)