The 'off' directive has been added, to set to off the effect chain
[ahxm.git] / ss_song.c
blob8557fafddf4db0d8cc496770a1d8ce3a3310bcd0
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 "ss_core.h"
34 #include "song.h"
35 #include "ss_gen.h"
36 #include "ss_eff.h"
37 #include "ss_ins.h"
38 #include "ss_input.h"
39 #include "ss_output.h"
41 /*******************
42 Data
43 ********************/
45 struct ss_ev_generic
47 song_ev_type type; /* event type */
48 int frame; /* frame number (time) */
49 int trk_id; /* track id */
52 struct ss_ev_note_off
54 song_ev_type type; /* SONG_EV_NOTE_OFF */
55 int frame;
56 int trk_id;
57 int note_id; /* note id */
60 struct ss_ev_note_on
62 song_ev_type type; /* SONG_EV_NOTE_ON */
63 int frame;
64 int trk_id;
65 int note_id; /* note id */
66 int note; /* MIDI-like note number */
67 float vol; /* volume */
70 struct ss_ev_ss_note_on_by_time
72 song_ev_type type; /* SONG_EV_SS_NOTE_ON_BY_TIME */
73 int frame;
74 int trk_id;
75 int note_id; /* note id */
76 int note; /* MIDI-like note (to find the wave) */
77 double len; /* note length (1: whole) */
78 float vol; /* volume */
81 struct ss_ev_ss_sustain
83 song_ev_type type; /* SONG_EV_SS_SUSTAIN */
84 int frame;
85 int trk_id;
86 double sustain; /* sustain time (in frames) */
89 struct ss_ev_ss_set_channel
91 song_ev_type type; /* SONG_EV_SS_SET_CHANNEL */
92 int frame;
93 int trk_id;
94 int channel; /* channel */
95 float vol; /* volume */
98 struct ss_ev_ss_wav
100 song_ev_type type; /* SONG_EV_SS_WAV */
101 int frame;
102 int trk_id;
103 char * file; /* path to .wav file */
104 int base; /* MIDI-like base note */
105 int min; /* MIDI-like minimum note */
106 int max; /* MIDI-like maximum note */
107 double loop_start; /* loop start */
108 double loop_end; /* loop end */
111 struct ss_ev_ss_pat
113 song_ev_type type; /* SONG_EV_SS_PAT */
114 int frame;
115 int trk_id;
116 char * file; /* path to .pat file */
119 struct ss_ev_ss_eff
121 song_ev_type type; /* effect type */
122 int frame;
123 int trk_id;
124 int channel; /* channel */
125 double size; /* size of effect */
126 float gain; /* gain */
127 double depth; /* depth */
128 double freq; /* freq */
129 double phase; /* phase */
130 float initial; /* initial vol */
131 float final; /* final vol */
134 union ss_ev
136 struct ss_ev_generic generic;
137 struct ss_ev_note_on note_on;
138 struct ss_ev_note_off note_off;
139 struct ss_ev_ss_note_on_by_time ss_note_on_by_time;
140 struct ss_ev_ss_sustain ss_sustain;
141 struct ss_ev_ss_set_channel ss_set_channel;
142 struct ss_ev_ss_wav ss_wav;
143 struct ss_ev_ss_pat ss_pat;
144 struct ss_ev_ss_eff ss_eff;
147 /* the softsynth song stream */
149 static union ss_ev * ss_song=NULL;
150 static int n_ss_ev=0;
153 /* the instruments */
155 #define SONG_INS_NUM 256
156 struct ss_ins song_ins[SONG_INS_NUM];
159 /*******************
160 Code
161 ********************/
163 static void add_ss_ev(union ss_ev * e)
164 /* adds a softsynth song event */
166 /* reallocs */
167 ss_song=(union ss_ev *)realloc(ss_song,
168 (n_ss_ev + 1) * sizeof(union ss_ev));
170 /* store */
171 memcpy(&ss_song[n_ss_ev], e, sizeof(union ss_ev));
173 n_ss_ev++;
177 static int ss_ev_cmp(const void * v1, const void * v2)
178 /* softsynth song event compare function for qsort() */
180 struct ss_ev_generic * e1;
181 struct ss_ev_generic * e2;
183 e1=(struct ss_ev_generic *)v1; e2=(struct ss_ev_generic *)v2;
185 if(e1->frame == e2->frame)
186 return(e1->type - e2->type);
188 return(e1->frame - e2->frame);
192 static int ss_song_convert_events(void)
193 /* converts generic song_ev events to softsynth events */
195 int note_id=1;
196 union song_ev * e;
197 union ss_ev sse;
198 int frame, frame_ac;
199 double fpw, time_ac;
200 int num, den;
201 int n;
202 int b_track=-1;
204 /* resets the ss stream */
205 if(ss_song != NULL)
207 free(ss_song);
208 ss_song=NULL;
211 n_ss_ev=0;
213 /* sorts the song */
214 song_sort();
216 fpw=0;
217 frame_ac=0;
218 time_ac=0;
219 num=den=4;
221 /* travels the song events generating softsynth song events */
222 for(n=0;n < n_song_ev;n++)
224 /* gets the song event */
225 e=&song[n];
227 /* calculates the frame */
228 frame=((e->generic.time - time_ac) * fpw) + frame_ac;
230 /* generic event data */
231 sse.generic.type=e->generic.type;
232 sse.generic.frame=frame;
233 sse.generic.trk_id=e->generic.trk_id;
235 /* account the biggest track seen */
236 if(b_track < e->generic.trk_id) b_track=e->generic.trk_id;
238 switch(e->generic.type)
240 case SONG_EV_TEMPO:
242 /* updates accumulations */
243 frame_ac += frame;
244 time_ac += e->generic.time;
246 /* calculates frames-per-whole based on new tempo */
247 fpw=(double) ss_frequency * 60.0;
248 fpw /= e->tempo.tempo / 4.0;
250 break;
252 case SONG_EV_METER:
254 /* just store the values */
255 num=e->meter.num;
256 den=e->meter.den;
258 break;
260 case SONG_EV_MEASURE:
262 printf("measure boundary check (must be 0): %d\n",
263 ((int) e->generic.time * num) % den);
265 break;
267 case SONG_EV_NOTE:
269 /* convert to note on / off pairs */
271 sse.note_on.type=SONG_EV_NOTE_ON;
272 sse.note_on.note_id=note_id++;
273 sse.note_on.note=e->note.note;
274 sse.note_on.vol=e->note.vol;
276 add_ss_ev(&sse);
278 frame += (int)(e->note.len * fpw);
280 sse.note_off.type=SONG_EV_NOTE_OFF;
281 sse.note_off.frame=frame;
283 add_ss_ev(&sse);
284 break;
286 case SONG_EV_SS_NOTE_ON_BY_TIME:
288 sse.ss_note_on_by_time.note_id=note_id++;
289 sse.ss_note_on_by_time.note=e->ss_note_by_time.note;
290 sse.ss_note_on_by_time.len=e->ss_note_by_time.len;
291 sse.ss_note_on_by_time.vol=e->ss_note_by_time.vol;
293 add_ss_ev(&sse);
295 frame += (int)(e->ss_note_by_time.len * fpw);
297 sse.note_off.type=SONG_EV_NOTE_OFF;
298 sse.note_off.frame=frame;
300 add_ss_ev(&sse);
301 break;
303 case SONG_EV_SS_WAV:
305 sse.ss_wav.file=e->ss_wav.file;
306 sse.ss_wav.base=e->ss_wav.base;
307 sse.ss_wav.min=e->ss_wav.min;
308 sse.ss_wav.max=e->ss_wav.max;
309 sse.ss_wav.loop_start=e->ss_wav.loop_start;
310 sse.ss_wav.loop_end=e->ss_wav.loop_end;
312 add_ss_ev(&sse);
313 break;
315 case SONG_EV_SS_PAT:
317 sse.ss_pat.file=e->ss_pat.file;
319 add_ss_ev(&sse);
320 break;
322 case SONG_EV_SS_SUSTAIN:
324 sse.ss_sustain.sustain=e->ss_sustain.sustain;
326 add_ss_ev(&sse);
327 break;
329 case SONG_EV_SS_SET_CHANNEL:
331 break;
333 case SONG_EV_SS_EFF_DELAY:
334 case SONG_EV_SS_EFF_ECHO:
335 case SONG_EV_SS_EFF_COMB:
336 case SONG_EV_SS_EFF_ALLPASS:
337 case SONG_EV_SS_EFF_FLANGER:
338 case SONG_EV_SS_EFF_WOBBLE:
339 case SONG_EV_SS_EFF_SQWOBBLE:
340 case SONG_EV_SS_EFF_FADER:
341 case SONG_EV_SS_EFF_REVERB:
342 case SONG_EV_SS_EFF_OFF:
344 sse.ss_eff.channel=e->ss_eff.channel;
345 sse.ss_eff.size=e->ss_eff.size;
346 sse.ss_eff.gain=e->ss_eff.gain;
347 sse.ss_eff.depth=e->ss_eff.depth;
348 sse.ss_eff.freq=e->ss_eff.freq;
349 sse.ss_eff.phase=e->ss_eff.phase;
350 sse.ss_eff.initial=e->ss_eff.initial;
351 sse.ss_eff.final=e->ss_eff.final;
353 add_ss_ev(&sse);
354 break;
356 case SONG_EV_MIDI_CHANNEL:
357 case SONG_EV_MIDI_PROGRAM:
359 /* ignored */
360 break;
362 case SONG_EV_NOTE_ON:
363 case SONG_EV_NOTE_OFF:
364 case SONG_EV_END:
366 /* never found in generic song streams */
367 break;
371 /* generates an end of event mark, a time after the last one */
372 sse.generic.type=SONG_EV_END;
373 sse.generic.frame=frame + ss_frequency;
374 add_ss_ev(&sse);
376 /* finally sort */
377 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
379 /* return the number of tracks */
380 return(b_track + 1);
384 int ss_song_render(void)
386 union ss_ev * e;
387 int frame;
388 int go;
389 int n;
390 float output[SS_MAX_CHANNELS];
391 int n_tracks;
392 struct ss_ins * i;
394 /* convert the song to ss events */
395 n_tracks=ss_song_convert_events();
397 frame=0;
398 go=1;
399 e=ss_song;
401 /* init the instruments */
402 for(n=0;n < n_tracks;n++)
403 ss_ins_init(&song_ins[n]);
405 /* loop the events */
406 while(go)
408 /* process all events for this exact frame */
409 while(e->generic.frame == frame)
411 /* take the instrument */
412 if(e->generic.trk_id == -1)
413 i=NULL;
414 else
415 i=&song_ins[e->generic.trk_id];
417 switch(e->generic.type)
419 case SONG_EV_NOTE_ON:
421 ss_ins_note_on(i, e->note_on.note,
422 e->note_on.vol, e->note_on.note_id);
424 break;
426 case SONG_EV_SS_NOTE_ON_BY_TIME:
428 break;
430 case SONG_EV_NOTE_OFF:
432 ss_ins_note_off(i, e->note_off.note_id);
434 break;
436 case SONG_EV_SS_SUSTAIN:
438 ss_ins_set_sustain(i, e->ss_sustain.sustain);
440 break;
442 case SONG_EV_SS_SET_CHANNEL:
444 break;
446 case SONG_EV_SS_WAV:
449 struct ss_wave * w;
451 w=ss_load_wav_file(e->ss_wav.file,
452 ss_note_frequency(e->ss_wav.base),
453 ss_note_frequency(e->ss_wav.min),
454 ss_note_frequency(e->ss_wav.max),
455 e->ss_wav.loop_start,
456 e->ss_wav.loop_end);
458 ss_ins_add_layer(i, w);
461 break;
463 case SONG_EV_SS_PAT:
465 ss_load_pat_file(i, e->ss_pat.file);
466 break;
468 case SONG_EV_SS_EFF_DELAY:
470 ss_eff_delay(&i->effs[e->ss_eff.channel],
471 e->ss_eff.size);
472 break;
474 case SONG_EV_SS_EFF_ECHO:
476 ss_eff_echo(&i->effs[e->ss_eff.channel],
477 e->ss_eff.size, e->ss_eff.gain);
478 break;
480 case SONG_EV_SS_EFF_COMB:
482 ss_eff_comb(&i->effs[e->ss_eff.channel],
483 e->ss_eff.size, e->ss_eff.gain);
484 break;
486 case SONG_EV_SS_EFF_ALLPASS:
488 ss_eff_allpass(&i->effs[e->ss_eff.channel],
489 e->ss_eff.size, e->ss_eff.gain);
490 break;
492 case SONG_EV_SS_EFF_FLANGER:
494 ss_eff_flanger(&i->effs[e->ss_eff.channel],
495 e->ss_eff.size, e->ss_eff.gain,
496 e->ss_eff.depth, e->ss_eff.freq,
497 e->ss_eff.phase);
498 break;
500 case SONG_EV_SS_EFF_WOBBLE:
502 ss_eff_wobble(&i->effs[e->ss_eff.channel],
503 e->ss_eff.freq, e->ss_eff.phase);
505 break;
507 case SONG_EV_SS_EFF_SQWOBBLE:
509 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
510 e->ss_eff.freq, e->ss_eff.phase);
512 break;
514 case SONG_EV_SS_EFF_FADER:
516 ss_eff_fader(&i->effs[e->ss_eff.channel],
517 e->ss_eff.size, e->ss_eff.initial,
518 e->ss_eff.final);
519 break;
521 case SONG_EV_SS_EFF_REVERB:
523 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
524 break;
526 case SONG_EV_SS_EFF_OFF:
528 ss_eff_off(&i->effs[e->ss_eff.channel]);
529 break;
531 case SONG_EV_END:
533 go=0;
534 break;
536 case SONG_EV_MIDI_CHANNEL:
537 case SONG_EV_MIDI_PROGRAM:
538 case SONG_EV_NOTE:
539 case SONG_EV_TEMPO:
540 case SONG_EV_METER:
541 case SONG_EV_MEASURE:
543 /* never found in ss song streams */
544 break;
547 /* next event */
548 e++;
551 /* reset frame samples */
552 ss_output_init_frame(output);
554 /* generate output from all instruments */
555 for(n=0;n < n_tracks;n++)
556 ss_ins_frame(&song_ins[n], output);
558 /* dump to sampling driver */
559 ss_output_write(output);
561 /* next frame */
562 frame++;
565 return(0);