Cue commands are indented.
[ahxm.git] / song.c
blobb22e6aed4f00f8b92c2dab85b3b00c848b2d14ad
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 "annhell.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 /*******************
49 Code
50 ********************/
52 /**
53 * song_clear - Clears the song stream
55 * Clears the song stream.
57 void song_clear(void)
59 if(song != NULL)
61 free(song);
62 song = NULL;
65 n_song_ev = 0;
69 /**
70 * add_song_ev - Adds a song event to the song stream
71 * @ev: the event
73 * Adds a song event to the song stream.
75 void add_song_ev(union song_ev * ev)
77 /* skip tracks if a solo is requested */
78 if(solo_track != -1 && ev->generic.trk_id >= 0 &&
79 ev->generic.trk_id != solo_track)
80 return;
82 /* reallocs */
83 song = (union song_ev *)realloc(song,
84 (n_song_ev + 1) * sizeof(union song_ev));
86 ev->generic.event_id = n_song_ev;
88 /* store */
89 memcpy(&song[n_song_ev], ev, sizeof(union song_ev));
91 n_song_ev++;
95 static int song_ev_cmp(const void * v1, const void * v2)
97 struct song_ev_generic * e1;
98 struct song_ev_generic * e2;
99 int ret;
101 e1 = (struct song_ev_generic *)v1; e2 = (struct song_ev_generic *)v2;
103 ret = (int) ((e1->time * 10000.0) - (e2->time * 10000.0));
105 /* same time? order by type of event */
106 if(ret == 0)
107 ret = e1->type - e2->type;
109 /* same time and same event? order by event id */
110 if(ret == 0)
111 ret = e1->event_id - e2->event_id;
113 return(ret);
117 static void song_trace_events(void)
119 union song_ev * e;
120 int n;
122 printf("** GENERIC SONG EVENT DUMP **\n\n");
123 printf("%9s %5s %5s Event and information\n",
124 "Time", "Track", "Ev.ID");
125 printf("---------------------------------------------------------------\n");
127 for(n = 0, e = song;n < n_song_ev;n++, e++)
129 printf("%9.4lf %5d %5d ",
130 e->generic.time, e->generic.trk_id,
131 e->generic.event_id);
133 switch(e->generic.type)
135 case SONG_EV_TEMPO:
137 printf("SONG_EV_TEMPO ");
138 printf("TEMPO:%lf", e->tempo.tempo);
139 break;
141 case SONG_EV_METER:
143 printf("SONG_EV_METER ");
144 printf("METER:%d/%d", e->meter.num, e->meter.den);
145 break;
147 case SONG_EV_MEASURE:
149 printf("SONG_EV_MEASURE ");
150 printf("LINE:%d", e->measure.line);
151 break;
153 case SONG_EV_SS_SUSTAIN:
155 printf("SONG_EV_SS_SUSTAIN ");
156 printf("SUSTAIN:%lf", e->ss_sustain.sustain);
157 break;
159 case SONG_EV_SS_VIBRATO:
161 printf("SONG_EV_SS_VIBRATO ");
162 printf("DEPTH:%lf FREQ:%lf",
163 e->ss_vibrato.vib_depth,
164 e->ss_vibrato.vib_freq);
165 break;
167 case SONG_EV_SS_CHANNEL:
169 printf("SONG_EV_SS_CHANNEL ");
170 printf("CHANNEL:%d VOL:%lf",
171 e->ss_channel.channel,
172 e->ss_channel.vol);
173 break;
175 case SONG_EV_SS_WAV:
177 printf("SONG_EV_SS_WAV ");
178 printf("FILE:'%s' BASE:%d MIN:%d MAX:%d START:%lf END:%lf",
179 e->ss_wav.file, e->ss_wav.base,
180 e->ss_wav.min, e->ss_wav.max,
181 e->ss_wav.loop_start, e->ss_wav.loop_end);
182 break;
184 case SONG_EV_SS_PAT:
186 printf("SONG_EV_SS_PAT ");
187 printf("FILE:'%s'", e->ss_pat.file);
188 break;
190 case SONG_EV_SS_EFF_DELAY:
192 printf("SONG_EV_SS_EFF_DELAY ");
193 printf("CHANNEL:%d ", e->ss_eff.channel);
194 printf("SIZE:%lf ", e->ss_eff.size);
195 break;
197 case SONG_EV_SS_EFF_ECHO:
199 printf("SONG_EV_SS_EFF_ECHO ");
200 printf("CHANNEL:%d ", e->ss_eff.channel);
201 printf("SIZE:%lf ", e->ss_eff.size);
202 printf("GAIN:%f ", e->ss_eff.gain);
203 break;
205 case SONG_EV_SS_EFF_COMB:
207 printf("SONG_EV_SS_EFF_COMB ");
208 printf("CHANNEL:%d ", e->ss_eff.channel);
209 printf("SIZE:%lf ", e->ss_eff.size);
210 printf("GAIN:%f ", e->ss_eff.gain);
211 break;
213 case SONG_EV_SS_EFF_ALLPASS:
215 printf("SONG_EV_SS_EFF_ALLPASS ");
216 printf("CHANNEL:%d ", e->ss_eff.channel);
217 printf("SIZE:%lf ", e->ss_eff.size);
218 printf("GAIN:%f ", e->ss_eff.gain);
219 break;
221 case SONG_EV_SS_EFF_FLANGER:
223 printf("SONG_EV_SS_EFF_FLANGER ");
224 printf("CHANNEL:%d ", e->ss_eff.channel);
225 printf("SIZE:%lf ", e->ss_eff.size);
226 printf("GAIN:%f ", e->ss_eff.gain);
227 printf("DEPTH:%lf ", e->ss_eff.depth);
228 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
229 e->ss_eff.phase);
230 break;
232 case SONG_EV_SS_EFF_WOBBLE:
234 printf("SONG_EV_SS_EFF_WOBBLE ");
235 printf("CHANNEL:%d ", e->ss_eff.channel);
236 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
237 e->ss_eff.phase);
238 break;
240 case SONG_EV_SS_EFF_SQWOBBLE:
242 printf("SONG_EV_SS_EFF_SQWOBBLE ");
243 printf("CHANNEL:%d ", e->ss_eff.channel);
244 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
245 e->ss_eff.phase);
246 break;
248 case SONG_EV_SS_EFF_FADER:
250 printf("SONG_EV_SS_EFF_FADER ");
251 printf("CHANNEL:%d ", e->ss_eff.channel);
252 printf("SIZE:%lf ", e->ss_eff.size);
253 printf("INITIAL:%f FINAL:%f", e->ss_eff.initial,
254 e->ss_eff.final);
255 break;
257 case SONG_EV_SS_EFF_REVERB:
259 printf("SONG_EV_SS_EFF_REVERB ");
260 printf("CHANNEL:%d ", e->ss_eff.channel);
261 break;
263 case SONG_EV_SS_EFF_OFF:
265 printf("SONG_EV_SS_EFF_OFF ");
266 printf("CHANNEL:%d ", e->ss_eff.channel);
267 break;
269 case SONG_EV_MIDI_CHANNEL:
271 printf("SONG_EV_MIDI_CHANNEL ");
272 printf("CHANNEL:%d ", e->midi_channel.channel);
273 break;
275 case SONG_EV_MIDI_PROGRAM:
277 printf("SONG_EV_MIDI_PROGRAM ");
278 printf("PROGRAM:%d ", e->midi_program.program);
279 break;
281 case SONG_EV_BACK:
283 printf("SONG_EV_BACK ");
284 printf("LEN:%lf", e->back.len);
285 break;
287 case SONG_EV_NOTE:
289 printf("SONG_EV_NOTE ");
290 printf("MIDI:%d LEN:%lf VOL:%f",
291 e->note.note, e->note.len, e->note.vol);
292 break;
294 case SONG_EV_SS_PITCH_STRETCH:
296 printf("SONG_EV_SS_PITCH_STRETCH ");
297 printf("MIDI:%d LEN:%lf VOL:%f",
298 e->ss_pitch_stretch.note,
299 e->ss_pitch_stretch.len,
300 e->ss_pitch_stretch.vol);
301 break;
303 case SONG_EV_SS_PRINT_WAVE_TEMPO:
305 printf("SONG_EV_SS_PRINT_WAVE_TEMPO ");
306 printf("MIDI:%d LEN:%lf",
307 e->ss_print_wave_tempo.note,
308 e->ss_print_wave_tempo.len);
309 break;
311 case SONG_EV_TRACK_INFO:
313 printf("SONG_EV_TRACK_INFO ");
314 printf("AUTHOR:'%s' NAME:'%s'",
315 e->track_info.author,
316 e->track_info.name);
317 break;
319 default:
320 printf("** Unexpected event type: %d",
321 e->generic.type);
324 printf("\n");
327 printf("\n");
332 * song_sort - Sorts the song stream
334 * Sorts the song stream.
336 void song_sort(void)
338 qsort(song, n_song_ev, sizeof(union song_ev), song_ev_cmp);
340 if(trace) song_trace_events();
345 * song_test_measure_boundary - Does a measure boundary check
346 * @ev_time: event time
347 * @num: meter numerator
348 * @den: meter denominator
350 * Does a measure boundary check. Returns 0 if the event time falls
351 * exactly between two measures, or nonzero otherwise.
353 int song_test_measure_boundary(double ev_time, int num, int den, int line)
355 int ret;
357 if((ret = ((int)(ev_time * (double) den)) % num))
358 printf("Measure boundary check failed in line %d\n", line);
360 return(ret);