Added attack support to the compiler and event converters.
[ahxm.git] / ss_song.c
blob8584ef5bdb083600d542e20ea8480fa13b003124
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2006 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 "ahxm.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 */
44 int event_id; /* event id */
47 struct ss_ev_note_off
49 song_ev_type type; /* SONG_EV_NOTE_OFF */
50 int frame;
51 int trk_id;
52 int event_id;
53 int note_id; /* note id */
56 struct ss_ev_note_on
58 song_ev_type type; /* SONG_EV_NOTE_ON */
59 int frame;
60 int trk_id;
61 int event_id;
62 int note_id; /* note id */
63 int note; /* MIDI-like note */
64 sample_t vol; /* volume */
67 struct ss_ev_ss_sustain
69 song_ev_type type; /* SONG_EV_SS_SUSTAIN */
70 int frame;
71 int trk_id;
72 int event_id;
73 double sustain; /* sustain time (in frames) */
76 struct ss_ev_ss_attack
78 song_ev_type type; /* SONG_EV_SS_ATTACK */
79 int frame;
80 int trk_id;
81 int event_id;
82 double attack; /* attack time (in frames) */
85 struct ss_ev_ss_vibrato
87 song_ev_type type; /* SONG_EV_SS_VIBRATO */
88 int frame;
89 int trk_id;
90 int event_id;
91 double vib_depth; /* vibrato depth (in msecs) */
92 double vib_freq; /* vibrato frequency (in Hzs) */
95 struct ss_ev_ss_portamento
97 song_ev_type type; /* SONG_EV_SS_PORTAMENTO */
98 int frame;
99 int trk_id;
100 int event_id;
101 double portamento; /* portamento */
104 struct ss_ev_ss_channel
106 song_ev_type type; /* SONG_EV_SS_CHANNEL */
107 int frame;
108 int trk_id;
109 int event_id;
110 int channel; /* channel */
111 sample_t vol; /* volume */
114 struct ss_ev_ss_wav
116 song_ev_type type; /* SONG_EV_SS_WAV */
117 int frame;
118 int trk_id;
119 int event_id;
120 char * file; /* path to .wav file */
121 int base; /* MIDI-like base note */
122 int min; /* MIDI-like minimum note */
123 int max; /* MIDI-like maximum note */
124 double loop_start; /* loop start */
125 double loop_end; /* loop end */
126 int first_channel; /* first channel to start spreading */
127 int skip_channels; /* channels to skip when spreading */
130 struct ss_ev_ss_pat
132 song_ev_type type; /* SONG_EV_SS_PAT */
133 int frame;
134 int trk_id;
135 int event_id;
136 char * file; /* path to .pat file */
139 struct ss_ev_tempo
141 song_ev_type type; /* SONG_EV_TEMPO */
142 int frame;
143 int trk_id;
144 int event_id;
145 double tempo; /* tempo in bmp */
148 struct ss_ev_pitch_stretch
150 song_ev_type type; /* SONG_EV_SS_PITCH_STRETCH */
151 int frame;
152 int trk_id;
153 int event_id;
154 int note_id; /* note id */
155 int note; /* MIDI-like note (to find the wave) */
156 double len; /* note length (1: whole note) */
157 sample_t vol; /* note volume (1: full volume) */
160 struct ss_ev_print_wave_tempo
162 song_ev_type type; /* SONG_EV_SS_PRINT_WAVE_TEMPO */
163 int frame;
164 int trk_id;
165 int event_id;
166 int note_id; /* note id */
167 int note; /* MIDI-like note (to find the wave) */
168 double len; /* note length (1: whole note) */
171 struct ss_ev_ss_eff
173 song_ev_type type; /* effect type */
174 int frame;
175 int trk_id;
176 int event_id;
177 int channel; /* channel */
178 double size; /* size of effect */
179 sample_t gain; /* gain */
180 double depth; /* depth */
181 double freq; /* freq */
182 double phase; /* phase */
183 sample_t initial; /* initial vol */
184 sample_t final; /* final vol */
187 struct ss_ev_song_info
189 song_ev_type type; /* SONG_EV_SONG_INFO */
190 int frame;
191 int trk_id;
192 int event_id;
193 char * author; /* track author */
194 char * name; /* track name */
197 union ss_ev
199 struct ss_ev_generic generic;
200 struct ss_ev_note_on note_on;
201 struct ss_ev_note_off note_off;
202 struct ss_ev_ss_sustain ss_sustain;
203 struct ss_ev_ss_vibrato ss_vibrato;
204 struct ss_ev_ss_portamento ss_portamento;
205 struct ss_ev_ss_channel ss_channel;
206 struct ss_ev_ss_wav ss_wav;
207 struct ss_ev_ss_pat ss_pat;
208 struct ss_ev_ss_eff ss_eff;
209 struct ss_ev_tempo tempo;
210 struct ss_ev_pitch_stretch ss_pitch_stretch;
211 struct ss_ev_print_wave_tempo ss_print_wave_tempo;
212 struct ss_ev_song_info song_info;
215 /* the softsynth song stream */
217 static union ss_ev * ss_song = NULL;
218 static int n_ss_ev = 0;
221 /* the instruments */
223 struct ss_ins ss_song_ins[SS_MAX_INSTRUMENTS];
225 /*******************
226 Code
227 ********************/
229 static void add_ss_ev(union ss_ev * e)
230 /* adds a softsynth song event */
232 GROW(ss_song, n_ss_ev, union ss_ev);
234 /* store */
235 memcpy(&ss_song[n_ss_ev], e, sizeof(union ss_ev));
237 n_ss_ev++;
241 static int ss_ev_cmp(const void * v1, const void * v2)
242 /* softsynth song event compare function for qsort() */
244 struct ss_ev_generic * e1;
245 struct ss_ev_generic * e2;
246 int ret;
248 e1 = (struct ss_ev_generic *)v1; e2 = (struct ss_ev_generic *)v2;
250 ret = e1->frame - e2->frame;
252 if(ret == 0)
253 ret = e1->type - e2->type;
255 if(ret == 0)
256 ret = e1->event_id - e2->event_id;
258 return(ret);
262 static void ss_song_convert_events(int * n_channels)
263 /* converts generic song_ev events to softsynth events */
265 int note_id = 1;
266 union song_ev * e;
267 union ss_ev sse;
268 int frame, frame_ac, f_frame;
269 double fpw, time_ac, time_ac_m;
270 int num, den;
271 int n;
273 *n_channels = -1;
275 /* resets the ss stream */
276 if(ss_song != NULL)
278 free(ss_song);
279 ss_song = NULL;
282 n_ss_ev = 0;
284 /* sorts the song */
285 song_sort();
287 fpw = 0;
288 frame = frame_ac = f_frame = 0;
289 time_ac = time_ac_m = 0;
290 num = den = 4;
292 /* travels the song events generating softsynth song events */
293 for(n = 0;n < n_song_ev;n++)
295 /* gets the song event */
296 e = &song[n];
298 /* calculates the frame */
299 frame = ((e->generic.time - time_ac) * fpw) + frame_ac;
301 /* generic event data */
302 sse.generic.type = e->generic.type;
303 sse.generic.frame = frame;
304 sse.generic.trk_id = e->generic.trk_id;
305 sse.generic.event_id = e->generic.event_id;
307 switch(e->generic.type)
309 case SONG_EV_TEMPO:
311 /* updates accumulations */
312 frame_ac = frame;
313 time_ac = e->generic.time;
315 /* calculates frames-per-whole based on new tempo */
316 fpw = (double) ss_frequency * 60.0;
317 fpw /= e->tempo.tempo / 4.0;
319 /* adds an event */
320 sse.tempo.tempo = e->tempo.tempo;
321 add_ss_ev(&sse);
323 break;
325 case SONG_EV_METER:
327 /* just store the values */
328 num = e->meter.num;
329 den = e->meter.den;
330 time_ac_m = e->meter.time;
332 break;
334 case SONG_EV_MEASURE:
336 song_test_measure_boundary(e->measure.time - time_ac_m,
337 num, den, e->measure.line);
338 break;
340 case SONG_EV_NOTE:
342 /* convert to note on / off pairs */
344 sse.note_on.type = SONG_EV_NOTE_ON;
345 sse.note_on.note_id = note_id++;
346 sse.note_on.note = e->note.note;
347 sse.note_on.vol = e->note.vol;
349 add_ss_ev(&sse);
351 frame += (int)(e->note.len * fpw);
353 sse.note_off.type = SONG_EV_NOTE_OFF;
354 sse.note_off.frame = frame;
356 add_ss_ev(&sse);
357 break;
359 case SONG_EV_BACK:
361 /* move the cursor back */
363 frame_ac -= (int)(e->back.len * fpw);
365 break;
367 case SONG_EV_SS_PITCH_STRETCH:
369 sse.ss_pitch_stretch.note_id = note_id++;
370 sse.ss_pitch_stretch.note = e->ss_pitch_stretch.note;
371 sse.ss_pitch_stretch.len = e->ss_pitch_stretch.len;
372 sse.ss_pitch_stretch.vol = e->ss_pitch_stretch.vol;
374 add_ss_ev(&sse);
376 frame += (int)(e->ss_pitch_stretch.len * fpw);
378 sse.note_off.type = SONG_EV_NOTE_OFF;
379 sse.note_off.frame = frame;
381 add_ss_ev(&sse);
382 break;
384 case SONG_EV_SS_PRINT_WAVE_TEMPO:
386 sse.ss_print_wave_tempo.note = e->ss_print_wave_tempo.note;
387 sse.ss_print_wave_tempo.len = e->ss_print_wave_tempo.len;
389 add_ss_ev(&sse);
390 break;
392 case SONG_EV_SS_WAV:
394 sse.ss_wav.file = e->ss_wav.file;
395 sse.ss_wav.base = e->ss_wav.base;
396 sse.ss_wav.min = e->ss_wav.min;
397 sse.ss_wav.max = e->ss_wav.max;
398 sse.ss_wav.loop_start = e->ss_wav.loop_start;
399 sse.ss_wav.loop_end = e->ss_wav.loop_end;
400 sse.ss_wav.first_channel = e->ss_wav.first_channel;
401 sse.ss_wav.skip_channels = e->ss_wav.skip_channels;
403 add_ss_ev(&sse);
404 break;
406 case SONG_EV_SS_PAT:
408 sse.ss_pat.file = e->ss_pat.file;
410 add_ss_ev(&sse);
411 break;
413 case SONG_EV_SS_SUSTAIN:
415 sse.ss_sustain.sustain = e->ss_sustain.sustain;
417 add_ss_ev(&sse);
418 break;
420 case SONG_EV_SS_ATTACK:
422 sse.ss_attack.attack = e->ss_attack.attack;
424 add_ss_ev(&sse);
425 break;
427 case SONG_EV_SS_VIBRATO:
429 sse.ss_vibrato.vib_depth = e->ss_vibrato.vib_depth;
430 sse.ss_vibrato.vib_freq = e->ss_vibrato.vib_freq;
432 add_ss_ev(&sse);
433 break;
435 case SONG_EV_SS_PORTAMENTO:
437 sse.ss_portamento.portamento = e->ss_portamento.portamento;
439 add_ss_ev(&sse);
440 break;
442 case SONG_EV_SS_CHANNEL:
444 sse.ss_channel.channel = e->ss_channel.channel;
445 sse.ss_channel.vol = e->ss_channel.vol;
447 /* count channels */
448 if(*n_channels < e->ss_channel.channel)
449 *n_channels = e->ss_channel.channel;
451 add_ss_ev(&sse);
452 break;
454 case SONG_EV_SS_EFF_DELAY:
455 case SONG_EV_SS_EFF_ECHO:
456 case SONG_EV_SS_EFF_COMB:
457 case SONG_EV_SS_EFF_ALLPASS:
458 case SONG_EV_SS_EFF_FLANGER:
459 case SONG_EV_SS_EFF_WOBBLE:
460 case SONG_EV_SS_EFF_SQWOBBLE:
461 case SONG_EV_SS_EFF_HFWOBBLE:
462 case SONG_EV_SS_EFF_FADER:
463 case SONG_EV_SS_EFF_REVERB:
464 case SONG_EV_SS_EFF_FOLDBACK:
465 case SONG_EV_SS_EFF_OFF:
467 sse.ss_eff.channel = e->ss_eff.channel;
468 sse.ss_eff.size = e->ss_eff.size;
469 sse.ss_eff.gain = e->ss_eff.gain;
470 sse.ss_eff.depth = e->ss_eff.depth;
471 sse.ss_eff.freq = e->ss_eff.freq;
472 sse.ss_eff.phase = e->ss_eff.phase;
473 sse.ss_eff.initial = e->ss_eff.initial;
474 sse.ss_eff.final = e->ss_eff.final;
476 add_ss_ev(&sse);
477 break;
479 case SONG_EV_SONG_INFO:
481 sse.song_info.author = e->song_info.author;
482 sse.song_info.name = e->song_info.name;
484 add_ss_ev(&sse);
485 break;
487 case SONG_EV_EOT:
489 add_ss_ev(&sse);
490 break;
492 case SONG_EV_MIDI_CHANNEL:
493 case SONG_EV_MIDI_PROGRAM:
495 /* ignored */
496 break;
498 case SONG_EV_NOTE_ON:
499 case SONG_EV_NOTE_OFF:
500 case SONG_EV_END:
502 /* never found in generic song streams */
503 break;
506 /* store the further frame seen */
507 if(f_frame < frame) f_frame = frame;
510 /* generates an end of event mark, a time after the last one */
511 sse.generic.type = SONG_EV_END;
512 sse.generic.frame = f_frame + ss_frequency;
513 sse.generic.event_id = -1;
514 add_ss_ev(&sse);
516 /* finally sort */
517 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
519 /* count one more */
520 (*n_channels)++;
524 static void ss_song_trace_events(void)
526 union ss_ev * e;
527 int n;
529 printf("** SOFTWARE SYNTHESIZER EVENT DUMP **\n\n");
530 printf("%10s %5s %5s Event and information\n",
531 "Frame", "Track", "Ev.ID");
532 printf("------------------------------------------------------------\n");
534 for(n = 0, e = ss_song;n < n_ss_ev;n++, e++)
536 printf("%10d %5d %5d ",
537 e->generic.frame, e->generic.trk_id, e->generic.event_id);
539 switch(e->generic.type)
541 case SONG_EV_TEMPO:
543 printf("SONG_EV_TEMPO ");
544 printf("%lf", e->tempo.tempo);
545 break;
547 case SONG_EV_SS_SUSTAIN:
549 printf("SONG_EV_SS_SUSTAIN ");
550 printf("SUSTAIN:%lf", e->ss_sustain.sustain);
551 break;
553 case SONG_EV_SS_ATTACK:
555 printf("SONG_EV_SS_ATTACK ");
556 printf("ATTACK:%lf", e->ss_attack.attack);
557 break;
559 case SONG_EV_SS_VIBRATO:
561 printf("SONG_EV_SS_VIBRATO ");
562 printf("DEPTH:%lf FREQ:%lf",
563 e->ss_vibrato.vib_depth,
564 e->ss_vibrato.vib_freq);
565 break;
567 case SONG_EV_SS_PORTAMENTO:
569 printf("SONG_EV_SS_PORTAMENTO ");
570 printf("VALUE:%lf",
571 e->ss_portamento.portamento);
572 break;
574 case SONG_EV_SS_CHANNEL:
576 printf("SONG_EV_SS_CHANNEL ");
577 printf("CHANNEL:%d VOL:%lf",
578 e->ss_channel.channel,
579 e->ss_channel.vol);
580 break;
582 case SONG_EV_SS_WAV:
584 printf("SONG_EV_SS_WAV ");
585 printf("FILE:'%s' BASE:%d MIN:%d MAX:%d START:%lf END:%lf",
586 e->ss_wav.file, e->ss_wav.base,
587 e->ss_wav.min, e->ss_wav.max,
588 e->ss_wav.loop_start, e->ss_wav.loop_end);
589 break;
591 case SONG_EV_SS_PAT:
593 printf("SONG_EV_SS_PAT ");
594 printf("FILE:'%s'", e->ss_pat.file);
595 break;
597 case SONG_EV_SS_EFF_DELAY:
599 printf("SONG_EV_SS_EFF_DELAY ");
600 printf("CHANNEL:%d ", e->ss_eff.channel);
601 printf("SIZE:%lf ", e->ss_eff.size);
602 break;
604 case SONG_EV_SS_EFF_ECHO:
606 printf("SONG_EV_SS_EFF_ECHO ");
607 printf("CHANNEL:%d ", e->ss_eff.channel);
608 printf("SIZE:%lf ", e->ss_eff.size);
609 printf("GAIN:%f ", e->ss_eff.gain);
610 break;
612 case SONG_EV_SS_EFF_COMB:
614 printf("SONG_EV_SS_EFF_COMB ");
615 printf("CHANNEL:%d ", e->ss_eff.channel);
616 printf("SIZE:%lf ", e->ss_eff.size);
617 printf("GAIN:%f ", e->ss_eff.gain);
618 break;
620 case SONG_EV_SS_EFF_ALLPASS:
622 printf("SONG_EV_SS_EFF_ALLPASS ");
623 printf("CHANNEL:%d ", e->ss_eff.channel);
624 printf("SIZE:%lf ", e->ss_eff.size);
625 printf("GAIN:%f ", e->ss_eff.gain);
626 break;
628 case SONG_EV_SS_EFF_FLANGER:
630 printf("SONG_EV_SS_EFF_FLANGER ");
631 printf("CHANNEL:%d ", e->ss_eff.channel);
632 printf("SIZE:%lf ", e->ss_eff.size);
633 printf("GAIN:%f ", e->ss_eff.gain);
634 printf("DEPTH:%lf ", e->ss_eff.depth);
635 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
636 e->ss_eff.phase);
637 break;
639 case SONG_EV_SS_EFF_WOBBLE:
641 printf("SONG_EV_SS_EFF_WOBBLE ");
642 printf("CHANNEL:%d ", e->ss_eff.channel);
643 printf("FREQ:%lf PHASE:%lf GAIN:%lf", e->ss_eff.freq,
644 e->ss_eff.phase, e->ss_eff.gain);
645 break;
647 case SONG_EV_SS_EFF_SQWOBBLE:
649 printf("SONG_EV_SS_EFF_SQWOBBLE ");
650 printf("CHANNEL:%d ", e->ss_eff.channel);
651 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
652 e->ss_eff.phase);
653 break;
655 case SONG_EV_SS_EFF_HFWOBBLE:
657 printf("SONG_EV_SS_EFF_HFWOBBLE ");
658 printf("CHANNEL:%d ", e->ss_eff.channel);
659 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
660 e->ss_eff.phase);
661 break;
663 case SONG_EV_SS_EFF_FADER:
665 printf("SONG_EV_SS_EFF_FADER ");
666 printf("CHANNEL:%d ", e->ss_eff.channel);
667 printf("SIZE:%lf ", e->ss_eff.size);
668 printf("INITIAL:%f FINAL:%f", e->ss_eff.initial,
669 e->ss_eff.final);
670 break;
672 case SONG_EV_SS_EFF_REVERB:
674 printf("SONG_EV_SS_EFF_REVERB ");
675 printf("CHANNEL:%d ", e->ss_eff.channel);
676 break;
678 case SONG_EV_SS_EFF_OFF:
680 printf("SONG_EV_SS_EFF_OFF ");
681 printf("CHANNEL:%d ", e->ss_eff.channel);
682 break;
684 case SONG_EV_SS_PITCH_STRETCH:
686 printf("SONG_EV_SS_PITCH_STRETCH ");
687 printf("MIDI:%d LEN:%lf VOL:%f",
688 e->ss_pitch_stretch.note,
689 e->ss_pitch_stretch.len,
690 e->ss_pitch_stretch.vol);
692 break;
694 case SONG_EV_SS_PRINT_WAVE_TEMPO:
696 printf("SONG_EV_SS_PRINT_WAVE_TEMPO ");
697 printf("MIDI:%d LEN:%lf",
698 e->ss_print_wave_tempo.note,
699 e->ss_print_wave_tempo.len);
700 break;
702 case SONG_EV_SONG_INFO:
704 printf("SONG_EV_SONG_INFO ");
705 printf("AUTHOR:'%s' NAME:'%s'",
706 e->song_info.author,
707 e->song_info.name);
708 break;
710 case SONG_EV_NOTE_ON:
712 printf("SONG_EV_NOTE_ON ");
713 printf("ID:%d MIDI:%d VOL:%f",
714 e->note_on.note_id, e->note_on.note,
715 e->note_on.vol);
716 break;
718 case SONG_EV_NOTE_OFF:
720 printf("SONG_EV_NOTE_OFF ");
721 printf("ID:%d", e->note_off.note_id);
722 break;
724 case SONG_EV_EOT:
726 printf("SONG_EV_EOT ");
727 break;
729 case SONG_EV_END:
731 printf("SONG_EV_END ");
732 break;
734 default:
735 printf("** Unexpected type: %d",
736 e->generic.type);
739 printf("\n");
742 printf("\n");
746 static union ss_ev * process_this_frame_events(union ss_ev * e, int skip_frames)
747 /* process the events attached to this frame */
749 static double tempo = 120.0;
750 static int frame = 0;
752 /* from the beginning? */
753 if(e == NULL)
755 e = ss_song;
756 tempo = 120.0;
757 frame = 0;
760 if(verbose >= 1)
762 if(frame % ss_frequency == 0)
764 int m = frame / ss_frequency;
765 printf("[%02d:%02d]\r", m / 60, m % 60);
766 fflush(stdout);
770 while(e != NULL && e->generic.frame == frame)
772 struct ss_ins * i;
773 struct ss_wave * w;
774 double freq;
776 if(e->generic.type == SONG_EV_NOTE_ON ||
777 e->generic.type == SONG_EV_NOTE_OFF ||
778 e->generic.type == SONG_EV_SS_PITCH_STRETCH)
780 if(frame < skip_frames)
782 e++;
783 frame = e->generic.frame;
784 continue;
788 /* take the instrument */
789 if(e->generic.trk_id < 0)
790 i = NULL;
791 else
792 i = &ss_song_ins[e->generic.trk_id];
794 switch(e->generic.type)
796 case SONG_EV_NOTE_ON:
798 if(ss_ins_note_on(i, e->note_on.note,
799 e->note_on.vol, e->note_on.note_id) < 0 &&
800 verbose >= 1)
801 printf("ss_ins_note_on error: track %d note %d\n",
802 e->note_on.trk_id, e->note_on.note);
804 break;
806 case SONG_EV_NOTE_OFF:
808 ss_ins_note_off(i, e->note_off.note_id);
810 break;
812 case SONG_EV_SS_SUSTAIN:
814 ss_ins_set_sustain(i, e->ss_sustain.sustain);
816 break;
818 case SONG_EV_SS_ATTACK:
820 ss_ins_set_attack(i, e->ss_attack.attack);
822 break;
824 case SONG_EV_SS_VIBRATO:
826 ss_ins_set_vibrato(i, e->ss_vibrato.vib_depth,
827 e->ss_vibrato.vib_freq);
829 break;
831 case SONG_EV_SS_PORTAMENTO:
833 ss_ins_set_portamento(i,
834 (e->ss_portamento.portamento * 44100.0)
835 / ((double) ss_frequency * 1000000.0));
837 break;
839 case SONG_EV_SS_CHANNEL:
841 ss_ins_set_channel(i, e->ss_channel.channel,
842 e->ss_channel.vol);
844 break;
846 case SONG_EV_SS_WAV:
848 w = ss_load_wav_file(e->ss_wav.file,
849 ss_note_frequency(e->ss_wav.base),
850 ss_note_frequency(e->ss_wav.min),
851 ss_note_frequency(e->ss_wav.max),
852 e->ss_wav.loop_start, e->ss_wav.loop_end,
853 e->ss_wav.first_channel, e->ss_wav.skip_channels);
855 /* fail if can't open wav */
856 if(w == NULL)
858 printf("Can't load wav '%s'\n", e->ss_wav.file);
859 e = NULL;
861 else
862 ss_ins_add_layer(i, w);
864 break;
866 case SONG_EV_SS_PAT:
868 if(ss_load_pat_file(i, e->ss_pat.file) < 0)
870 printf("Can't load pat '%s'\n", e->ss_pat.file);
871 e = NULL;
874 break;
876 case SONG_EV_SS_EFF_DELAY:
878 ss_eff_delay(&i->effs[e->ss_eff.channel], e->ss_eff.size);
879 break;
881 case SONG_EV_SS_EFF_ECHO:
883 ss_eff_echo(&i->effs[e->ss_eff.channel],
884 e->ss_eff.size, e->ss_eff.gain);
885 break;
887 case SONG_EV_SS_EFF_COMB:
889 ss_eff_comb(&i->effs[e->ss_eff.channel],
890 e->ss_eff.size, e->ss_eff.gain);
891 break;
893 case SONG_EV_SS_EFF_ALLPASS:
895 ss_eff_allpass(&i->effs[e->ss_eff.channel],
896 e->ss_eff.size, e->ss_eff.gain);
897 break;
899 case SONG_EV_SS_EFF_FLANGER:
901 ss_eff_flanger(&i->effs[e->ss_eff.channel],
902 e->ss_eff.size, e->ss_eff.gain,
903 e->ss_eff.depth, e->ss_eff.freq,
904 e->ss_eff.phase);
905 break;
907 case SONG_EV_SS_EFF_WOBBLE:
909 ss_eff_wobble(&i->effs[e->ss_eff.channel],
910 e->ss_eff.freq, e->ss_eff.phase,
911 e->ss_eff.gain);
913 break;
915 case SONG_EV_SS_EFF_SQWOBBLE:
917 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
918 e->ss_eff.freq, e->ss_eff.phase);
920 break;
922 case SONG_EV_SS_EFF_HFWOBBLE:
924 ss_eff_half_wobble(&i->effs[e->ss_eff.channel],
925 e->ss_eff.freq, e->ss_eff.phase);
927 break;
929 case SONG_EV_SS_EFF_FADER:
931 ss_eff_fader(&i->effs[e->ss_eff.channel],
932 e->ss_eff.size, e->ss_eff.initial,
933 e->ss_eff.final);
934 break;
936 case SONG_EV_SS_EFF_REVERB:
938 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
939 break;
941 case SONG_EV_SS_EFF_FOLDBACK:
943 ss_eff_foldback(&i->effs[e->ss_eff.channel],
944 e->ss_eff.gain);
945 break;
947 case SONG_EV_SS_EFF_OFF:
949 ss_eff_off(&i->effs[e->ss_eff.channel]);
950 break;
952 case SONG_EV_TEMPO:
954 /* just store the last tempo */
955 tempo = e->tempo.tempo;
956 break;
958 case SONG_EV_SS_PITCH_STRETCH:
960 /* find the wave */
961 freq = ss_note_frequency(e->ss_pitch_stretch.note);
962 w = ss_ins_find_layer(i, freq, NULL);
964 /* calculate optimal frequency */
965 freq = ss_pitch_from_tempo(w, tempo,
966 e->ss_pitch_stretch.len);
968 /* play the note */
969 if(ss_ins_play(i, freq, e->ss_pitch_stretch.vol,
970 e->ss_pitch_stretch.note_id, w) < 0 &&
971 verbose >= 1)
972 printf("ss_ins_play error: track %d freq %f\n",
973 e->ss_pitch_stretch.trk_id, freq);
975 break;
977 case SONG_EV_SS_PRINT_WAVE_TEMPO:
979 /* find the wave */
980 freq = ss_note_frequency(e->ss_print_wave_tempo.note);
981 w = ss_ins_find_layer(i, freq, NULL);
983 /* print the optimal tempo */
984 printf("Optimal tempo: %lf\n",
985 ss_tempo_from_wave(w,
986 e->ss_print_wave_tempo.note,
987 e->ss_print_wave_tempo.len));
989 break;
991 case SONG_EV_SONG_INFO:
993 /* add a new song (track) */
994 cue_file_song_info(frame, e->song_info.author,
995 e->song_info.name);
996 break;
998 case SONG_EV_EOT:
1000 /* end of track; trigger possible cleaning */
1001 ss_ins_disable(i);
1002 break;
1004 case SONG_EV_END:
1006 e = NULL;
1007 break;
1009 case SONG_EV_BACK:
1010 case SONG_EV_MIDI_CHANNEL:
1011 case SONG_EV_MIDI_PROGRAM:
1012 case SONG_EV_NOTE:
1013 case SONG_EV_METER:
1014 case SONG_EV_MEASURE:
1016 /* never found in ss song streams */
1017 break;
1020 /* next event */
1021 if(e) e++;
1024 frame++;
1026 return(e);
1030 int ss_song_render(int skip_secs, char * driver, char * devfile)
1032 int n, i = 0;
1033 sample_t output[SS_MAX_CHANNELS];
1034 int skip_frames;
1035 int n_channels;
1036 union ss_ev * e = NULL;
1038 /* convert the song to ss events */
1039 ss_song_convert_events(&n_channels);
1041 if(verbose >= 2)
1042 printf("Tracks: %d Channels: %d Events: %d\n",
1043 n_song_tracks, n_channels, n_ss_ev);
1045 if(trace)
1047 ss_song_trace_events();
1048 return(0);
1051 /* set the number of channels, unless forced */
1052 if(ss_nchannels == -1)
1053 ss_nchannels = n_channels > 0 ? n_channels : 2;
1055 if(ss_output_open(driver, devfile) < 0)
1057 printf("Error: can't init driver\n");
1058 return(2);
1061 /* init the generators */
1062 ss_gen_init();
1064 /* init the instruments */
1065 for(n = 0;n < n_song_tracks;n++)
1066 ss_ins_init(&ss_song_ins[n]);
1068 /* calculate the frame to start playing */
1069 skip_frames = skip_secs * ss_frequency;
1071 /* main loop */
1074 /* process all events in this frame */
1075 /* if((e = process_this_frame_events(e, skip_frames)) == NULL)
1076 break;*/
1077 e = process_this_frame_events(e, skip_frames);
1079 /* reset frame samples */
1080 ss_output_init_frame(output);
1082 /* generate output from all instruments */
1083 for(n = i = 0;n < n_song_tracks;n++)
1084 i += ss_ins_frame(&ss_song_ins[n], output);
1086 /* dump to sampling driver */
1087 ss_output_write(output);
1088 } while(i);
1090 if(verbose >= 1) printf("\n");
1092 ss_output_close();
1094 return(0);