Updated TODO.
[ahxm.git] / midi_song.c
blob834f887cb21e8e616b2fd1f898c3fd0f6f99795b
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 = add_midi_ev(e);
179 e->type = SONG_EV_NOTE_ON;
180 e->vel = (int) (e->vol * 127.0);
182 msecs += (int) (e->len * mspw);
184 e = add_midi_ev(e);
186 e->type = SONG_EV_NOTE_OFF;
187 e->msecs = msecs;
189 break;
191 case SONG_EV_BACK:
193 /* move the cursor back */
194 msecs_ac -= (int) (e->len * mspw);
195 break;
197 case SONG_EV_MIDI_PROGRAM:
199 add_midi_ev(e);
200 break;
202 case SONG_EV_SS_PITCH_STRETCH:
203 case SONG_EV_SS_PRINT_WAVE_TEMPO:
204 case SONG_EV_SS_WAV:
205 case SONG_EV_SS_PAT:
206 case SONG_EV_SS_SUSTAIN:
207 case SONG_EV_SS_ATTACK:
208 case SONG_EV_SS_VIBRATO:
209 case SONG_EV_SS_PORTAMENTO:
210 case SONG_EV_SS_CHANNEL:
211 case SONG_EV_SS_EFF_DELAY:
212 case SONG_EV_SS_EFF_ECHO:
213 case SONG_EV_SS_EFF_COMB:
214 case SONG_EV_SS_EFF_ALLPASS:
215 case SONG_EV_SS_EFF_FLANGER:
216 case SONG_EV_SS_EFF_WOBBLE:
217 case SONG_EV_SS_EFF_SQWOBBLE:
218 case SONG_EV_SS_EFF_HFWOBBLE:
219 case SONG_EV_SS_EFF_FADER:
220 case SONG_EV_SS_EFF_REVERB:
221 case SONG_EV_SS_EFF_FOLDBACK:
222 case SONG_EV_SS_EFF_ATAN:
223 case SONG_EV_SS_EFF_DISTORT:
224 case SONG_EV_SS_EFF_OVERDRIVE:
225 case SONG_EV_SS_EFF_OFF:
226 case SONG_EV_NOP:
228 /* ignored */
229 break;
231 case SONG_EV_SONG_INFO:
233 /* song info should be used */
234 break;
236 case SONG_EV_EOT:
238 /* end of track; trigger possible cleaning */
239 break;
241 case SONG_EV_NOTE_ON:
242 case SONG_EV_NOTE_OFF:
243 case SONG_EV_END:
245 /* never found in generic song streams */
246 break;
249 /* store the further time seen */
250 if (f_msecs < msecs)
251 f_msecs = msecs;
254 /* generates an end of event mark, a time after the last one */
255 e = add_event(&midi_song, &n_midi_ev);
257 e->type = SONG_EV_END;
258 e->msecs = f_msecs + 1000;
262 int midi_song_play(int skip_secs)
264 struct song_ev *e;
265 int msecs, msecs_p, msecs_d;
266 int go;
267 struct timespec ts;
268 unsigned char midimsg[1024];
269 int mi;
270 int skip_msecs;
272 /* convert the song to MIDI events */
273 midi_song_convert_events();
275 /* sort by time */
276 qsort(midi_song, n_midi_ev, sizeof(struct song_ev), msecs_type_cmp);
278 msecs = msecs_p = 0;
279 go = 1;
280 e = midi_song;
282 /* calculate the millisecond to start playing */
283 skip_msecs = skip_secs * 1000;
285 /* loop the events */
286 while (go) {
287 /* clear buffer */
288 mi = 0;
290 if (verbose >= 1 && msecs % 1000 == 0) {
291 int m = msecs / 1000;
292 printf("[%02d:%02d]\r", m / 60, m % 60);
293 fflush(stdout);
296 /* process all events for this exact time */
297 while (e->msecs == msecs) {
298 switch (e->type) {
299 case SONG_EV_NOTE_ON:
301 midimsg[mi++] = MIDI_MSG_NOTE_ON | e->channel;
302 midimsg[mi++] = e->value;
303 midimsg[mi++] = e->vel;
305 break;
307 case SONG_EV_NOTE_OFF:
309 midimsg[mi++] = MIDI_MSG_NOTE_OFF | e->channel;
310 midimsg[mi++] = e->value;
311 midimsg[mi++] = 0;
313 break;
315 case SONG_EV_MIDI_PROGRAM:
317 midimsg[mi++] = MIDI_MSG_PROGRAM | e->channel;
318 midimsg[mi++] = e->value;
320 break;
322 case SONG_EV_END:
324 go = 0;
325 break;
327 default:
328 /* ignore the rest */
329 break;
332 /* next event */
333 e++;
336 if (!go)
337 break;
339 /* get time of next event */
340 msecs_p = msecs;
341 msecs = e->msecs;
342 msecs_d = msecs - msecs_p;
344 if (msecs >= skip_msecs) {
345 /* if there are pending messages, write them */
346 if (mi)
347 write(midi_fd, midimsg, mi);
349 /* calculate the time to sleep */
350 ts.tv_sec = (time_t) msecs_d / 1000;
351 ts.tv_nsec = (long) ((msecs_d * 1000000) % 1000000000);
353 nanosleep(&ts, NULL);
357 if (verbose >= 1)
358 printf("\n");
360 return 0;
364 int midi_device_open(char *devfile)
366 if (devfile == NULL)
367 devfile = "/dev/midi";
369 return (midi_fd = open(devfile, O_WRONLY));
373 void midi_device_close(void)
375 close(midi_fd);