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
39 /* the song event stream */
40 union song_ev
* song
= NULL
;
42 /* number of song events */
45 /* solo track (-1, no solo) */
48 /* number of tracks in song */
49 int n_song_tracks
= 0;
56 * song_clear - Clears the song stream
58 * Clears the song stream.
74 * add_song_ev - Adds a song event to the song stream
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
)
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
;
99 memcpy(&song
[n_song_ev
], ev
, sizeof(union song_ev
));
105 static void song_trace_events(void)
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
)
125 printf("SONG_EV_TEMPO ");
126 printf("TEMPO:%lf", e
->tempo
.tempo
);
131 printf("SONG_EV_METER ");
132 printf("METER:%d/%d", e
->meter
.num
, e
->meter
.den
);
135 case SONG_EV_MEASURE
:
137 printf("SONG_EV_MEASURE ");
138 printf("LINE:%d", e
->measure
.line
);
141 case SONG_EV_SS_SUSTAIN
:
143 printf("SONG_EV_SS_SUSTAIN ");
144 printf("SUSTAIN:%lf", e
->ss_sustain
.sustain
);
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
);
155 case SONG_EV_SS_PORTAMENTO
:
157 printf("SONG_EV_SS_PORTAMENTO ");
158 printf("VALUE: %lf", e
->ss_portamento
.portamento
);
161 case SONG_EV_SS_CHANNEL
:
163 printf("SONG_EV_SS_CHANNEL ");
164 printf("CHANNEL:%d VOL:%lf",
165 e
->ss_channel
.channel
,
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
);
180 printf("SONG_EV_SS_PAT ");
181 printf("FILE:'%s'", e
->ss_pat
.file
);
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
);
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
);
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
);
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
);
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
,
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
);
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
,
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
,
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
,
259 case SONG_EV_SS_EFF_REVERB
:
261 printf("SONG_EV_SS_EFF_REVERB ");
262 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
265 case SONG_EV_SS_EFF_OFF
:
267 printf("SONG_EV_SS_EFF_OFF ");
268 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
271 case SONG_EV_MIDI_CHANNEL
:
273 printf("SONG_EV_MIDI_CHANNEL ");
274 printf("CHANNEL:%d ", e
->midi_channel
.channel
);
277 case SONG_EV_MIDI_PROGRAM
:
279 printf("SONG_EV_MIDI_PROGRAM ");
280 printf("PROGRAM:%d ", e
->midi_program
.program
);
285 printf("SONG_EV_BACK ");
286 printf("LEN:%lf", e
->back
.len
);
291 printf("SONG_EV_NOTE ");
292 printf("MIDI:%d LEN:%lf VOL:%f",
293 e
->note
.note
, e
->note
.len
, e
->note
.vol
);
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
);
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
);
313 case SONG_EV_SONG_INFO
:
315 printf("SONG_EV_SONG_INFO ");
316 printf("AUTHOR:'%s' NAME:'%s'",
323 printf("SONG_EV_EOT ");
327 printf("** Unexpected event type: %d",
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
;
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 */
351 ret
= e1
->type
- e2
->type
;
353 /* same time and same event? order by event id */
355 ret
= e1
->event_id
- e2
->event_id
;
361 static void add_eot_events(void)
362 /* travels all events and adds EOT ones */
366 for(n
= 0;n
< n_song_tracks
;n
++)
372 for(m
= 0;m
< n_song_ev
;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.
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
)
418 if((ret
= ((int)(ev_time
* (double) den
)) % num
))
419 printf("Measure boundary check failed in line %d\n", line
);