Fixed typo in RELEASE_NOTES.
[ahxm.git] / ss_song.c
blobe3604ddbf5f8cc2cb520e4744e57aae8afeda9e8
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2007 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_attack ss_attack;
204 struct ss_ev_ss_vibrato ss_vibrato;
205 struct ss_ev_ss_portamento ss_portamento;
206 struct ss_ev_ss_channel ss_channel;
207 struct ss_ev_ss_wav ss_wav;
208 struct ss_ev_ss_pat ss_pat;
209 struct ss_ev_ss_eff ss_eff;
210 struct ss_ev_tempo tempo;
211 struct ss_ev_pitch_stretch ss_pitch_stretch;
212 struct ss_ev_print_wave_tempo ss_print_wave_tempo;
213 struct ss_ev_song_info song_info;
216 /* the softsynth song stream */
218 static union ss_ev * ss_song = NULL;
219 static int n_ss_ev = 0;
222 /* the instruments */
224 struct ss_ins ss_song_ins[SS_MAX_INSTRUMENTS];
226 /*******************
227 Code
228 ********************/
230 static void add_ss_ev(union ss_ev * e)
231 /* adds a softsynth song event */
233 GROW(ss_song, n_ss_ev, union ss_ev);
235 /* store */
236 memcpy(&ss_song[n_ss_ev], e, sizeof(union ss_ev));
238 n_ss_ev++;
242 static int ss_ev_cmp(const void * v1, const void * v2)
243 /* softsynth song event compare function for qsort() */
245 struct ss_ev_generic * e1;
246 struct ss_ev_generic * e2;
247 int ret;
249 e1 = (struct ss_ev_generic *)v1; e2 = (struct ss_ev_generic *)v2;
251 ret = e1->frame - e2->frame;
253 if(ret == 0)
254 ret = e1->type - e2->type;
256 if(ret == 0)
257 ret = e1->event_id - e2->event_id;
259 return(ret);
263 static void ss_song_convert_events(int * n_channels)
264 /* converts generic song_ev events to softsynth events */
266 int note_id = 1;
267 union song_ev * e;
268 union ss_ev sse;
269 int frame, frame_ac, f_frame;
270 double fpw, time_ac, time_ac_m;
271 int num, den;
272 int n;
274 *n_channels = -1;
276 /* resets the ss stream */
277 if(ss_song != NULL)
279 free(ss_song);
280 ss_song = NULL;
283 n_ss_ev = 0;
285 /* sorts the song */
286 song_sort();
288 fpw = 0;
289 frame = frame_ac = f_frame = 0;
290 time_ac = time_ac_m = 0;
291 num = den = 4;
293 /* travels the song events generating softsynth song events */
294 for(n = 0;n < n_song_ev;n++)
296 /* gets the song event */
297 e = &song[n];
299 /* calculates the frame */
300 frame = ((e->generic.time - time_ac) * fpw) + frame_ac;
302 /* generic event data */
303 sse.generic.type = e->generic.type;
304 sse.generic.frame = frame;
305 sse.generic.trk_id = e->generic.trk_id;
306 sse.generic.event_id = e->generic.event_id;
308 switch(e->generic.type)
310 case SONG_EV_TEMPO:
312 /* updates accumulations */
313 frame_ac = frame;
314 time_ac = e->generic.time;
316 /* calculates frames-per-whole based on new tempo */
317 fpw = (double) ss_frequency * 60.0;
318 fpw /= e->tempo.tempo / 4.0;
320 /* adds an event */
321 sse.tempo.tempo = e->tempo.tempo;
322 add_ss_ev(&sse);
324 break;
326 case SONG_EV_METER:
328 /* just store the values */
329 num = e->meter.num;
330 den = e->meter.den;
331 time_ac_m = e->meter.time;
333 break;
335 case SONG_EV_MEASURE:
337 song_test_measure_boundary(e->measure.time - time_ac_m,
338 num, den, e->measure.line);
339 break;
341 case SONG_EV_NOTE:
343 /* convert to note on / off pairs */
345 sse.note_on.type = SONG_EV_NOTE_ON;
346 sse.note_on.note_id = note_id++;
347 sse.note_on.note = e->note.note;
348 sse.note_on.vol = e->note.vol;
350 add_ss_ev(&sse);
352 frame += (int)(e->note.len * fpw);
354 sse.note_off.type = SONG_EV_NOTE_OFF;
355 sse.note_off.frame = frame;
357 add_ss_ev(&sse);
358 break;
360 case SONG_EV_BACK:
362 /* move the cursor back */
364 frame_ac -= (int)(e->back.len * fpw);
366 break;
368 case SONG_EV_SS_PITCH_STRETCH:
370 sse.ss_pitch_stretch.note_id = note_id++;
371 sse.ss_pitch_stretch.note = e->ss_pitch_stretch.note;
372 sse.ss_pitch_stretch.len = e->ss_pitch_stretch.len;
373 sse.ss_pitch_stretch.vol = e->ss_pitch_stretch.vol;
375 add_ss_ev(&sse);
377 frame += (int)(e->ss_pitch_stretch.len * fpw);
379 sse.note_off.type = SONG_EV_NOTE_OFF;
380 sse.note_off.frame = frame;
382 add_ss_ev(&sse);
383 break;
385 case SONG_EV_SS_PRINT_WAVE_TEMPO:
387 sse.ss_print_wave_tempo.note = e->ss_print_wave_tempo.note;
388 sse.ss_print_wave_tempo.len = e->ss_print_wave_tempo.len;
390 add_ss_ev(&sse);
391 break;
393 case SONG_EV_SS_WAV:
395 sse.ss_wav.file = e->ss_wav.file;
396 sse.ss_wav.base = e->ss_wav.base;
397 sse.ss_wav.min = e->ss_wav.min;
398 sse.ss_wav.max = e->ss_wav.max;
399 sse.ss_wav.loop_start = e->ss_wav.loop_start;
400 sse.ss_wav.loop_end = e->ss_wav.loop_end;
401 sse.ss_wav.first_channel = e->ss_wav.first_channel;
402 sse.ss_wav.skip_channels = e->ss_wav.skip_channels;
404 add_ss_ev(&sse);
405 break;
407 case SONG_EV_SS_PAT:
409 sse.ss_pat.file = e->ss_pat.file;
411 add_ss_ev(&sse);
412 break;
414 case SONG_EV_SS_SUSTAIN:
416 sse.ss_sustain.sustain = e->ss_sustain.sustain;
418 add_ss_ev(&sse);
419 break;
421 case SONG_EV_SS_ATTACK:
423 sse.ss_attack.attack = e->ss_attack.attack;
425 add_ss_ev(&sse);
426 break;
428 case SONG_EV_SS_VIBRATO:
430 sse.ss_vibrato.vib_depth = e->ss_vibrato.vib_depth;
431 sse.ss_vibrato.vib_freq = e->ss_vibrato.vib_freq;
433 add_ss_ev(&sse);
434 break;
436 case SONG_EV_SS_PORTAMENTO:
438 sse.ss_portamento.portamento = e->ss_portamento.portamento;
440 add_ss_ev(&sse);
441 break;
443 case SONG_EV_SS_CHANNEL:
445 sse.ss_channel.channel = e->ss_channel.channel;
446 sse.ss_channel.vol = e->ss_channel.vol;
448 /* count channels */
449 if(*n_channels < e->ss_channel.channel)
450 *n_channels = e->ss_channel.channel;
452 add_ss_ev(&sse);
453 break;
455 case SONG_EV_SS_EFF_DELAY:
456 case SONG_EV_SS_EFF_ECHO:
457 case SONG_EV_SS_EFF_COMB:
458 case SONG_EV_SS_EFF_ALLPASS:
459 case SONG_EV_SS_EFF_FLANGER:
460 case SONG_EV_SS_EFF_WOBBLE:
461 case SONG_EV_SS_EFF_SQWOBBLE:
462 case SONG_EV_SS_EFF_HFWOBBLE:
463 case SONG_EV_SS_EFF_FADER:
464 case SONG_EV_SS_EFF_REVERB:
465 case SONG_EV_SS_EFF_FOLDBACK:
466 case SONG_EV_SS_EFF_ATAN:
467 case SONG_EV_SS_EFF_DISTORT:
468 case SONG_EV_SS_EFF_OVERDRIVE:
469 case SONG_EV_SS_EFF_OFF:
471 sse.ss_eff.channel = e->ss_eff.channel;
472 sse.ss_eff.size = e->ss_eff.size;
473 sse.ss_eff.gain = e->ss_eff.gain;
474 sse.ss_eff.depth = e->ss_eff.depth;
475 sse.ss_eff.freq = e->ss_eff.freq;
476 sse.ss_eff.phase = e->ss_eff.phase;
477 sse.ss_eff.initial = e->ss_eff.initial;
478 sse.ss_eff.final = e->ss_eff.final;
480 add_ss_ev(&sse);
481 break;
483 case SONG_EV_SONG_INFO:
485 sse.song_info.author = e->song_info.author;
486 sse.song_info.name = e->song_info.name;
488 add_ss_ev(&sse);
489 break;
491 case SONG_EV_EOT:
493 add_ss_ev(&sse);
494 break;
496 case SONG_EV_MIDI_CHANNEL:
497 case SONG_EV_MIDI_PROGRAM:
499 /* ignored */
500 break;
502 case SONG_EV_NOTE_ON:
503 case SONG_EV_NOTE_OFF:
504 case SONG_EV_END:
506 /* never found in generic song streams */
507 break;
510 /* store the further frame seen */
511 if(f_frame < frame) f_frame = frame;
514 /* generates an end of event mark, a time after the last one */
515 sse.generic.type = SONG_EV_END;
516 sse.generic.frame = f_frame + ss_frequency;
517 sse.generic.event_id = -1;
518 add_ss_ev(&sse);
520 /* finally sort */
521 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
523 /* count one more */
524 (*n_channels)++;
528 static void ss_song_trace_events(void)
530 union ss_ev * e;
531 int n;
533 printf("** SOFTWARE SYNTHESIZER EVENT DUMP **\n\n");
534 printf("%10s %5s %5s Event and information\n",
535 "Frame", "Track", "Ev.ID");
536 printf("------------------------------------------------------------\n");
538 for(n = 0, e = ss_song;n < n_ss_ev;n++, e++)
540 printf("%10d %5d %5d ",
541 e->generic.frame, e->generic.trk_id, e->generic.event_id);
543 switch(e->generic.type)
545 case SONG_EV_TEMPO:
547 printf("SONG_EV_TEMPO ");
548 printf("%lf", e->tempo.tempo);
549 break;
551 case SONG_EV_SS_SUSTAIN:
553 printf("SONG_EV_SS_SUSTAIN ");
554 printf("SUSTAIN:%lf", e->ss_sustain.sustain);
555 break;
557 case SONG_EV_SS_ATTACK:
559 printf("SONG_EV_SS_ATTACK ");
560 printf("ATTACK:%lf", e->ss_attack.attack);
561 break;
563 case SONG_EV_SS_VIBRATO:
565 printf("SONG_EV_SS_VIBRATO ");
566 printf("DEPTH:%lf FREQ:%lf",
567 e->ss_vibrato.vib_depth,
568 e->ss_vibrato.vib_freq);
569 break;
571 case SONG_EV_SS_PORTAMENTO:
573 printf("SONG_EV_SS_PORTAMENTO ");
574 printf("VALUE:%lf",
575 e->ss_portamento.portamento);
576 break;
578 case SONG_EV_SS_CHANNEL:
580 printf("SONG_EV_SS_CHANNEL ");
581 printf("CHANNEL:%d VOL:%lf",
582 e->ss_channel.channel,
583 e->ss_channel.vol);
584 break;
586 case SONG_EV_SS_WAV:
588 printf("SONG_EV_SS_WAV ");
589 printf("FILE:'%s' BASE:%d MIN:%d MAX:%d START:%lf END:%lf",
590 e->ss_wav.file, e->ss_wav.base,
591 e->ss_wav.min, e->ss_wav.max,
592 e->ss_wav.loop_start, e->ss_wav.loop_end);
593 break;
595 case SONG_EV_SS_PAT:
597 printf("SONG_EV_SS_PAT ");
598 printf("FILE:'%s'", e->ss_pat.file);
599 break;
601 case SONG_EV_SS_EFF_DELAY:
603 printf("SONG_EV_SS_EFF_DELAY ");
604 printf("CHANNEL:%d ", e->ss_eff.channel);
605 printf("SIZE:%lf ", e->ss_eff.size);
606 break;
608 case SONG_EV_SS_EFF_ECHO:
610 printf("SONG_EV_SS_EFF_ECHO ");
611 printf("CHANNEL:%d ", e->ss_eff.channel);
612 printf("SIZE:%lf ", e->ss_eff.size);
613 printf("GAIN:%f ", e->ss_eff.gain);
614 break;
616 case SONG_EV_SS_EFF_COMB:
618 printf("SONG_EV_SS_EFF_COMB ");
619 printf("CHANNEL:%d ", e->ss_eff.channel);
620 printf("SIZE:%lf ", e->ss_eff.size);
621 printf("GAIN:%f ", e->ss_eff.gain);
622 break;
624 case SONG_EV_SS_EFF_ALLPASS:
626 printf("SONG_EV_SS_EFF_ALLPASS ");
627 printf("CHANNEL:%d ", e->ss_eff.channel);
628 printf("SIZE:%lf ", e->ss_eff.size);
629 printf("GAIN:%f ", e->ss_eff.gain);
630 break;
632 case SONG_EV_SS_EFF_FLANGER:
634 printf("SONG_EV_SS_EFF_FLANGER ");
635 printf("CHANNEL:%d ", e->ss_eff.channel);
636 printf("SIZE:%lf ", e->ss_eff.size);
637 printf("GAIN:%f ", e->ss_eff.gain);
638 printf("DEPTH:%lf ", e->ss_eff.depth);
639 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
640 e->ss_eff.phase);
641 break;
643 case SONG_EV_SS_EFF_WOBBLE:
645 printf("SONG_EV_SS_EFF_WOBBLE ");
646 printf("CHANNEL:%d ", e->ss_eff.channel);
647 printf("FREQ:%lf PHASE:%lf GAIN:%lf", e->ss_eff.freq,
648 e->ss_eff.phase, e->ss_eff.gain);
649 break;
651 case SONG_EV_SS_EFF_SQWOBBLE:
653 printf("SONG_EV_SS_EFF_SQWOBBLE ");
654 printf("CHANNEL:%d ", e->ss_eff.channel);
655 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
656 e->ss_eff.phase);
657 break;
659 case SONG_EV_SS_EFF_HFWOBBLE:
661 printf("SONG_EV_SS_EFF_HFWOBBLE ");
662 printf("CHANNEL:%d ", e->ss_eff.channel);
663 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
664 e->ss_eff.phase);
665 break;
667 case SONG_EV_SS_EFF_FADER:
669 printf("SONG_EV_SS_EFF_FADER ");
670 printf("CHANNEL:%d ", e->ss_eff.channel);
671 printf("SIZE:%lf ", e->ss_eff.size);
672 printf("INITIAL:%f FINAL:%f", e->ss_eff.initial,
673 e->ss_eff.final);
674 break;
676 case SONG_EV_SS_EFF_REVERB:
678 printf("SONG_EV_SS_EFF_REVERB ");
679 printf("CHANNEL:%d ", e->ss_eff.channel);
680 break;
682 case SONG_EV_SS_EFF_FOLDBACK:
684 printf("SONG_EV_SS_EFF_FOLDBACK ");
685 printf("CHANNEL:%d THRESHOLD:%f", e->ss_eff.channel,
686 e->ss_eff.gain);
687 break;
689 case SONG_EV_SS_EFF_ATAN:
691 printf("SONG_EV_SS_EFF_ATAN ");
692 printf("CHANNEL:%d GAIN:%f", e->ss_eff.channel,
693 e->ss_eff.gain);
694 break;
696 case SONG_EV_SS_EFF_DISTORT:
698 printf("SONG_EV_SS_EFF_DISTORT ");
699 printf("CHANNEL:%d GAIN:%f", e->ss_eff.channel,
700 e->ss_eff.gain);
701 break;
703 case SONG_EV_SS_EFF_OVERDRIVE:
705 printf("SONG_EV_SS_EFF_OVERDRIVE ");
706 printf("CHANNEL:%d GAIN:%f", e->ss_eff.channel,
707 e->ss_eff.gain);
708 break;
710 case SONG_EV_SS_EFF_OFF:
712 printf("SONG_EV_SS_EFF_OFF ");
713 printf("CHANNEL:%d ", e->ss_eff.channel);
714 break;
716 case SONG_EV_SS_PITCH_STRETCH:
718 printf("SONG_EV_SS_PITCH_STRETCH ");
719 printf("MIDI:%d LEN:%lf VOL:%f",
720 e->ss_pitch_stretch.note,
721 e->ss_pitch_stretch.len,
722 e->ss_pitch_stretch.vol);
724 break;
726 case SONG_EV_SS_PRINT_WAVE_TEMPO:
728 printf("SONG_EV_SS_PRINT_WAVE_TEMPO ");
729 printf("MIDI:%d LEN:%lf",
730 e->ss_print_wave_tempo.note,
731 e->ss_print_wave_tempo.len);
732 break;
734 case SONG_EV_SONG_INFO:
736 printf("SONG_EV_SONG_INFO ");
737 printf("AUTHOR:'%s' NAME:'%s'",
738 e->song_info.author,
739 e->song_info.name);
740 break;
742 case SONG_EV_NOTE_ON:
744 printf("SONG_EV_NOTE_ON ");
745 printf("ID:%d MIDI:%d VOL:%f",
746 e->note_on.note_id, e->note_on.note,
747 e->note_on.vol);
748 break;
750 case SONG_EV_NOTE_OFF:
752 printf("SONG_EV_NOTE_OFF ");
753 printf("ID:%d", e->note_off.note_id);
754 break;
756 case SONG_EV_EOT:
758 printf("SONG_EV_EOT ");
759 break;
761 case SONG_EV_END:
763 printf("SONG_EV_END ");
764 break;
766 default:
767 printf("** Unexpected type: %d",
768 e->generic.type);
771 printf("\n");
774 printf("\n");
778 static union ss_ev * process_this_frame_events(union ss_ev * e, int skip_frames)
779 /* process the events attached to this frame */
781 static double tempo = 120.0;
782 static int frame = 0;
784 /* from the beginning? */
785 if(e == NULL)
787 e = ss_song;
788 tempo = 120.0;
789 frame = 0;
792 if(verbose >= 1)
794 if(frame % ss_frequency == 0)
796 int m = frame / ss_frequency;
797 printf("[%02d:%02d]\r", m / 60, m % 60);
798 fflush(stdout);
802 while(e != NULL && e->generic.frame == frame)
804 struct ss_ins * i;
805 struct ss_wave * w;
806 double freq;
808 if(e->generic.type == SONG_EV_NOTE_ON ||
809 e->generic.type == SONG_EV_NOTE_OFF ||
810 e->generic.type == SONG_EV_SS_PITCH_STRETCH)
812 if(frame < skip_frames)
814 e++;
815 frame = e->generic.frame;
816 continue;
820 /* take the instrument */
821 if(e->generic.trk_id < 0)
822 i = NULL;
823 else
824 i = &ss_song_ins[e->generic.trk_id];
826 switch(e->generic.type)
828 case SONG_EV_NOTE_ON:
830 if(ss_ins_note_on(i, e->note_on.note,
831 e->note_on.vol, e->note_on.note_id) < 0 &&
832 verbose >= 1)
833 printf("ss_ins_note_on error: track %d note %d\n",
834 e->note_on.trk_id, e->note_on.note);
836 break;
838 case SONG_EV_NOTE_OFF:
840 ss_ins_note_off(i, e->note_off.note_id);
842 break;
844 case SONG_EV_SS_SUSTAIN:
846 ss_ins_set_sustain(i, e->ss_sustain.sustain);
848 break;
850 case SONG_EV_SS_ATTACK:
852 ss_ins_set_attack(i, e->ss_attack.attack);
854 break;
856 case SONG_EV_SS_VIBRATO:
858 ss_ins_set_vibrato(i, e->ss_vibrato.vib_depth,
859 e->ss_vibrato.vib_freq);
861 break;
863 case SONG_EV_SS_PORTAMENTO:
865 ss_ins_set_portamento(i,
866 (e->ss_portamento.portamento * 44100.0)
867 / ((double) ss_frequency * 1000000.0));
869 break;
871 case SONG_EV_SS_CHANNEL:
873 ss_ins_set_channel(i, e->ss_channel.channel,
874 e->ss_channel.vol);
876 break;
878 case SONG_EV_SS_WAV:
880 w = ss_load_wav_file(e->ss_wav.file,
881 ss_note_frequency(e->ss_wav.base),
882 ss_note_frequency(e->ss_wav.min),
883 ss_note_frequency(e->ss_wav.max),
884 e->ss_wav.loop_start, e->ss_wav.loop_end,
885 e->ss_wav.first_channel, e->ss_wav.skip_channels);
887 /* fail if can't open wav */
888 if(w == NULL)
890 printf("Can't load wav '%s'\n", e->ss_wav.file);
891 e = NULL;
893 else
894 ss_ins_add_layer(i, w);
896 break;
898 case SONG_EV_SS_PAT:
900 if(ss_load_pat_file(i, e->ss_pat.file) < 0)
902 printf("Can't load pat '%s'\n", e->ss_pat.file);
903 e = NULL;
906 break;
908 case SONG_EV_SS_EFF_DELAY:
910 ss_eff_delay(&i->effs[e->ss_eff.channel], e->ss_eff.size);
911 break;
913 case SONG_EV_SS_EFF_ECHO:
915 ss_eff_echo(&i->effs[e->ss_eff.channel],
916 e->ss_eff.size, e->ss_eff.gain);
917 break;
919 case SONG_EV_SS_EFF_COMB:
921 ss_eff_comb(&i->effs[e->ss_eff.channel],
922 e->ss_eff.size, e->ss_eff.gain);
923 break;
925 case SONG_EV_SS_EFF_ALLPASS:
927 ss_eff_allpass(&i->effs[e->ss_eff.channel],
928 e->ss_eff.size, e->ss_eff.gain);
929 break;
931 case SONG_EV_SS_EFF_FLANGER:
933 ss_eff_flanger(&i->effs[e->ss_eff.channel],
934 e->ss_eff.size, e->ss_eff.gain,
935 e->ss_eff.depth, e->ss_eff.freq,
936 e->ss_eff.phase);
937 break;
939 case SONG_EV_SS_EFF_WOBBLE:
941 ss_eff_wobble(&i->effs[e->ss_eff.channel],
942 e->ss_eff.freq, e->ss_eff.phase,
943 e->ss_eff.gain);
945 break;
947 case SONG_EV_SS_EFF_SQWOBBLE:
949 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
950 e->ss_eff.freq, e->ss_eff.phase);
952 break;
954 case SONG_EV_SS_EFF_HFWOBBLE:
956 ss_eff_half_wobble(&i->effs[e->ss_eff.channel],
957 e->ss_eff.freq, e->ss_eff.phase);
959 break;
961 case SONG_EV_SS_EFF_FADER:
963 ss_eff_fader(&i->effs[e->ss_eff.channel],
964 e->ss_eff.size, e->ss_eff.initial,
965 e->ss_eff.final);
966 break;
968 case SONG_EV_SS_EFF_REVERB:
970 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
971 break;
973 case SONG_EV_SS_EFF_FOLDBACK:
975 ss_eff_foldback(&i->effs[e->ss_eff.channel],
976 e->ss_eff.gain);
977 break;
979 case SONG_EV_SS_EFF_ATAN:
981 ss_eff_atan(&i->effs[e->ss_eff.channel],
982 e->ss_eff.gain);
983 break;
985 case SONG_EV_SS_EFF_DISTORT:
987 ss_eff_distort(&i->effs[e->ss_eff.channel],
988 e->ss_eff.gain);
989 break;
991 case SONG_EV_SS_EFF_OVERDRIVE:
993 ss_eff_overdrive(&i->effs[e->ss_eff.channel],
994 e->ss_eff.gain);
995 break;
997 case SONG_EV_SS_EFF_OFF:
999 ss_eff_off(&i->effs[e->ss_eff.channel]);
1000 break;
1002 case SONG_EV_TEMPO:
1004 /* just store the last tempo */
1005 tempo = e->tempo.tempo;
1006 break;
1008 case SONG_EV_SS_PITCH_STRETCH:
1010 /* find the wave */
1011 freq = ss_note_frequency(e->ss_pitch_stretch.note);
1012 w = ss_ins_find_layer(i, freq, NULL);
1014 /* calculate optimal frequency */
1015 freq = ss_pitch_from_tempo(w, tempo,
1016 e->ss_pitch_stretch.len);
1018 /* play the note */
1019 if(ss_ins_play(i, freq, e->ss_pitch_stretch.vol,
1020 e->ss_pitch_stretch.note_id, w) < 0 &&
1021 verbose >= 1)
1022 printf("ss_ins_play error: track %d freq %f\n",
1023 e->ss_pitch_stretch.trk_id, freq);
1025 break;
1027 case SONG_EV_SS_PRINT_WAVE_TEMPO:
1029 /* find the wave */
1030 freq = ss_note_frequency(e->ss_print_wave_tempo.note);
1031 w = ss_ins_find_layer(i, freq, NULL);
1033 /* print the optimal tempo */
1034 printf("Optimal tempo: %lf\n",
1035 ss_tempo_from_wave(w,
1036 e->ss_print_wave_tempo.note,
1037 e->ss_print_wave_tempo.len));
1039 break;
1041 case SONG_EV_SONG_INFO:
1043 /* add a new song (track) */
1044 cue_file_song_info(frame, e->song_info.author,
1045 e->song_info.name);
1046 break;
1048 case SONG_EV_EOT:
1050 /* end of track; trigger possible cleaning */
1051 ss_ins_disable(i);
1052 break;
1054 case SONG_EV_END:
1056 e = NULL;
1057 break;
1059 case SONG_EV_BACK:
1060 case SONG_EV_MIDI_CHANNEL:
1061 case SONG_EV_MIDI_PROGRAM:
1062 case SONG_EV_NOTE:
1063 case SONG_EV_METER:
1064 case SONG_EV_MEASURE:
1066 /* never found in ss song streams */
1067 break;
1070 /* next event */
1071 if(e) e++;
1074 frame++;
1076 return(e);
1080 int ss_song_render(int skip_secs, char * driver, char * devfile)
1082 int n, i = 0;
1083 sample_t output[SS_MAX_CHANNELS];
1084 int skip_frames;
1085 int n_channels;
1086 union ss_ev * e = NULL;
1088 /* convert the song to ss events */
1089 ss_song_convert_events(&n_channels);
1091 if(verbose >= 2)
1092 printf("Tracks: %d Channels: %d Events: %d\n",
1093 n_song_tracks, n_channels, n_ss_ev);
1095 if(trace)
1097 ss_song_trace_events();
1098 return(0);
1101 /* set the number of channels, unless forced */
1102 if(ss_nchannels == -1)
1103 ss_nchannels = n_channels > 0 ? n_channels : 2;
1105 if(ss_output_open(driver, devfile) < 0)
1107 printf("Error: can't init driver\n");
1108 return(2);
1111 /* init the generators */
1112 ss_gen_init();
1114 /* init the instruments */
1115 for(n = 0;n < n_song_tracks;n++)
1116 ss_ins_init(&ss_song_ins[n]);
1118 /* calculate the frame to start playing */
1119 skip_frames = skip_secs * ss_frequency;
1121 /* main loop */
1124 /* process all events in this frame */
1125 /* if((e = process_this_frame_events(e, skip_frames)) == NULL)
1126 break;*/
1127 e = process_this_frame_events(e, skip_frames);
1129 /* reset frame samples */
1130 ss_output_init_frame(output);
1132 /* generate output from all instruments */
1133 for(n = i = 0;n < n_song_tracks;n++)
1134 i += ss_ins_frame(&ss_song_ins[n], output);
1136 /* dump to sampling driver */
1137 ss_output_write(output);
1138 } while(i);
1140 if(verbose >= 1) printf("\n");
1142 ss_output_close();
1144 return(0);