Line number is included in measure boundary check events, but due to
[ahxm.git] / ss_song.c
blob1443f685d80c88af4fbcabe48893bf1813f561a5
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 Angel Ortega <angel@triptico.com>
6 ss_song.c - Software synth 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 struct ss_ev_generic
41 song_ev_type type; /* event type */
42 int frame; /* frame number (time) */
43 int trk_id; /* track id */
46 struct ss_ev_note_off
48 song_ev_type type; /* SONG_EV_NOTE_OFF */
49 int frame;
50 int trk_id;
51 int note_id; /* note id */
54 struct ss_ev_note_on
56 song_ev_type type; /* SONG_EV_NOTE_ON */
57 int frame;
58 int trk_id;
59 int note_id; /* note id */
60 int note; /* MIDI-like note */
61 float vol; /* volume */
64 struct ss_ev_ss_sustain
66 song_ev_type type; /* SONG_EV_SS_SUSTAIN */
67 int frame;
68 int trk_id;
69 double sustain; /* sustain time (in frames) */
72 struct ss_ev_ss_vibrato
74 song_ev_type type; /* SONG_EV_SS_VIBRATO */
75 int frame;
76 int trk_id;
77 double vib_depth; /* vibrato depth (in msecs) */
78 double vib_freq; /* vibrato frequency (in Hzs) */
81 struct ss_ev_ss_channel
83 song_ev_type type; /* SONG_EV_SS_CHANNEL */
84 int frame;
85 int trk_id;
86 int channel; /* channel */
87 float vol; /* volume */
90 struct ss_ev_ss_wav
92 song_ev_type type; /* SONG_EV_SS_WAV */
93 int frame;
94 int trk_id;
95 char * file; /* path to .wav file */
96 int base; /* MIDI-like base note */
97 int min; /* MIDI-like minimum note */
98 int max; /* MIDI-like maximum note */
99 double loop_start; /* loop start */
100 double loop_end; /* loop end */
103 struct ss_ev_ss_pat
105 song_ev_type type; /* SONG_EV_SS_PAT */
106 int frame;
107 int trk_id;
108 char * file; /* path to .pat file */
111 struct ss_ev_tempo
113 song_ev_type type; /* SONG_EV_TEMPO */
114 int frame;
115 int trk_id;
116 double tempo; /* tempo in bmp */
119 struct ss_ev_pitch_stretch
121 song_ev_type type; /* SONG_EV_SS_PITCH_STRETCH */
122 int frame;
123 int trk_id;
124 int note_id; /* note id */
125 int note; /* MIDI-like note (to find the wave) */
126 double len; /* note length (1: whole note) */
127 float vol; /* note volume (1: full volume) */
130 struct ss_ev_ss_eff
132 song_ev_type type; /* effect type */
133 int frame;
134 int trk_id;
135 int channel; /* channel */
136 double size; /* size of effect */
137 float gain; /* gain */
138 double depth; /* depth */
139 double freq; /* freq */
140 double phase; /* phase */
141 float initial; /* initial vol */
142 float final; /* final vol */
145 union ss_ev
147 struct ss_ev_generic generic;
148 struct ss_ev_note_on note_on;
149 struct ss_ev_note_off note_off;
150 struct ss_ev_ss_sustain ss_sustain;
151 struct ss_ev_ss_vibrato ss_vibrato;
152 struct ss_ev_ss_channel ss_channel;
153 struct ss_ev_ss_wav ss_wav;
154 struct ss_ev_ss_pat ss_pat;
155 struct ss_ev_ss_eff ss_eff;
156 struct ss_ev_tempo tempo;
157 struct ss_ev_pitch_stretch ss_pitch_stretch;
160 /* the softsynth song stream */
162 static union ss_ev * ss_song=NULL;
163 static int n_ss_ev=0;
166 /* the instruments */
168 #define SONG_INS_NUM 256
169 struct ss_ins song_ins[SONG_INS_NUM];
172 /*******************
173 Code
174 ********************/
176 static void add_ss_ev(union ss_ev * e)
177 /* adds a softsynth song event */
179 /* reallocs */
180 ss_song=(union ss_ev *)realloc(ss_song,
181 (n_ss_ev + 1) * sizeof(union ss_ev));
183 /* store */
184 memcpy(&ss_song[n_ss_ev], e, sizeof(union ss_ev));
186 n_ss_ev++;
190 static int ss_ev_cmp(const void * v1, const void * v2)
191 /* softsynth song event compare function for qsort() */
193 struct ss_ev_generic * e1;
194 struct ss_ev_generic * e2;
196 e1=(struct ss_ev_generic *)v1; e2=(struct ss_ev_generic *)v2;
198 if(e1->frame == e2->frame)
199 return(e1->type - e2->type);
201 return(e1->frame - e2->frame);
205 static int ss_song_convert_events(void)
206 /* converts generic song_ev events to softsynth events */
208 int note_id=1;
209 union song_ev * e;
210 union ss_ev sse;
211 int frame, frame_ac;
212 double fpw, time_ac;
213 int num, den;
214 int n;
215 int b_track=-1;
217 /* resets the ss stream */
218 if(ss_song != NULL)
220 free(ss_song);
221 ss_song=NULL;
224 n_ss_ev=0;
226 /* sorts the song */
227 song_sort();
229 fpw=0;
230 frame=frame_ac=0;
231 time_ac=0;
232 num=den=4;
234 /* travels the song events generating softsynth song events */
235 for(n=0;n < n_song_ev;n++)
237 /* gets the song event */
238 e=&song[n];
240 /* calculates the frame */
241 frame=((e->generic.time - time_ac) * fpw) + frame_ac;
243 /* generic event data */
244 sse.generic.type=e->generic.type;
245 sse.generic.frame=frame;
246 sse.generic.trk_id=e->generic.trk_id;
248 /* account the biggest track seen */
249 if(b_track < e->generic.trk_id) b_track=e->generic.trk_id;
251 switch(e->generic.type)
253 case SONG_EV_TEMPO:
255 /* updates accumulations */
256 frame_ac += frame;
257 time_ac += e->generic.time;
259 /* calculates frames-per-whole based on new tempo */
260 fpw=(double) ss_frequency * 60.0;
261 fpw /= e->tempo.tempo / 4.0;
263 /* adds an event */
264 sse.tempo.tempo=e->tempo.tempo;
265 add_ss_ev(&sse);
267 break;
269 case SONG_EV_METER:
271 /* just store the values */
272 num=e->meter.num;
273 den=e->meter.den;
275 break;
277 case SONG_EV_MEASURE:
279 printf("measure boundary check (must be 0): %d (line %d)\n",
280 ((int) (e->measure.time * den)) % num,
281 e->measure.line);
283 break;
285 case SONG_EV_NOTE:
287 /* convert to note on / off pairs */
289 sse.note_on.type=SONG_EV_NOTE_ON;
290 sse.note_on.note_id=note_id++;
291 sse.note_on.note=e->note.note;
292 sse.note_on.vol=e->note.vol;
294 add_ss_ev(&sse);
296 frame += (int)(e->note.len * fpw);
298 sse.note_off.type=SONG_EV_NOTE_OFF;
299 sse.note_off.frame=frame;
301 add_ss_ev(&sse);
302 break;
304 case SONG_EV_SS_PITCH_STRETCH:
306 sse.ss_pitch_stretch.note_id=note_id++;
307 sse.ss_pitch_stretch.note=e->ss_pitch_stretch.note;
308 sse.ss_pitch_stretch.len=e->ss_pitch_stretch.len;
309 sse.ss_pitch_stretch.vol=e->ss_pitch_stretch.vol;
311 add_ss_ev(&sse);
313 frame += (int)(e->ss_pitch_stretch.len * fpw);
315 sse.note_off.type=SONG_EV_NOTE_OFF;
316 sse.note_off.frame=frame;
318 add_ss_ev(&sse);
319 break;
321 case SONG_EV_SS_WAV:
323 sse.ss_wav.file=e->ss_wav.file;
324 sse.ss_wav.base=e->ss_wav.base;
325 sse.ss_wav.min=e->ss_wav.min;
326 sse.ss_wav.max=e->ss_wav.max;
327 sse.ss_wav.loop_start=e->ss_wav.loop_start;
328 sse.ss_wav.loop_end=e->ss_wav.loop_end;
330 add_ss_ev(&sse);
331 break;
333 case SONG_EV_SS_PAT:
335 sse.ss_pat.file=e->ss_pat.file;
337 add_ss_ev(&sse);
338 break;
340 case SONG_EV_SS_SUSTAIN:
342 sse.ss_sustain.sustain=e->ss_sustain.sustain;
344 add_ss_ev(&sse);
345 break;
347 case SONG_EV_SS_VIBRATO:
349 sse.ss_vibrato.vib_depth=e->ss_vibrato.vib_depth;
350 sse.ss_vibrato.vib_freq=e->ss_vibrato.vib_freq;
352 add_ss_ev(&sse);
353 break;
355 case SONG_EV_SS_CHANNEL:
357 sse.ss_channel.channel=e->ss_channel.channel;
358 sse.ss_channel.vol=e->ss_channel.vol;
360 add_ss_ev(&sse);
361 break;
363 case SONG_EV_SS_EFF_DELAY:
364 case SONG_EV_SS_EFF_ECHO:
365 case SONG_EV_SS_EFF_COMB:
366 case SONG_EV_SS_EFF_ALLPASS:
367 case SONG_EV_SS_EFF_FLANGER:
368 case SONG_EV_SS_EFF_WOBBLE:
369 case SONG_EV_SS_EFF_SQWOBBLE:
370 case SONG_EV_SS_EFF_FADER:
371 case SONG_EV_SS_EFF_REVERB:
372 case SONG_EV_SS_EFF_OFF:
374 sse.ss_eff.channel=e->ss_eff.channel;
375 sse.ss_eff.size=e->ss_eff.size;
376 sse.ss_eff.gain=e->ss_eff.gain;
377 sse.ss_eff.depth=e->ss_eff.depth;
378 sse.ss_eff.freq=e->ss_eff.freq;
379 sse.ss_eff.phase=e->ss_eff.phase;
380 sse.ss_eff.initial=e->ss_eff.initial;
381 sse.ss_eff.final=e->ss_eff.final;
383 add_ss_ev(&sse);
384 break;
386 case SONG_EV_MIDI_CHANNEL:
387 case SONG_EV_MIDI_PROGRAM:
389 /* ignored */
390 break;
392 case SONG_EV_NOTE_ON:
393 case SONG_EV_NOTE_OFF:
394 case SONG_EV_END:
396 /* never found in generic song streams */
397 break;
401 /* generates an end of event mark, a time after the last one */
402 sse.generic.type=SONG_EV_END;
403 sse.generic.frame=frame + ss_frequency;
404 add_ss_ev(&sse);
406 /* finally sort */
407 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
409 /* return the number of tracks */
410 return(b_track + 1);
414 int ss_song_render(void)
416 union ss_ev * e;
417 int frame;
418 int go;
419 int n;
420 float output[SS_MAX_CHANNELS];
421 int n_tracks;
422 struct ss_ins * i;
423 double tempo=120.0;
425 /* convert the song to ss events */
426 n_tracks=ss_song_convert_events();
428 frame=0;
429 go=1;
430 e=ss_song;
432 /* init the instruments */
433 for(n=0;n < n_tracks;n++)
434 ss_ins_init(&song_ins[n]);
436 /* loop the events */
437 while(go)
439 /* process all events for this exact frame */
440 while(e->generic.frame == frame)
442 /* take the instrument */
443 if(e->generic.trk_id == -1)
444 i=NULL;
445 else
446 i=&song_ins[e->generic.trk_id];
448 switch(e->generic.type)
450 case SONG_EV_NOTE_ON:
452 ss_ins_note_on(i, e->note_on.note,
453 e->note_on.vol, e->note_on.note_id);
455 break;
457 case SONG_EV_NOTE_OFF:
459 ss_ins_note_off(i, e->note_off.note_id);
461 break;
463 case SONG_EV_SS_SUSTAIN:
465 ss_ins_set_sustain(i, e->ss_sustain.sustain);
467 break;
469 case SONG_EV_SS_VIBRATO:
471 ss_ins_set_vibrato(i, e->ss_vibrato.vib_depth,
472 e->ss_vibrato.vib_freq);
474 break;
476 case SONG_EV_SS_CHANNEL:
478 ss_ins_set_channel(i, e->ss_channel.channel,
479 e->ss_channel.vol);
481 break;
483 case SONG_EV_SS_WAV:
486 struct ss_wave * w;
488 w=ss_load_wav_file(e->ss_wav.file,
489 ss_note_frequency(e->ss_wav.base),
490 ss_note_frequency(e->ss_wav.min),
491 ss_note_frequency(e->ss_wav.max),
492 e->ss_wav.loop_start,
493 e->ss_wav.loop_end);
495 ss_ins_add_layer(i, w);
498 break;
500 case SONG_EV_SS_PAT:
502 ss_load_pat_file(i, e->ss_pat.file);
503 break;
505 case SONG_EV_SS_EFF_DELAY:
507 ss_eff_delay(&i->effs[e->ss_eff.channel],
508 e->ss_eff.size);
509 break;
511 case SONG_EV_SS_EFF_ECHO:
513 ss_eff_echo(&i->effs[e->ss_eff.channel],
514 e->ss_eff.size, e->ss_eff.gain);
515 break;
517 case SONG_EV_SS_EFF_COMB:
519 ss_eff_comb(&i->effs[e->ss_eff.channel],
520 e->ss_eff.size, e->ss_eff.gain);
521 break;
523 case SONG_EV_SS_EFF_ALLPASS:
525 ss_eff_allpass(&i->effs[e->ss_eff.channel],
526 e->ss_eff.size, e->ss_eff.gain);
527 break;
529 case SONG_EV_SS_EFF_FLANGER:
531 ss_eff_flanger(&i->effs[e->ss_eff.channel],
532 e->ss_eff.size, e->ss_eff.gain,
533 e->ss_eff.depth, e->ss_eff.freq,
534 e->ss_eff.phase);
535 break;
537 case SONG_EV_SS_EFF_WOBBLE:
539 ss_eff_wobble(&i->effs[e->ss_eff.channel],
540 e->ss_eff.freq, e->ss_eff.phase);
542 break;
544 case SONG_EV_SS_EFF_SQWOBBLE:
546 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
547 e->ss_eff.freq, e->ss_eff.phase);
549 break;
551 case SONG_EV_SS_EFF_FADER:
553 ss_eff_fader(&i->effs[e->ss_eff.channel],
554 e->ss_eff.size, e->ss_eff.initial,
555 e->ss_eff.final);
556 break;
558 case SONG_EV_SS_EFF_REVERB:
560 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
561 break;
563 case SONG_EV_SS_EFF_OFF:
565 ss_eff_off(&i->effs[e->ss_eff.channel]);
566 break;
568 case SONG_EV_TEMPO:
570 /* just store the last tempo */
571 tempo=e->tempo.tempo;
572 break;
574 case SONG_EV_SS_PITCH_STRETCH:
577 int n=0;
578 struct ss_wave * w;
579 double freq;
581 /* find the wave */
582 freq=ss_note_frequency(e->ss_pitch_stretch.note);
583 w=ss_ins_find_layer(i, freq, &n);
585 /* calculate optimal frequency */
586 freq=ss_pitch_from_tempo(w, tempo, e->ss_pitch_stretch.len);
588 /* play the note */
589 ss_ins_play(i, freq, e->ss_pitch_stretch.vol,
590 e->ss_pitch_stretch.note_id, w);
593 break;
595 case SONG_EV_END:
597 go=0;
598 break;
600 case SONG_EV_MIDI_CHANNEL:
601 case SONG_EV_MIDI_PROGRAM:
602 case SONG_EV_NOTE:
603 case SONG_EV_METER:
604 case SONG_EV_MEASURE:
606 /* never found in ss song streams */
607 break;
610 /* next event */
611 e++;
614 /* reset frame samples */
615 ss_output_init_frame(output);
617 /* generate output from all instruments */
618 for(n=0;n < n_tracks;n++)
619 ss_ins_frame(&song_ins[n], output);
621 /* dump to sampling driver */
622 ss_output_write(output);
624 /* next frame */
625 frame++;
628 return(0);