Use const in ss_output.c where possible.
[ahxm.git] / midi_song.c
blob9411f33de943147d1859bd8acd670e68c1e10369
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2007 Angel Ortega <angel@triptico.com>
6 midi_song.c - MIDI song event stream management
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
38 #include "ahxm.h"
40 /*******************
41 Data
42 ********************/
44 /* the MIDI song stream */
46 static struct song_ev *midi_song = NULL;
47 static int n_midi_ev = 0;
49 /* the MIDI tracks: just a track/channel table */
51 #define MIDI_TRACK_NUM 256
52 static int track_channel[MIDI_TRACK_NUM];
54 /* MIDI message types */
56 #define MIDI_MSG_NOTE_ON 0x90
57 #define MIDI_MSG_NOTE_OFF 0x80
58 #define MIDI_MSG_CONTROLLER 0xB0
59 #define MIDI_MSG_PROGRAM 0xC0
61 /* MIDI device fd */
62 int midi_fd = -1;
65 /*******************
66 Code
67 ********************/
69 static struct song_ev * add_midi_ev(struct song_ev *e)
70 /* adds a MIDI song event */
72 if (e->channel < 0)
73 return NULL;
75 return copy_event(&midi_song, &n_midi_ev, e);
79 static int msecs_type_cmp(const void *v1, const void *v2)
80 /* MIDI song event compare function for qsort(), by time (for playing) */
82 struct song_ev *e1;
83 struct song_ev *e2;
85 e1 = (struct song_ev *) v1;
86 e2 = (struct song_ev *) v2;
88 if (e1->msecs == e2->msecs)
89 return e1->type - e2->type;
91 return e1->msecs - e2->msecs;
95 static void midi_song_convert_events(void)
96 /* converts generic song_ev events to MIDI events */
98 struct song_ev *e;
99 int msecs, msecs_ac, f_msecs;
100 double time_ac, time_ac_m;
101 int num, den;
102 double mspw;
103 int n;
105 /* resets the MIDI stream */
106 if (midi_song != NULL) {
107 free(midi_song);
108 midi_song = NULL;
111 n_midi_ev = 0;
113 /* sorts the song */
114 song_sort();
116 mspw = 0;
117 msecs = msecs_ac = f_msecs = 0;
118 time_ac = time_ac_m = 0;
119 num = den = 4;
121 /* by default, all channels are 'mute' until
122 a channel is explicitly set */
123 for (n = 0; n < MIDI_TRACK_NUM; n++)
124 track_channel[n] = -1;
126 /* travels the song events generating MIDI song events */
127 for (n = 0; n < n_song_ev; n++) {
128 /* gets the song event */
129 e = &song[n];
131 /* calculates the msecs */
132 msecs = ((e->time - time_ac) * mspw) + msecs_ac;
133 e->msecs = msecs;
135 /* if it's not a generic message, set MIDI channel */
136 if (e->trk_id >= 0)
137 e->channel = track_channel[e->trk_id];
139 switch (e->type) {
140 case SONG_EV_TEMPO:
142 /* updates accumulations */
143 msecs_ac = msecs;
144 time_ac = e->time;
146 /* calculates milliseconds-per-whole based on new tempo */
147 mspw = 1000.0 * 60.0;
148 mspw /= (e->amount / 4.0);
150 break;
152 case SONG_EV_METER:
154 /* just store the values */
155 num = e->min;
156 den = e->max;
157 time_ac_m = e->time;
159 break;
161 case SONG_EV_MEASURE:
163 song_test_measure_boundary(e->time - time_ac_m,
164 num, den, e->value);
165 break;
167 case SONG_EV_MIDI_CHANNEL:
169 /* stores the channel for this track */
170 track_channel[e->trk_id] = e->channel;
171 break;
173 case SONG_EV_NOTE:
175 /* convert to note on / off pairs */
177 e->vel = (int) (e->vol * 127.0);
179 add_midi_ev(e);
181 e = add_midi_ev(e);
182 e->type = SONG_EV_NOTE_OFF;
184 msecs += (int) (e->len * mspw);
185 e->msecs = msecs;
187 break;
189 case SONG_EV_BACK:
191 /* move the cursor back */
192 msecs_ac -= (int) (e->len * mspw);
193 break;
195 case SONG_EV_MIDI_PROGRAM:
197 add_midi_ev(e);
198 break;
200 case SONG_EV_SS_PITCH_STRETCH:
201 case SONG_EV_SS_PRINT_WAVE_TEMPO:
202 case SONG_EV_SS_WAV:
203 case SONG_EV_SS_PAT:
204 case SONG_EV_SS_SUSTAIN:
205 case SONG_EV_SS_ATTACK:
206 case SONG_EV_SS_VIBRATO:
207 case SONG_EV_SS_PORTAMENTO:
208 case SONG_EV_SS_CHANNEL:
209 case SONG_EV_SS_EFF_DELAY:
210 case SONG_EV_SS_EFF_ECHO:
211 case SONG_EV_SS_EFF_COMB:
212 case SONG_EV_SS_EFF_ALLPASS:
213 case SONG_EV_SS_EFF_FLANGER:
214 case SONG_EV_SS_EFF_WOBBLE:
215 case SONG_EV_SS_EFF_SQWOBBLE:
216 case SONG_EV_SS_EFF_HFWOBBLE:
217 case SONG_EV_SS_EFF_FADER:
218 case SONG_EV_SS_EFF_REVERB:
219 case SONG_EV_SS_EFF_FOLDBACK:
220 case SONG_EV_SS_EFF_ATAN:
221 case SONG_EV_SS_EFF_DISTORT:
222 case SONG_EV_SS_EFF_OVERDRIVE:
223 case SONG_EV_SS_EFF_OFF:
224 case SONG_EV_NOP:
226 /* ignored */
227 break;
229 case SONG_EV_SONG_INFO:
231 /* song info should be used */
232 break;
234 case SONG_EV_EOT:
236 /* end of track; trigger possible cleaning */
237 break;
239 case SONG_EV_NOTE_OFF:
240 case SONG_EV_END:
242 /* never found in generic song streams */
243 break;
246 /* store the further time seen */
247 if (f_msecs < msecs)
248 f_msecs = msecs;
251 /* generates an end of event mark, a time after the last one */
252 e = add_event(&midi_song, &n_midi_ev);
254 e->type = SONG_EV_END;
255 e->msecs = f_msecs + 1000;
259 int midi_song_play(int skip_secs)
261 struct song_ev *e;
262 int msecs, msecs_p, msecs_d;
263 int go;
264 struct timespec ts;
265 unsigned char midimsg[1024];
266 int mi;
267 int skip_msecs;
269 /* convert the song to MIDI events */
270 midi_song_convert_events();
272 /* sort by time */
273 qsort(midi_song, n_midi_ev, sizeof(struct song_ev), msecs_type_cmp);
275 msecs = msecs_p = 0;
276 go = 1;
277 e = midi_song;
279 /* calculate the millisecond to start playing */
280 skip_msecs = skip_secs * 1000;
282 /* loop the events */
283 while (go) {
284 /* clear buffer */
285 mi = 0;
287 if (verbose >= 1 && msecs % 1000 == 0) {
288 int m = msecs / 1000;
289 printf("[%02d:%02d]\r", m / 60, m % 60);
290 fflush(stdout);
293 /* process all events for this exact time */
294 while (e->msecs == msecs) {
295 switch (e->type) {
296 case SONG_EV_NOTE:
298 midimsg[mi++] = MIDI_MSG_NOTE_ON | e->channel;
299 midimsg[mi++] = e->value;
300 midimsg[mi++] = e->vel;
302 break;
304 case SONG_EV_NOTE_OFF:
306 midimsg[mi++] = MIDI_MSG_NOTE_OFF | e->channel;
307 midimsg[mi++] = e->value;
308 midimsg[mi++] = 0;
310 break;
312 case SONG_EV_MIDI_PROGRAM:
314 midimsg[mi++] = MIDI_MSG_PROGRAM | e->channel;
315 midimsg[mi++] = e->value;
317 break;
319 case SONG_EV_END:
321 go = 0;
322 break;
324 default:
325 /* ignore the rest */
326 break;
329 /* next event */
330 e++;
333 if (!go)
334 break;
336 /* get time of next event */
337 msecs_p = msecs;
338 msecs = e->msecs;
339 msecs_d = msecs - msecs_p;
341 if (msecs >= skip_msecs) {
342 /* if there are pending messages, write them */
343 if (mi)
344 write(midi_fd, midimsg, mi);
346 /* calculate the time to sleep */
347 ts.tv_sec = (time_t) msecs_d / 1000;
348 ts.tv_nsec = (long) ((msecs_d * 1000000) % 1000000000);
350 nanosleep(&ts, NULL);
354 if (verbose >= 1)
355 printf("\n");
357 return 0;
361 int midi_device_open(char *devfile)
363 if (devfile == NULL)
364 devfile = "/dev/midi";
366 return (midi_fd = open(devfile, O_WRONLY));
370 void midi_device_close(void)
372 close(midi_fd);