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