The song statistics message in ss_song.c (shown when
[ahxm.git] / song.c
blob71a66efdd6edeb0e7e1e7c25a45604f761bac6d9
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 int song_ev_cmp(const void * v1, const void * v2)
107 struct song_ev_generic * e1;
108 struct song_ev_generic * e2;
109 int ret;
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 */
116 if(ret == 0)
117 ret = e1->type - e2->type;
119 /* same time and same event? order by event id */
120 if(ret == 0)
121 ret = e1->event_id - e2->event_id;
123 return(ret);
127 static void song_trace_events(void)
129 union song_ev * e;
130 int n;
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)
145 case SONG_EV_TEMPO:
147 printf("SONG_EV_TEMPO ");
148 printf("TEMPO:%lf", e->tempo.tempo);
149 break;
151 case SONG_EV_METER:
153 printf("SONG_EV_METER ");
154 printf("METER:%d/%d", e->meter.num, e->meter.den);
155 break;
157 case SONG_EV_MEASURE:
159 printf("SONG_EV_MEASURE ");
160 printf("LINE:%d", e->measure.line);
161 break;
163 case SONG_EV_SS_SUSTAIN:
165 printf("SONG_EV_SS_SUSTAIN ");
166 printf("SUSTAIN:%lf", e->ss_sustain.sustain);
167 break;
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);
175 break;
177 case SONG_EV_SS_PORTAMENTO:
179 printf("SONG_EV_SS_PORTAMENTO ");
180 printf("VALUE: %lf", e->ss_portamento.portamento);
181 break;
183 case SONG_EV_SS_CHANNEL:
185 printf("SONG_EV_SS_CHANNEL ");
186 printf("CHANNEL:%d VOL:%lf",
187 e->ss_channel.channel,
188 e->ss_channel.vol);
189 break;
191 case SONG_EV_SS_WAV:
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);
198 break;
200 case SONG_EV_SS_PAT:
202 printf("SONG_EV_SS_PAT ");
203 printf("FILE:'%s'", e->ss_pat.file);
204 break;
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);
211 break;
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);
219 break;
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);
227 break;
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);
235 break;
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,
245 e->ss_eff.phase);
246 break;
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);
254 break;
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,
261 e->ss_eff.phase);
262 break;
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,
269 e->ss_eff.phase);
270 break;
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,
278 e->ss_eff.final);
279 break;
281 case SONG_EV_SS_EFF_REVERB:
283 printf("SONG_EV_SS_EFF_REVERB ");
284 printf("CHANNEL:%d ", e->ss_eff.channel);
285 break;
287 case SONG_EV_SS_EFF_OFF:
289 printf("SONG_EV_SS_EFF_OFF ");
290 printf("CHANNEL:%d ", e->ss_eff.channel);
291 break;
293 case SONG_EV_MIDI_CHANNEL:
295 printf("SONG_EV_MIDI_CHANNEL ");
296 printf("CHANNEL:%d ", e->midi_channel.channel);
297 break;
299 case SONG_EV_MIDI_PROGRAM:
301 printf("SONG_EV_MIDI_PROGRAM ");
302 printf("PROGRAM:%d ", e->midi_program.program);
303 break;
305 case SONG_EV_BACK:
307 printf("SONG_EV_BACK ");
308 printf("LEN:%lf", e->back.len);
309 break;
311 case SONG_EV_NOTE:
313 printf("SONG_EV_NOTE ");
314 printf("MIDI:%d LEN:%lf VOL:%f",
315 e->note.note, e->note.len, e->note.vol);
316 break;
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);
325 break;
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);
333 break;
335 case SONG_EV_SONG_INFO:
337 printf("SONG_EV_SONG_INFO ");
338 printf("AUTHOR:'%s' NAME:'%s'",
339 e->song_info.author,
340 e->song_info.name);
341 break;
343 case SONG_EV_EOT:
345 printf("SONG_EV_EOT ");
346 break;
348 default:
349 printf("** Unexpected event type: %d",
350 e->generic.type);
353 printf("\n");
356 printf("\n");
361 * song_sort - Sorts the song stream
363 * Sorts the song stream.
365 void song_sort(void)
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)
384 int ret;
386 if((ret = ((int)(ev_time * (double) den)) % num))
387 printf("Measure boundary check failed in line %d\n", line);
389 return(ret);