Don't generate 'mark-overpassed' errors if moving to the END mark.
[ahxm.git] / ss_song.c
blob61aae87cfcb6aa88c50ed95d2bf381e73f970b44
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_vibrato
78 song_ev_type type; /* SONG_EV_SS_VIBRATO */
79 int frame;
80 int trk_id;
81 int event_id;
82 double vib_depth; /* vibrato depth (in msecs) */
83 double vib_freq; /* vibrato frequency (in Hzs) */
86 struct ss_ev_ss_portamento
88 song_ev_type type; /* SONG_EV_SS_PORTAMENTO */
89 int frame;
90 int trk_id;
91 int event_id;
92 double portamento; /* portamento */
95 struct ss_ev_ss_channel
97 song_ev_type type; /* SONG_EV_SS_CHANNEL */
98 int frame;
99 int trk_id;
100 int event_id;
101 int channel; /* channel */
102 sample_t vol; /* volume */
105 struct ss_ev_ss_wav
107 song_ev_type type; /* SONG_EV_SS_WAV */
108 int frame;
109 int trk_id;
110 int event_id;
111 char * file; /* path to .wav file */
112 int base; /* MIDI-like base note */
113 int min; /* MIDI-like minimum note */
114 int max; /* MIDI-like maximum note */
115 double loop_start; /* loop start */
116 double loop_end; /* loop end */
117 int first_channel; /* first channel to start spreading */
118 int skip_channels; /* channels to skip when spreading */
121 struct ss_ev_ss_pat
123 song_ev_type type; /* SONG_EV_SS_PAT */
124 int frame;
125 int trk_id;
126 int event_id;
127 char * file; /* path to .pat file */
130 struct ss_ev_tempo
132 song_ev_type type; /* SONG_EV_TEMPO */
133 int frame;
134 int trk_id;
135 int event_id;
136 double tempo; /* tempo in bmp */
139 struct ss_ev_pitch_stretch
141 song_ev_type type; /* SONG_EV_SS_PITCH_STRETCH */
142 int frame;
143 int trk_id;
144 int event_id;
145 int note_id; /* note id */
146 int note; /* MIDI-like note (to find the wave) */
147 double len; /* note length (1: whole note) */
148 sample_t vol; /* note volume (1: full volume) */
151 struct ss_ev_print_wave_tempo
153 song_ev_type type; /* SONG_EV_SS_PRINT_WAVE_TEMPO */
154 int frame;
155 int trk_id;
156 int event_id;
157 int note_id; /* note id */
158 int note; /* MIDI-like note (to find the wave) */
159 double len; /* note length (1: whole note) */
162 struct ss_ev_ss_eff
164 song_ev_type type; /* effect type */
165 int frame;
166 int trk_id;
167 int event_id;
168 int channel; /* channel */
169 double size; /* size of effect */
170 sample_t gain; /* gain */
171 double depth; /* depth */
172 double freq; /* freq */
173 double phase; /* phase */
174 sample_t initial; /* initial vol */
175 sample_t final; /* final vol */
178 struct ss_ev_song_info
180 song_ev_type type; /* SONG_EV_SONG_INFO */
181 int frame;
182 int trk_id;
183 int event_id;
184 char * author; /* track author */
185 char * name; /* track name */
188 union ss_ev
190 struct ss_ev_generic generic;
191 struct ss_ev_note_on note_on;
192 struct ss_ev_note_off note_off;
193 struct ss_ev_ss_sustain ss_sustain;
194 struct ss_ev_ss_vibrato ss_vibrato;
195 struct ss_ev_ss_portamento ss_portamento;
196 struct ss_ev_ss_channel ss_channel;
197 struct ss_ev_ss_wav ss_wav;
198 struct ss_ev_ss_pat ss_pat;
199 struct ss_ev_ss_eff ss_eff;
200 struct ss_ev_tempo tempo;
201 struct ss_ev_pitch_stretch ss_pitch_stretch;
202 struct ss_ev_print_wave_tempo ss_print_wave_tempo;
203 struct ss_ev_song_info song_info;
206 /* the softsynth song stream */
208 static union ss_ev * ss_song = NULL;
209 static int n_ss_ev = 0;
212 /* the instruments */
214 struct ss_ins ss_song_ins[SS_MAX_INSTRUMENTS];
216 /*******************
217 Code
218 ********************/
220 static void add_ss_ev(union ss_ev * e)
221 /* adds a softsynth song event */
223 GROW(ss_song, n_ss_ev, union ss_ev);
225 /* store */
226 memcpy(&ss_song[n_ss_ev], e, sizeof(union ss_ev));
228 n_ss_ev++;
232 static int ss_ev_cmp(const void * v1, const void * v2)
233 /* softsynth song event compare function for qsort() */
235 struct ss_ev_generic * e1;
236 struct ss_ev_generic * e2;
237 int ret;
239 e1 = (struct ss_ev_generic *)v1; e2 = (struct ss_ev_generic *)v2;
241 ret = e1->frame - e2->frame;
243 if(ret == 0)
244 ret = e1->type - e2->type;
246 if(ret == 0)
247 ret = e1->event_id - e2->event_id;
249 return(ret);
253 static void ss_song_convert_events(int * n_channels)
254 /* converts generic song_ev events to softsynth events */
256 int note_id = 1;
257 union song_ev * e;
258 union ss_ev sse;
259 int frame, frame_ac, f_frame;
260 double fpw, time_ac, time_ac_m;
261 int num, den;
262 int n;
264 *n_channels = -1;
266 /* resets the ss stream */
267 if(ss_song != NULL)
269 free(ss_song);
270 ss_song = NULL;
273 n_ss_ev = 0;
275 /* sorts the song */
276 song_sort();
278 fpw = 0;
279 frame = frame_ac = f_frame = 0;
280 time_ac = time_ac_m = 0;
281 num = den = 4;
283 /* travels the song events generating softsynth song events */
284 for(n = 0;n < n_song_ev;n++)
286 /* gets the song event */
287 e = &song[n];
289 /* calculates the frame */
290 frame = ((e->generic.time - time_ac) * fpw) + frame_ac;
292 /* generic event data */
293 sse.generic.type = e->generic.type;
294 sse.generic.frame = frame;
295 sse.generic.trk_id = e->generic.trk_id;
296 sse.generic.event_id = e->generic.event_id;
298 switch(e->generic.type)
300 case SONG_EV_TEMPO:
302 /* updates accumulations */
303 frame_ac = frame;
304 time_ac = e->generic.time;
306 /* calculates frames-per-whole based on new tempo */
307 fpw = (double) ss_frequency * 60.0;
308 fpw /= e->tempo.tempo / 4.0;
310 /* adds an event */
311 sse.tempo.tempo = e->tempo.tempo;
312 add_ss_ev(&sse);
314 break;
316 case SONG_EV_METER:
318 /* just store the values */
319 num = e->meter.num;
320 den = e->meter.den;
321 time_ac_m = e->meter.time;
323 break;
325 case SONG_EV_MEASURE:
327 song_test_measure_boundary(e->measure.time - time_ac_m,
328 num, den, e->measure.line);
329 break;
331 case SONG_EV_NOTE:
333 /* convert to note on / off pairs */
335 sse.note_on.type = SONG_EV_NOTE_ON;
336 sse.note_on.note_id = note_id++;
337 sse.note_on.note = e->note.note;
338 sse.note_on.vol = e->note.vol;
340 add_ss_ev(&sse);
342 frame += (int)(e->note.len * fpw);
344 sse.note_off.type = SONG_EV_NOTE_OFF;
345 sse.note_off.frame = frame;
347 add_ss_ev(&sse);
348 break;
350 case SONG_EV_BACK:
352 /* move the cursor back */
354 frame_ac -= (int)(e->back.len * fpw);
356 break;
358 case SONG_EV_SS_PITCH_STRETCH:
360 sse.ss_pitch_stretch.note_id = note_id++;
361 sse.ss_pitch_stretch.note = e->ss_pitch_stretch.note;
362 sse.ss_pitch_stretch.len = e->ss_pitch_stretch.len;
363 sse.ss_pitch_stretch.vol = e->ss_pitch_stretch.vol;
365 add_ss_ev(&sse);
367 frame += (int)(e->ss_pitch_stretch.len * fpw);
369 sse.note_off.type = SONG_EV_NOTE_OFF;
370 sse.note_off.frame = frame;
372 add_ss_ev(&sse);
373 break;
375 case SONG_EV_SS_PRINT_WAVE_TEMPO:
377 sse.ss_print_wave_tempo.note = e->ss_print_wave_tempo.note;
378 sse.ss_print_wave_tempo.len = e->ss_print_wave_tempo.len;
380 add_ss_ev(&sse);
381 break;
383 case SONG_EV_SS_WAV:
385 sse.ss_wav.file = e->ss_wav.file;
386 sse.ss_wav.base = e->ss_wav.base;
387 sse.ss_wav.min = e->ss_wav.min;
388 sse.ss_wav.max = e->ss_wav.max;
389 sse.ss_wav.loop_start = e->ss_wav.loop_start;
390 sse.ss_wav.loop_end = e->ss_wav.loop_end;
391 sse.ss_wav.first_channel = e->ss_wav.first_channel;
392 sse.ss_wav.skip_channels = e->ss_wav.skip_channels;
394 add_ss_ev(&sse);
395 break;
397 case SONG_EV_SS_PAT:
399 sse.ss_pat.file = e->ss_pat.file;
401 add_ss_ev(&sse);
402 break;
404 case SONG_EV_SS_SUSTAIN:
406 sse.ss_sustain.sustain = e->ss_sustain.sustain;
408 add_ss_ev(&sse);
409 break;
411 case SONG_EV_SS_VIBRATO:
413 sse.ss_vibrato.vib_depth = e->ss_vibrato.vib_depth;
414 sse.ss_vibrato.vib_freq = e->ss_vibrato.vib_freq;
416 add_ss_ev(&sse);
417 break;
419 case SONG_EV_SS_PORTAMENTO:
421 sse.ss_portamento.portamento = e->ss_portamento.portamento;
423 add_ss_ev(&sse);
424 break;
426 case SONG_EV_SS_CHANNEL:
428 sse.ss_channel.channel = e->ss_channel.channel;
429 sse.ss_channel.vol = e->ss_channel.vol;
431 /* count channels */
432 if(*n_channels < e->ss_channel.channel)
433 *n_channels = e->ss_channel.channel;
435 add_ss_ev(&sse);
436 break;
438 case SONG_EV_SS_EFF_DELAY:
439 case SONG_EV_SS_EFF_ECHO:
440 case SONG_EV_SS_EFF_COMB:
441 case SONG_EV_SS_EFF_ALLPASS:
442 case SONG_EV_SS_EFF_FLANGER:
443 case SONG_EV_SS_EFF_WOBBLE:
444 case SONG_EV_SS_EFF_SQWOBBLE:
445 case SONG_EV_SS_EFF_HFWOBBLE:
446 case SONG_EV_SS_EFF_FADER:
447 case SONG_EV_SS_EFF_REVERB:
448 case SONG_EV_SS_EFF_FOLDBACK:
449 case SONG_EV_SS_EFF_OFF:
451 sse.ss_eff.channel = e->ss_eff.channel;
452 sse.ss_eff.size = e->ss_eff.size;
453 sse.ss_eff.gain = e->ss_eff.gain;
454 sse.ss_eff.depth = e->ss_eff.depth;
455 sse.ss_eff.freq = e->ss_eff.freq;
456 sse.ss_eff.phase = e->ss_eff.phase;
457 sse.ss_eff.initial = e->ss_eff.initial;
458 sse.ss_eff.final = e->ss_eff.final;
460 add_ss_ev(&sse);
461 break;
463 case SONG_EV_SONG_INFO:
465 sse.song_info.author = e->song_info.author;
466 sse.song_info.name = e->song_info.name;
468 add_ss_ev(&sse);
469 break;
471 case SONG_EV_EOT:
473 add_ss_ev(&sse);
474 break;
476 case SONG_EV_MIDI_CHANNEL:
477 case SONG_EV_MIDI_PROGRAM:
479 /* ignored */
480 break;
482 case SONG_EV_NOTE_ON:
483 case SONG_EV_NOTE_OFF:
484 case SONG_EV_END:
486 /* never found in generic song streams */
487 break;
490 /* store the further frame seen */
491 if(f_frame < frame) f_frame = frame;
494 /* generates an end of event mark, a time after the last one */
495 sse.generic.type = SONG_EV_END;
496 sse.generic.frame = f_frame + ss_frequency;
497 sse.generic.event_id = -1;
498 add_ss_ev(&sse);
500 /* finally sort */
501 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
503 /* count one more */
504 (*n_channels)++;
508 static void ss_song_trace_events(void)
510 union ss_ev * e;
511 int n;
513 printf("** SOFTWARE SYNTHESIZER EVENT DUMP **\n\n");
514 printf("%10s %5s %5s Event and information\n",
515 "Frame", "Track", "Ev.ID");
516 printf("------------------------------------------------------------\n");
518 for(n = 0, e = ss_song;n < n_ss_ev;n++, e++)
520 printf("%10d %5d %5d ",
521 e->generic.frame, e->generic.trk_id, e->generic.event_id);
523 switch(e->generic.type)
525 case SONG_EV_TEMPO:
527 printf("SONG_EV_TEMPO ");
528 printf("%lf", e->tempo.tempo);
529 break;
531 case SONG_EV_SS_SUSTAIN:
533 printf("SONG_EV_SS_SUSTAIN ");
534 printf("SUSTAIN:%lf", e->ss_sustain.sustain);
535 break;
537 case SONG_EV_SS_VIBRATO:
539 printf("SONG_EV_SS_VIBRATO ");
540 printf("DEPTH:%lf FREQ:%lf",
541 e->ss_vibrato.vib_depth,
542 e->ss_vibrato.vib_freq);
543 break;
545 case SONG_EV_SS_PORTAMENTO:
547 printf("SONG_EV_SS_PORTAMENTO ");
548 printf("VALUE:%lf",
549 e->ss_portamento.portamento);
550 break;
552 case SONG_EV_SS_CHANNEL:
554 printf("SONG_EV_SS_CHANNEL ");
555 printf("CHANNEL:%d VOL:%lf",
556 e->ss_channel.channel,
557 e->ss_channel.vol);
558 break;
560 case SONG_EV_SS_WAV:
562 printf("SONG_EV_SS_WAV ");
563 printf("FILE:'%s' BASE:%d MIN:%d MAX:%d START:%lf END:%lf",
564 e->ss_wav.file, e->ss_wav.base,
565 e->ss_wav.min, e->ss_wav.max,
566 e->ss_wav.loop_start, e->ss_wav.loop_end);
567 break;
569 case SONG_EV_SS_PAT:
571 printf("SONG_EV_SS_PAT ");
572 printf("FILE:'%s'", e->ss_pat.file);
573 break;
575 case SONG_EV_SS_EFF_DELAY:
577 printf("SONG_EV_SS_EFF_DELAY ");
578 printf("CHANNEL:%d ", e->ss_eff.channel);
579 printf("SIZE:%lf ", e->ss_eff.size);
580 break;
582 case SONG_EV_SS_EFF_ECHO:
584 printf("SONG_EV_SS_EFF_ECHO ");
585 printf("CHANNEL:%d ", e->ss_eff.channel);
586 printf("SIZE:%lf ", e->ss_eff.size);
587 printf("GAIN:%f ", e->ss_eff.gain);
588 break;
590 case SONG_EV_SS_EFF_COMB:
592 printf("SONG_EV_SS_EFF_COMB ");
593 printf("CHANNEL:%d ", e->ss_eff.channel);
594 printf("SIZE:%lf ", e->ss_eff.size);
595 printf("GAIN:%f ", e->ss_eff.gain);
596 break;
598 case SONG_EV_SS_EFF_ALLPASS:
600 printf("SONG_EV_SS_EFF_ALLPASS ");
601 printf("CHANNEL:%d ", e->ss_eff.channel);
602 printf("SIZE:%lf ", e->ss_eff.size);
603 printf("GAIN:%f ", e->ss_eff.gain);
604 break;
606 case SONG_EV_SS_EFF_FLANGER:
608 printf("SONG_EV_SS_EFF_FLANGER ");
609 printf("CHANNEL:%d ", e->ss_eff.channel);
610 printf("SIZE:%lf ", e->ss_eff.size);
611 printf("GAIN:%f ", e->ss_eff.gain);
612 printf("DEPTH:%lf ", e->ss_eff.depth);
613 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
614 e->ss_eff.phase);
615 break;
617 case SONG_EV_SS_EFF_WOBBLE:
619 printf("SONG_EV_SS_EFF_WOBBLE ");
620 printf("CHANNEL:%d ", e->ss_eff.channel);
621 printf("FREQ:%lf PHASE:%lf GAIN:%lf", e->ss_eff.freq,
622 e->ss_eff.phase, e->ss_eff.gain);
623 break;
625 case SONG_EV_SS_EFF_SQWOBBLE:
627 printf("SONG_EV_SS_EFF_SQWOBBLE ");
628 printf("CHANNEL:%d ", e->ss_eff.channel);
629 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
630 e->ss_eff.phase);
631 break;
633 case SONG_EV_SS_EFF_HFWOBBLE:
635 printf("SONG_EV_SS_EFF_HFWOBBLE ");
636 printf("CHANNEL:%d ", e->ss_eff.channel);
637 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
638 e->ss_eff.phase);
639 break;
641 case SONG_EV_SS_EFF_FADER:
643 printf("SONG_EV_SS_EFF_FADER ");
644 printf("CHANNEL:%d ", e->ss_eff.channel);
645 printf("SIZE:%lf ", e->ss_eff.size);
646 printf("INITIAL:%f FINAL:%f", e->ss_eff.initial,
647 e->ss_eff.final);
648 break;
650 case SONG_EV_SS_EFF_REVERB:
652 printf("SONG_EV_SS_EFF_REVERB ");
653 printf("CHANNEL:%d ", e->ss_eff.channel);
654 break;
656 case SONG_EV_SS_EFF_OFF:
658 printf("SONG_EV_SS_EFF_OFF ");
659 printf("CHANNEL:%d ", e->ss_eff.channel);
660 break;
662 case SONG_EV_SS_PITCH_STRETCH:
664 printf("SONG_EV_SS_PITCH_STRETCH ");
665 printf("MIDI:%d LEN:%lf VOL:%f",
666 e->ss_pitch_stretch.note,
667 e->ss_pitch_stretch.len,
668 e->ss_pitch_stretch.vol);
670 break;
672 case SONG_EV_SS_PRINT_WAVE_TEMPO:
674 printf("SONG_EV_SS_PRINT_WAVE_TEMPO ");
675 printf("MIDI:%d LEN:%lf",
676 e->ss_print_wave_tempo.note,
677 e->ss_print_wave_tempo.len);
678 break;
680 case SONG_EV_SONG_INFO:
682 printf("SONG_EV_SONG_INFO ");
683 printf("AUTHOR:'%s' NAME:'%s'",
684 e->song_info.author,
685 e->song_info.name);
686 break;
688 case SONG_EV_NOTE_ON:
690 printf("SONG_EV_NOTE_ON ");
691 printf("ID:%d MIDI:%d VOL:%f",
692 e->note_on.note_id, e->note_on.note,
693 e->note_on.vol);
694 break;
696 case SONG_EV_NOTE_OFF:
698 printf("SONG_EV_NOTE_OFF ");
699 printf("ID:%d", e->note_off.note_id);
700 break;
702 case SONG_EV_EOT:
704 printf("SONG_EV_EOT ");
705 break;
707 case SONG_EV_END:
709 printf("SONG_EV_END ");
710 break;
712 default:
713 printf("** Unexpected type: %d",
714 e->generic.type);
717 printf("\n");
720 printf("\n");
724 static union ss_ev * process_this_frame_events(union ss_ev * e, int skip_frames)
725 /* process the events attached to this frame */
727 static double tempo = 120.0;
728 static int frame = 0;
730 /* from the beginning? */
731 if(e == NULL)
733 e = ss_song;
734 tempo = 120.0;
735 frame = 0;
738 if(verbose >= 1)
740 if(frame % ss_frequency == 0)
742 int m = frame / ss_frequency;
743 printf("[%02d:%02d]\r", m / 60, m % 60);
744 fflush(stdout);
748 while(e != NULL && e->generic.frame == frame)
750 struct ss_ins * i;
751 struct ss_wave * w;
752 double freq;
754 if(e->generic.type == SONG_EV_NOTE_ON ||
755 e->generic.type == SONG_EV_NOTE_OFF ||
756 e->generic.type == SONG_EV_SS_PITCH_STRETCH)
758 if(frame < skip_frames)
760 e++;
761 frame = e->generic.frame;
762 continue;
766 /* take the instrument */
767 if(e->generic.trk_id < 0)
768 i = NULL;
769 else
770 i = &ss_song_ins[e->generic.trk_id];
772 switch(e->generic.type)
774 case SONG_EV_NOTE_ON:
776 if(ss_ins_note_on(i, e->note_on.note,
777 e->note_on.vol, e->note_on.note_id) < 0 &&
778 verbose >= 1)
779 printf("ss_ins_note_on error: track %d note %d\n",
780 e->note_on.trk_id, e->note_on.note);
782 break;
784 case SONG_EV_NOTE_OFF:
786 ss_ins_note_off(i, e->note_off.note_id);
788 break;
790 case SONG_EV_SS_SUSTAIN:
792 ss_ins_set_sustain(i, e->ss_sustain.sustain);
794 break;
796 case SONG_EV_SS_VIBRATO:
798 ss_ins_set_vibrato(i, e->ss_vibrato.vib_depth,
799 e->ss_vibrato.vib_freq);
801 break;
803 case SONG_EV_SS_PORTAMENTO:
805 ss_ins_set_portamento(i,
806 (e->ss_portamento.portamento * 44100.0)
807 / ((double) ss_frequency * 1000000.0));
809 break;
811 case SONG_EV_SS_CHANNEL:
813 ss_ins_set_channel(i, e->ss_channel.channel,
814 e->ss_channel.vol);
816 break;
818 case SONG_EV_SS_WAV:
820 w = ss_load_wav_file(e->ss_wav.file,
821 ss_note_frequency(e->ss_wav.base),
822 ss_note_frequency(e->ss_wav.min),
823 ss_note_frequency(e->ss_wav.max),
824 e->ss_wav.loop_start, e->ss_wav.loop_end,
825 e->ss_wav.first_channel, e->ss_wav.skip_channels);
827 /* fail if can't open wav */
828 if(w == NULL)
830 printf("Can't load wav '%s'\n", e->ss_wav.file);
831 e = NULL;
833 else
834 ss_ins_add_layer(i, w);
836 break;
838 case SONG_EV_SS_PAT:
840 if(ss_load_pat_file(i, e->ss_pat.file) < 0)
842 printf("Can't load pat '%s'\n", e->ss_pat.file);
843 e = NULL;
846 break;
848 case SONG_EV_SS_EFF_DELAY:
850 ss_eff_delay(&i->effs[e->ss_eff.channel], e->ss_eff.size);
851 break;
853 case SONG_EV_SS_EFF_ECHO:
855 ss_eff_echo(&i->effs[e->ss_eff.channel],
856 e->ss_eff.size, e->ss_eff.gain);
857 break;
859 case SONG_EV_SS_EFF_COMB:
861 ss_eff_comb(&i->effs[e->ss_eff.channel],
862 e->ss_eff.size, e->ss_eff.gain);
863 break;
865 case SONG_EV_SS_EFF_ALLPASS:
867 ss_eff_allpass(&i->effs[e->ss_eff.channel],
868 e->ss_eff.size, e->ss_eff.gain);
869 break;
871 case SONG_EV_SS_EFF_FLANGER:
873 ss_eff_flanger(&i->effs[e->ss_eff.channel],
874 e->ss_eff.size, e->ss_eff.gain,
875 e->ss_eff.depth, e->ss_eff.freq,
876 e->ss_eff.phase);
877 break;
879 case SONG_EV_SS_EFF_WOBBLE:
881 ss_eff_wobble(&i->effs[e->ss_eff.channel],
882 e->ss_eff.freq, e->ss_eff.phase,
883 e->ss_eff.gain);
885 break;
887 case SONG_EV_SS_EFF_SQWOBBLE:
889 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
890 e->ss_eff.freq, e->ss_eff.phase);
892 break;
894 case SONG_EV_SS_EFF_HFWOBBLE:
896 ss_eff_half_wobble(&i->effs[e->ss_eff.channel],
897 e->ss_eff.freq, e->ss_eff.phase);
899 break;
901 case SONG_EV_SS_EFF_FADER:
903 ss_eff_fader(&i->effs[e->ss_eff.channel],
904 e->ss_eff.size, e->ss_eff.initial,
905 e->ss_eff.final);
906 break;
908 case SONG_EV_SS_EFF_REVERB:
910 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
911 break;
913 case SONG_EV_SS_EFF_FOLDBACK:
915 ss_eff_foldback(&i->effs[e->ss_eff.channel],
916 e->ss_eff.gain);
917 break;
919 case SONG_EV_SS_EFF_OFF:
921 ss_eff_off(&i->effs[e->ss_eff.channel]);
922 break;
924 case SONG_EV_TEMPO:
926 /* just store the last tempo */
927 tempo = e->tempo.tempo;
928 break;
930 case SONG_EV_SS_PITCH_STRETCH:
932 /* find the wave */
933 freq = ss_note_frequency(e->ss_pitch_stretch.note);
934 w = ss_ins_find_layer(i, freq, NULL);
936 /* calculate optimal frequency */
937 freq = ss_pitch_from_tempo(w, tempo,
938 e->ss_pitch_stretch.len);
940 /* play the note */
941 if(ss_ins_play(i, freq, e->ss_pitch_stretch.vol,
942 e->ss_pitch_stretch.note_id, w) < 0 &&
943 verbose >= 1)
944 printf("ss_ins_play error: track %d freq %f\n",
945 e->ss_pitch_stretch.trk_id, freq);
947 break;
949 case SONG_EV_SS_PRINT_WAVE_TEMPO:
951 /* find the wave */
952 freq = ss_note_frequency(e->ss_print_wave_tempo.note);
953 w = ss_ins_find_layer(i, freq, NULL);
955 /* print the optimal tempo */
956 printf("Optimal tempo: %lf\n",
957 ss_tempo_from_wave(w,
958 e->ss_print_wave_tempo.note,
959 e->ss_print_wave_tempo.len));
961 break;
963 case SONG_EV_SONG_INFO:
965 /* add a new song (track) */
966 cue_file_song_info(frame, e->song_info.author,
967 e->song_info.name);
968 break;
970 case SONG_EV_EOT:
972 /* end of track; trigger possible cleaning */
973 ss_ins_disable(i);
974 break;
976 case SONG_EV_END:
978 e = NULL;
979 break;
981 case SONG_EV_BACK:
982 case SONG_EV_MIDI_CHANNEL:
983 case SONG_EV_MIDI_PROGRAM:
984 case SONG_EV_NOTE:
985 case SONG_EV_METER:
986 case SONG_EV_MEASURE:
988 /* never found in ss song streams */
989 break;
992 /* next event */
993 if(e) e++;
996 frame++;
998 return(e);
1002 int ss_song_render(int skip_secs, char * driver, char * devfile)
1004 int n, i = 0;
1005 sample_t output[SS_MAX_CHANNELS];
1006 int skip_frames;
1007 int n_channels;
1008 union ss_ev * e = NULL;
1010 /* convert the song to ss events */
1011 ss_song_convert_events(&n_channels);
1013 if(verbose >= 2)
1014 printf("Tracks: %d Channels: %d Events: %d\n",
1015 n_song_tracks, n_channels, n_ss_ev);
1017 if(trace)
1019 ss_song_trace_events();
1020 return(0);
1023 /* set the number of channels, unless forced */
1024 if(ss_nchannels == -1)
1025 ss_nchannels = n_channels > 0 ? n_channels : 2;
1027 if(ss_output_open(driver, devfile) < 0)
1029 printf("Error: can't init driver\n");
1030 return(2);
1033 /* init the generators */
1034 ss_gen_init();
1036 /* init the instruments */
1037 for(n = 0;n < n_song_tracks;n++)
1038 ss_ins_init(&ss_song_ins[n]);
1040 /* calculate the frame to start playing */
1041 skip_frames = skip_secs * ss_frequency;
1043 /* main loop */
1046 /* process all events in this frame */
1047 /* if((e = process_this_frame_events(e, skip_frames)) == NULL)
1048 break;*/
1049 e = process_this_frame_events(e, skip_frames);
1051 /* reset frame samples */
1052 ss_output_init_frame(output);
1054 /* generate output from all instruments */
1055 for(n = i = 0;n < n_song_tracks;n++)
1056 i += ss_ins_frame(&ss_song_ins[n], output);
1058 /* dump to sampling driver */
1059 ss_output_write(output);
1060 } while(i);
1062 if(verbose >= 1) printf("\n");
1064 ss_output_close();
1066 return(0);