Updated TODO.
[ahxm.git] / ss_song.c
blobb30f238b6b09d54f928638fefd14ea8b8ae2db2d
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 struct ss_ins song_ins[SS_MAX_INSTRUMENTS];
171 /*******************
172 Code
173 ********************/
175 static void add_ss_ev(union ss_ev * e)
176 /* adds a softsynth song event */
178 /* reallocs */
179 ss_song=(union ss_ev *)realloc(ss_song,
180 (n_ss_ev + 1) * sizeof(union ss_ev));
182 /* store */
183 memcpy(&ss_song[n_ss_ev], e, sizeof(union ss_ev));
185 n_ss_ev++;
189 static int ss_ev_cmp(const void * v1, const void * v2)
190 /* softsynth song event compare function for qsort() */
192 struct ss_ev_generic * e1;
193 struct ss_ev_generic * e2;
195 e1=(struct ss_ev_generic *)v1; e2=(struct ss_ev_generic *)v2;
197 if(e1->frame == e2->frame)
198 return(e1->type - e2->type);
200 return(e1->frame - e2->frame);
204 static int ss_song_convert_events(void)
205 /* converts generic song_ev events to softsynth events */
207 int note_id=1;
208 union song_ev * e;
209 union ss_ev sse;
210 int frame, frame_ac, f_frame;
211 double fpw, time_ac, time_ac_m;
212 int num, den;
213 int n;
214 int b_track=-1;
216 /* resets the ss stream */
217 if(ss_song != NULL)
219 free(ss_song);
220 ss_song=NULL;
223 n_ss_ev=0;
225 /* sorts the song */
226 song_sort();
228 fpw=0;
229 frame=frame_ac=f_frame=0;
230 time_ac=time_ac_m=0;
231 num=den=4;
233 /* travels the song events generating softsynth song events */
234 for(n=0;n < n_song_ev;n++)
236 /* gets the song event */
237 e=&song[n];
239 /* calculates the frame */
240 frame=((e->generic.time - time_ac) * fpw) + frame_ac;
242 /* generic event data */
243 sse.generic.type=e->generic.type;
244 sse.generic.frame=frame;
245 sse.generic.trk_id=e->generic.trk_id;
247 /* account the biggest track seen */
248 if(b_track < e->generic.trk_id) b_track=e->generic.trk_id;
250 switch(e->generic.type)
252 case SONG_EV_TEMPO:
254 /* updates accumulations */
255 frame_ac += frame;
256 time_ac += e->generic.time;
258 /* calculates frames-per-whole based on new tempo */
259 fpw=(double) ss_frequency * 60.0;
260 fpw /= e->tempo.tempo / 4.0;
262 /* adds an event */
263 sse.tempo.tempo=e->tempo.tempo;
264 add_ss_ev(&sse);
266 break;
268 case SONG_EV_METER:
270 /* just store the values */
271 num=e->meter.num;
272 den=e->meter.den;
273 time_ac_m=e->meter.time;
275 break;
277 case SONG_EV_MEASURE:
279 song_test_measure_boundary(e->measure.time - time_ac_m,
280 num, den, e->measure.line);
281 break;
283 case SONG_EV_NOTE:
285 /* convert to note on / off pairs */
287 sse.note_on.type=SONG_EV_NOTE_ON;
288 sse.note_on.note_id=note_id++;
289 sse.note_on.note=e->note.note;
290 sse.note_on.vol=e->note.vol;
292 add_ss_ev(&sse);
294 frame += (int)(e->note.len * fpw);
296 sse.note_off.type=SONG_EV_NOTE_OFF;
297 sse.note_off.frame=frame;
299 add_ss_ev(&sse);
300 break;
302 case SONG_EV_SS_PITCH_STRETCH:
304 sse.ss_pitch_stretch.note_id=note_id++;
305 sse.ss_pitch_stretch.note=e->ss_pitch_stretch.note;
306 sse.ss_pitch_stretch.len=e->ss_pitch_stretch.len;
307 sse.ss_pitch_stretch.vol=e->ss_pitch_stretch.vol;
309 add_ss_ev(&sse);
311 frame += (int)(e->ss_pitch_stretch.len * fpw);
313 sse.note_off.type=SONG_EV_NOTE_OFF;
314 sse.note_off.frame=frame;
316 add_ss_ev(&sse);
317 break;
319 case SONG_EV_SS_WAV:
321 sse.ss_wav.file=e->ss_wav.file;
322 sse.ss_wav.base=e->ss_wav.base;
323 sse.ss_wav.min=e->ss_wav.min;
324 sse.ss_wav.max=e->ss_wav.max;
325 sse.ss_wav.loop_start=e->ss_wav.loop_start;
326 sse.ss_wav.loop_end=e->ss_wav.loop_end;
328 add_ss_ev(&sse);
329 break;
331 case SONG_EV_SS_PAT:
333 sse.ss_pat.file=e->ss_pat.file;
335 add_ss_ev(&sse);
336 break;
338 case SONG_EV_SS_SUSTAIN:
340 sse.ss_sustain.sustain=e->ss_sustain.sustain;
342 add_ss_ev(&sse);
343 break;
345 case SONG_EV_SS_VIBRATO:
347 sse.ss_vibrato.vib_depth=e->ss_vibrato.vib_depth;
348 sse.ss_vibrato.vib_freq=e->ss_vibrato.vib_freq;
350 add_ss_ev(&sse);
351 break;
353 case SONG_EV_SS_CHANNEL:
355 sse.ss_channel.channel=e->ss_channel.channel;
356 sse.ss_channel.vol=e->ss_channel.vol;
358 add_ss_ev(&sse);
359 break;
361 case SONG_EV_SS_EFF_DELAY:
362 case SONG_EV_SS_EFF_ECHO:
363 case SONG_EV_SS_EFF_COMB:
364 case SONG_EV_SS_EFF_ALLPASS:
365 case SONG_EV_SS_EFF_FLANGER:
366 case SONG_EV_SS_EFF_WOBBLE:
367 case SONG_EV_SS_EFF_SQWOBBLE:
368 case SONG_EV_SS_EFF_FADER:
369 case SONG_EV_SS_EFF_REVERB:
370 case SONG_EV_SS_EFF_OFF:
372 sse.ss_eff.channel=e->ss_eff.channel;
373 sse.ss_eff.size=e->ss_eff.size;
374 sse.ss_eff.gain=e->ss_eff.gain;
375 sse.ss_eff.depth=e->ss_eff.depth;
376 sse.ss_eff.freq=e->ss_eff.freq;
377 sse.ss_eff.phase=e->ss_eff.phase;
378 sse.ss_eff.initial=e->ss_eff.initial;
379 sse.ss_eff.final=e->ss_eff.final;
381 add_ss_ev(&sse);
382 break;
384 case SONG_EV_MIDI_CHANNEL:
385 case SONG_EV_MIDI_PROGRAM:
387 /* ignored */
388 break;
390 case SONG_EV_NOTE_ON:
391 case SONG_EV_NOTE_OFF:
392 case SONG_EV_END:
394 /* never found in generic song streams */
395 break;
398 /* store the further frame seen */
399 if(f_frame < frame) f_frame=frame;
402 /* generates an end of event mark, a time after the last one */
403 sse.generic.type=SONG_EV_END;
404 sse.generic.frame=f_frame + ss_frequency;
405 add_ss_ev(&sse);
407 /* finally sort */
408 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
410 /* return the number of tracks */
411 return(b_track + 1);
415 int ss_song_render(int skip_secs)
417 union ss_ev * e;
418 int frame;
419 int go;
420 int n;
421 float output[SS_MAX_CHANNELS];
422 int n_tracks;
423 struct ss_ins * i;
424 double tempo=120.0;
425 int skip_frames;
427 /* convert the song to ss events */
428 n_tracks=ss_song_convert_events();
430 frame=0;
431 go=1;
432 e=ss_song;
434 /* init the instruments */
435 for(n=0;n < n_tracks;n++)
436 ss_ins_init(&song_ins[n]);
438 /* calculate the frame to start playing */
439 skip_frames=skip_secs * ss_frequency;
441 /* loop the events */
442 while(go)
444 /* process all events for this exact frame */
445 while(e->generic.frame == frame)
447 /* take the instrument */
448 if(e->generic.trk_id == -1)
449 i=NULL;
450 else
451 i=&song_ins[e->generic.trk_id];
453 switch(e->generic.type)
455 case SONG_EV_NOTE_ON:
457 ss_ins_note_on(i, e->note_on.note,
458 e->note_on.vol, e->note_on.note_id);
460 break;
462 case SONG_EV_NOTE_OFF:
464 ss_ins_note_off(i, e->note_off.note_id);
466 break;
468 case SONG_EV_SS_SUSTAIN:
470 ss_ins_set_sustain(i, e->ss_sustain.sustain);
472 break;
474 case SONG_EV_SS_VIBRATO:
476 ss_ins_set_vibrato(i, e->ss_vibrato.vib_depth,
477 e->ss_vibrato.vib_freq);
479 break;
481 case SONG_EV_SS_CHANNEL:
483 ss_ins_set_channel(i, e->ss_channel.channel,
484 e->ss_channel.vol);
486 break;
488 case SONG_EV_SS_WAV:
491 struct ss_wave * w;
493 w=ss_load_wav_file(e->ss_wav.file,
494 ss_note_frequency(e->ss_wav.base),
495 ss_note_frequency(e->ss_wav.min),
496 ss_note_frequency(e->ss_wav.max),
497 e->ss_wav.loop_start,
498 e->ss_wav.loop_end);
500 ss_ins_add_layer(i, w);
503 break;
505 case SONG_EV_SS_PAT:
507 ss_load_pat_file(i, e->ss_pat.file);
508 break;
510 case SONG_EV_SS_EFF_DELAY:
512 ss_eff_delay(&i->effs[e->ss_eff.channel],
513 e->ss_eff.size);
514 break;
516 case SONG_EV_SS_EFF_ECHO:
518 ss_eff_echo(&i->effs[e->ss_eff.channel],
519 e->ss_eff.size, e->ss_eff.gain);
520 break;
522 case SONG_EV_SS_EFF_COMB:
524 ss_eff_comb(&i->effs[e->ss_eff.channel],
525 e->ss_eff.size, e->ss_eff.gain);
526 break;
528 case SONG_EV_SS_EFF_ALLPASS:
530 ss_eff_allpass(&i->effs[e->ss_eff.channel],
531 e->ss_eff.size, e->ss_eff.gain);
532 break;
534 case SONG_EV_SS_EFF_FLANGER:
536 ss_eff_flanger(&i->effs[e->ss_eff.channel],
537 e->ss_eff.size, e->ss_eff.gain,
538 e->ss_eff.depth, e->ss_eff.freq,
539 e->ss_eff.phase);
540 break;
542 case SONG_EV_SS_EFF_WOBBLE:
544 ss_eff_wobble(&i->effs[e->ss_eff.channel],
545 e->ss_eff.freq, e->ss_eff.phase);
547 break;
549 case SONG_EV_SS_EFF_SQWOBBLE:
551 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
552 e->ss_eff.freq, e->ss_eff.phase);
554 break;
556 case SONG_EV_SS_EFF_FADER:
558 ss_eff_fader(&i->effs[e->ss_eff.channel],
559 e->ss_eff.size, e->ss_eff.initial,
560 e->ss_eff.final);
561 break;
563 case SONG_EV_SS_EFF_REVERB:
565 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
566 break;
568 case SONG_EV_SS_EFF_OFF:
570 ss_eff_off(&i->effs[e->ss_eff.channel]);
571 break;
573 case SONG_EV_TEMPO:
575 /* just store the last tempo */
576 tempo=e->tempo.tempo;
577 break;
579 case SONG_EV_SS_PITCH_STRETCH:
582 int n=0;
583 struct ss_wave * w;
584 double freq;
586 /* find the wave */
587 freq=ss_note_frequency(e->ss_pitch_stretch.note);
588 w=ss_ins_find_layer(i, freq, &n);
590 /* calculate optimal frequency */
591 freq=ss_pitch_from_tempo(w, tempo, e->ss_pitch_stretch.len);
593 /* play the note */
594 ss_ins_play(i, freq, e->ss_pitch_stretch.vol,
595 e->ss_pitch_stretch.note_id, w);
598 break;
600 case SONG_EV_END:
602 go=0;
603 break;
605 case SONG_EV_MIDI_CHANNEL:
606 case SONG_EV_MIDI_PROGRAM:
607 case SONG_EV_NOTE:
608 case SONG_EV_METER:
609 case SONG_EV_MEASURE:
611 /* never found in ss song streams */
612 break;
615 /* next event */
616 e++;
619 /* reset frame samples */
620 ss_output_init_frame(output);
622 /* generate output from all instruments */
623 for(n=0;n < n_tracks;n++)
624 ss_ins_frame(&song_ins[n], output);
626 /* dump to sampling driver */
627 if(frame >= skip_frames)
628 ss_output_write(output);
630 /* next frame */
631 frame++;
634 return(0);