Cached files are written to stdout if debugging is set.
[ahxm.git] / ss_song.c
blobb1b078d838a1df7664f33c695f58703f189000b2
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 sample_t 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 sample_t 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 sample_t vol; /* note volume (1: full volume) */
130 struct ss_ev_print_wave_tempo
132 song_ev_type type; /* SONG_EV_SS_PRINT_WAVE_TEMPO */
133 int frame;
134 int trk_id;
135 int note_id; /* note id */
136 int note; /* MIDI-like note (to find the wave) */
137 double len; /* note length (1: whole note) */
140 struct ss_ev_ss_eff
142 song_ev_type type; /* effect type */
143 int frame;
144 int trk_id;
145 int channel; /* channel */
146 double size; /* size of effect */
147 sample_t gain; /* gain */
148 double depth; /* depth */
149 double freq; /* freq */
150 double phase; /* phase */
151 sample_t initial; /* initial vol */
152 sample_t final; /* final vol */
155 union ss_ev
157 struct ss_ev_generic generic;
158 struct ss_ev_note_on note_on;
159 struct ss_ev_note_off note_off;
160 struct ss_ev_ss_sustain ss_sustain;
161 struct ss_ev_ss_vibrato ss_vibrato;
162 struct ss_ev_ss_channel ss_channel;
163 struct ss_ev_ss_wav ss_wav;
164 struct ss_ev_ss_pat ss_pat;
165 struct ss_ev_ss_eff ss_eff;
166 struct ss_ev_tempo tempo;
167 struct ss_ev_pitch_stretch ss_pitch_stretch;
168 struct ss_ev_print_wave_tempo ss_print_wave_tempo;
171 /* the softsynth song stream */
173 static union ss_ev * ss_song=NULL;
174 static int n_ss_ev=0;
177 /* the instruments */
179 struct ss_ins ss_song_ins[SS_MAX_INSTRUMENTS];
182 /*******************
183 Code
184 ********************/
186 static void add_ss_ev(union ss_ev * e)
187 /* adds a softsynth song event */
189 /* reallocs */
190 ss_song=(union ss_ev *)realloc(ss_song,
191 (n_ss_ev + 1) * sizeof(union ss_ev));
193 /* store */
194 memcpy(&ss_song[n_ss_ev], e, sizeof(union ss_ev));
196 n_ss_ev++;
200 static int ss_ev_cmp(const void * v1, const void * v2)
201 /* softsynth song event compare function for qsort() */
203 struct ss_ev_generic * e1;
204 struct ss_ev_generic * e2;
206 e1=(struct ss_ev_generic *)v1; e2=(struct ss_ev_generic *)v2;
208 if(e1->frame == e2->frame)
209 return(e1->type - e2->type);
211 return(e1->frame - e2->frame);
215 static int ss_song_convert_events(void)
216 /* converts generic song_ev events to softsynth events */
218 int note_id=1;
219 union song_ev * e;
220 union ss_ev sse;
221 int frame, frame_ac, f_frame;
222 double fpw, time_ac, time_ac_m;
223 int num, den;
224 int n;
225 int b_track=-1;
227 /* resets the ss stream */
228 if(ss_song != NULL)
230 free(ss_song);
231 ss_song=NULL;
234 n_ss_ev=0;
236 /* sorts the song */
237 song_sort();
239 fpw=0;
240 frame=frame_ac=f_frame=0;
241 time_ac=time_ac_m=0;
242 num=den=4;
244 /* travels the song events generating softsynth song events */
245 for(n=0;n < n_song_ev;n++)
247 /* gets the song event */
248 e=&song[n];
250 /* calculates the frame */
251 frame=((e->generic.time - time_ac) * fpw) + frame_ac;
253 /* generic event data */
254 sse.generic.type=e->generic.type;
255 sse.generic.frame=frame;
256 sse.generic.trk_id=e->generic.trk_id;
258 /* account the biggest track seen */
259 if(b_track < e->generic.trk_id) b_track=e->generic.trk_id;
261 switch(e->generic.type)
263 case SONG_EV_TEMPO:
265 /* updates accumulations */
266 frame_ac += frame;
267 time_ac += e->generic.time;
269 /* calculates frames-per-whole based on new tempo */
270 fpw=(double) ss_frequency * 60.0;
271 fpw /= e->tempo.tempo / 4.0;
273 /* adds an event */
274 sse.tempo.tempo=e->tempo.tempo;
275 add_ss_ev(&sse);
277 break;
279 case SONG_EV_METER:
281 /* just store the values */
282 num=e->meter.num;
283 den=e->meter.den;
284 time_ac_m=e->meter.time;
286 break;
288 case SONG_EV_MEASURE:
290 song_test_measure_boundary(e->measure.time - time_ac_m,
291 num, den, e->measure.line);
292 break;
294 case SONG_EV_NOTE:
296 /* convert to note on / off pairs */
298 sse.note_on.type=SONG_EV_NOTE_ON;
299 sse.note_on.note_id=note_id++;
300 sse.note_on.note=e->note.note;
301 sse.note_on.vol=e->note.vol;
303 add_ss_ev(&sse);
305 frame += (int)(e->note.len * fpw);
307 sse.note_off.type=SONG_EV_NOTE_OFF;
308 sse.note_off.frame=frame;
310 add_ss_ev(&sse);
311 break;
313 case SONG_EV_SS_PITCH_STRETCH:
315 sse.ss_pitch_stretch.note_id=note_id++;
316 sse.ss_pitch_stretch.note=e->ss_pitch_stretch.note;
317 sse.ss_pitch_stretch.len=e->ss_pitch_stretch.len;
318 sse.ss_pitch_stretch.vol=e->ss_pitch_stretch.vol;
320 add_ss_ev(&sse);
322 frame += (int)(e->ss_pitch_stretch.len * fpw);
324 sse.note_off.type=SONG_EV_NOTE_OFF;
325 sse.note_off.frame=frame;
327 add_ss_ev(&sse);
328 break;
330 case SONG_EV_SS_PRINT_WAVE_TEMPO:
332 sse.ss_print_wave_tempo.note=e->ss_print_wave_tempo.note;
333 sse.ss_print_wave_tempo.len=e->ss_print_wave_tempo.len;
335 add_ss_ev(&sse);
336 break;
338 case SONG_EV_SS_WAV:
340 sse.ss_wav.file=e->ss_wav.file;
341 sse.ss_wav.base=e->ss_wav.base;
342 sse.ss_wav.min=e->ss_wav.min;
343 sse.ss_wav.max=e->ss_wav.max;
344 sse.ss_wav.loop_start=e->ss_wav.loop_start;
345 sse.ss_wav.loop_end=e->ss_wav.loop_end;
347 add_ss_ev(&sse);
348 break;
350 case SONG_EV_SS_PAT:
352 sse.ss_pat.file=e->ss_pat.file;
354 add_ss_ev(&sse);
355 break;
357 case SONG_EV_SS_SUSTAIN:
359 sse.ss_sustain.sustain=e->ss_sustain.sustain;
361 add_ss_ev(&sse);
362 break;
364 case SONG_EV_SS_VIBRATO:
366 sse.ss_vibrato.vib_depth=e->ss_vibrato.vib_depth;
367 sse.ss_vibrato.vib_freq=e->ss_vibrato.vib_freq;
369 add_ss_ev(&sse);
370 break;
372 case SONG_EV_SS_CHANNEL:
374 sse.ss_channel.channel=e->ss_channel.channel;
375 sse.ss_channel.vol=e->ss_channel.vol;
377 add_ss_ev(&sse);
378 break;
380 case SONG_EV_SS_EFF_DELAY:
381 case SONG_EV_SS_EFF_ECHO:
382 case SONG_EV_SS_EFF_COMB:
383 case SONG_EV_SS_EFF_ALLPASS:
384 case SONG_EV_SS_EFF_FLANGER:
385 case SONG_EV_SS_EFF_WOBBLE:
386 case SONG_EV_SS_EFF_SQWOBBLE:
387 case SONG_EV_SS_EFF_FADER:
388 case SONG_EV_SS_EFF_REVERB:
389 case SONG_EV_SS_EFF_OFF:
391 sse.ss_eff.channel=e->ss_eff.channel;
392 sse.ss_eff.size=e->ss_eff.size;
393 sse.ss_eff.gain=e->ss_eff.gain;
394 sse.ss_eff.depth=e->ss_eff.depth;
395 sse.ss_eff.freq=e->ss_eff.freq;
396 sse.ss_eff.phase=e->ss_eff.phase;
397 sse.ss_eff.initial=e->ss_eff.initial;
398 sse.ss_eff.final=e->ss_eff.final;
400 add_ss_ev(&sse);
401 break;
403 case SONG_EV_MIDI_CHANNEL:
404 case SONG_EV_MIDI_PROGRAM:
406 /* ignored */
407 break;
409 case SONG_EV_NOTE_ON:
410 case SONG_EV_NOTE_OFF:
411 case SONG_EV_END:
413 /* never found in generic song streams */
414 break;
417 /* store the further frame seen */
418 if(f_frame < frame) f_frame=frame;
421 /* generates an end of event mark, a time after the last one */
422 sse.generic.type=SONG_EV_END;
423 sse.generic.frame=f_frame + ss_frequency;
424 add_ss_ev(&sse);
426 /* finally sort */
427 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
429 /* return the number of tracks */
430 return(b_track + 1);
434 int ss_song_render(int skip_secs)
436 union ss_ev * e;
437 int frame;
438 int go;
439 int n;
440 sample_t output[SS_MAX_CHANNELS];
441 int n_tracks;
442 struct ss_ins * i;
443 double tempo=120.0;
444 int skip_frames;
445 struct ss_wave * w;
446 double freq;
448 /* convert the song to ss events */
449 n_tracks=ss_song_convert_events();
451 frame=0;
452 go=1;
453 e=ss_song;
455 /* init the instruments */
456 for(n=0;n < n_tracks;n++)
457 ss_ins_init(&ss_song_ins[n]);
459 /* calculate the frame to start playing */
460 skip_frames=skip_secs * ss_frequency;
462 /* loop the events */
463 while(go)
465 /* process all events for this exact frame */
466 while(e->generic.frame == frame)
468 /* take the instrument */
469 if(e->generic.trk_id == -1)
470 i=NULL;
471 else
472 i=&ss_song_ins[e->generic.trk_id];
474 switch(e->generic.type)
476 case SONG_EV_NOTE_ON:
478 ss_ins_note_on(i, e->note_on.note,
479 e->note_on.vol, e->note_on.note_id);
481 break;
483 case SONG_EV_NOTE_OFF:
485 ss_ins_note_off(i, e->note_off.note_id);
487 break;
489 case SONG_EV_SS_SUSTAIN:
491 ss_ins_set_sustain(i, e->ss_sustain.sustain);
493 break;
495 case SONG_EV_SS_VIBRATO:
497 ss_ins_set_vibrato(i, e->ss_vibrato.vib_depth,
498 e->ss_vibrato.vib_freq);
500 break;
502 case SONG_EV_SS_CHANNEL:
504 ss_ins_set_channel(i, e->ss_channel.channel,
505 e->ss_channel.vol);
507 break;
509 case SONG_EV_SS_WAV:
511 w=ss_load_wave_file(e->ss_wav.file,
512 ss_note_frequency(e->ss_wav.base),
513 ss_note_frequency(e->ss_wav.min),
514 ss_note_frequency(e->ss_wav.max),
515 e->ss_wav.loop_start, e->ss_wav.loop_end);
517 /* fail if can't open wav */
518 if(w == NULL)
520 printf("Can't load wav '%s'\n", e->ss_wav.file);
521 go=0;
523 else
524 ss_ins_add_layer(i, w);
526 break;
528 case SONG_EV_SS_PAT:
530 if(ss_load_pat_file(i, e->ss_pat.file) < 0)
532 printf("Can't load pat '%s'\n", e->ss_pat.file);
533 go=0;
536 break;
538 case SONG_EV_SS_EFF_DELAY:
540 ss_eff_delay(&i->effs[e->ss_eff.channel],
541 e->ss_eff.size);
542 break;
544 case SONG_EV_SS_EFF_ECHO:
546 ss_eff_echo(&i->effs[e->ss_eff.channel],
547 e->ss_eff.size, e->ss_eff.gain);
548 break;
550 case SONG_EV_SS_EFF_COMB:
552 ss_eff_comb(&i->effs[e->ss_eff.channel],
553 e->ss_eff.size, e->ss_eff.gain);
554 break;
556 case SONG_EV_SS_EFF_ALLPASS:
558 ss_eff_allpass(&i->effs[e->ss_eff.channel],
559 e->ss_eff.size, e->ss_eff.gain);
560 break;
562 case SONG_EV_SS_EFF_FLANGER:
564 ss_eff_flanger(&i->effs[e->ss_eff.channel],
565 e->ss_eff.size, e->ss_eff.gain,
566 e->ss_eff.depth, e->ss_eff.freq,
567 e->ss_eff.phase);
568 break;
570 case SONG_EV_SS_EFF_WOBBLE:
572 ss_eff_wobble(&i->effs[e->ss_eff.channel],
573 e->ss_eff.freq, e->ss_eff.phase);
575 break;
577 case SONG_EV_SS_EFF_SQWOBBLE:
579 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
580 e->ss_eff.freq, e->ss_eff.phase);
582 break;
584 case SONG_EV_SS_EFF_FADER:
586 ss_eff_fader(&i->effs[e->ss_eff.channel],
587 e->ss_eff.size, e->ss_eff.initial,
588 e->ss_eff.final);
589 break;
591 case SONG_EV_SS_EFF_REVERB:
593 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
594 break;
596 case SONG_EV_SS_EFF_OFF:
598 ss_eff_off(&i->effs[e->ss_eff.channel]);
599 break;
601 case SONG_EV_TEMPO:
603 /* just store the last tempo */
604 tempo=e->tempo.tempo;
605 break;
607 case SONG_EV_SS_PITCH_STRETCH:
609 /* find the wave */
610 freq=ss_note_frequency(e->ss_pitch_stretch.note);
611 w=ss_ins_find_layer(i, freq, NULL);
613 /* calculate optimal frequency */
614 freq=ss_pitch_from_tempo(w, tempo,
615 e->ss_pitch_stretch.len);
617 /* play the note */
618 ss_ins_play(i, freq, e->ss_pitch_stretch.vol,
619 e->ss_pitch_stretch.note_id, w);
621 break;
623 case SONG_EV_SS_PRINT_WAVE_TEMPO:
625 /* find the wave */
626 freq=ss_note_frequency(e->ss_print_wave_tempo.note);
627 w=ss_ins_find_layer(i, freq, NULL);
629 /* print the optimal tempo */
630 printf("Optimal tempo: %lf\n",
631 ss_tempo_from_wave(w,
632 e->ss_print_wave_tempo.note,
633 e->ss_print_wave_tempo.len));
635 break;
637 case SONG_EV_END:
639 go=0;
640 break;
642 case SONG_EV_MIDI_CHANNEL:
643 case SONG_EV_MIDI_PROGRAM:
644 case SONG_EV_NOTE:
645 case SONG_EV_METER:
646 case SONG_EV_MEASURE:
648 /* never found in ss song streams */
649 break;
652 /* next event */
653 e++;
656 /* reset frame samples */
657 ss_output_init_frame(output);
659 /* generate output from all instruments */
660 for(n=0;n < n_tracks;n++)
661 ss_ins_frame(&ss_song_ins[n], output);
663 /* dump to sampling driver */
664 if(frame >= skip_frames)
665 ss_output_write(output);
667 /* next frame */
668 frame++;
671 return(0);