Added lex and yacc code for the 'pitch_stretch' message.
[ahxm.git] / ss_song.c
blobcf3e027db99b946b1f2dc18384e96d588389bc3c
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_set_channel
80 song_ev_type type; /* SONG_EV_SS_SET_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_set_channel ss_set_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_SET_CHANNEL:
344 break;
346 case SONG_EV_SS_EFF_DELAY:
347 case SONG_EV_SS_EFF_ECHO:
348 case SONG_EV_SS_EFF_COMB:
349 case SONG_EV_SS_EFF_ALLPASS:
350 case SONG_EV_SS_EFF_FLANGER:
351 case SONG_EV_SS_EFF_WOBBLE:
352 case SONG_EV_SS_EFF_SQWOBBLE:
353 case SONG_EV_SS_EFF_FADER:
354 case SONG_EV_SS_EFF_REVERB:
355 case SONG_EV_SS_EFF_OFF:
357 sse.ss_eff.channel=e->ss_eff.channel;
358 sse.ss_eff.size=e->ss_eff.size;
359 sse.ss_eff.gain=e->ss_eff.gain;
360 sse.ss_eff.depth=e->ss_eff.depth;
361 sse.ss_eff.freq=e->ss_eff.freq;
362 sse.ss_eff.phase=e->ss_eff.phase;
363 sse.ss_eff.initial=e->ss_eff.initial;
364 sse.ss_eff.final=e->ss_eff.final;
366 add_ss_ev(&sse);
367 break;
369 case SONG_EV_MIDI_CHANNEL:
370 case SONG_EV_MIDI_PROGRAM:
372 /* ignored */
373 break;
375 case SONG_EV_NOTE_ON:
376 case SONG_EV_NOTE_OFF:
377 case SONG_EV_END:
379 /* never found in generic song streams */
380 break;
384 /* generates an end of event mark, a time after the last one */
385 sse.generic.type=SONG_EV_END;
386 sse.generic.frame=frame + ss_frequency;
387 add_ss_ev(&sse);
389 /* finally sort */
390 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
392 /* return the number of tracks */
393 return(b_track + 1);
397 int ss_song_render(void)
399 union ss_ev * e;
400 int frame;
401 int go;
402 int n;
403 float output[SS_MAX_CHANNELS];
404 int n_tracks;
405 struct ss_ins * i;
406 double tempo=120.0;
408 /* convert the song to ss events */
409 n_tracks=ss_song_convert_events();
411 frame=0;
412 go=1;
413 e=ss_song;
415 /* init the instruments */
416 for(n=0;n < n_tracks;n++)
417 ss_ins_init(&song_ins[n]);
419 /* loop the events */
420 while(go)
422 /* process all events for this exact frame */
423 while(e->generic.frame == frame)
425 /* take the instrument */
426 if(e->generic.trk_id == -1)
427 i=NULL;
428 else
429 i=&song_ins[e->generic.trk_id];
431 switch(e->generic.type)
433 case SONG_EV_NOTE_ON:
435 ss_ins_note_on(i, e->note_on.note,
436 e->note_on.vol, e->note_on.note_id);
438 break;
440 case SONG_EV_NOTE_OFF:
442 ss_ins_note_off(i, e->note_off.note_id);
444 break;
446 case SONG_EV_SS_SUSTAIN:
448 ss_ins_set_sustain(i, e->ss_sustain.sustain);
450 break;
452 case SONG_EV_SS_SET_CHANNEL:
454 break;
456 case SONG_EV_SS_WAV:
459 struct ss_wave * w;
461 w=ss_load_wav_file(e->ss_wav.file,
462 ss_note_frequency(e->ss_wav.base),
463 ss_note_frequency(e->ss_wav.min),
464 ss_note_frequency(e->ss_wav.max),
465 e->ss_wav.loop_start,
466 e->ss_wav.loop_end);
468 ss_ins_add_layer(i, w);
471 break;
473 case SONG_EV_SS_PAT:
475 ss_load_pat_file(i, e->ss_pat.file);
476 break;
478 case SONG_EV_SS_EFF_DELAY:
480 ss_eff_delay(&i->effs[e->ss_eff.channel],
481 e->ss_eff.size);
482 break;
484 case SONG_EV_SS_EFF_ECHO:
486 ss_eff_echo(&i->effs[e->ss_eff.channel],
487 e->ss_eff.size, e->ss_eff.gain);
488 break;
490 case SONG_EV_SS_EFF_COMB:
492 ss_eff_comb(&i->effs[e->ss_eff.channel],
493 e->ss_eff.size, e->ss_eff.gain);
494 break;
496 case SONG_EV_SS_EFF_ALLPASS:
498 ss_eff_allpass(&i->effs[e->ss_eff.channel],
499 e->ss_eff.size, e->ss_eff.gain);
500 break;
502 case SONG_EV_SS_EFF_FLANGER:
504 ss_eff_flanger(&i->effs[e->ss_eff.channel],
505 e->ss_eff.size, e->ss_eff.gain,
506 e->ss_eff.depth, e->ss_eff.freq,
507 e->ss_eff.phase);
508 break;
510 case SONG_EV_SS_EFF_WOBBLE:
512 ss_eff_wobble(&i->effs[e->ss_eff.channel],
513 e->ss_eff.freq, e->ss_eff.phase);
515 break;
517 case SONG_EV_SS_EFF_SQWOBBLE:
519 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
520 e->ss_eff.freq, e->ss_eff.phase);
522 break;
524 case SONG_EV_SS_EFF_FADER:
526 ss_eff_fader(&i->effs[e->ss_eff.channel],
527 e->ss_eff.size, e->ss_eff.initial,
528 e->ss_eff.final);
529 break;
531 case SONG_EV_SS_EFF_REVERB:
533 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
534 break;
536 case SONG_EV_SS_EFF_OFF:
538 ss_eff_off(&i->effs[e->ss_eff.channel]);
539 break;
541 case SONG_EV_TEMPO:
543 /* just store the last tempo */
544 tempo=e->tempo.tempo;
545 break;
547 case SONG_EV_SS_PITCH_STRETCH:
550 int n=0;
551 struct ss_wave * w;
552 double freq;
554 /* find the wave */
555 freq=ss_note_frequency(e->ss_pitch_stretch.note);
556 w=ss_ins_find_layer(i, freq, &n);
558 /* calculate optimal frequency */
559 freq=ss_pitch_from_tempo(w, tempo, e->ss_pitch_stretch.len);
561 /* play the note */
562 ss_ins_note_on_by_freq(i, freq,
563 e->ss_pitch_stretch.vol,
564 e->ss_pitch_stretch.note_id);
567 break;
569 case SONG_EV_END:
571 go=0;
572 break;
574 case SONG_EV_MIDI_CHANNEL:
575 case SONG_EV_MIDI_PROGRAM:
576 case SONG_EV_NOTE:
577 case SONG_EV_METER:
578 case SONG_EV_MEASURE:
580 /* never found in ss song streams */
581 break;
584 /* next event */
585 e++;
588 /* reset frame samples */
589 ss_output_init_frame(output);
591 /* generate output from all instruments */
592 for(n=0;n < n_tracks;n++)
593 ss_ins_frame(&song_ins[n], output);
595 /* dump to sampling driver */
596 ss_output_write(output);
598 /* next frame */
599 frame++;
602 return(0);