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
41 struct midi_ev_generic
43 song_ev_type type
; /* event type */
44 int msecs
; /* time in milliseconds */
45 int trk_id
; /* track id */
46 int channel
; /* MIDI channel */
49 struct midi_ev_program
51 song_ev_type type
; /* SONG_EV_MIDI_PROGRAM */
55 int program
; /* MIDI program number */
58 struct midi_ev_note_off
60 song_ev_type type
; /* SONG_EV_NOTE_OFF */
64 int note
; /* MIDI note */
67 struct midi_ev_note_on
69 song_ev_type type
; /* SONG_EV_NOTE_ON */
73 int note
; /* MIDI note */
74 int vel
; /* velocity (volume) */
79 struct midi_ev_generic generic
;
80 struct midi_ev_program midi_program
;
81 struct midi_ev_note_on note_on
;
82 struct midi_ev_note_off note_off
;
86 /* the MIDI song stream */
88 static union midi_ev
* midi_song
=NULL
;
89 static int n_midi_ev
=0;
91 /* the MIDI tracks: just a track/channel table */
93 #define MIDI_TRACK_NUM 256
94 static int track_channel
[MIDI_TRACK_NUM
];
96 /* MIDI message types */
98 #define MIDI_MSG_NOTE_ON 0x90
99 #define MIDI_MSG_NOTE_OFF 0x80
100 #define MIDI_MSG_CONTROLLER 0xB0
101 #define MIDI_MSG_PROGRAM 0xC0
109 ********************/
111 static void add_midi_ev(union midi_ev
* e
)
112 /* adds a MIDI song event */
114 if(e
->generic
.channel
< 0)
118 midi_song
=(union midi_ev
*)realloc(midi_song
,
119 (n_midi_ev
+ 1) * sizeof(union midi_ev
));
122 memcpy(&midi_song
[n_midi_ev
], e
, sizeof(union midi_ev
));
128 static int midi_ev_cmp_by_time(const void * v1
, const void * v2
)
129 /* MIDI song event compare function for qsort(), by time (for playing) */
131 struct midi_ev_generic
* e1
;
132 struct midi_ev_generic
* e2
;
134 e1
=(struct midi_ev_generic
*)v1
; e2
=(struct midi_ev_generic
*)v2
;
136 if(e1
->msecs
== e2
->msecs
)
137 return(e1
->type
- e2
->type
);
139 return(e1
->msecs
- e2
->msecs
);
143 static int midi_song_convert_events(void)
144 /* converts generic song_ev events to MIDI events */
148 int msecs
, msecs_ac
, f_msecs
;
149 double time_ac
, time_ac_m
;
155 /* resets the MIDI stream */
156 if(midi_song
!= NULL
)
168 msecs
=msecs_ac
=f_msecs
=0;
172 /* by default, all channels are 'mute' until
173 a channel is explicitly set */
174 for(n
=0;n
< MIDI_TRACK_NUM
;n
++)
177 /* travels the song events generating MIDI song events */
178 for(n
=0;n
< n_song_ev
;n
++)
180 /* gets the song event */
183 /* calculates the msecs */
184 msecs
=((e
->generic
.time
- time_ac
) * mspw
) + msecs_ac
;
186 /* generic event data */
187 me
.generic
.type
=e
->generic
.type
;
188 me
.generic
.msecs
=msecs
;
189 me
.generic
.trk_id
=e
->generic
.trk_id
;
191 /* if it's not a generic message, set MIDI channel */
192 if(e
->generic
.trk_id
>= 0)
193 me
.generic
.channel
=track_channel
[e
->generic
.trk_id
];
195 /* account the biggest track number seen */
196 if(b_track
< e
->generic
.trk_id
) b_track
=e
->generic
.trk_id
;
198 switch(e
->generic
.type
)
202 /* updates accumulations */
204 time_ac
+= e
->generic
.time
;
206 /* calculates milliseconds-per-whole based on new tempo */
207 mspw
= 1000.0 * 60.0;
208 mspw
/= (e
->tempo
.tempo
/ 4.0);
214 /* just store the values */
217 time_ac_m
=e
->meter
.time
;
221 case SONG_EV_MEASURE
:
223 song_test_measure_boundary(e
->measure
.time
- time_ac_m
,
224 num
, den
, e
->measure
.line
);
227 case SONG_EV_MIDI_CHANNEL
:
229 /* stores the channel for this track */
230 track_channel
[e
->midi_channel
.trk_id
]=
231 e
->midi_channel
.channel
;
236 /* convert to note on / off pairs */
238 me
.note_on
.type
=SONG_EV_NOTE_ON
;
239 me
.note_on
.note
=e
->note
.note
;
240 me
.note_on
.vel
=(int)(e
->note
.vol
* 127.0);
244 msecs
+= (int)(e
->note
.len
* mspw
);
246 me
.note_off
.type
=SONG_EV_NOTE_OFF
;
247 me
.note_off
.msecs
=msecs
;
252 case SONG_EV_MIDI_PROGRAM
:
254 /* set MIDI program (instrument) */
255 me
.midi_program
.program
=e
->midi_program
.program
;
260 case SONG_EV_SS_PITCH_STRETCH
:
263 case SONG_EV_SS_SUSTAIN
:
264 case SONG_EV_SS_VIBRATO
:
265 case SONG_EV_SS_CHANNEL
:
266 case SONG_EV_SS_EFF_DELAY
:
267 case SONG_EV_SS_EFF_ECHO
:
268 case SONG_EV_SS_EFF_COMB
:
269 case SONG_EV_SS_EFF_ALLPASS
:
270 case SONG_EV_SS_EFF_FLANGER
:
271 case SONG_EV_SS_EFF_WOBBLE
:
272 case SONG_EV_SS_EFF_SQWOBBLE
:
273 case SONG_EV_SS_EFF_FADER
:
274 case SONG_EV_SS_EFF_REVERB
:
275 case SONG_EV_SS_EFF_OFF
:
277 /* softsynth events are ignored */
280 case SONG_EV_NOTE_ON
:
281 case SONG_EV_NOTE_OFF
:
284 /* never found in generic song streams */
288 /* store the further time seen */
289 if(f_msecs
< msecs
) f_msecs
=msecs
;
292 /* generates an end of event mark, a time after the last one */
293 me
.generic
.type
=SONG_EV_END
;
294 me
.generic
.msecs
=f_msecs
+ 1000;
297 /* return the number of tracks */
302 int midi_song_play(int skip_secs
)
305 int msecs
, msecs_p
, msecs_d
;
309 unsigned char midimsg
[1024];
313 /* convert the song to MIDI events */
314 n_tracks
=midi_song_convert_events();
317 qsort(midi_song
, n_midi_ev
, sizeof(union midi_ev
), midi_ev_cmp_by_time
);
323 /* calculate the millisecond to start playing */
324 skip_msecs
=skip_secs
* 1000;
326 /* loop the events */
332 /* process all events for this exact time */
333 while(e
->generic
.msecs
== msecs
)
335 switch(e
->generic
.type
)
337 case SONG_EV_NOTE_ON
:
339 midimsg
[mi
++]=MIDI_MSG_NOTE_ON
|e
->note_on
.channel
;
340 midimsg
[mi
++]=e
->note_on
.note
;
341 midimsg
[mi
++]=e
->note_on
.vel
;
345 case SONG_EV_NOTE_OFF
:
347 midimsg
[mi
++]=MIDI_MSG_NOTE_OFF
|e
->note_off
.channel
;
348 midimsg
[mi
++]=e
->note_off
.note
;
353 case SONG_EV_MIDI_PROGRAM
:
355 midimsg
[mi
++]=MIDI_MSG_PROGRAM
|e
->midi_program
.channel
;
356 midimsg
[mi
++]=e
->midi_program
.program
;
366 /* ignore the rest */
377 /* get time of next event */
379 msecs
=e
->generic
.msecs
;
380 msecs_d
=msecs
- msecs_p
;
382 if(msecs
>= skip_msecs
)
384 /* if there are pending messages, write them */
386 write(midi_fd
, midimsg
, mi
);
388 /* calculate the time to sleep */
389 ts
.tv_sec
=(time_t) msecs_d
/ 1000;
390 ts
.tv_nsec
=(long) ((msecs_d
* 1000000) % 1000000000);
392 nanosleep(&ts
, NULL
);