New 'channel' and 'vol' language directives, to control volumes for
[ahxm.git] / ss_song.c
blob128d6199e861dcfffaa955ac82c137a99ce78306
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 */
67 float vol; /* volume */
70 struct ss_ev_ss_sustain
72 song_ev_type type; /* SONG_EV_SS_SUSTAIN */
73 int frame;
74 int trk_id;
75 double sustain; /* sustain time (in frames) */
78 struct ss_ev_ss_channel
80 song_ev_type type; /* SONG_EV_SS_CHANNEL */
81 int frame;
82 int trk_id;
83 int channel; /* channel */
84 float vol; /* volume */
87 struct ss_ev_ss_wav
89 song_ev_type type; /* SONG_EV_SS_WAV */
90 int frame;
91 int trk_id;
92 char * file; /* path to .wav file */
93 int base; /* MIDI-like base note */
94 int min; /* MIDI-like minimum note */
95 int max; /* MIDI-like maximum note */
96 double loop_start; /* loop start */
97 double loop_end; /* loop end */
100 struct ss_ev_ss_pat
102 song_ev_type type; /* SONG_EV_SS_PAT */
103 int frame;
104 int trk_id;
105 char * file; /* path to .pat file */
108 struct ss_ev_tempo
110 song_ev_type type; /* SONG_EV_TEMPO */
111 int frame;
112 int trk_id;
113 double tempo; /* tempo in bmp */
116 struct ss_ev_pitch_stretch
118 song_ev_type type; /* SONG_EV_SS_PITCH_STRETCH */
119 int frame;
120 int trk_id;
121 int note_id; /* note id */
122 int note; /* MIDI-like note (to find the wave) */
123 double len; /* note length (1: whole note) */
124 float vol; /* note volume (1: full volume) */
127 struct ss_ev_ss_eff
129 song_ev_type type; /* effect type */
130 int frame;
131 int trk_id;
132 int channel; /* channel */
133 double size; /* size of effect */
134 float gain; /* gain */
135 double depth; /* depth */
136 double freq; /* freq */
137 double phase; /* phase */
138 float initial; /* initial vol */
139 float final; /* final vol */
142 union ss_ev
144 struct ss_ev_generic generic;
145 struct ss_ev_note_on note_on;
146 struct ss_ev_note_off note_off;
147 struct ss_ev_ss_sustain ss_sustain;
148 struct ss_ev_ss_channel ss_channel;
149 struct ss_ev_ss_wav ss_wav;
150 struct ss_ev_ss_pat ss_pat;
151 struct ss_ev_ss_eff ss_eff;
152 struct ss_ev_tempo tempo;
153 struct ss_ev_pitch_stretch ss_pitch_stretch;
156 /* the softsynth song stream */
158 static union ss_ev * ss_song=NULL;
159 static int n_ss_ev=0;
162 /* the instruments */
164 #define SONG_INS_NUM 256
165 struct ss_ins song_ins[SONG_INS_NUM];
168 /*******************
169 Code
170 ********************/
172 static void add_ss_ev(union ss_ev * e)
173 /* adds a softsynth song event */
175 /* reallocs */
176 ss_song=(union ss_ev *)realloc(ss_song,
177 (n_ss_ev + 1) * sizeof(union ss_ev));
179 /* store */
180 memcpy(&ss_song[n_ss_ev], e, sizeof(union ss_ev));
182 n_ss_ev++;
186 static int ss_ev_cmp(const void * v1, const void * v2)
187 /* softsynth song event compare function for qsort() */
189 struct ss_ev_generic * e1;
190 struct ss_ev_generic * e2;
192 e1=(struct ss_ev_generic *)v1; e2=(struct ss_ev_generic *)v2;
194 if(e1->frame == e2->frame)
195 return(e1->type - e2->type);
197 return(e1->frame - e2->frame);
201 static int ss_song_convert_events(void)
202 /* converts generic song_ev events to softsynth events */
204 int note_id=1;
205 union song_ev * e;
206 union ss_ev sse;
207 int frame, frame_ac;
208 double fpw, time_ac;
209 int num, den;
210 int n;
211 int b_track=-1;
213 /* resets the ss stream */
214 if(ss_song != NULL)
216 free(ss_song);
217 ss_song=NULL;
220 n_ss_ev=0;
222 /* sorts the song */
223 song_sort();
225 fpw=0;
226 frame_ac=0;
227 time_ac=0;
228 num=den=4;
230 /* travels the song events generating softsynth song events */
231 for(n=0;n < n_song_ev;n++)
233 /* gets the song event */
234 e=&song[n];
236 /* calculates the frame */
237 frame=((e->generic.time - time_ac) * fpw) + frame_ac;
239 /* generic event data */
240 sse.generic.type=e->generic.type;
241 sse.generic.frame=frame;
242 sse.generic.trk_id=e->generic.trk_id;
244 /* account the biggest track seen */
245 if(b_track < e->generic.trk_id) b_track=e->generic.trk_id;
247 switch(e->generic.type)
249 case SONG_EV_TEMPO:
251 /* updates accumulations */
252 frame_ac += frame;
253 time_ac += e->generic.time;
255 /* calculates frames-per-whole based on new tempo */
256 fpw=(double) ss_frequency * 60.0;
257 fpw /= e->tempo.tempo / 4.0;
259 /* adds an event */
260 sse.tempo.tempo=e->tempo.tempo;
261 add_ss_ev(&sse);
263 break;
265 case SONG_EV_METER:
267 /* just store the values */
268 num=e->meter.num;
269 den=e->meter.den;
271 break;
273 case SONG_EV_MEASURE:
275 printf("measure boundary check (must be 0): %d\n",
276 ((int) e->generic.time * num) % den);
278 break;
280 case SONG_EV_NOTE:
282 /* convert to note on / off pairs */
284 sse.note_on.type=SONG_EV_NOTE_ON;
285 sse.note_on.note_id=note_id++;
286 sse.note_on.note=e->note.note;
287 sse.note_on.vol=e->note.vol;
289 add_ss_ev(&sse);
291 frame += (int)(e->note.len * fpw);
293 sse.note_off.type=SONG_EV_NOTE_OFF;
294 sse.note_off.frame=frame;
296 add_ss_ev(&sse);
297 break;
299 case SONG_EV_SS_PITCH_STRETCH:
301 sse.ss_pitch_stretch.note_id=note_id++;
302 sse.ss_pitch_stretch.note=e->ss_pitch_stretch.note;
303 sse.ss_pitch_stretch.len=e->ss_pitch_stretch.len;
304 sse.ss_pitch_stretch.vol=e->ss_pitch_stretch.vol;
306 add_ss_ev(&sse);
308 frame += (int)(e->ss_pitch_stretch.len * fpw);
310 sse.note_off.type=SONG_EV_NOTE_OFF;
311 sse.note_off.frame=frame;
313 add_ss_ev(&sse);
314 break;
316 case SONG_EV_SS_WAV:
318 sse.ss_wav.file=e->ss_wav.file;
319 sse.ss_wav.base=e->ss_wav.base;
320 sse.ss_wav.min=e->ss_wav.min;
321 sse.ss_wav.max=e->ss_wav.max;
322 sse.ss_wav.loop_start=e->ss_wav.loop_start;
323 sse.ss_wav.loop_end=e->ss_wav.loop_end;
325 add_ss_ev(&sse);
326 break;
328 case SONG_EV_SS_PAT:
330 sse.ss_pat.file=e->ss_pat.file;
332 add_ss_ev(&sse);
333 break;
335 case SONG_EV_SS_SUSTAIN:
337 sse.ss_sustain.sustain=e->ss_sustain.sustain;
339 add_ss_ev(&sse);
340 break;
342 case SONG_EV_SS_CHANNEL:
344 sse.ss_channel.channel=e->ss_channel.channel;
345 sse.ss_channel.vol=e->ss_channel.vol;
347 add_ss_ev(&sse);
348 break;
350 case SONG_EV_SS_EFF_DELAY:
351 case SONG_EV_SS_EFF_ECHO:
352 case SONG_EV_SS_EFF_COMB:
353 case SONG_EV_SS_EFF_ALLPASS:
354 case SONG_EV_SS_EFF_FLANGER:
355 case SONG_EV_SS_EFF_WOBBLE:
356 case SONG_EV_SS_EFF_SQWOBBLE:
357 case SONG_EV_SS_EFF_FADER:
358 case SONG_EV_SS_EFF_REVERB:
359 case SONG_EV_SS_EFF_OFF:
361 sse.ss_eff.channel=e->ss_eff.channel;
362 sse.ss_eff.size=e->ss_eff.size;
363 sse.ss_eff.gain=e->ss_eff.gain;
364 sse.ss_eff.depth=e->ss_eff.depth;
365 sse.ss_eff.freq=e->ss_eff.freq;
366 sse.ss_eff.phase=e->ss_eff.phase;
367 sse.ss_eff.initial=e->ss_eff.initial;
368 sse.ss_eff.final=e->ss_eff.final;
370 add_ss_ev(&sse);
371 break;
373 case SONG_EV_MIDI_CHANNEL:
374 case SONG_EV_MIDI_PROGRAM:
376 /* ignored */
377 break;
379 case SONG_EV_NOTE_ON:
380 case SONG_EV_NOTE_OFF:
381 case SONG_EV_END:
383 /* never found in generic song streams */
384 break;
388 /* generates an end of event mark, a time after the last one */
389 sse.generic.type=SONG_EV_END;
390 sse.generic.frame=frame + ss_frequency;
391 add_ss_ev(&sse);
393 /* finally sort */
394 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
396 /* return the number of tracks */
397 return(b_track + 1);
401 int ss_song_render(void)
403 union ss_ev * e;
404 int frame;
405 int go;
406 int n;
407 float output[SS_MAX_CHANNELS];
408 int n_tracks;
409 struct ss_ins * i;
410 double tempo=120.0;
412 /* convert the song to ss events */
413 n_tracks=ss_song_convert_events();
415 frame=0;
416 go=1;
417 e=ss_song;
419 /* init the instruments */
420 for(n=0;n < n_tracks;n++)
421 ss_ins_init(&song_ins[n]);
423 /* loop the events */
424 while(go)
426 /* process all events for this exact frame */
427 while(e->generic.frame == frame)
429 /* take the instrument */
430 if(e->generic.trk_id == -1)
431 i=NULL;
432 else
433 i=&song_ins[e->generic.trk_id];
435 switch(e->generic.type)
437 case SONG_EV_NOTE_ON:
439 ss_ins_note_on(i, e->note_on.note,
440 e->note_on.vol, e->note_on.note_id);
442 break;
444 case SONG_EV_NOTE_OFF:
446 ss_ins_note_off(i, e->note_off.note_id);
448 break;
450 case SONG_EV_SS_SUSTAIN:
452 ss_ins_set_sustain(i, e->ss_sustain.sustain);
454 break;
456 case SONG_EV_SS_CHANNEL:
458 ss_ins_set_channel(i, e->ss_channel.channel,
459 e->ss_channel.vol);
461 break;
463 case SONG_EV_SS_WAV:
466 struct ss_wave * w;
468 w=ss_load_wav_file(e->ss_wav.file,
469 ss_note_frequency(e->ss_wav.base),
470 ss_note_frequency(e->ss_wav.min),
471 ss_note_frequency(e->ss_wav.max),
472 e->ss_wav.loop_start,
473 e->ss_wav.loop_end);
475 ss_ins_add_layer(i, w);
478 break;
480 case SONG_EV_SS_PAT:
482 ss_load_pat_file(i, e->ss_pat.file);
483 break;
485 case SONG_EV_SS_EFF_DELAY:
487 ss_eff_delay(&i->effs[e->ss_eff.channel],
488 e->ss_eff.size);
489 break;
491 case SONG_EV_SS_EFF_ECHO:
493 ss_eff_echo(&i->effs[e->ss_eff.channel],
494 e->ss_eff.size, e->ss_eff.gain);
495 break;
497 case SONG_EV_SS_EFF_COMB:
499 ss_eff_comb(&i->effs[e->ss_eff.channel],
500 e->ss_eff.size, e->ss_eff.gain);
501 break;
503 case SONG_EV_SS_EFF_ALLPASS:
505 ss_eff_allpass(&i->effs[e->ss_eff.channel],
506 e->ss_eff.size, e->ss_eff.gain);
507 break;
509 case SONG_EV_SS_EFF_FLANGER:
511 ss_eff_flanger(&i->effs[e->ss_eff.channel],
512 e->ss_eff.size, e->ss_eff.gain,
513 e->ss_eff.depth, e->ss_eff.freq,
514 e->ss_eff.phase);
515 break;
517 case SONG_EV_SS_EFF_WOBBLE:
519 ss_eff_wobble(&i->effs[e->ss_eff.channel],
520 e->ss_eff.freq, e->ss_eff.phase);
522 break;
524 case SONG_EV_SS_EFF_SQWOBBLE:
526 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
527 e->ss_eff.freq, e->ss_eff.phase);
529 break;
531 case SONG_EV_SS_EFF_FADER:
533 ss_eff_fader(&i->effs[e->ss_eff.channel],
534 e->ss_eff.size, e->ss_eff.initial,
535 e->ss_eff.final);
536 break;
538 case SONG_EV_SS_EFF_REVERB:
540 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
541 break;
543 case SONG_EV_SS_EFF_OFF:
545 ss_eff_off(&i->effs[e->ss_eff.channel]);
546 break;
548 case SONG_EV_TEMPO:
550 /* just store the last tempo */
551 tempo=e->tempo.tempo;
552 break;
554 case SONG_EV_SS_PITCH_STRETCH:
557 int n=0;
558 struct ss_wave * w;
559 double freq;
561 /* find the wave */
562 freq=ss_note_frequency(e->ss_pitch_stretch.note);
563 w=ss_ins_find_layer(i, freq, &n);
565 /* calculate optimal frequency */
566 freq=ss_pitch_from_tempo(w, tempo, e->ss_pitch_stretch.len);
568 /* play the note */
569 ss_ins_note_on_by_freq(i, freq,
570 e->ss_pitch_stretch.vol,
571 e->ss_pitch_stretch.note_id);
574 break;
576 case SONG_EV_END:
578 go=0;
579 break;
581 case SONG_EV_MIDI_CHANNEL:
582 case SONG_EV_MIDI_PROGRAM:
583 case SONG_EV_NOTE:
584 case SONG_EV_METER:
585 case SONG_EV_MEASURE:
587 /* never found in ss song streams */
588 break;
591 /* next event */
592 e++;
595 /* reset frame samples */
596 ss_output_init_frame(output);
598 /* generate output from all instruments */
599 for(n=0;n < n_tracks;n++)
600 ss_ins_frame(&song_ins[n], output);
602 /* dump to sampling driver */
603 ss_output_write(output);
605 /* next frame */
606 frame++;
609 return(0);