Don't generate 'mark-overpassed' errors if moving to the END mark.
[ahxm.git] / song.c
blobccd5a2614f821226c35eeb3570d113ff54665650
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2006 Angel Ortega <angel@triptico.com>
6 song.c - Device-independent 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>
33 #include "ahxm.h"
35 /*******************
36 Data
37 ********************/
39 /* the song event stream */
40 union song_ev * song = NULL;
42 /* number of song events */
43 int n_song_ev = 0;
45 /* solo track (-1, no solo) */
46 int solo_track = -1;
48 /* number of tracks in song */
49 int n_song_tracks = 0;
51 /*******************
52 Code
53 ********************/
55 /**
56 * song_clear - Clears the song stream
58 * Clears the song stream.
60 void song_clear(void)
62 if(song != NULL)
64 free(song);
65 song = NULL;
68 n_song_ev = 0;
69 n_song_tracks = 0;
73 /**
74 * add_song_ev - Adds a song event to the song stream
75 * @type: event type
76 * @time: event time
77 * @ev: the event
79 * Adds a song event to the song stream.
81 void add_song_ev(song_ev_type type, double time, union song_ev * ev)
83 /* skip tracks if a solo is requested */
84 if(solo_track != -1 && ev->generic.trk_id >= 0 &&
85 ev->generic.trk_id != solo_track)
86 return;
88 /* account number of tracks */
89 if(n_song_tracks < ev->generic.trk_id + 1)
90 n_song_tracks = ev->generic.trk_id + 1;
92 GROW(song, n_song_ev, union song_ev);
94 ev->generic.type = type;
95 ev->generic.time = time;
96 ev->generic.event_id = n_song_ev;
98 /* store */
99 memcpy(&song[n_song_ev], ev, sizeof(union song_ev));
101 n_song_ev++;
105 static void song_trace_events(void)
107 union song_ev * e;
108 int n;
110 printf("** GENERIC SONG EVENT DUMP **\n\n");
111 printf("%9s %5s %5s Event and information\n",
112 "Time", "Track", "Ev.ID");
113 printf("---------------------------------------------------------------\n");
115 for(n = 0, e = song;n < n_song_ev;n++, e++)
117 printf("%9.4lf %5d %5d ",
118 e->generic.time, e->generic.trk_id,
119 e->generic.event_id);
121 switch(e->generic.type)
123 case SONG_EV_TEMPO:
125 printf("SONG_EV_TEMPO ");
126 printf("TEMPO:%lf", e->tempo.tempo);
127 break;
129 case SONG_EV_METER:
131 printf("SONG_EV_METER ");
132 printf("METER:%d/%d", e->meter.num, e->meter.den);
133 break;
135 case SONG_EV_MEASURE:
137 printf("SONG_EV_MEASURE ");
138 printf("LINE:%d", e->measure.line);
139 break;
141 case SONG_EV_SS_SUSTAIN:
143 printf("SONG_EV_SS_SUSTAIN ");
144 printf("SUSTAIN:%lf", e->ss_sustain.sustain);
145 break;
147 case SONG_EV_SS_VIBRATO:
149 printf("SONG_EV_SS_VIBRATO ");
150 printf("DEPTH:%lf FREQ:%lf",
151 e->ss_vibrato.vib_depth,
152 e->ss_vibrato.vib_freq);
153 break;
155 case SONG_EV_SS_PORTAMENTO:
157 printf("SONG_EV_SS_PORTAMENTO ");
158 printf("VALUE: %lf", e->ss_portamento.portamento);
159 break;
161 case SONG_EV_SS_CHANNEL:
163 printf("SONG_EV_SS_CHANNEL ");
164 printf("CHANNEL:%d VOL:%lf",
165 e->ss_channel.channel,
166 e->ss_channel.vol);
167 break;
169 case SONG_EV_SS_WAV:
171 printf("SONG_EV_SS_WAV ");
172 printf("FILE:'%s' BASE:%d MIN:%d MAX:%d START:%lf END:%lf",
173 e->ss_wav.file, e->ss_wav.base,
174 e->ss_wav.min, e->ss_wav.max,
175 e->ss_wav.loop_start, e->ss_wav.loop_end);
176 break;
178 case SONG_EV_SS_PAT:
180 printf("SONG_EV_SS_PAT ");
181 printf("FILE:'%s'", e->ss_pat.file);
182 break;
184 case SONG_EV_SS_EFF_DELAY:
186 printf("SONG_EV_SS_EFF_DELAY ");
187 printf("CHANNEL:%d ", e->ss_eff.channel);
188 printf("SIZE:%lf ", e->ss_eff.size);
189 break;
191 case SONG_EV_SS_EFF_ECHO:
193 printf("SONG_EV_SS_EFF_ECHO ");
194 printf("CHANNEL:%d ", e->ss_eff.channel);
195 printf("SIZE:%lf ", e->ss_eff.size);
196 printf("GAIN:%f ", e->ss_eff.gain);
197 break;
199 case SONG_EV_SS_EFF_COMB:
201 printf("SONG_EV_SS_EFF_COMB ");
202 printf("CHANNEL:%d ", e->ss_eff.channel);
203 printf("SIZE:%lf ", e->ss_eff.size);
204 printf("GAIN:%f ", e->ss_eff.gain);
205 break;
207 case SONG_EV_SS_EFF_ALLPASS:
209 printf("SONG_EV_SS_EFF_ALLPASS ");
210 printf("CHANNEL:%d ", e->ss_eff.channel);
211 printf("SIZE:%lf ", e->ss_eff.size);
212 printf("GAIN:%f ", e->ss_eff.gain);
213 break;
215 case SONG_EV_SS_EFF_FLANGER:
217 printf("SONG_EV_SS_EFF_FLANGER ");
218 printf("CHANNEL:%d ", e->ss_eff.channel);
219 printf("SIZE:%lf ", e->ss_eff.size);
220 printf("GAIN:%f ", e->ss_eff.gain);
221 printf("DEPTH:%lf ", e->ss_eff.depth);
222 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
223 e->ss_eff.phase);
224 break;
226 case SONG_EV_SS_EFF_WOBBLE:
228 printf("SONG_EV_SS_EFF_WOBBLE ");
229 printf("CHANNEL:%d ", e->ss_eff.channel);
230 printf("FREQ:%lf PHASE:%lf GAIN:%lf", e->ss_eff.freq,
231 e->ss_eff.phase, e->ss_eff.gain);
232 break;
234 case SONG_EV_SS_EFF_SQWOBBLE:
236 printf("SONG_EV_SS_EFF_SQWOBBLE ");
237 printf("CHANNEL:%d ", e->ss_eff.channel);
238 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
239 e->ss_eff.phase);
240 break;
242 case SONG_EV_SS_EFF_HFWOBBLE:
244 printf("SONG_EV_SS_EFF_HFWOBBLE ");
245 printf("CHANNEL:%d ", e->ss_eff.channel);
246 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
247 e->ss_eff.phase);
248 break;
250 case SONG_EV_SS_EFF_FADER:
252 printf("SONG_EV_SS_EFF_FADER ");
253 printf("CHANNEL:%d ", e->ss_eff.channel);
254 printf("SIZE:%lf ", e->ss_eff.size);
255 printf("INITIAL:%f FINAL:%f", e->ss_eff.initial,
256 e->ss_eff.final);
257 break;
259 case SONG_EV_SS_EFF_REVERB:
261 printf("SONG_EV_SS_EFF_REVERB ");
262 printf("CHANNEL:%d ", e->ss_eff.channel);
263 break;
265 case SONG_EV_SS_EFF_OFF:
267 printf("SONG_EV_SS_EFF_OFF ");
268 printf("CHANNEL:%d ", e->ss_eff.channel);
269 break;
271 case SONG_EV_MIDI_CHANNEL:
273 printf("SONG_EV_MIDI_CHANNEL ");
274 printf("CHANNEL:%d ", e->midi_channel.channel);
275 break;
277 case SONG_EV_MIDI_PROGRAM:
279 printf("SONG_EV_MIDI_PROGRAM ");
280 printf("PROGRAM:%d ", e->midi_program.program);
281 break;
283 case SONG_EV_BACK:
285 printf("SONG_EV_BACK ");
286 printf("LEN:%lf", e->back.len);
287 break;
289 case SONG_EV_NOTE:
291 printf("SONG_EV_NOTE ");
292 printf("MIDI:%d LEN:%lf VOL:%f",
293 e->note.note, e->note.len, e->note.vol);
294 break;
296 case SONG_EV_SS_PITCH_STRETCH:
298 printf("SONG_EV_SS_PITCH_STRETCH ");
299 printf("MIDI:%d LEN:%lf VOL:%f",
300 e->ss_pitch_stretch.note,
301 e->ss_pitch_stretch.len,
302 e->ss_pitch_stretch.vol);
303 break;
305 case SONG_EV_SS_PRINT_WAVE_TEMPO:
307 printf("SONG_EV_SS_PRINT_WAVE_TEMPO ");
308 printf("MIDI:%d LEN:%lf",
309 e->ss_print_wave_tempo.note,
310 e->ss_print_wave_tempo.len);
311 break;
313 case SONG_EV_SONG_INFO:
315 printf("SONG_EV_SONG_INFO ");
316 printf("AUTHOR:'%s' NAME:'%s'",
317 e->song_info.author,
318 e->song_info.name);
319 break;
321 case SONG_EV_EOT:
323 printf("SONG_EV_EOT ");
324 break;
326 default:
327 printf("** Unexpected event type: %d",
328 e->generic.type);
331 printf("\n");
334 printf("\n");
338 static int song_ev_cmp(const void * v1, const void * v2)
339 /* sorts events by time, then type, then event_id */
341 struct song_ev_generic * e1;
342 struct song_ev_generic * e2;
343 int ret;
345 e1 = (struct song_ev_generic *)v1; e2 = (struct song_ev_generic *)v2;
347 ret = (int) ((e1->time * 10000.0) - (e2->time * 10000.0));
349 /* same time? order by type of event */
350 if(ret == 0)
351 ret = e1->type - e2->type;
353 /* same time and same event? order by event id */
354 if(ret == 0)
355 ret = e1->event_id - e2->event_id;
357 return(ret);
361 static void add_eot_events(void)
362 /* travels all events and adds EOT ones */
364 int n, m;
366 for(n = 0;n < n_song_tracks;n++)
368 union song_ev * ep;
369 union song_ev e;
370 double t = -1;
372 for(m = 0;m < n_song_ev;m++)
374 ep = &song[m];
376 /* if this is the track we're looking for
377 and this event time is bigger, store it */
378 if(ep->generic.trk_id == n && ep->generic.time > t)
379 t = ep->generic.time;
382 /* now t has the biggest time; add an EOT event with t */
383 e.generic.trk_id = n;
384 add_song_ev(SONG_EV_EOT, t, &e);
390 * song_sort - Sorts the song stream
392 * Sorts the song stream.
394 void song_sort(void)
396 add_eot_events();
398 /* sorts by time, type and event_id */
399 qsort(song, n_song_ev, sizeof(union song_ev), song_ev_cmp);
401 if(trace) song_trace_events();
406 * song_test_measure_boundary - Does a measure boundary check
407 * @ev_time: event time
408 * @num: meter numerator
409 * @den: meter denominator
411 * Does a measure boundary check. Returns 0 if the event time falls
412 * exactly between two measures, or nonzero otherwise.
414 int song_test_measure_boundary(double ev_time, int num, int den, int line)
416 int ret;
418 if((ret = ((int)(ev_time * (double) den)) % num))
419 printf("Measure boundary check failed in line %d\n", line);
421 return(ret);