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 int song_ev_cmp(const void * v1
, const void * v2
)
107 struct song_ev_generic
* e1
;
108 struct song_ev_generic
* e2
;
111 e1
= (struct song_ev_generic
*)v1
; e2
= (struct song_ev_generic
*)v2
;
113 ret
= (int) ((e1
->time
* 10000.0) - (e2
->time
* 10000.0));
115 /* same time? order by type of event */
117 ret
= e1
->type
- e2
->type
;
119 /* same time and same event? order by event id */
121 ret
= e1
->event_id
- e2
->event_id
;
127 static void song_trace_events(void)
132 printf("** GENERIC SONG EVENT DUMP **\n\n");
133 printf("%9s %5s %5s Event and information\n",
134 "Time", "Track", "Ev.ID");
135 printf("---------------------------------------------------------------\n");
137 for(n
= 0, e
= song
;n
< n_song_ev
;n
++, e
++)
139 printf("%9.4lf %5d %5d ",
140 e
->generic
.time
, e
->generic
.trk_id
,
141 e
->generic
.event_id
);
143 switch(e
->generic
.type
)
147 printf("SONG_EV_TEMPO ");
148 printf("TEMPO:%lf", e
->tempo
.tempo
);
153 printf("SONG_EV_METER ");
154 printf("METER:%d/%d", e
->meter
.num
, e
->meter
.den
);
157 case SONG_EV_MEASURE
:
159 printf("SONG_EV_MEASURE ");
160 printf("LINE:%d", e
->measure
.line
);
163 case SONG_EV_SS_SUSTAIN
:
165 printf("SONG_EV_SS_SUSTAIN ");
166 printf("SUSTAIN:%lf", e
->ss_sustain
.sustain
);
169 case SONG_EV_SS_VIBRATO
:
171 printf("SONG_EV_SS_VIBRATO ");
172 printf("DEPTH:%lf FREQ:%lf",
173 e
->ss_vibrato
.vib_depth
,
174 e
->ss_vibrato
.vib_freq
);
177 case SONG_EV_SS_PORTAMENTO
:
179 printf("SONG_EV_SS_PORTAMENTO ");
180 printf("VALUE: %lf", e
->ss_portamento
.portamento
);
183 case SONG_EV_SS_CHANNEL
:
185 printf("SONG_EV_SS_CHANNEL ");
186 printf("CHANNEL:%d VOL:%lf",
187 e
->ss_channel
.channel
,
193 printf("SONG_EV_SS_WAV ");
194 printf("FILE:'%s' BASE:%d MIN:%d MAX:%d START:%lf END:%lf",
195 e
->ss_wav
.file
, e
->ss_wav
.base
,
196 e
->ss_wav
.min
, e
->ss_wav
.max
,
197 e
->ss_wav
.loop_start
, e
->ss_wav
.loop_end
);
202 printf("SONG_EV_SS_PAT ");
203 printf("FILE:'%s'", e
->ss_pat
.file
);
206 case SONG_EV_SS_EFF_DELAY
:
208 printf("SONG_EV_SS_EFF_DELAY ");
209 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
210 printf("SIZE:%lf ", e
->ss_eff
.size
);
213 case SONG_EV_SS_EFF_ECHO
:
215 printf("SONG_EV_SS_EFF_ECHO ");
216 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
217 printf("SIZE:%lf ", e
->ss_eff
.size
);
218 printf("GAIN:%f ", e
->ss_eff
.gain
);
221 case SONG_EV_SS_EFF_COMB
:
223 printf("SONG_EV_SS_EFF_COMB ");
224 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
225 printf("SIZE:%lf ", e
->ss_eff
.size
);
226 printf("GAIN:%f ", e
->ss_eff
.gain
);
229 case SONG_EV_SS_EFF_ALLPASS
:
231 printf("SONG_EV_SS_EFF_ALLPASS ");
232 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
233 printf("SIZE:%lf ", e
->ss_eff
.size
);
234 printf("GAIN:%f ", e
->ss_eff
.gain
);
237 case SONG_EV_SS_EFF_FLANGER
:
239 printf("SONG_EV_SS_EFF_FLANGER ");
240 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
241 printf("SIZE:%lf ", e
->ss_eff
.size
);
242 printf("GAIN:%f ", e
->ss_eff
.gain
);
243 printf("DEPTH:%lf ", e
->ss_eff
.depth
);
244 printf("FREQ:%lf PHASE:%lf", e
->ss_eff
.freq
,
248 case SONG_EV_SS_EFF_WOBBLE
:
250 printf("SONG_EV_SS_EFF_WOBBLE ");
251 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
252 printf("FREQ:%lf PHASE:%lf GAIN:%lf", e
->ss_eff
.freq
,
253 e
->ss_eff
.phase
, e
->ss_eff
.gain
);
256 case SONG_EV_SS_EFF_SQWOBBLE
:
258 printf("SONG_EV_SS_EFF_SQWOBBLE ");
259 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
260 printf("FREQ:%lf PHASE:%lf", e
->ss_eff
.freq
,
264 case SONG_EV_SS_EFF_HFWOBBLE
:
266 printf("SONG_EV_SS_EFF_HFWOBBLE ");
267 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
268 printf("FREQ:%lf PHASE:%lf", e
->ss_eff
.freq
,
272 case SONG_EV_SS_EFF_FADER
:
274 printf("SONG_EV_SS_EFF_FADER ");
275 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
276 printf("SIZE:%lf ", e
->ss_eff
.size
);
277 printf("INITIAL:%f FINAL:%f", e
->ss_eff
.initial
,
281 case SONG_EV_SS_EFF_REVERB
:
283 printf("SONG_EV_SS_EFF_REVERB ");
284 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
287 case SONG_EV_SS_EFF_OFF
:
289 printf("SONG_EV_SS_EFF_OFF ");
290 printf("CHANNEL:%d ", e
->ss_eff
.channel
);
293 case SONG_EV_MIDI_CHANNEL
:
295 printf("SONG_EV_MIDI_CHANNEL ");
296 printf("CHANNEL:%d ", e
->midi_channel
.channel
);
299 case SONG_EV_MIDI_PROGRAM
:
301 printf("SONG_EV_MIDI_PROGRAM ");
302 printf("PROGRAM:%d ", e
->midi_program
.program
);
307 printf("SONG_EV_BACK ");
308 printf("LEN:%lf", e
->back
.len
);
313 printf("SONG_EV_NOTE ");
314 printf("MIDI:%d LEN:%lf VOL:%f",
315 e
->note
.note
, e
->note
.len
, e
->note
.vol
);
318 case SONG_EV_SS_PITCH_STRETCH
:
320 printf("SONG_EV_SS_PITCH_STRETCH ");
321 printf("MIDI:%d LEN:%lf VOL:%f",
322 e
->ss_pitch_stretch
.note
,
323 e
->ss_pitch_stretch
.len
,
324 e
->ss_pitch_stretch
.vol
);
327 case SONG_EV_SS_PRINT_WAVE_TEMPO
:
329 printf("SONG_EV_SS_PRINT_WAVE_TEMPO ");
330 printf("MIDI:%d LEN:%lf",
331 e
->ss_print_wave_tempo
.note
,
332 e
->ss_print_wave_tempo
.len
);
335 case SONG_EV_SONG_INFO
:
337 printf("SONG_EV_SONG_INFO ");
338 printf("AUTHOR:'%s' NAME:'%s'",
345 printf("SONG_EV_EOT ");
349 printf("** Unexpected event type: %d",
361 * song_sort - Sorts the song stream
363 * Sorts the song stream.
367 qsort(song
, n_song_ev
, sizeof(union song_ev
), song_ev_cmp
);
369 if(trace
) song_trace_events();
374 * song_test_measure_boundary - Does a measure boundary check
375 * @ev_time: event time
376 * @num: meter numerator
377 * @den: meter denominator
379 * Does a measure boundary check. Returns 0 if the event time falls
380 * exactly between two measures, or nonzero otherwise.
382 int song_test_measure_boundary(double ev_time
, int num
, int den
, int line
)
386 if((ret
= ((int)(ev_time
* (double) den
)) % num
))
387 printf("Measure boundary check failed in line %d\n", line
);