The song statistics message in ss_song.c (shown when
[ahxm.git] / ss_song.c
blob23e8ffb389ef12a0b761f5fec58e726be3c22555
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_tracks, 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_tracks = -1;
265 *n_channels = -1;
267 /* resets the ss stream */
268 if(ss_song != NULL)
270 free(ss_song);
271 ss_song = NULL;
274 n_ss_ev = 0;
276 /* sorts the song */
277 song_sort();
279 fpw = 0;
280 frame = frame_ac = f_frame = 0;
281 time_ac = time_ac_m = 0;
282 num = den = 4;
284 /* travels the song events generating softsynth song events */
285 for(n = 0;n < n_song_ev;n++)
287 /* gets the song event */
288 e = &song[n];
290 /* calculates the frame */
291 frame = ((e->generic.time - time_ac) * fpw) + frame_ac;
293 /* generic event data */
294 sse.generic.type = e->generic.type;
295 sse.generic.frame = frame;
296 sse.generic.trk_id = e->generic.trk_id;
297 sse.generic.event_id = e->generic.event_id;
299 /* account the biggest track seen */
300 if(*n_tracks < e->generic.trk_id) *n_tracks = e->generic.trk_id;
302 switch(e->generic.type)
304 case SONG_EV_TEMPO:
306 /* updates accumulations */
307 frame_ac = frame;
308 time_ac = e->generic.time;
310 /* calculates frames-per-whole based on new tempo */
311 fpw = (double) ss_frequency * 60.0;
312 fpw /= e->tempo.tempo / 4.0;
314 /* adds an event */
315 sse.tempo.tempo = e->tempo.tempo;
316 add_ss_ev(&sse);
318 break;
320 case SONG_EV_METER:
322 /* just store the values */
323 num = e->meter.num;
324 den = e->meter.den;
325 time_ac_m = e->meter.time;
327 break;
329 case SONG_EV_MEASURE:
331 song_test_measure_boundary(e->measure.time - time_ac_m,
332 num, den, e->measure.line);
333 break;
335 case SONG_EV_NOTE:
337 /* convert to note on / off pairs */
339 sse.note_on.type = SONG_EV_NOTE_ON;
340 sse.note_on.note_id = note_id++;
341 sse.note_on.note = e->note.note;
342 sse.note_on.vol = e->note.vol;
344 add_ss_ev(&sse);
346 frame += (int)(e->note.len * fpw);
348 sse.note_off.type = SONG_EV_NOTE_OFF;
349 sse.note_off.frame = frame;
351 add_ss_ev(&sse);
352 break;
354 case SONG_EV_BACK:
356 /* move the cursor back */
358 frame_ac -= (int)(e->back.len * fpw);
360 break;
362 case SONG_EV_SS_PITCH_STRETCH:
364 sse.ss_pitch_stretch.note_id = note_id++;
365 sse.ss_pitch_stretch.note = e->ss_pitch_stretch.note;
366 sse.ss_pitch_stretch.len = e->ss_pitch_stretch.len;
367 sse.ss_pitch_stretch.vol = e->ss_pitch_stretch.vol;
369 add_ss_ev(&sse);
371 frame += (int)(e->ss_pitch_stretch.len * fpw);
373 sse.note_off.type = SONG_EV_NOTE_OFF;
374 sse.note_off.frame = frame;
376 add_ss_ev(&sse);
377 break;
379 case SONG_EV_SS_PRINT_WAVE_TEMPO:
381 sse.ss_print_wave_tempo.note = e->ss_print_wave_tempo.note;
382 sse.ss_print_wave_tempo.len = e->ss_print_wave_tempo.len;
384 add_ss_ev(&sse);
385 break;
387 case SONG_EV_SS_WAV:
389 sse.ss_wav.file = e->ss_wav.file;
390 sse.ss_wav.base = e->ss_wav.base;
391 sse.ss_wav.min = e->ss_wav.min;
392 sse.ss_wav.max = e->ss_wav.max;
393 sse.ss_wav.loop_start = e->ss_wav.loop_start;
394 sse.ss_wav.loop_end = e->ss_wav.loop_end;
395 sse.ss_wav.first_channel = e->ss_wav.first_channel;
396 sse.ss_wav.skip_channels = e->ss_wav.skip_channels;
398 add_ss_ev(&sse);
399 break;
401 case SONG_EV_SS_PAT:
403 sse.ss_pat.file = e->ss_pat.file;
405 add_ss_ev(&sse);
406 break;
408 case SONG_EV_SS_SUSTAIN:
410 sse.ss_sustain.sustain = e->ss_sustain.sustain;
412 add_ss_ev(&sse);
413 break;
415 case SONG_EV_SS_VIBRATO:
417 sse.ss_vibrato.vib_depth = e->ss_vibrato.vib_depth;
418 sse.ss_vibrato.vib_freq = e->ss_vibrato.vib_freq;
420 add_ss_ev(&sse);
421 break;
423 case SONG_EV_SS_PORTAMENTO:
425 sse.ss_portamento.portamento = e->ss_portamento.portamento;
427 add_ss_ev(&sse);
428 break;
430 case SONG_EV_SS_CHANNEL:
432 sse.ss_channel.channel = e->ss_channel.channel;
433 sse.ss_channel.vol = e->ss_channel.vol;
435 /* count channels */
436 if(*n_channels < e->ss_channel.channel)
437 *n_channels = e->ss_channel.channel;
439 add_ss_ev(&sse);
440 break;
442 case SONG_EV_SS_EFF_DELAY:
443 case SONG_EV_SS_EFF_ECHO:
444 case SONG_EV_SS_EFF_COMB:
445 case SONG_EV_SS_EFF_ALLPASS:
446 case SONG_EV_SS_EFF_FLANGER:
447 case SONG_EV_SS_EFF_WOBBLE:
448 case SONG_EV_SS_EFF_SQWOBBLE:
449 case SONG_EV_SS_EFF_HFWOBBLE:
450 case SONG_EV_SS_EFF_FADER:
451 case SONG_EV_SS_EFF_REVERB:
452 case SONG_EV_SS_EFF_FOLDBACK:
453 case SONG_EV_SS_EFF_OFF:
455 sse.ss_eff.channel = e->ss_eff.channel;
456 sse.ss_eff.size = e->ss_eff.size;
457 sse.ss_eff.gain = e->ss_eff.gain;
458 sse.ss_eff.depth = e->ss_eff.depth;
459 sse.ss_eff.freq = e->ss_eff.freq;
460 sse.ss_eff.phase = e->ss_eff.phase;
461 sse.ss_eff.initial = e->ss_eff.initial;
462 sse.ss_eff.final = e->ss_eff.final;
464 add_ss_ev(&sse);
465 break;
467 case SONG_EV_SONG_INFO:
469 sse.song_info.author = e->song_info.author;
470 sse.song_info.name = e->song_info.name;
472 add_ss_ev(&sse);
473 break;
475 case SONG_EV_EOT:
477 add_ss_ev(&sse);
478 break;
480 case SONG_EV_MIDI_CHANNEL:
481 case SONG_EV_MIDI_PROGRAM:
483 /* ignored */
484 break;
486 case SONG_EV_NOTE_ON:
487 case SONG_EV_NOTE_OFF:
488 case SONG_EV_END:
490 /* never found in generic song streams */
491 break;
494 /* store the further frame seen */
495 if(f_frame < frame) f_frame = frame;
498 /* generates an end of event mark, a time after the last one */
499 sse.generic.type = SONG_EV_END;
500 sse.generic.frame = f_frame + ss_frequency;
501 sse.generic.event_id = -1;
502 add_ss_ev(&sse);
504 /* finally sort */
505 qsort(ss_song, n_ss_ev, sizeof(union ss_ev), ss_ev_cmp);
507 /* count one more */
508 (*n_tracks)++;
509 (*n_channels)++;
513 static void ss_song_trace_events(void)
515 union ss_ev * e;
516 int n;
518 printf("** SOFTWARE SYNTHESIZER EVENT DUMP **\n\n");
519 printf("%10s %5s %5s Event and information\n",
520 "Frame", "Track", "Ev.ID");
521 printf("------------------------------------------------------------\n");
523 for(n = 0, e = ss_song;n < n_ss_ev;n++, e++)
525 printf("%10d %5d %5d ",
526 e->generic.frame, e->generic.trk_id, e->generic.event_id);
528 switch(e->generic.type)
530 case SONG_EV_TEMPO:
532 printf("SONG_EV_TEMPO ");
533 printf("%lf", e->tempo.tempo);
534 break;
536 case SONG_EV_SS_SUSTAIN:
538 printf("SONG_EV_SS_SUSTAIN ");
539 printf("SUSTAIN:%lf", e->ss_sustain.sustain);
540 break;
542 case SONG_EV_SS_VIBRATO:
544 printf("SONG_EV_SS_VIBRATO ");
545 printf("DEPTH:%lf FREQ:%lf",
546 e->ss_vibrato.vib_depth,
547 e->ss_vibrato.vib_freq);
548 break;
550 case SONG_EV_SS_PORTAMENTO:
552 printf("SONG_EV_SS_PORTAMENTO ");
553 printf("VALUE:%lf",
554 e->ss_portamento.portamento);
555 break;
557 case SONG_EV_SS_CHANNEL:
559 printf("SONG_EV_SS_CHANNEL ");
560 printf("CHANNEL:%d VOL:%lf",
561 e->ss_channel.channel,
562 e->ss_channel.vol);
563 break;
565 case SONG_EV_SS_WAV:
567 printf("SONG_EV_SS_WAV ");
568 printf("FILE:'%s' BASE:%d MIN:%d MAX:%d START:%lf END:%lf",
569 e->ss_wav.file, e->ss_wav.base,
570 e->ss_wav.min, e->ss_wav.max,
571 e->ss_wav.loop_start, e->ss_wav.loop_end);
572 break;
574 case SONG_EV_SS_PAT:
576 printf("SONG_EV_SS_PAT ");
577 printf("FILE:'%s'", e->ss_pat.file);
578 break;
580 case SONG_EV_SS_EFF_DELAY:
582 printf("SONG_EV_SS_EFF_DELAY ");
583 printf("CHANNEL:%d ", e->ss_eff.channel);
584 printf("SIZE:%lf ", e->ss_eff.size);
585 break;
587 case SONG_EV_SS_EFF_ECHO:
589 printf("SONG_EV_SS_EFF_ECHO ");
590 printf("CHANNEL:%d ", e->ss_eff.channel);
591 printf("SIZE:%lf ", e->ss_eff.size);
592 printf("GAIN:%f ", e->ss_eff.gain);
593 break;
595 case SONG_EV_SS_EFF_COMB:
597 printf("SONG_EV_SS_EFF_COMB ");
598 printf("CHANNEL:%d ", e->ss_eff.channel);
599 printf("SIZE:%lf ", e->ss_eff.size);
600 printf("GAIN:%f ", e->ss_eff.gain);
601 break;
603 case SONG_EV_SS_EFF_ALLPASS:
605 printf("SONG_EV_SS_EFF_ALLPASS ");
606 printf("CHANNEL:%d ", e->ss_eff.channel);
607 printf("SIZE:%lf ", e->ss_eff.size);
608 printf("GAIN:%f ", e->ss_eff.gain);
609 break;
611 case SONG_EV_SS_EFF_FLANGER:
613 printf("SONG_EV_SS_EFF_FLANGER ");
614 printf("CHANNEL:%d ", e->ss_eff.channel);
615 printf("SIZE:%lf ", e->ss_eff.size);
616 printf("GAIN:%f ", e->ss_eff.gain);
617 printf("DEPTH:%lf ", e->ss_eff.depth);
618 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
619 e->ss_eff.phase);
620 break;
622 case SONG_EV_SS_EFF_WOBBLE:
624 printf("SONG_EV_SS_EFF_WOBBLE ");
625 printf("CHANNEL:%d ", e->ss_eff.channel);
626 printf("FREQ:%lf PHASE:%lf GAIN:%lf", e->ss_eff.freq,
627 e->ss_eff.phase, e->ss_eff.gain);
628 break;
630 case SONG_EV_SS_EFF_SQWOBBLE:
632 printf("SONG_EV_SS_EFF_SQWOBBLE ");
633 printf("CHANNEL:%d ", e->ss_eff.channel);
634 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
635 e->ss_eff.phase);
636 break;
638 case SONG_EV_SS_EFF_HFWOBBLE:
640 printf("SONG_EV_SS_EFF_HFWOBBLE ");
641 printf("CHANNEL:%d ", e->ss_eff.channel);
642 printf("FREQ:%lf PHASE:%lf", e->ss_eff.freq,
643 e->ss_eff.phase);
644 break;
646 case SONG_EV_SS_EFF_FADER:
648 printf("SONG_EV_SS_EFF_FADER ");
649 printf("CHANNEL:%d ", e->ss_eff.channel);
650 printf("SIZE:%lf ", e->ss_eff.size);
651 printf("INITIAL:%f FINAL:%f", e->ss_eff.initial,
652 e->ss_eff.final);
653 break;
655 case SONG_EV_SS_EFF_REVERB:
657 printf("SONG_EV_SS_EFF_REVERB ");
658 printf("CHANNEL:%d ", e->ss_eff.channel);
659 break;
661 case SONG_EV_SS_EFF_OFF:
663 printf("SONG_EV_SS_EFF_OFF ");
664 printf("CHANNEL:%d ", e->ss_eff.channel);
665 break;
667 case SONG_EV_SS_PITCH_STRETCH:
669 printf("SONG_EV_SS_PITCH_STRETCH ");
670 printf("MIDI:%d LEN:%lf VOL:%f",
671 e->ss_pitch_stretch.note,
672 e->ss_pitch_stretch.len,
673 e->ss_pitch_stretch.vol);
675 break;
677 case SONG_EV_SS_PRINT_WAVE_TEMPO:
679 printf("SONG_EV_SS_PRINT_WAVE_TEMPO ");
680 printf("MIDI:%d LEN:%lf",
681 e->ss_print_wave_tempo.note,
682 e->ss_print_wave_tempo.len);
683 break;
685 case SONG_EV_SONG_INFO:
687 printf("SONG_EV_SONG_INFO ");
688 printf("AUTHOR:'%s' NAME:'%s'",
689 e->song_info.author,
690 e->song_info.name);
691 break;
693 case SONG_EV_NOTE_ON:
695 printf("SONG_EV_NOTE_ON ");
696 printf("ID:%d MIDI:%d VOL:%f",
697 e->note_on.note_id, e->note_on.note,
698 e->note_on.vol);
699 break;
701 case SONG_EV_NOTE_OFF:
703 printf("SONG_EV_NOTE_OFF ");
704 printf("ID:%d", e->note_off.note_id);
705 break;
707 case SONG_EV_EOT:
709 printf("SONG_EV_EOT ");
710 break;
712 case SONG_EV_END:
714 printf("SONG_EV_END ");
715 break;
717 default:
718 printf("** Unexpected type: %d",
719 e->generic.type);
722 printf("\n");
725 printf("\n");
729 static int process_this_frame_events(int skip_frames)
730 /* process the events attached to this frame */
732 static union ss_ev * e = NULL;
733 static double tempo = 120.0;
734 static int frame = 0;
735 int nomore = 0;
737 /* from the beginning? */
738 if(e == NULL)
740 e = ss_song;
741 tempo = 120.0;
742 frame = 0;
745 if(verbose >= 1)
747 if(frame % ss_frequency == 0)
749 int m = frame / ss_frequency;
750 printf("[%02d:%02d]\r", m / 60, m % 60);
751 fflush(stdout);
755 while(!nomore && e->generic.frame == frame)
757 struct ss_ins * i;
758 struct ss_wave * w;
759 double freq;
761 if(e->generic.type == SONG_EV_NOTE_ON ||
762 e->generic.type == SONG_EV_NOTE_OFF ||
763 e->generic.type == SONG_EV_SS_PITCH_STRETCH)
765 if(frame < skip_frames)
767 e++;
768 frame = e->generic.frame;
769 continue;
773 /* take the instrument */
774 if(e->generic.trk_id < 0)
775 i = NULL;
776 else
777 i = &ss_song_ins[e->generic.trk_id];
779 switch(e->generic.type)
781 case SONG_EV_NOTE_ON:
783 if(ss_ins_note_on(i, e->note_on.note,
784 e->note_on.vol, e->note_on.note_id) < 0 &&
785 verbose >= 1)
786 printf("ss_ins_note_on error: track %d note %d\n",
787 e->note_on.trk_id, e->note_on.note);
789 break;
791 case SONG_EV_NOTE_OFF:
793 ss_ins_note_off(i, e->note_off.note_id);
795 break;
797 case SONG_EV_SS_SUSTAIN:
799 ss_ins_set_sustain(i, e->ss_sustain.sustain);
801 break;
803 case SONG_EV_SS_VIBRATO:
805 ss_ins_set_vibrato(i, e->ss_vibrato.vib_depth,
806 e->ss_vibrato.vib_freq);
808 break;
810 case SONG_EV_SS_PORTAMENTO:
812 ss_ins_set_portamento(i,
813 (e->ss_portamento.portamento * 44100.0)
814 / ((double) ss_frequency * 1000000.0));
816 break;
818 case SONG_EV_SS_CHANNEL:
820 ss_ins_set_channel(i, e->ss_channel.channel,
821 e->ss_channel.vol);
823 break;
825 case SONG_EV_SS_WAV:
827 w = ss_load_wav_file(e->ss_wav.file,
828 ss_note_frequency(e->ss_wav.base),
829 ss_note_frequency(e->ss_wav.min),
830 ss_note_frequency(e->ss_wav.max),
831 e->ss_wav.loop_start, e->ss_wav.loop_end,
832 e->ss_wav.first_channel, e->ss_wav.skip_channels);
834 /* fail if can't open wav */
835 if(w == NULL)
837 printf("Can't load wav '%s'\n", e->ss_wav.file);
838 nomore = 1;
840 else
841 ss_ins_add_layer(i, w);
843 break;
845 case SONG_EV_SS_PAT:
847 if(ss_load_pat_file(i, e->ss_pat.file) < 0)
849 printf("Can't load pat '%s'\n", e->ss_pat.file);
850 nomore = 1;
853 break;
855 case SONG_EV_SS_EFF_DELAY:
857 ss_eff_delay(&i->effs[e->ss_eff.channel], e->ss_eff.size);
858 break;
860 case SONG_EV_SS_EFF_ECHO:
862 ss_eff_echo(&i->effs[e->ss_eff.channel],
863 e->ss_eff.size, e->ss_eff.gain);
864 break;
866 case SONG_EV_SS_EFF_COMB:
868 ss_eff_comb(&i->effs[e->ss_eff.channel],
869 e->ss_eff.size, e->ss_eff.gain);
870 break;
872 case SONG_EV_SS_EFF_ALLPASS:
874 ss_eff_allpass(&i->effs[e->ss_eff.channel],
875 e->ss_eff.size, e->ss_eff.gain);
876 break;
878 case SONG_EV_SS_EFF_FLANGER:
880 ss_eff_flanger(&i->effs[e->ss_eff.channel],
881 e->ss_eff.size, e->ss_eff.gain,
882 e->ss_eff.depth, e->ss_eff.freq,
883 e->ss_eff.phase);
884 break;
886 case SONG_EV_SS_EFF_WOBBLE:
888 ss_eff_wobble(&i->effs[e->ss_eff.channel],
889 e->ss_eff.freq, e->ss_eff.phase,
890 e->ss_eff.gain);
892 break;
894 case SONG_EV_SS_EFF_SQWOBBLE:
896 ss_eff_square_wobble(&i->effs[e->ss_eff.channel],
897 e->ss_eff.freq, e->ss_eff.phase);
899 break;
901 case SONG_EV_SS_EFF_HFWOBBLE:
903 ss_eff_half_wobble(&i->effs[e->ss_eff.channel],
904 e->ss_eff.freq, e->ss_eff.phase);
906 break;
908 case SONG_EV_SS_EFF_FADER:
910 ss_eff_fader(&i->effs[e->ss_eff.channel],
911 e->ss_eff.size, e->ss_eff.initial,
912 e->ss_eff.final);
913 break;
915 case SONG_EV_SS_EFF_REVERB:
917 ss_eff_reverb(&i->effs[e->ss_eff.channel]);
918 break;
920 case SONG_EV_SS_EFF_FOLDBACK:
922 ss_eff_foldback(&i->effs[e->ss_eff.channel],
923 e->ss_eff.gain);
924 break;
926 case SONG_EV_SS_EFF_OFF:
928 ss_eff_off(&i->effs[e->ss_eff.channel]);
929 break;
931 case SONG_EV_TEMPO:
933 /* just store the last tempo */
934 tempo = e->tempo.tempo;
935 break;
937 case SONG_EV_SS_PITCH_STRETCH:
939 /* find the wave */
940 freq = ss_note_frequency(e->ss_pitch_stretch.note);
941 w = ss_ins_find_layer(i, freq, NULL);
943 /* calculate optimal frequency */
944 freq = ss_pitch_from_tempo(w, tempo,
945 e->ss_pitch_stretch.len);
947 /* play the note */
948 if(ss_ins_play(i, freq, e->ss_pitch_stretch.vol,
949 e->ss_pitch_stretch.note_id, w) < 0 &&
950 verbose >= 1)
951 printf("ss_ins_play error: track %d freq %f\n",
952 e->ss_pitch_stretch.trk_id, freq);
954 break;
956 case SONG_EV_SS_PRINT_WAVE_TEMPO:
958 /* find the wave */
959 freq = ss_note_frequency(e->ss_print_wave_tempo.note);
960 w = ss_ins_find_layer(i, freq, NULL);
962 /* print the optimal tempo */
963 printf("Optimal tempo: %lf\n",
964 ss_tempo_from_wave(w,
965 e->ss_print_wave_tempo.note,
966 e->ss_print_wave_tempo.len));
968 break;
970 case SONG_EV_SONG_INFO:
972 /* add a new song (track) */
973 cue_file_song_info(frame, e->song_info.author,
974 e->song_info.name);
975 break;
977 case SONG_EV_EOT:
979 /* end of track; trigger possible cleaning */
980 /*ss_ins_disable(i);*/
981 break;
983 case SONG_EV_END:
985 nomore = 1;
986 break;
988 case SONG_EV_BACK:
989 case SONG_EV_MIDI_CHANNEL:
990 case SONG_EV_MIDI_PROGRAM:
991 case SONG_EV_NOTE:
992 case SONG_EV_METER:
993 case SONG_EV_MEASURE:
995 /* never found in ss song streams */
996 break;
999 /* next event */
1000 e++;
1003 if(nomore)
1004 e = NULL;
1006 frame++;
1008 return(nomore);
1012 int ss_song_render(int skip_secs, char * driver, char * devfile)
1014 int n;
1015 sample_t output[SS_MAX_CHANNELS];
1016 int skip_frames;
1017 int n_tracks;
1018 int n_channels;
1020 /* convert the song to ss events */
1021 ss_song_convert_events(&n_tracks, &n_channels);
1023 if(verbose >= 2)
1024 printf("Tracks: %d Channels: %d Events: %d\n",
1025 n_song_tracks, n_channels, n_ss_ev);
1027 if(trace)
1029 ss_song_trace_events();
1030 return(0);
1033 /* set the number of channels, unless forced */
1034 if(ss_nchannels == -1)
1035 ss_nchannels = n_channels > 0 ? n_channels : 2;
1037 if(ss_output_open(driver, devfile) < 0)
1039 printf("Error: can't init driver\n");
1040 return(2);
1043 /* init the generators */
1044 ss_gen_init();
1046 /* init the instruments */
1047 for(n = 0;n < n_tracks;n++)
1048 ss_ins_init(&ss_song_ins[n]);
1050 /* calculate the frame to start playing */
1051 skip_frames = skip_secs * ss_frequency;
1053 /* main loop */
1054 for(;;)
1056 /* process all events in this frame */
1057 if(process_this_frame_events(skip_frames))
1058 break;
1060 /* reset frame samples */
1061 ss_output_init_frame(output);
1063 /* generate output from all instruments */
1064 for(n = 0;n < n_tracks;n++)
1065 ss_ins_frame(&ss_song_ins[n], output);
1067 /* dump to sampling driver */
1068 ss_output_write(output);
1071 if(verbose >= 1) printf("\n");
1073 ss_output_close();
1075 return(0);