Deleted copy_song_ev().
[ahxm.git] / ss_song.c
blobe79c75d0b3442bd2474ab58a6dec8e95e7834646
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_MIDI_CHANNEL:
228 case SONG_EV_MIDI_PROGRAM:
230 /* ignored */
231 break;
233 case SONG_EV_NOTE_ON:
234 case SONG_EV_NOTE_OFF:
235 case SONG_EV_END:
237 /* never found in generic song streams */
238 break;
241 /* store the further frame seen */
242 if (f_frame < frame)
243 f_frame = frame;
246 /* generates an end of event mark, a time after the last one */
247 e = add_event(&ss_song, &n_ss_ev);
249 e->type = SONG_EV_END;
250 e->frame = f_frame + ss_frequency;
251 e->event_id = -1;
253 /* finally sort */
254 qsort(ss_song, n_ss_ev, sizeof(struct song_ev), frame_type_eventid_cmp);
256 /* count one more */
257 (*n_channels)++;
261 static void ss_song_trace_events(void)
263 struct song_ev *e;
264 int n;
266 printf("** SOFTWARE SYNTHESIZER EVENT DUMP **\n\n");
267 printf("%10s %5s %5s Event and information\n", "Frame", "Track", "Ev.ID");
268 printf("------------------------------------------------------------\n");
270 for (n = 0, e = ss_song; n < n_ss_ev; n++, e++) {
271 printf("%10d %5d %5d ",
272 e->frame, e->trk_id, e->event_id);
274 switch (e->type) {
275 case SONG_EV_TEMPO:
277 printf("SONG_EV_TEMPO ");
278 printf("%lf", e->amount);
279 break;
281 case SONG_EV_SS_SUSTAIN:
283 printf("SONG_EV_SS_SUSTAIN ");
284 printf("SUSTAIN:%lf", e->amount);
285 break;
287 case SONG_EV_SS_ATTACK:
289 printf("SONG_EV_SS_ATTACK ");
290 printf("ATTACK:%lf", e->amount);
291 break;
293 case SONG_EV_SS_VIBRATO:
295 printf("SONG_EV_SS_VIBRATO ");
296 printf("DEPTH:%lf FREQ:%lf", e->depth, e->freq);
297 break;
299 case SONG_EV_SS_PORTAMENTO:
301 printf("SONG_EV_SS_PORTAMENTO ");
302 printf("VALUE:%lf", e->amount);
303 break;
305 case SONG_EV_SS_CHANNEL:
307 printf("SONG_EV_SS_CHANNEL ");
308 printf("CHANNEL:%d VOL:%lf", e->channel, e->vol);
309 break;
311 case SONG_EV_SS_WAV:
313 printf("SONG_EV_SS_WAV ");
314 printf("FILE:'%s' BASE:%d MIN:%d MAX:%d START:%lf END:%lf",
315 e->name, e->value, e->min, e->max,
316 e->start, e->end);
317 break;
319 case SONG_EV_SS_PAT:
321 printf("SONG_EV_SS_PAT ");
322 printf("FILE:'%s'", e->name);
323 break;
325 case SONG_EV_SS_EFF_DELAY:
327 printf("SONG_EV_SS_EFF_DELAY ");
328 printf("CHANNEL:%d ", e->channel);
329 printf("SIZE:%lf ", e->len);
330 break;
332 case SONG_EV_SS_EFF_ECHO:
334 printf("SONG_EV_SS_EFF_ECHO ");
335 printf("CHANNEL:%d ", e->channel);
336 printf("SIZE:%lf ", e->len);
337 printf("GAIN:%f ", e->vol);
338 break;
340 case SONG_EV_SS_EFF_COMB:
342 printf("SONG_EV_SS_EFF_COMB ");
343 printf("CHANNEL:%d ", e->channel);
344 printf("SIZE:%lf ", e->len);
345 printf("GAIN:%f ", e->vol);
346 break;
348 case SONG_EV_SS_EFF_ALLPASS:
350 printf("SONG_EV_SS_EFF_ALLPASS ");
351 printf("CHANNEL:%d ", e->channel);
352 printf("SIZE:%lf ", e->len);
353 printf("GAIN:%f ", e->vol);
354 break;
356 case SONG_EV_SS_EFF_FLANGER:
358 printf("SONG_EV_SS_EFF_FLANGER ");
359 printf("CHANNEL:%d ", e->channel);
360 printf("SIZE:%lf ", e->len);
361 printf("GAIN:%f ", e->vol);
362 printf("DEPTH:%lf ", e->depth);
363 printf("FREQ:%lf PHASE:%lf", e->freq, e->phase);
364 break;
366 case SONG_EV_SS_EFF_WOBBLE:
368 printf("SONG_EV_SS_EFF_WOBBLE ");
369 printf("CHANNEL:%d ", e->channel);
370 printf("FREQ:%lf PHASE:%lf GAIN:%lf", e->freq,
371 e->phase, e->vol);
372 break;
374 case SONG_EV_SS_EFF_SQWOBBLE:
376 printf("SONG_EV_SS_EFF_SQWOBBLE ");
377 printf("CHANNEL:%d ", e->channel);
378 printf("FREQ:%lf PHASE:%lf", e->freq, e->phase);
379 break;
381 case SONG_EV_SS_EFF_HFWOBBLE:
383 printf("SONG_EV_SS_EFF_HFWOBBLE ");
384 printf("CHANNEL:%d ", e->channel);
385 printf("FREQ:%lf PHASE:%lf", e->freq, e->phase);
386 break;
388 case SONG_EV_SS_EFF_FADER:
390 printf("SONG_EV_SS_EFF_FADER ");
391 printf("CHANNEL:%d ", e->channel);
392 printf("SIZE:%lf ", e->len);
393 printf("INITIAL:%f FINAL:%f", e->initial, e->final);
394 break;
396 case SONG_EV_SS_EFF_REVERB:
398 printf("SONG_EV_SS_EFF_REVERB ");
399 printf("CHANNEL:%d ", e->channel);
400 break;
402 case SONG_EV_SS_EFF_FOLDBACK:
404 printf("SONG_EV_SS_EFF_FOLDBACK ");
405 printf("CHANNEL:%d THRESHOLD:%f", e->channel, e->vol);
406 break;
408 case SONG_EV_SS_EFF_ATAN:
410 printf("SONG_EV_SS_EFF_ATAN ");
411 printf("CHANNEL:%d GAIN:%f", e->channel, e->vol);
412 break;
414 case SONG_EV_SS_EFF_DISTORT:
416 printf("SONG_EV_SS_EFF_DISTORT ");
417 printf("CHANNEL:%d GAIN:%f", e->channel, e->vol);
418 break;
420 case SONG_EV_SS_EFF_OVERDRIVE:
422 printf("SONG_EV_SS_EFF_OVERDRIVE ");
423 printf("CHANNEL:%d GAIN:%f", e->channel, e->vol);
424 break;
426 case SONG_EV_SS_EFF_OFF:
428 printf("SONG_EV_SS_EFF_OFF ");
429 printf("CHANNEL:%d ", e->channel);
430 break;
432 case SONG_EV_SS_PITCH_STRETCH:
434 printf("SONG_EV_SS_PITCH_STRETCH ");
435 printf("MIDI:%d LEN:%lf VOL:%f", e->value, e->len, e->vol);
437 break;
439 case SONG_EV_SS_PRINT_WAVE_TEMPO:
441 printf("SONG_EV_SS_PRINT_WAVE_TEMPO ");
442 printf("MIDI:%d LEN:%lf", e->value, e->len);
443 break;
445 case SONG_EV_SONG_INFO:
447 printf("SONG_EV_SONG_INFO ");
448 printf("AUTHOR:'%s' NAME:'%s'", e->author, e->name);
449 break;
451 case SONG_EV_NOTE_ON:
453 printf("SONG_EV_NOTE_ON ");
454 printf("ID:%d MIDI:%d VOL:%f", e->note_id, e->value, e->vol);
455 break;
457 case SONG_EV_NOTE_OFF:
459 printf("SONG_EV_NOTE_OFF ");
460 printf("ID:%d", e->note_id);
461 break;
463 case SONG_EV_EOT:
465 printf("SONG_EV_EOT ");
466 break;
468 case SONG_EV_END:
470 printf("SONG_EV_END ");
471 break;
473 default:
474 printf("** Unexpected type: %d", e->type);
477 printf("\n");
480 printf("\n");
484 static struct song_ev *process_this_frame_events(struct song_ev *e, int skip_frames)
485 /* process the events attached to this frame */
487 static double tempo = 120.0;
488 static int frame = 0;
490 /* from the beginning? */
491 if (e == NULL) {
492 e = ss_song;
493 tempo = 120.0;
494 frame = 0;
497 if (verbose >= 1 && frame % ss_frequency == 0) {
498 int m = frame / ss_frequency;
499 printf("[%02d:%02d]\r", m / 60, m % 60);
500 fflush(stdout);
503 while (e != NULL && e->frame == frame) {
504 struct ss_ins *i;
505 struct ss_wave *w;
506 double freq;
508 if (e->type == SONG_EV_NOTE_ON ||
509 e->type == SONG_EV_NOTE_OFF ||
510 e->type == SONG_EV_SS_PITCH_STRETCH) {
511 if (frame < skip_frames) {
512 e++;
513 frame = e->frame;
514 continue;
518 /* take the instrument */
519 if (e->trk_id < 0)
520 i = NULL;
521 else
522 i = &ss_song_ins[e->trk_id];
524 switch (e->type) {
525 case SONG_EV_NOTE_ON:
527 if (ss_ins_note_on(i, e->value, e->vol, e->note_id) < 0 &&
528 verbose >= 1)
529 printf("ss_ins_note_on error: track %d note %d\n",
530 e->trk_id, e->value);
532 break;
534 case SONG_EV_NOTE_OFF:
536 ss_ins_note_off(i, e->note_id);
538 break;
540 case SONG_EV_SS_SUSTAIN:
542 ss_ins_set_sustain(i, e->amount);
544 break;
546 case SONG_EV_SS_ATTACK:
548 ss_ins_set_attack(i, e->amount);
550 break;
552 case SONG_EV_SS_VIBRATO:
554 ss_ins_set_vibrato(i, e->depth, e->freq);
556 break;
558 case SONG_EV_SS_PORTAMENTO:
560 ss_ins_set_portamento(i, (e->amount * 44100.0)
561 / ((double) ss_frequency * 1000000.0));
563 break;
565 case SONG_EV_SS_CHANNEL:
567 ss_ins_set_channel(i, e->channel, e->vol);
569 break;
571 case SONG_EV_SS_WAV:
573 w = ss_load_wav_file(e->name,
574 ss_note_frequency(e->value),
575 ss_note_frequency(e->min),
576 ss_note_frequency(e->max),
577 e->start, e->end,
578 e->channel, e->skip_channels);
580 /* fail if can't open wav */
581 if (w == NULL) {
582 printf("Can't load wav '%s'\n", e->name);
583 e = NULL;
585 else
586 ss_ins_add_layer(i, w);
588 break;
590 case SONG_EV_SS_PAT:
592 if (ss_load_pat_file(i, e->name) < 0) {
593 printf("Can't load pat '%s'\n", e->name);
594 e = NULL;
597 break;
599 case SONG_EV_SS_EFF_DELAY:
601 ss_eff_delay(&i->effs[e->channel], e->len);
602 break;
604 case SONG_EV_SS_EFF_ECHO:
606 ss_eff_echo(&i->effs[e->channel], e->len, e->vol);
607 break;
609 case SONG_EV_SS_EFF_COMB:
611 ss_eff_comb(&i->effs[e->channel], e->len, e->vol);
612 break;
614 case SONG_EV_SS_EFF_ALLPASS:
616 ss_eff_allpass(&i->effs[e->channel], e->len, e->vol);
617 break;
619 case SONG_EV_SS_EFF_FLANGER:
621 ss_eff_flanger(&i->effs[e->channel],
622 e->len, e->vol, e->depth, e->freq, e->phase);
623 break;
625 case SONG_EV_SS_EFF_WOBBLE:
627 ss_eff_wobble(&i->effs[e->channel], e->freq, e->phase, e->vol);
629 break;
631 case SONG_EV_SS_EFF_SQWOBBLE:
633 ss_eff_square_wobble(&i->effs[e->channel], e->freq, e->phase);
635 break;
637 case SONG_EV_SS_EFF_HFWOBBLE:
639 ss_eff_half_wobble(&i->effs[e->channel], e->freq, e->phase);
641 break;
643 case SONG_EV_SS_EFF_FADER:
645 ss_eff_fader(&i->effs[e->channel], e->len, e->initial, e->final);
646 break;
648 case SONG_EV_SS_EFF_REVERB:
650 ss_eff_reverb(&i->effs[e->channel]);
651 break;
653 case SONG_EV_SS_EFF_FOLDBACK:
655 ss_eff_foldback(&i->effs[e->channel], e->vol);
656 break;
658 case SONG_EV_SS_EFF_ATAN:
660 ss_eff_atan(&i->effs[e->channel], e->vol);
661 break;
663 case SONG_EV_SS_EFF_DISTORT:
665 ss_eff_distort(&i->effs[e->channel], e->vol);
666 break;
668 case SONG_EV_SS_EFF_OVERDRIVE:
670 ss_eff_overdrive(&i->effs[e->channel], e->vol);
671 break;
673 case SONG_EV_SS_EFF_OFF:
675 ss_eff_off(&i->effs[e->channel]);
676 break;
678 case SONG_EV_TEMPO:
680 /* just store the last tempo */
681 tempo = e->amount;
682 break;
684 case SONG_EV_SS_PITCH_STRETCH:
686 /* find the wave */
687 freq = ss_note_frequency(e->value);
688 w = ss_ins_find_layer(i, freq, NULL);
690 /* calculate optimal frequency */
691 freq = ss_pitch_from_tempo(w, tempo, e->len);
693 /* play the note */
694 if (ss_ins_play(i, freq, e->vol,
695 e->note_id, w) < 0 && verbose >= 1)
696 printf("ss_ins_play error: track %d freq %f\n",
697 e->trk_id, freq);
699 break;
701 case SONG_EV_SS_PRINT_WAVE_TEMPO:
703 /* find the wave */
704 freq = ss_note_frequency(e->value);
705 w = ss_ins_find_layer(i, freq, NULL);
707 /* print the optimal tempo */
708 printf("Optimal tempo: %lf\n",
709 ss_tempo_from_wave(w, e->value, e->len));
711 break;
713 case SONG_EV_SONG_INFO:
715 /* add a new song (track) */
716 cue_file_song_info(frame, e->author, e->name);
717 break;
719 case SONG_EV_EOT:
721 /* end of track; trigger possible cleaning */
722 ss_ins_disable(i);
723 break;
725 case SONG_EV_END:
727 e = NULL;
728 break;
730 case SONG_EV_BACK:
731 case SONG_EV_MIDI_CHANNEL:
732 case SONG_EV_MIDI_PROGRAM:
733 case SONG_EV_NOTE:
734 case SONG_EV_METER:
735 case SONG_EV_MEASURE:
737 /* never found in ss song streams */
738 break;
741 /* next event */
742 if (e)
743 e++;
746 frame++;
748 return e;
752 int ss_song_render(int skip_secs, char *driver, char *devfile)
754 int n, i = 0;
755 sample_t output[SS_MAX_CHANNELS];
756 int skip_frames;
757 int n_channels;
758 struct song_ev *e = NULL;
760 /* convert the song to ss events */
761 ss_song_convert_events(&n_channels);
763 if (verbose >= 2)
764 printf("Tracks: %d Channels: %d Events: %d\n",
765 n_song_tracks, n_channels, n_ss_ev);
767 if (trace) {
768 ss_song_trace_events();
769 return 0;
772 /* set the number of channels, unless forced */
773 if (ss_nchannels == -1)
774 ss_nchannels = n_channels > 0 ? n_channels : 2;
776 if (ss_output_open(driver, devfile) < 0) {
777 printf("Error: can't init driver\n");
778 return 2;
781 /* init the generators */
782 ss_gen_init();
784 /* init the instruments */
785 for (n = 0; n < n_song_tracks; n++)
786 ss_ins_init(&ss_song_ins[n]);
788 /* calculate the frame to start playing */
789 skip_frames = skip_secs * ss_frequency;
791 /* main loop */
792 do {
793 /* process all events in this frame */
794 e = process_this_frame_events(e, skip_frames);
796 /* reset frame samples */
797 ss_output_init_frame(output);
799 /* generate output from all instruments */
800 for (n = i = 0; n < n_song_tracks; n++)
801 i += ss_ins_frame(&ss_song_ins[n], output);
803 /* dump to sampling driver */
804 ss_output_write(output);
805 } while (i);
807 if (verbose >= 1)
808 printf("\n");
810 ss_output_close();
812 return 0;