]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/nokia2tuba.sh
Fix macOS SDL2 framework permissions
[xonotic/xonotic.git] / misc / tools / nokia2tuba.sh
1 #!/bin/sh
2
3 bpm=120
4 transpose=0
5 defaultoctave=1
6 defaultlength=4
7 baseoctave=1
8 gato=-0.1
9
10 tuba_note_42="moveleft back crouch fire"
11 tuba_note_43="back crouch fire"
12 tuba_note_44="moveright back crouch fire"
13 tuba_note_47="forward moveright crouch fire"
14 tuba_note_48="crouch fire"
15 tuba_note_49="moveleft back crouch fire2"
16 tuba_note_50="moveright crouch fire"
17 tuba_note_51="forward moveleft crouch fire"
18 tuba_note_52="forward crouch fire"
19 tuba_note_53="moveleft crouch fire"
20 tuba_note_54="moveleft back fire"
21 tuba_note_55="back fire"
22 tuba_note_56="back moveright fire"
23 tuba_note_57="moveright crouch fire2"
24 tuba_note_58="forward moveleft crouch fire2"
25 tuba_note_59="forward moveright fire"
26 tuba_note_60="fire"
27 tuba_note_61="moveleft back fire2"
28 tuba_note_62="moveright fire"
29 tuba_note_63="forward moveleft fire"
30 tuba_note_64="forward fire"
31 tuba_note_65="moveleft fire"
32 tuba_note_66="forward moveright fire2"
33 tuba_note_67="fire2"
34 tuba_note_68="back moveright jump fire"
35 tuba_note_69="moveright fire2"
36 tuba_note_70="forward moveleft fire2"
37 tuba_note_71="forward fire2"
38 tuba_note_72="moveleft fire2"
39 tuba_note_73="moveleft back jump fire2"
40 tuba_note_74="moveright jump fire"
41 tuba_note_75="forward moveleft jump fire"
42 tuba_note_76="forward jump fire"
43 tuba_note_77="moveleft jump fire"
44 tuba_note_78="forward moveright jump fire2"
45 tuba_note_79="jump fire2"
46 tuba_note_81="moveright jump fire2"
47 tuba_note_82="forward moveleft jump fire2"
48 tuba_note_83="forward jump fire2"
49 tuba_note_84="moveleft jump fire2"
50
51 tuba() {
52         plusminus=$1
53         eval "tuba_note=\$tuba_note_$pitch"
54         if [ -z "$tuba_note" ]; then
55                 echo >&2 "Cannot play note $pitch"
56         fi
57         semi=
58         for n in $tuba_note; do
59                 echo -n "${semi}${plusminus}${n}"
60                 semi=';'
61         done
62 }
63
64 time=0
65 playnote() {
66         # Move the dot where it belongs.
67         case "$octave" in
68                 *.)
69                         octave=${octave%.}
70                         length=$length.
71                         ;;
72         esac
73         # Normalize the note.
74         pitch=''
75         case "$note" in
76                 [Cc]_|_[Cc]) pitch=-1 ;;
77                 [Cc]) pitch=0 ;;
78                 [Cc][#+]|[#+][Cc]|[Dd]_|_[Dd]) pitch=1 ;;
79                 [Dd]) pitch=2 ;;
80                 [Dd][#+]|[#+][Dd]|[Ee]_|_[Ee]) pitch=3 ;;
81                 [Ee]|[Ff]_|_[Ff]) pitch=4 ;;
82                 [Ff]|[Ee][#+]|[#+][Ee]) pitch=5 ;;
83                 [Ff][#+]|[#+][Ff]|[Gg]_|_[Gg]) pitch=6 ;;
84                 [Gg]) pitch=7 ;;
85                 [Gg][#+]|[#+][Gg]|[Aa]_|_[Aa]) pitch=8 ;;
86                 [Aa]) pitch=9 ;;
87                 [Aa][#+]|[#+][Aa]|[Bb]_|_[Bb]) pitch=10 ;;
88                 [Bb]) pitch=11 ;;
89                 [Bb][#+]|[#+][Bb]) pitch=12 ;;
90                 [p-]) pitch='' ;;
91                 *) echo >&2 "Unrecognized note: $note" ;;
92         esac
93         echo "// $length$note$octave"
94         # Calculate the duration.
95         case "$length" in
96                 .)
97                         length=$defaultlength
98                         f=1.5
99                         ;;
100                 '')
101                         length=$defaultlength
102                         f=1
103                         ;;
104                 *.)
105                         f=1.5
106                         ;;
107                 *)
108                         f=1
109                         ;;
110         esac
111         duration=$(echo "240 / $bpm / ${length%.} * $f" | bc -l)
112         if [ -n "$pitch" ]; then
113         # Calculate the MIDI pitch.
114                 if [ -z "$octave" ]; then
115                         octave=$defaultoctave
116                 fi
117                 pitch=$((pitch + (octave - baseoctave) * 12 + transpose + 60))
118                 case "$gato" in
119                         -*)
120                                 noteoff=$(echo "$time + $duration + $gato" | bc -l)
121                                 ;;
122                         *)
123                                 noteoff=$(echo "$time + $gato" | bc -l)
124                                 ;;
125                 esac
126                 echo "defer $time \"$(tuba +)\""
127                 echo "defer $noteoff \"$(tuba -)\""
128         fi
129         time=$(echo "$time + $duration" | bc -l)
130 }
131
132 notes=$*
133 case "$notes" in
134         *:*)
135                 notes=${notes#*:}
136                 baseoctave=5
137                 ;;
138 esac
139 while [ -n "$notes" ]; do
140         note=${notes%%[:, ]*}
141         notes=${notes#$note}
142         notes=${notes#?}
143         case "$note" in
144                 ml)
145                         gato=-0.04
146                         ;;
147                 ms)
148                         gato=0.04
149                         ;;
150                 mn)
151                         gato=-0.1
152                         ;;
153                 b=*)
154                         bpm=${note#*=}
155                         ;;
156                 d=*)
157                         defaultlength=${note#*=}
158                         ;;
159                 t=*)
160                         transpose=${note#*=}
161                         ;;
162                 o=*)
163                         defaultoctave=${note#*=}
164                         ;;
165                 O=*)
166                         baseoctave=${note#*=}
167                         ;;
168                 *)
169                         octave=${note##*[cCdDeEfFgGaAbBp-]}
170                         octave=${octave##[#+_]}
171                         note=${note%$octave}
172                         length=${note%[cCdDeEfFgGaAbBp-]*}
173                         length=${length%%[#+_]}
174                         note=${note#$length}
175                         playnote
176                         ;;
177         esac
178 done