The @ command (track) in the arpeggiator has been changed to /, as it
[ahxm.git] / midi_song.c
blob3e071059d1a09c95a8ea30afa080acf361b5957e
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2006 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 "annhell.h"
40 /*******************
41 Data
42 ********************/
44 struct midi_ev_generic
46 song_ev_type type; /* event type */
47 int msecs; /* time in milliseconds */
48 int trk_id; /* track id */
49 int channel; /* MIDI channel */
52 struct midi_ev_program
54 song_ev_type type; /* SONG_EV_MIDI_PROGRAM */
55 int msecs;
56 int trk_id;
57 int channel;
58 int program; /* MIDI program number */
61 struct midi_ev_note_off
63 song_ev_type type; /* SONG_EV_NOTE_OFF */
64 int msecs;
65 int trk_id;
66 int channel;
67 int note; /* MIDI note */
70 struct midi_ev_note_on
72 song_ev_type type; /* SONG_EV_NOTE_ON */
73 int msecs;
74 int trk_id;
75 int channel;
76 int note; /* MIDI note */
77 int vel; /* velocity (volume) */
80 union midi_ev
82 struct midi_ev_generic generic;
83 struct midi_ev_program midi_program;
84 struct midi_ev_note_on note_on;
85 struct midi_ev_note_off note_off;
89 /* the MIDI song stream */
91 static union midi_ev * midi_song = NULL;
92 static int n_midi_ev = 0;
94 /* the MIDI tracks: just a track/channel table */
96 #define MIDI_TRACK_NUM 256
97 static int track_channel[MIDI_TRACK_NUM];
99 /* MIDI message types */
101 #define MIDI_MSG_NOTE_ON 0x90
102 #define MIDI_MSG_NOTE_OFF 0x80
103 #define MIDI_MSG_CONTROLLER 0xB0
104 #define MIDI_MSG_PROGRAM 0xC0
106 /* MIDI device fd */
107 int midi_fd = -1;
110 /*******************
111 Code
112 ********************/
114 static void add_midi_ev(union midi_ev * e)
115 /* adds a MIDI song event */
117 if(e->generic.channel < 0)
118 return;
120 GROW(midi_song, n_midi_ev, union midi_ev);
122 /* store */
123 memcpy(&midi_song[n_midi_ev], e, sizeof(union midi_ev));
125 n_midi_ev++;
129 static int midi_ev_cmp_by_time(const void * v1, const void * v2)
130 /* MIDI song event compare function for qsort(), by time (for playing) */
132 struct midi_ev_generic * e1;
133 struct midi_ev_generic * e2;
135 e1 = (struct midi_ev_generic *)v1; e2=(struct midi_ev_generic *)v2;
137 if(e1->msecs == e2->msecs)
138 return(e1->type - e2->type);
140 return(e1->msecs - e2->msecs);
144 static int midi_song_convert_events(void)
145 /* converts generic song_ev events to MIDI events */
147 union song_ev * e;
148 union midi_ev me;
149 int msecs, msecs_ac, f_msecs;
150 double time_ac, time_ac_m;
151 int num, den;
152 double mspw;
153 int n;
154 int b_track = -1;
156 /* resets the MIDI stream */
157 if(midi_song != NULL)
159 free(midi_song);
160 midi_song = NULL;
163 n_midi_ev = 0;
165 /* sorts the song */
166 song_sort();
168 mspw = 0;
169 msecs = msecs_ac = f_msecs = 0;
170 time_ac = time_ac_m = 0;
171 num = den = 4;
173 /* by default, all channels are 'mute' until
174 a channel is explicitly set */
175 for(n = 0;n < MIDI_TRACK_NUM;n++)
176 track_channel[n] = -1;
178 /* travels the song events generating MIDI song events */
179 for(n = 0;n < n_song_ev;n++)
181 /* gets the song event */
182 e = &song[n];
184 /* calculates the msecs */
185 msecs = ((e->generic.time - time_ac) * mspw) + msecs_ac;
187 /* generic event data */
188 me.generic.type = e->generic.type;
189 me.generic.msecs = msecs;
190 me.generic.trk_id = e->generic.trk_id;
192 /* if it's not a generic message, set MIDI channel */
193 if(e->generic.trk_id >= 0)
194 me.generic.channel = track_channel[e->generic.trk_id];
196 /* account the biggest track number seen */
197 if(b_track < e->generic.trk_id) b_track = e->generic.trk_id;
199 switch(e->generic.type)
201 case SONG_EV_TEMPO:
203 /* updates accumulations */
204 msecs_ac = msecs;
205 time_ac = e->generic.time;
207 /* calculates milliseconds-per-whole based on new tempo */
208 mspw = 1000.0 * 60.0;
209 mspw /= (e->tempo.tempo / 4.0);
211 break;
213 case SONG_EV_METER:
215 /* just store the values */
216 num = e->meter.num;
217 den = e->meter.den;
218 time_ac_m = e->meter.time;
220 break;
222 case SONG_EV_MEASURE:
224 song_test_measure_boundary(e->measure.time - time_ac_m,
225 num, den, e->measure.line);
226 break;
228 case SONG_EV_MIDI_CHANNEL:
230 /* stores the channel for this track */
231 track_channel[e->midi_channel.trk_id] =
232 e->midi_channel.channel;
233 break;
235 case SONG_EV_NOTE:
237 /* convert to note on / off pairs */
239 me.note_on.type = SONG_EV_NOTE_ON;
240 me.note_on.note = e->note.note;
241 me.note_on.vel = (int)(e->note.vol * 127.0);
243 add_midi_ev(&me);
245 msecs += (int)(e->note.len * mspw);
247 me.note_off.type = SONG_EV_NOTE_OFF;
248 me.note_off.msecs = msecs;
250 add_midi_ev(&me);
251 break;
253 case SONG_EV_BACK:
255 /* move the cursor back */
256 msecs_ac -= (int)(e->back.len * mspw);
257 break;
259 case SONG_EV_MIDI_PROGRAM:
261 /* set MIDI program (instrument) */
262 me.midi_program.program = e->midi_program.program;
264 add_midi_ev(&me);
265 break;
267 case SONG_EV_SS_PITCH_STRETCH:
268 case SONG_EV_SS_PRINT_WAVE_TEMPO:
269 case SONG_EV_SS_WAV:
270 case SONG_EV_SS_PAT:
271 case SONG_EV_SS_SUSTAIN:
272 case SONG_EV_SS_VIBRATO:
273 case SONG_EV_SS_CHANNEL:
274 case SONG_EV_SS_EFF_DELAY:
275 case SONG_EV_SS_EFF_ECHO:
276 case SONG_EV_SS_EFF_COMB:
277 case SONG_EV_SS_EFF_ALLPASS:
278 case SONG_EV_SS_EFF_FLANGER:
279 case SONG_EV_SS_EFF_WOBBLE:
280 case SONG_EV_SS_EFF_SQWOBBLE:
281 case SONG_EV_SS_EFF_FADER:
282 case SONG_EV_SS_EFF_REVERB:
283 case SONG_EV_SS_EFF_OFF:
285 /* softsynth events are ignored */
286 break;
288 case SONG_EV_SONG_INFO:
290 /* song info should be used */
291 break;
293 case SONG_EV_NOTE_ON:
294 case SONG_EV_NOTE_OFF:
295 case SONG_EV_END:
297 /* never found in generic song streams */
298 break;
301 /* store the further time seen */
302 if(f_msecs < msecs) f_msecs = msecs;
305 /* generates an end of event mark, a time after the last one */
306 me.generic.type = SONG_EV_END;
307 me.generic.msecs = f_msecs + 1000;
308 add_midi_ev(&me);
310 /* return the number of tracks */
311 return(b_track + 1);
315 int midi_song_play(int skip_secs)
317 union midi_ev * e;
318 int msecs, msecs_p, msecs_d;
319 int go;
320 int n_tracks;
321 struct timespec ts;
322 unsigned char midimsg[1024];
323 int mi;
324 int skip_msecs;
326 /* convert the song to MIDI events */
327 n_tracks = midi_song_convert_events();
329 /* sort by time */
330 qsort(midi_song, n_midi_ev, sizeof(union midi_ev), midi_ev_cmp_by_time);
332 msecs = msecs_p = 0;
333 go = 1;
334 e = midi_song;
336 /* calculate the millisecond to start playing */
337 skip_msecs = skip_secs * 1000;
339 /* loop the events */
340 while(go)
342 /* clear buffer */
343 mi = 0;
345 if(verbose >= 1)
347 if(msecs % 1000 == 0)
349 int m = msecs / 1000;
350 printf("[%02d:%02d]\r", m / 60, m % 60);
351 fflush(stdout);
355 /* process all events for this exact time */
356 while(e->generic.msecs == msecs)
358 switch(e->generic.type)
360 case SONG_EV_NOTE_ON:
362 midimsg[mi++] = MIDI_MSG_NOTE_ON|e->note_on.channel;
363 midimsg[mi++] = e->note_on.note;
364 midimsg[mi++] = e->note_on.vel;
366 break;
368 case SONG_EV_NOTE_OFF:
370 midimsg[mi++] = MIDI_MSG_NOTE_OFF|e->note_off.channel;
371 midimsg[mi++] = e->note_off.note;
372 midimsg[mi++] = 0;
374 break;
376 case SONG_EV_MIDI_PROGRAM:
378 midimsg[mi++] = MIDI_MSG_PROGRAM|e->midi_program.channel;
379 midimsg[mi++] = e->midi_program.program;
381 break;
383 case SONG_EV_END:
385 go = 0;
386 break;
388 default:
389 /* ignore the rest */
390 break;
393 /* next event */
394 e++;
397 if(!go)
398 break;
400 /* get time of next event */
401 msecs_p = msecs;
402 msecs = e->generic.msecs;
403 msecs_d = msecs - msecs_p;
405 if(msecs >= skip_msecs)
407 /* if there are pending messages, write them */
408 if(mi)
409 write(midi_fd, midimsg, mi);
411 /* calculate the time to sleep */
412 ts.tv_sec = (time_t) msecs_d / 1000;
413 ts.tv_nsec = (long) ((msecs_d * 1000000) % 1000000000);
415 nanosleep(&ts, NULL);
419 if(verbose >= 1) printf("\n");
421 return(0);
425 int midi_device_open(char * devfile)
427 if(devfile == NULL)
428 devfile = "/dev/midi";
430 return((midi_fd = open(devfile, O_WRONLY)));
434 void midi_device_close(void)
436 close(midi_fd);