Updated TODO.
[ahxm.git] / ss_song.c
blob069e3e870c617a5a0a10748b14729337773d2147
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 /* the softsynth song stream */
41 static struct song_ev *ss_song = NULL;
42 static int n_ss_ev = 0;
44 /* the instruments */
46 struct ss_ins ss_song_ins[SS_MAX_INSTRUMENTS];
48 /*******************
49 Code
50 ********************/
52 static struct song_ev * add_ss_ev(struct song_ev *e)
53 /* adds a softsynth song event */
55 return copy_event(&ss_song, &n_ss_ev, e);
59 static int frame_type_eventid_cmp(const void *v1, const void *v2)
60 /* softsynth song event compare function for qsort() */
62 struct song_ev *e1;
63 struct song_ev *e2;
64 int ret;
66 e1 = (struct song_ev *) v1;
67 e2 = (struct song_ev *) v2;
69 ret = e1->frame - e2->frame;
71 if (ret == 0)
72 ret = e1->type - e2->type;
74 if (ret == 0)
75 ret = e1->event_id - e2->event_id;
77 return ret;
81 static void ss_song_convert_events(int *n_channels)
82 /* converts generic song_ev events to softsynth events */
84 int note_id = 1;
85 struct song_ev *e;
86 int frame, frame_ac, f_frame;
87 double fpw, time_ac, time_ac_m;
88 int num, den;
89 int n;
91 *n_channels = -1;
93 /* resets the ss stream */
94 if (ss_song != NULL) {
95 free(ss_song);
96 ss_song = NULL;
99 n_ss_ev = 0;
101 /* sorts the song */
102 song_sort();
104 fpw = 0;
105 frame = frame_ac = f_frame = 0;
106 time_ac = time_ac_m = 0;
107 num = den = 4;
109 /* travels the song events generating softsynth song events */
110 for (n = 0; n < n_song_ev; n++) {
111 /* gets the song event */
112 e = &song[n];
114 /* calculates the frame */
115 frame = ((e->time - time_ac) * fpw) + frame_ac;
116 e->frame = frame;
118 switch (e->type) {
119 case SONG_EV_TEMPO:
121 /* updates accumulations */
122 frame_ac = frame;
123 time_ac = e->time;
125 /* calculates frames-per-whole based on new tempo */
126 fpw = (double) ss_frequency * 60.0;
127 fpw /= e->amount / 4.0;
129 add_ss_ev(e);
131 break;
133 case SONG_EV_METER:
135 /* just store the values */
136 num = e->min;
137 den = e->max;
138 time_ac_m = e->time;
140 break;
142 case SONG_EV_MEASURE:
144 song_test_measure_boundary(e->time - time_ac_m,
145 num, den, e->value);
146 break;
148 case SONG_EV_NOTE:
150 /* convert to note on / off pairs */
152 e = add_ss_ev(e);
154 e->type = SONG_EV_NOTE_ON;
155 e->note_id = note_id++;
157 frame += (int) (e->len * fpw);
159 e = add_ss_ev(e);
161 e->type = SONG_EV_NOTE_OFF;
162 e->frame = frame;
164 break;
166 case SONG_EV_BACK:
168 /* move the cursor back */
170 frame_ac -= (int) (e->len * fpw);
172 break;
174 case SONG_EV_SS_PITCH_STRETCH:
176 e = add_ss_ev(e);
178 e->note_id = note_id++;
180 frame += (int) (e->len * fpw);
182 e = add_ss_ev(e);
184 e->type = SONG_EV_NOTE_OFF;
185 e->frame = frame;
187 break;
189 case SONG_EV_SS_PRINT_WAVE_TEMPO:
190 case SONG_EV_SS_WAV:
191 case SONG_EV_SS_PAT:
192 case SONG_EV_SS_SUSTAIN:
193 case SONG_EV_SS_ATTACK:
194 case SONG_EV_SS_VIBRATO:
195 case SONG_EV_SS_PORTAMENTO:
196 case SONG_EV_SS_EFF_DELAY:
197 case SONG_EV_SS_EFF_ECHO:
198 case SONG_EV_SS_EFF_COMB:
199 case SONG_EV_SS_EFF_ALLPASS:
200 case SONG_EV_SS_EFF_FLANGER:
201 case SONG_EV_SS_EFF_WOBBLE:
202 case SONG_EV_SS_EFF_SQWOBBLE:
203 case SONG_EV_SS_EFF_HFWOBBLE:
204 case SONG_EV_SS_EFF_FADER:
205 case SONG_EV_SS_EFF_REVERB:
206 case SONG_EV_SS_EFF_FOLDBACK:
207 case SONG_EV_SS_EFF_ATAN:
208 case SONG_EV_SS_EFF_DISTORT:
209 case SONG_EV_SS_EFF_OVERDRIVE:
210 case SONG_EV_SS_EFF_OFF:
211 case SONG_EV_SONG_INFO:
212 case SONG_EV_EOT:
214 /* just copy */
215 add_ss_ev(e);
216 break;
218 case SONG_EV_SS_CHANNEL:
220 /* count channels */
221 if (*n_channels < e->channel)
222 *n_channels = e->channel;
224 add_ss_ev(e);
225 break;
227 case SONG_EV_NOP:
228 case SONG_EV_MIDI_CHANNEL:
229 case SONG_EV_MIDI_PROGRAM:
231 /* ignored */
232 break;
234 case SONG_EV_NOTE_ON:
235 case SONG_EV_NOTE_OFF:
236 case SONG_EV_END:
238 /* never found in generic song streams */
239 break;
242 /* store the further frame seen */
243 if (f_frame < frame)
244 f_frame = frame;
247 /* generates an end of event mark, a time after the last one */
248 e = add_event(&ss_song, &n_ss_ev);
250 e->type = SONG_EV_END;
251 e->frame = f_frame + ss_frequency;
252 e->event_id = -1;
254 /* finally sort */
255 qsort(ss_song, n_ss_ev, sizeof(struct song_ev), frame_type_eventid_cmp);
257 /* count one more */
258 (*n_channels)++;
262 static void ss_song_trace_events(void)
264 struct song_ev *e;
265 int n;
267 printf("** SOFTWARE SYNTHESIZER EVENT DUMP **\n\n");
268 printf("%10s %5s %5s Event and information\n", "Frame", "Track", "Ev.ID");
269 printf("------------------------------------------------------------\n");
271 for (n = 0, e = ss_song; n < n_ss_ev; n++, e++) {
272 printf("%10d %5d %5d ",
273 e->frame, e->trk_id, e->event_id);
275 switch (e->type) {
276 case SONG_EV_TEMPO:
278 printf("SONG_EV_TEMPO ");
279 printf("%lf", e->amount);
280 break;
282 case SONG_EV_SS_SUSTAIN:
284 printf("SONG_EV_SS_SUSTAIN ");
285 printf("SUSTAIN:%lf", e->amount);
286 break;
288 case SONG_EV_SS_ATTACK:
290 printf("SONG_EV_SS_ATTACK ");
291 printf("ATTACK:%lf", e->amount);
292 break;
294 case SONG_EV_SS_VIBRATO:
296 printf("SONG_EV_SS_VIBRATO ");
297 printf("DEPTH:%lf FREQ:%lf", e->depth, e->freq);
298 break;
300 case SONG_EV_SS_PORTAMENTO:
302 printf("SONG_EV_SS_PORTAMENTO ");
303 printf("VALUE:%lf", e->amount);
304 break;
306 case SONG_EV_SS_CHANNEL:
308 printf("SONG_EV_SS_CHANNEL ");
309 printf("CHANNEL:%d VOL:%lf", e->channel, e->vol);
310 break;
312 case SONG_EV_SS_WAV:
314 printf("SONG_EV_SS_WAV ");
315 printf("FILE:'%s' BASE:%d MIN:%d MAX:%d START:%lf END:%lf",
316 e->name, e->value, e->min, e->max,
317 e->start, e->end);
318 break;
320 case SONG_EV_SS_PAT:
322 printf("SONG_EV_SS_PAT ");
323 printf("FILE:'%s'", e->name);
324 break;
326 case SONG_EV_SS_EFF_DELAY:
328 printf("SONG_EV_SS_EFF_DELAY ");
329 printf("CHANNEL:%d ", e->channel);
330 printf("SIZE:%lf ", e->len);
331 break;
333 case SONG_EV_SS_EFF_ECHO:
335 printf("SONG_EV_SS_EFF_ECHO ");
336 printf("CHANNEL:%d ", e->channel);
337 printf("SIZE:%lf ", e->len);
338 printf("GAIN:%f ", e->vol);
339 break;
341 case SONG_EV_SS_EFF_COMB:
343 printf("SONG_EV_SS_EFF_COMB ");
344 printf("CHANNEL:%d ", e->channel);
345 printf("SIZE:%lf ", e->len);
346 printf("GAIN:%f ", e->vol);
347 break;
349 case SONG_EV_SS_EFF_ALLPASS:
351 printf("SONG_EV_SS_EFF_ALLPASS ");
352 printf("CHANNEL:%d ", e->channel);
353 printf("SIZE:%lf ", e->len);
354 printf("GAIN:%f ", e->vol);
355 break;
357 case SONG_EV_SS_EFF_FLANGER:
359 printf("SONG_EV_SS_EFF_FLANGER ");
360 printf("CHANNEL:%d ", e->channel);
361 printf("SIZE:%lf ", e->len);
362 printf("GAIN:%f ", e->vol);
363 printf("DEPTH:%lf ", e->depth);
364 printf("FREQ:%lf PHASE:%lf", e->freq, e->phase);
365 break;
367 case SONG_EV_SS_EFF_WOBBLE:
369 printf("SONG_EV_SS_EFF_WOBBLE ");
370 printf("CHANNEL:%d ", e->channel);
371 printf("FREQ:%lf PHASE:%lf GAIN:%lf", e->freq,
372 e->phase, e->vol);
373 break;
375 case SONG_EV_SS_EFF_SQWOBBLE:
377 printf("SONG_EV_SS_EFF_SQWOBBLE ");
378 printf("CHANNEL:%d ", e->channel);
379 printf("FREQ:%lf PHASE:%lf", e->freq, e->phase);
380 break;
382 case SONG_EV_SS_EFF_HFWOBBLE:
384 printf("SONG_EV_SS_EFF_HFWOBBLE ");
385 printf("CHANNEL:%d ", e->channel);
386 printf("FREQ:%lf PHASE:%lf", e->freq, e->phase);
387 break;
389 case SONG_EV_SS_EFF_FADER:
391 printf("SONG_EV_SS_EFF_FADER ");
392 printf("CHANNEL:%d ", e->channel);
393 printf("SIZE:%lf ", e->len);
394 printf("INITIAL:%f FINAL:%f", e->initial, e->final);
395 break;
397 case SONG_EV_SS_EFF_REVERB:
399 printf("SONG_EV_SS_EFF_REVERB ");
400 printf("CHANNEL:%d ", e->channel);
401 break;
403 case SONG_EV_SS_EFF_FOLDBACK:
405 printf("SONG_EV_SS_EFF_FOLDBACK ");
406 printf("CHANNEL:%d THRESHOLD:%f", e->channel, e->vol);
407 break;
409 case SONG_EV_SS_EFF_ATAN:
411 printf("SONG_EV_SS_EFF_ATAN ");
412 printf("CHANNEL:%d GAIN:%f", e->channel, e->vol);
413 break;
415 case SONG_EV_SS_EFF_DISTORT:
417 printf("SONG_EV_SS_EFF_DISTORT ");
418 printf("CHANNEL:%d GAIN:%f", e->channel, e->vol);
419 break;
421 case SONG_EV_SS_EFF_OVERDRIVE:
423 printf("SONG_EV_SS_EFF_OVERDRIVE ");
424 printf("CHANNEL:%d GAIN:%f", e->channel, e->vol);
425 break;
427 case SONG_EV_SS_EFF_OFF:
429 printf("SONG_EV_SS_EFF_OFF ");
430 printf("CHANNEL:%d ", e->channel);
431 break;
433 case SONG_EV_SS_PITCH_STRETCH:
435 printf("SONG_EV_SS_PITCH_STRETCH ");
436 printf("MIDI:%d LEN:%lf VOL:%f", e->value, e->len, e->vol);
438 break;
440 case SONG_EV_SS_PRINT_WAVE_TEMPO:
442 printf("SONG_EV_SS_PRINT_WAVE_TEMPO ");
443 printf("MIDI:%d LEN:%lf", e->value, e->len);
444 break;
446 case SONG_EV_SONG_INFO:
448 printf("SONG_EV_SONG_INFO ");
449 printf("AUTHOR:'%s' NAME:'%s'", e->author, e->name);
450 break;
452 case SONG_EV_NOTE_ON:
454 printf("SONG_EV_NOTE_ON ");
455 printf("ID:%d MIDI:%d VOL:%f", e->note_id, e->value, e->vol);
456 break;
458 case SONG_EV_NOTE_OFF:
460 printf("SONG_EV_NOTE_OFF ");
461 printf("ID:%d", e->note_id);
462 break;
464 case SONG_EV_EOT:
466 printf("SONG_EV_EOT ");
467 break;
469 case SONG_EV_END:
471 printf("SONG_EV_END ");
472 break;
474 case SONG_EV_NOP:
476 printf("SONG_EV_NOP ");
477 break;
479 default:
480 printf("** Unexpected type: %d", e->type);
483 printf("\n");
486 printf("\n");
490 static struct song_ev *process_this_frame_events(struct song_ev *e, int skip_frames)
491 /* process the events attached to this frame */
493 static double tempo = 120.0;
494 static int frame = 0;
496 /* from the beginning? */
497 if (e == NULL) {
498 e = ss_song;
499 tempo = 120.0;
500 frame = 0;
503 if (verbose >= 1 && frame % ss_frequency == 0) {
504 int m = frame / ss_frequency;
505 printf("[%02d:%02d]\r", m / 60, m % 60);
506 fflush(stdout);
509 while (e != NULL && e->frame == frame) {
510 struct ss_ins *i;
511 struct ss_wave *w;
512 double freq;
514 if (e->type == SONG_EV_NOTE_ON ||
515 e->type == SONG_EV_NOTE_OFF ||
516 e->type == SONG_EV_SS_PITCH_STRETCH) {
517 if (frame < skip_frames) {
518 e++;
519 frame = e->frame;
520 continue;
524 /* take the instrument */
525 if (e->trk_id < 0)
526 i = NULL;
527 else
528 i = &ss_song_ins[e->trk_id];
530 switch (e->type) {
531 case SONG_EV_NOTE_ON:
533 if (ss_ins_note_on(i, e->value, e->vol, e->note_id) < 0 &&
534 verbose >= 1)
535 printf("ss_ins_note_on error: track %d note %d\n",
536 e->trk_id, e->value);
538 break;
540 case SONG_EV_NOTE_OFF:
542 ss_ins_note_off(i, e->note_id);
544 break;
546 case SONG_EV_SS_SUSTAIN:
548 ss_ins_set_sustain(i, e->amount);
550 break;
552 case SONG_EV_SS_ATTACK:
554 ss_ins_set_attack(i, e->amount);
556 break;
558 case SONG_EV_SS_VIBRATO:
560 ss_ins_set_vibrato(i, e->depth, e->freq);
562 break;
564 case SONG_EV_SS_PORTAMENTO:
566 ss_ins_set_portamento(i, (e->amount * 44100.0)
567 / ((double) ss_frequency * 1000000.0));
569 break;
571 case SONG_EV_SS_CHANNEL:
573 ss_ins_set_channel(i, e->channel, e->vol);
575 break;
577 case SONG_EV_SS_WAV:
579 w = ss_load_wav_file(e->name,
580 ss_note_frequency(e->value),
581 ss_note_frequency(e->min),
582 ss_note_frequency(e->max),
583 e->start, e->end,
584 e->channel, e->skip_channels);
586 /* fail if can't open wav */
587 if (w == NULL) {
588 printf("Can't load wav '%s'\n", e->name);
589 e = NULL;
591 else
592 ss_ins_add_layer(i, w);
594 break;
596 case SONG_EV_SS_PAT:
598 if (ss_load_pat_file(i, e->name) < 0) {
599 printf("Can't load pat '%s'\n", e->name);
600 e = NULL;
603 break;
605 case SONG_EV_SS_EFF_DELAY:
607 ss_eff_delay(&i->effs[e->channel], e->len);
608 break;
610 case SONG_EV_SS_EFF_ECHO:
612 ss_eff_echo(&i->effs[e->channel], e->len, e->vol);
613 break;
615 case SONG_EV_SS_EFF_COMB:
617 ss_eff_comb(&i->effs[e->channel], e->len, e->vol);
618 break;
620 case SONG_EV_SS_EFF_ALLPASS:
622 ss_eff_allpass(&i->effs[e->channel], e->len, e->vol);
623 break;
625 case SONG_EV_SS_EFF_FLANGER:
627 ss_eff_flanger(&i->effs[e->channel],
628 e->len, e->vol, e->depth, e->freq, e->phase);
629 break;
631 case SONG_EV_SS_EFF_WOBBLE:
633 ss_eff_wobble(&i->effs[e->channel], e->freq, e->phase, e->vol);
635 break;
637 case SONG_EV_SS_EFF_SQWOBBLE:
639 ss_eff_square_wobble(&i->effs[e->channel], e->freq, e->phase);
641 break;
643 case SONG_EV_SS_EFF_HFWOBBLE:
645 ss_eff_half_wobble(&i->effs[e->channel], e->freq, e->phase);
647 break;
649 case SONG_EV_SS_EFF_FADER:
651 ss_eff_fader(&i->effs[e->channel], e->len, e->initial, e->final);
652 break;
654 case SONG_EV_SS_EFF_REVERB:
656 ss_eff_reverb(&i->effs[e->channel]);
657 break;
659 case SONG_EV_SS_EFF_FOLDBACK:
661 ss_eff_foldback(&i->effs[e->channel], e->vol);
662 break;
664 case SONG_EV_SS_EFF_ATAN:
666 ss_eff_atan(&i->effs[e->channel], e->vol);
667 break;
669 case SONG_EV_SS_EFF_DISTORT:
671 ss_eff_distort(&i->effs[e->channel], e->vol);
672 break;
674 case SONG_EV_SS_EFF_OVERDRIVE:
676 ss_eff_overdrive(&i->effs[e->channel], e->vol);
677 break;
679 case SONG_EV_SS_EFF_OFF:
681 ss_eff_off(&i->effs[e->channel]);
682 break;
684 case SONG_EV_TEMPO:
686 /* just store the last tempo */
687 tempo = e->amount;
688 break;
690 case SONG_EV_SS_PITCH_STRETCH:
692 /* find the wave */
693 freq = ss_note_frequency(e->value);
694 w = ss_ins_find_layer(i, freq, NULL);
696 /* calculate optimal frequency */
697 freq = ss_pitch_from_tempo(w, tempo, e->len);
699 /* play the note */
700 if (ss_ins_play(i, freq, e->vol,
701 e->note_id, w) < 0 && verbose >= 1)
702 printf("ss_ins_play error: track %d freq %f\n",
703 e->trk_id, freq);
705 break;
707 case SONG_EV_SS_PRINT_WAVE_TEMPO:
709 /* find the wave */
710 freq = ss_note_frequency(e->value);
711 w = ss_ins_find_layer(i, freq, NULL);
713 /* print the optimal tempo */
714 printf("Optimal tempo: %lf\n",
715 ss_tempo_from_wave(w, e->value, e->len));
717 break;
719 case SONG_EV_SONG_INFO:
721 /* add a new song (track) */
722 cue_file_song_info(frame, e->author, e->name);
723 break;
725 case SONG_EV_EOT:
727 /* end of track; trigger possible cleaning */
728 ss_ins_disable(i);
729 break;
731 case SONG_EV_END:
733 e = NULL;
734 break;
736 case SONG_EV_BACK:
737 case SONG_EV_MIDI_CHANNEL:
738 case SONG_EV_MIDI_PROGRAM:
739 case SONG_EV_NOTE:
740 case SONG_EV_METER:
741 case SONG_EV_MEASURE:
742 case SONG_EV_NOP:
744 /* never found in ss song streams */
745 break;
748 /* next event */
749 if (e)
750 e++;
753 frame++;
755 return e;
759 int ss_song_render(int skip_secs, char *driver, char *devfile)
761 int n, i = 0;
762 sample_t output[SS_MAX_CHANNELS];
763 int skip_frames;
764 int n_channels;
765 struct song_ev *e = NULL;
767 /* convert the song to ss events */
768 ss_song_convert_events(&n_channels);
770 if (verbose >= 2)
771 printf("Tracks: %d Channels: %d Events: %d\n",
772 n_song_tracks, n_channels, n_ss_ev);
774 if (trace) {
775 ss_song_trace_events();
776 return 0;
779 /* set the number of channels, unless forced */
780 if (ss_nchannels == -1)
781 ss_nchannels = n_channels > 0 ? n_channels : 2;
783 if (ss_output_open(driver, devfile) < 0) {
784 printf("Error: can't init driver\n");
785 return 2;
788 /* init the generators */
789 ss_gen_init();
791 /* init the instruments */
792 for (n = 0; n < n_song_tracks; n++)
793 ss_ins_init(&ss_song_ins[n]);
795 /* calculate the frame to start playing */
796 skip_frames = skip_secs * ss_frequency;
798 /* main loop */
799 do {
800 /* process all events in this frame */
801 e = process_this_frame_events(e, skip_frames);
803 /* reset frame samples */
804 ss_output_init_frame(output);
806 /* generate output from all instruments */
807 for (n = i = 0; n < n_song_tracks; n++)
808 i += ss_ins_frame(&ss_song_ins[n], output);
810 /* dump to sampling driver */
811 ss_output_write(output);
812 } while (i);
814 if (verbose >= 1)
815 printf("\n");
817 ss_output_close();
819 return 0;