An event_id has been added to all events, to ensure an event with
[ahxm.git] / midi_song.c
blob9bc3bc46de3866ff9924e1645a0fb2d91f7623a4
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 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 /* reallocs */
121 midi_song=(union midi_ev *)realloc(midi_song,
122 (n_midi_ev + 1) * sizeof(union midi_ev));
124 /* store */
125 memcpy(&midi_song[n_midi_ev], e, sizeof(union midi_ev));
127 n_midi_ev++;
131 static int midi_ev_cmp_by_time(const void * v1, const void * v2)
132 /* MIDI song event compare function for qsort(), by time (for playing) */
134 struct midi_ev_generic * e1;
135 struct midi_ev_generic * e2;
137 e1=(struct midi_ev_generic *)v1; e2=(struct midi_ev_generic *)v2;
139 if(e1->msecs == e2->msecs)
140 return(e1->type - e2->type);
142 return(e1->msecs - e2->msecs);
146 static int midi_song_convert_events(void)
147 /* converts generic song_ev events to MIDI events */
149 union song_ev * e;
150 union midi_ev me;
151 int msecs, msecs_ac, f_msecs;
152 double time_ac, time_ac_m;
153 int num, den;
154 double mspw;
155 int n;
156 int b_track=-1;
158 /* resets the MIDI stream */
159 if(midi_song != NULL)
161 free(midi_song);
162 midi_song=NULL;
165 n_midi_ev=0;
167 /* sorts the song */
168 song_sort();
170 mspw=0;
171 msecs=msecs_ac=f_msecs=0;
172 time_ac=time_ac_m=0;
173 num=den=4;
175 /* by default, all channels are 'mute' until
176 a channel is explicitly set */
177 for(n=0;n < MIDI_TRACK_NUM;n++)
178 track_channel[n]=-1;
180 /* travels the song events generating MIDI song events */
181 for(n=0;n < n_song_ev;n++)
183 /* gets the song event */
184 e=&song[n];
186 /* calculates the msecs */
187 msecs=((e->generic.time - time_ac) * mspw) + msecs_ac;
189 /* generic event data */
190 me.generic.type=e->generic.type;
191 me.generic.msecs=msecs;
192 me.generic.trk_id=e->generic.trk_id;
194 /* if it's not a generic message, set MIDI channel */
195 if(e->generic.trk_id >= 0)
196 me.generic.channel=track_channel[e->generic.trk_id];
198 /* account the biggest track number seen */
199 if(b_track < e->generic.trk_id) b_track=e->generic.trk_id;
201 switch(e->generic.type)
203 case SONG_EV_TEMPO:
205 /* updates accumulations */
206 msecs_ac += msecs;
207 time_ac += e->generic.time;
209 /* calculates milliseconds-per-whole based on new tempo */
210 mspw = 1000.0 * 60.0;
211 mspw /= (e->tempo.tempo / 4.0);
213 break;
215 case SONG_EV_METER:
217 /* just store the values */
218 num=e->meter.num;
219 den=e->meter.den;
220 time_ac_m=e->meter.time;
222 break;
224 case SONG_EV_MEASURE:
226 song_test_measure_boundary(e->measure.time - time_ac_m,
227 num, den, e->measure.line);
228 break;
230 case SONG_EV_MIDI_CHANNEL:
232 /* stores the channel for this track */
233 track_channel[e->midi_channel.trk_id]=
234 e->midi_channel.channel;
235 break;
237 case SONG_EV_NOTE:
239 /* convert to note on / off pairs */
241 me.note_on.type=SONG_EV_NOTE_ON;
242 me.note_on.note=e->note.note;
243 me.note_on.vel=(int)(e->note.vol * 127.0);
245 add_midi_ev(&me);
247 msecs += (int)(e->note.len * mspw);
249 me.note_off.type=SONG_EV_NOTE_OFF;
250 me.note_off.msecs=msecs;
252 add_midi_ev(&me);
253 break;
255 case SONG_EV_BACK:
257 /* move the cursor back */
258 msecs_ac -= (int)(e->back.len * mspw);
259 break;
261 case SONG_EV_MIDI_PROGRAM:
263 /* set MIDI program (instrument) */
264 me.midi_program.program=e->midi_program.program;
266 add_midi_ev(&me);
267 break;
269 case SONG_EV_SS_PITCH_STRETCH:
270 case SONG_EV_SS_PRINT_WAVE_TEMPO:
271 case SONG_EV_SS_WAV:
272 case SONG_EV_SS_PAT:
273 case SONG_EV_SS_SUSTAIN:
274 case SONG_EV_SS_VIBRATO:
275 case SONG_EV_SS_CHANNEL:
276 case SONG_EV_SS_EFF_DELAY:
277 case SONG_EV_SS_EFF_ECHO:
278 case SONG_EV_SS_EFF_COMB:
279 case SONG_EV_SS_EFF_ALLPASS:
280 case SONG_EV_SS_EFF_FLANGER:
281 case SONG_EV_SS_EFF_WOBBLE:
282 case SONG_EV_SS_EFF_SQWOBBLE:
283 case SONG_EV_SS_EFF_FADER:
284 case SONG_EV_SS_EFF_REVERB:
285 case SONG_EV_SS_EFF_OFF:
287 /* softsynth events are ignored */
288 break;
290 case SONG_EV_NOTE_ON:
291 case SONG_EV_NOTE_OFF:
292 case SONG_EV_END:
294 /* never found in generic song streams */
295 break;
298 /* store the further time seen */
299 if(f_msecs < msecs) f_msecs=msecs;
302 /* generates an end of event mark, a time after the last one */
303 me.generic.type=SONG_EV_END;
304 me.generic.msecs=f_msecs + 1000;
305 add_midi_ev(&me);
307 /* return the number of tracks */
308 return(b_track + 1);
312 int midi_song_play(int skip_secs)
314 union midi_ev * e;
315 int msecs, msecs_p, msecs_d;
316 int go;
317 int n_tracks;
318 struct timespec ts;
319 unsigned char midimsg[1024];
320 int mi;
321 int skip_msecs;
323 /* convert the song to MIDI events */
324 n_tracks=midi_song_convert_events();
326 /* sort by time */
327 qsort(midi_song, n_midi_ev, sizeof(union midi_ev), midi_ev_cmp_by_time);
329 msecs=msecs_p=0;
330 go=1;
331 e=midi_song;
333 /* calculate the millisecond to start playing */
334 skip_msecs=skip_secs * 1000;
336 /* loop the events */
337 while(go)
339 /* clear buffer */
340 mi=0;
342 /* process all events for this exact time */
343 while(e->generic.msecs == msecs)
345 switch(e->generic.type)
347 case SONG_EV_NOTE_ON:
349 midimsg[mi++]=MIDI_MSG_NOTE_ON|e->note_on.channel;
350 midimsg[mi++]=e->note_on.note;
351 midimsg[mi++]=e->note_on.vel;
353 break;
355 case SONG_EV_NOTE_OFF:
357 midimsg[mi++]=MIDI_MSG_NOTE_OFF|e->note_off.channel;
358 midimsg[mi++]=e->note_off.note;
359 midimsg[mi++]=0;
361 break;
363 case SONG_EV_MIDI_PROGRAM:
365 midimsg[mi++]=MIDI_MSG_PROGRAM|e->midi_program.channel;
366 midimsg[mi++]=e->midi_program.program;
368 break;
370 case SONG_EV_END:
372 go=0;
373 break;
375 default:
376 /* ignore the rest */
377 break;
380 /* next event */
381 e++;
384 if(!go)
385 break;
387 /* get time of next event */
388 msecs_p=msecs;
389 msecs=e->generic.msecs;
390 msecs_d=msecs - msecs_p;
392 if(msecs >= skip_msecs)
394 /* if there are pending messages, write them */
395 if(mi)
396 write(midi_fd, midimsg, mi);
398 /* calculate the time to sleep */
399 ts.tv_sec=(time_t) msecs_d / 1000;
400 ts.tv_nsec=(long) ((msecs_d * 1000000) % 1000000000);
402 nanosleep(&ts, NULL);
406 return(0);
410 int midi_device_open(char * devfile)
412 if(devfile == NULL)
413 devfile="/dev/midi";
415 return((midi_fd=open(devfile, O_WRONLY)));
419 void midi_device_close(void)
421 close(midi_fd);