Some symbols starting with _ have been renamed, mainly _song and _n_song_ev
[ahxm.git] / ss_song.c
blob038fb9eea79bdc080ba0b0a10235cda3a901e850
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:
343 sse.ss_eff.channel=e->ss_eff.channel;
344 sse.ss_eff.size=e->ss_eff.size;
345 sse.ss_eff.gain=e->ss_eff.gain;
346 sse.ss_eff.depth=e->ss_eff.depth;
347 sse.ss_eff.freq=e->ss_eff.freq;
348 sse.ss_eff.phase=e->ss_eff.phase;
349 sse.ss_eff.initial=e->ss_eff.initial;
350 sse.ss_eff.final=e->ss_eff.final;
352 add_ss_ev(&sse);
353 break;
355 case SONG_EV_MIDI_CHANNEL:
356 case SONG_EV_MIDI_PROGRAM:
358 /* ignored */
359 break;
361 case SONG_EV_NOTE_ON:
362 case SONG_EV_NOTE_OFF:
363 case SONG_EV_END:
365 /* never found in generic song streams */
366 break;
370 /* generates an end of event mark, a time after the last one */
371 sse.generic.type=SONG_EV_END;
372 sse.generic.frame=frame + ss_frequency;
373 add_ss_ev(&sse);
375 /* finally sort */
376 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
378 /* return the number of tracks */
379 return(b_track + 1);
383 int ss_song_render(void)
385 union ss_ev * e;
386 int frame;
387 int go;
388 int n;
389 float output[SS_MAX_CHANNELS];
390 int n_tracks;
391 struct ss_ins * i;
393 /* convert the song to ss events */
394 n_tracks=ss_song_convert_events();
396 frame=0;
397 go=1;
398 e=ss_song;
400 /* init the instruments */
401 for(n=0;n < n_tracks;n++)
402 ss_ins_init(&song_ins[n]);
404 /* loop the events */
405 while(go)
407 /* process all events for this exact frame */
408 while(e->generic.frame == frame)
410 /* take the instrument */
411 if(e->generic.trk_id == -1)
412 i=NULL;
413 else
414 i=&song_ins[e->generic.trk_id];
416 switch(e->generic.type)
418 case SONG_EV_NOTE_ON:
420 ss_ins_note_on(i, e->note_on.note,
421 e->note_on.vol, e->note_on.note_id);
423 break;
425 case SONG_EV_SS_NOTE_ON_BY_TIME:
427 break;
429 case SONG_EV_NOTE_OFF:
431 ss_ins_note_off(i, e->note_off.note_id);
433 break;
435 case SONG_EV_SS_SUSTAIN:
437 ss_ins_set_sustain(i, e->ss_sustain.sustain);
439 break;
441 case SONG_EV_SS_SET_CHANNEL:
443 break;
445 case SONG_EV_SS_WAV:
448 struct ss_wave * w;
450 w=ss_load_wav_file(e->ss_wav.file,
451 ss_note_frequency(e->ss_wav.base),
452 ss_note_frequency(e->ss_wav.min),
453 ss_note_frequency(e->ss_wav.max),
454 e->ss_wav.loop_start,
455 e->ss_wav.loop_end);
457 ss_ins_add_layer(i, w);
460 break;
462 case SONG_EV_SS_PAT:
464 ss_load_pat_file(i, e->ss_pat.file);
465 break;
467 case SONG_EV_SS_EFF_DELAY:
469 ss_eff_delay(&i->effs[e->ss_eff.channel],
470 e->ss_eff.size);
471 break;
473 case SONG_EV_SS_EFF_ECHO:
475 ss_eff_echo(&i->effs[e->ss_eff.channel],
476 e->ss_eff.size, e->ss_eff.gain);
477 break;
479 case SONG_EV_SS_EFF_COMB:
481 ss_eff_comb(&i->effs[e->ss_eff.channel],
482 e->ss_eff.size, e->ss_eff.gain);
483 break;
485 case SONG_EV_SS_EFF_ALLPASS:
487 ss_eff_allpass(&i->effs[e->ss_eff.channel],
488 e->ss_eff.size, e->ss_eff.gain);
489 break;
491 case SONG_EV_SS_EFF_FLANGER:
493 ss_eff_flanger(&i->effs[e->ss_eff.channel],
494 e->ss_eff.size, e->ss_eff.gain,
495 e->ss_eff.depth, e->ss_eff.freq,
496 e->ss_eff.phase);
497 break;
499 case SONG_EV_SS_EFF_WOBBLE:
501 ss_eff_wobble(&i->effs[e->ss_eff.channel],
502 e->ss_eff.freq, e->ss_eff.phase);
504 break;
506 case SONG_EV_SS_EFF_SQWOBBLE:
508 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
509 e->ss_eff.freq, e->ss_eff.phase);
511 break;
513 case SONG_EV_SS_EFF_FADER:
515 ss_eff_fader(&i->effs[e->ss_eff.channel],
516 e->ss_eff.size, e->ss_eff.initial,
517 e->ss_eff.final);
518 break;
520 case SONG_EV_SS_EFF_REVERB:
522 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
523 break;
525 case SONG_EV_END:
527 go=0;
528 break;
530 case SONG_EV_MIDI_CHANNEL:
531 case SONG_EV_MIDI_PROGRAM:
532 case SONG_EV_NOTE:
533 case SONG_EV_TEMPO:
534 case SONG_EV_METER:
535 case SONG_EV_MEASURE:
537 /* never found in ss song streams */
538 break;
541 /* next event */
542 e++;
545 /* reset frame samples */
546 ss_output_init_frame(output);
548 /* generate output from all instruments */
549 for(n=0;n < n_tracks;n++)
550 ss_ins_frame(&song_ins[n], output);
552 /* dump to sampling driver */
553 ss_output_write(output);
555 /* next frame */
556 frame++;
559 return(0);