/
babble.sh
executable file
·67 lines (58 loc) · 1.21 KB
/
babble.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env bash
EXIT_AFTER_ONE=0
function usage {
echo "Usage: $(basename $0) [-e] GRID"
echo " -e exit after one iteration"
echo " GRID the grid file to play"
exit 1
}
while getopts "e" OPT; do
case "$OPT" in
"e") EXIT_AFTER_ONE=1 ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
grid="$1"
width=100
tempo=100
cursor=0
# 1 iteration / beat
while true; do
__t0=$(gdate "+%s.%N")
__control=true
while IFS='' read -r __line; do
# ignore comments
if [ "${__line:0:1}" = "#" ]; then
continue
fi
if $__control; then
if [ -z "$__line" ]; then
__control=false
echo "$grid: cursor = $cursor / tempo = $tempo / width = $width"
continue
else
eval $__line
fi
else
__c="${__line:$cursor:1}"
case "$__c" in
".")
continue
;;
*)
cmd="${__line:$width} \$$__c"
eval echo "$cmd"
eval $cmd &
;;
esac
fi
done < "$grid"
echo
cursor=$(expr \( $cursor + 1 \) % $width)
if [ $EXIT_AFTER_ONE -eq 1 ] && [ $cursor -eq 0 ] ; then
exit
fi
__t1=$(gdate "+%s.%N")
sleep $(echo "( 60 / $tempo ) - ( $__t1 - $__t0 )" | bc -l)
done