3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2007 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 struct song_ev
*song
= NULL
;
42 /* number of song events */
45 /* number of tracks in song */
46 int n_song_tracks
= 0;
53 * add_event - Adds an event to an event list.
54 * @song: A pointer to an event list (dynamically allocated)
55 * @count: A pointer to an integer (number of elements)
57 * Increments the @song event list to fit @count number of
58 * elements, probably reallocating its memory.
60 * Returns a pointer to the new element.
62 struct song_ev
* add_event(struct song_ev
**song
, int *count
)
66 GROW(*song
, *count
, struct song_ev
);
69 memset(r
, '\0', sizeof(struct song_ev
));
78 * copy_event - Appends an event to an event list.
79 * @song: the event list
80 * @count: a pointer to the number of elements
81 * @e: the event to be copied
83 * Appends the @e event to the @song event list.
85 struct song_ev
* copy_event(struct song_ev
**song
, int *count
, struct song_ev
*e
)
87 struct song_ev
* r
= add_event(song
, count
);
89 memcpy(r
, e
, sizeof(struct song_ev
));
96 * song_clear - Clears the song stream
98 * Clears the song stream.
100 void song_clear(void)
111 static void song_trace_events(void)
116 printf("** GENERIC SONG EVENT DUMP **\n\n");
117 printf("%9s %5s %5s Event and information\n", "Time", "Track", "Ev.ID");
118 printf("---------------------------------------------------------------\n");
120 for (n
= 0, e
= song
; n
< n_song_ev
; n
++, e
++) {
121 printf("%9.4lf %5d %5d ",
122 e
->time
, e
->trk_id
, e
->event_id
);
127 printf("SONG_EV_TEMPO ");
128 printf("TEMPO:%lf", e
->amount
);
133 printf("SONG_EV_METER ");
134 printf("METER:%d/%d", e
->min
, e
->max
);
137 case SONG_EV_MEASURE
:
139 printf("SONG_EV_MEASURE ");
140 printf("LINE:%d", e
->value
);
143 case SONG_EV_SS_SUSTAIN
:
145 printf("SONG_EV_SS_SUSTAIN ");
146 printf("SUSTAIN:%lf", e
->amount
);
149 case SONG_EV_SS_VIBRATO
:
151 printf("SONG_EV_SS_VIBRATO ");
152 printf("DEPTH:%lf FREQ:%lf", e
->depth
, e
->freq
);
155 case SONG_EV_SS_PORTAMENTO
:
157 printf("SONG_EV_SS_PORTAMENTO ");
158 printf("VALUE: %lf", e
->amount
);
161 case SONG_EV_SS_CHANNEL
:
163 printf("SONG_EV_SS_CHANNEL ");
164 printf("CHANNEL:%d VOL:%lf", e
->channel
, e
->vol
);
169 printf("SONG_EV_SS_WAV ");
170 printf("FILE:'%s' BASE:%d MIN:%d MAX:%d START:%lf END:%lf",
178 printf("SONG_EV_SS_PAT ");
179 printf("FILE:'%s'", e
->name
);
182 case SONG_EV_SS_EFF_DELAY
:
184 printf("SONG_EV_SS_EFF_DELAY ");
185 printf("CHANNEL:%d ", e
->channel
);
186 printf("SIZE:%lf ", e
->len
);
189 case SONG_EV_SS_EFF_ECHO
:
191 printf("SONG_EV_SS_EFF_ECHO ");
192 printf("CHANNEL:%d ", e
->channel
);
193 printf("SIZE:%lf ", e
->len
);
194 printf("GAIN:%f ", e
->vol
);
197 case SONG_EV_SS_EFF_COMB
:
199 printf("SONG_EV_SS_EFF_COMB ");
200 printf("CHANNEL:%d ", e
->channel
);
201 printf("SIZE:%lf ", e
->len
);
202 printf("GAIN:%f ", e
->vol
);
205 case SONG_EV_SS_EFF_ALLPASS
:
207 printf("SONG_EV_SS_EFF_ALLPASS ");
208 printf("CHANNEL:%d ", e
->channel
);
209 printf("SIZE:%lf ", e
->len
);
210 printf("GAIN:%f ", e
->vol
);
213 case SONG_EV_SS_EFF_FLANGER
:
215 printf("SONG_EV_SS_EFF_FLANGER ");
216 printf("CHANNEL:%d ", e
->channel
);
217 printf("SIZE:%lf ", e
->len
);
218 printf("GAIN:%f ", e
->vol
);
219 printf("DEPTH:%lf ", e
->depth
);
220 printf("FREQ:%lf PHASE:%lf", e
->freq
, e
->phase
);
223 case SONG_EV_SS_EFF_WOBBLE
:
225 printf("SONG_EV_SS_EFF_WOBBLE ");
226 printf("CHANNEL:%d ", e
->channel
);
227 printf("FREQ:%lf PHASE:%lf GAIN:%lf", e
->freq
,
231 case SONG_EV_SS_EFF_SQWOBBLE
:
233 printf("SONG_EV_SS_EFF_SQWOBBLE ");
234 printf("CHANNEL:%d ", e
->channel
);
235 printf("FREQ:%lf PHASE:%lf", e
->freq
, e
->phase
);
238 case SONG_EV_SS_EFF_HFWOBBLE
:
240 printf("SONG_EV_SS_EFF_HFWOBBLE ");
241 printf("CHANNEL:%d ", e
->channel
);
242 printf("FREQ:%lf PHASE:%lf", e
->freq
, e
->phase
);
245 case SONG_EV_SS_EFF_FADER
:
247 printf("SONG_EV_SS_EFF_FADER ");
248 printf("CHANNEL:%d ", e
->channel
);
249 printf("SIZE:%lf ", e
->len
);
250 printf("INITIAL:%f FINAL:%f", e
->initial
, e
->final
);
253 case SONG_EV_SS_EFF_REVERB
:
255 printf("SONG_EV_SS_EFF_REVERB ");
256 printf("CHANNEL:%d ", e
->channel
);
259 case SONG_EV_SS_EFF_FOLDBACK
:
261 printf("SONG_EV_SS_EFF_FOLDBACK ");
262 printf("CHANNEL:%d THRESHOLD:%f", e
->channel
, e
->vol
);
265 case SONG_EV_SS_EFF_ATAN
:
267 printf("SONG_EV_SS_EFF_ATAN ");
268 printf("CHANNEL:%d GAIN:%f", e
->channel
, e
->vol
);
271 case SONG_EV_SS_EFF_DISTORT
:
273 printf("SONG_EV_SS_EFF_DISTORT ");
274 printf("CHANNEL:%d GAIN:%f", e
->channel
, e
->vol
);
277 case SONG_EV_SS_EFF_OVERDRIVE
:
279 printf("SONG_EV_SS_EFF_OVERDRIVE ");
280 printf("CHANNEL:%d GAIN:%f", e
->channel
, e
->vol
);
283 case SONG_EV_SS_EFF_OFF
:
285 printf("SONG_EV_SS_EFF_OFF ");
286 printf("CHANNEL:%d ", e
->channel
);
289 case SONG_EV_MIDI_CHANNEL
:
291 printf("SONG_EV_MIDI_CHANNEL ");
292 printf("CHANNEL:%d ", e
->channel
);
295 case SONG_EV_MIDI_PROGRAM
:
297 printf("SONG_EV_MIDI_PROGRAM ");
298 printf("PROGRAM:%d ", e
->value
);
303 printf("SONG_EV_BACK ");
304 printf("LEN:%lf", e
->len
);
309 printf("SONG_EV_NOTE ");
310 printf("MIDI:%d LEN:%lf VOL:%f", e
->value
, e
->len
, e
->vol
);
313 case SONG_EV_SS_PITCH_STRETCH
:
315 printf("SONG_EV_SS_PITCH_STRETCH ");
316 printf("MIDI:%d LEN:%lf VOL:%f", e
->value
, e
->len
, e
->vol
);
319 case SONG_EV_SS_PRINT_WAVE_TEMPO
:
321 printf("SONG_EV_SS_PRINT_WAVE_TEMPO ");
322 printf("MIDI:%d LEN:%lf", e
->value
, e
->len
);
325 case SONG_EV_SONG_INFO
:
327 printf("SONG_EV_SONG_INFO ");
328 printf("AUTHOR:'%s' NAME:'%s'", e
->author
, e
->name
);
333 printf("SONG_EV_EOT ");
338 printf("SONG_EV_NOP ");
342 printf("** Unexpected event type: %d", e
->type
);
352 static int time_type_eventid_cmp(const void *v1
, const void *v2
)
353 /* sorts events by time, then type, then event_id */
359 e1
= (struct song_ev
*) v1
;
360 e2
= (struct song_ev
*) v2
;
362 ret
= (int) ((e1
->time
* 10000.0) - (e2
->time
* 10000.0));
364 /* same time? order by type of event */
366 ret
= e1
->type
- e2
->type
;
368 /* same time and same event? order by event id */
370 ret
= e1
->event_id
- e2
->event_id
;
376 static void count_tracks(void)
377 /* sets n_song_tracks */
383 for(n
= 0; n
< n_song_ev
; n
++) {
388 if (n_song_tracks
< e
->trk_id
+ 1)
389 n_song_tracks
= e
->trk_id
+ 1;
394 static void add_eot_events(void)
395 /* travels all events and adds EOT ones */
399 for (n
= 0; n
< n_song_tracks
; n
++) {
403 for (m
= 0; m
< n_song_ev
; m
++) {
406 /* if this is the track we're looking for
407 and this event time is bigger, store it */
408 if (e
->trk_id
== n
&& e
->time
> t
)
412 /* now t has the biggest time; add an EOT event with t */
413 e
= add_event(&song
, &n_song_ev
);
415 e
->type
= SONG_EV_EOT
;
418 e
->event_id
= n_song_ev
- 1;
424 * mute_tracks - Converts track events to NOPs.
425 * @trk_id: Track to be muted. If negative, all buts this will be muted
427 * Converts events to NOP. If @trk_id is positive, all events for that
428 * track id will be converted to NOP; if it's negative, all events BUT
429 * for the ones in that track will be muted.
431 void mute_tracks(int trk_id
)
435 for (n
= 0; n
< n_song_ev
; n
++) {
440 /* try only 'real' tracks */
441 if (e
->trk_id
>= 0) {
443 if (e
->trk_id
== trk_id
)
444 e
->type
= SONG_EV_NOP
;
447 if (e
->trk_id
!= -trk_id
)
448 e
->type
= SONG_EV_NOP
;
456 * song_sort - Sorts the song stream
458 * Sorts the song stream.
465 /* sorts by time, type and event_id */
466 qsort(song
, n_song_ev
, sizeof(struct song_ev
), time_type_eventid_cmp
);
474 * song_test_measure_boundary - Does a measure boundary check
475 * @ev_time: event time
476 * @num: meter numerator
477 * @den: meter denominator
479 * Does a measure boundary check. Returns 0 if the event time falls
480 * exactly between two measures, or nonzero otherwise.
482 int song_test_measure_boundary(double ev_time
, int num
, int den
, int line
)
486 if ((ret
= ((int) (ev_time
* (double) den
)) % num
))
487 printf("Measure boundary check failed in line %d\n", line
);