Fix file permissions.
[ahxm.git] / ss_song.c
blobc6272bd39eccc9667b478f828fcaf50dd236fca2
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2008 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"
36 /** data **/
38 /* the softsynth song stream */
40 static struct song_ev *ss_song = NULL;
41 static int n_ss_ev = 0;
43 /* the instruments */
45 struct ss_ins ss_song_ins[SS_MAX_INSTRUMENTS];
48 /** code **/
50 static struct song_ev *add_ss_ev(const struct song_ev *e)
51 /* adds a softsynth song event */
53 return copy_event(&ss_song, &n_ss_ev, e);
57 static int frame_type_eventid_cmp(const void *v1, const void *v2)
58 /* softsynth song event compare function for qsort() */
60 struct song_ev *e1;
61 struct song_ev *e2;
62 int ret;
64 e1 = (struct song_ev *) v1;
65 e2 = (struct song_ev *) v2;
67 ret = e1->frame - e2->frame;
69 if (ret == 0)
70 ret = e1->type - e2->type;
72 if (ret == 0)
73 ret = e1->event_id - e2->event_id;
75 return ret;
79 static void ss_song_convert_events(int *n_channels)
80 /* converts generic song_ev events to softsynth events */
82 int note_id = 1;
83 struct song_ev *e;
84 int frame, frame_ac, f_frame;
85 double fpw, time_ac, time_ac_m;
86 int num, den;
87 int n;
89 *n_channels = -1;
91 /* resets the ss stream */
92 if (ss_song != NULL) {
93 free(ss_song);
94 ss_song = NULL;
97 n_ss_ev = 0;
99 /* sorts the song */
100 song_sort();
102 fpw = 0;
103 frame = frame_ac = f_frame = 0;
104 time_ac = time_ac_m = 0;
105 num = den = 4;
107 /* travels the song events generating softsynth song events */
108 for (n = 0; n < n_song_ev; n++) {
109 /* gets the song event */
110 e = &song[n];
112 /* calculates the frame */
113 frame = ((e->time - time_ac) * fpw) + frame_ac;
114 e->frame = frame;
116 switch (e->type) {
117 case SONG_EV_TEMPO:
119 /* updates accumulations */
120 frame_ac = frame;
121 time_ac = e->time;
123 /* calculates frames-per-whole based on new tempo */
124 fpw = (double) ss_frequency *60.0;
125 fpw /= e->amount / 4.0;
127 add_ss_ev(e);
129 break;
131 case SONG_EV_METER:
133 /* just store the values */
134 num = e->min;
135 den = e->max;
136 time_ac_m = e->time;
138 break;
140 case SONG_EV_MEASURE:
142 song_test_measure_boundary(e->time - time_ac_m,
143 num, den, e->value);
144 break;
146 case SONG_EV_NOTE:
148 /* assign a note id */
149 e->note_id = note_id++;
151 /* copy */
152 add_ss_ev(e);
154 /* copy again, but set as NOTE_OFF */
155 e = add_ss_ev(e);
156 e->type = SONG_EV_NOTE_OFF;
158 frame += (int) (e->len * fpw);
159 e->frame = frame;
161 break;
163 case SONG_EV_BACK:
165 /* move the cursor back */
167 frame_ac -= (int) (e->len * fpw);
169 break;
171 case SONG_EV_SS_PITCH_STRETCH:
173 /* assign a note id */
174 e->note_id = note_id++;
176 /* copy */
177 add_ss_ev(e);
179 /* and copy again, as a note off */
180 e = add_ss_ev(e);
181 e->type = SONG_EV_NOTE_OFF;
183 frame += (int) (e->len * fpw);
184 e->frame = frame;
186 break;
188 case SONG_EV_SS_PRINT_WAVE_TEMPO:
189 case SONG_EV_SS_WAV:
190 case SONG_EV_SS_PAT:
191 case SONG_EV_SS_SF2:
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_SS_MASTER_VOLUME:
212 case SONG_EV_SONG_INFO:
213 case SONG_EV_EOT:
215 /* just copy */
216 add_ss_ev(e);
217 break;
219 case SONG_EV_SS_CHANNEL:
221 /* count channels */
222 if (*n_channels < e->channel)
223 *n_channels = e->channel;
225 add_ss_ev(e);
226 break;
228 case SONG_EV_NOP:
229 case SONG_EV_MIDI_CHANNEL:
230 case SONG_EV_MIDI_PROGRAM:
232 /* ignored */
233 break;
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),
256 frame_type_eventid_cmp);
258 /* count one more */
259 (*n_channels)++;
263 static const struct song_ev *process_this_frame_events(const struct song_ev
264 *e, int skip_frames)
265 /* process the events attached to this frame */
267 static double tempo = 120.0;
268 static int frame = 0;
270 /* from the beginning? */
271 if (e == NULL) {
272 e = ss_song;
273 tempo = 120.0;
274 frame = 0;
277 if (verbose >= 1 && frame % ss_frequency == 0) {
278 int m = frame / ss_frequency;
279 printf("[%02d:%02d]\r", m / 60, m % 60);
280 fflush(stdout);
283 while (e != NULL && e->frame == frame) {
284 struct ss_ins *i;
285 struct ss_wave *w;
286 double freq;
288 if (e->type == SONG_EV_NOTE ||
289 e->type == SONG_EV_NOTE_OFF ||
290 e->type == SONG_EV_SS_PITCH_STRETCH) {
291 if (frame < skip_frames) {
292 e++;
293 frame = e->frame;
294 continue;
298 /* take the instrument */
299 if (e->trk_id < 0)
300 i = NULL;
301 else
302 i = &ss_song_ins[e->trk_id];
304 switch (e->type) {
305 case SONG_EV_NOTE:
307 if (ss_ins_note_on(i, e->value, e->vol, e->note_id) < 0 &&
308 verbose >= 1)
309 printf("ss_ins_note_on error: track %d note %d\n",
310 e->trk_id, e->value);
312 break;
314 case SONG_EV_NOTE_OFF:
316 ss_ins_note_off(i, e->note_id);
318 break;
320 case SONG_EV_SS_SUSTAIN:
322 ss_ins_set_sustain(i, e->amount);
324 break;
326 case SONG_EV_SS_ATTACK:
328 ss_ins_set_attack(i, e->amount);
330 break;
332 case SONG_EV_SS_VIBRATO:
334 ss_ins_set_vibrato(i, e->depth, e->freq);
336 break;
338 case SONG_EV_SS_PORTAMENTO:
340 ss_ins_set_portamento(i, (e->amount * 44100.0)
341 / ((double) ss_frequency * 1000000.0));
343 break;
345 case SONG_EV_SS_CHANNEL:
347 ss_ins_set_channel(i, e->channel, e->vol);
349 break;
351 case SONG_EV_SS_WAV:
353 w = ss_load_wav_file(e->name,
354 ss_note_frequency(e->value),
355 ss_note_frequency(e->min),
356 ss_note_frequency(e->max),
357 e->start, e->end,
358 e->channel, e->skip_channels);
360 /* fail if can't open wav */
361 if (w == NULL) {
362 printf("Can't load wav '%s'\n", e->name);
363 e = NULL;
365 else
366 ss_ins_add_layer(i, w);
368 break;
370 case SONG_EV_SS_PAT:
372 if (ss_load_pat_file(i, e->name) < 0) {
373 printf("Can't load pat '%s'\n", e->name);
374 e = NULL;
377 break;
379 case SONG_EV_SS_SF2:
381 if (ss_load_sf2_file(i, e->name, e->str2) < 0) {
382 printf("Can't load instrument from sf2 '%s'\n", e->name);
383 e = NULL;
386 break;
388 case SONG_EV_SS_EFF_DELAY:
390 ss_eff_delay(&i->effs[e->channel], e->len);
391 break;
393 case SONG_EV_SS_EFF_ECHO:
395 ss_eff_echo(&i->effs[e->channel], e->len, e->vol);
396 break;
398 case SONG_EV_SS_EFF_COMB:
400 ss_eff_comb(&i->effs[e->channel], e->len, e->vol);
401 break;
403 case SONG_EV_SS_EFF_ALLPASS:
405 ss_eff_allpass(&i->effs[e->channel], e->len, e->vol);
406 break;
408 case SONG_EV_SS_EFF_FLANGER:
410 ss_eff_flanger(&i->effs[e->channel],
411 e->len, e->vol, e->depth, e->freq, e->phase);
412 break;
414 case SONG_EV_SS_EFF_WOBBLE:
416 ss_eff_wobble(&i->effs[e->channel], e->freq, e->phase, e->vol);
418 break;
420 case SONG_EV_SS_EFF_SQWOBBLE:
422 ss_eff_square_wobble(&i->effs[e->channel], e->freq, e->phase);
424 break;
426 case SONG_EV_SS_EFF_HFWOBBLE:
428 ss_eff_half_wobble(&i->effs[e->channel], e->freq, e->phase);
430 break;
432 case SONG_EV_SS_EFF_FADER:
434 ss_eff_fader(&i->effs[e->channel], e->len, e->initial,
435 e->final);
436 break;
438 case SONG_EV_SS_EFF_REVERB:
440 ss_eff_reverb(&i->effs[e->channel]);
441 break;
443 case SONG_EV_SS_EFF_FOLDBACK:
445 ss_eff_foldback(&i->effs[e->channel], e->vol);
446 break;
448 case SONG_EV_SS_EFF_ATAN:
450 ss_eff_atan(&i->effs[e->channel], e->vol);
451 break;
453 case SONG_EV_SS_EFF_DISTORT:
455 ss_eff_distort(&i->effs[e->channel], e->vol);
456 break;
458 case SONG_EV_SS_EFF_OVERDRIVE:
460 ss_eff_overdrive(&i->effs[e->channel], e->vol);
461 break;
463 case SONG_EV_SS_EFF_OFF:
465 ss_eff_off(&i->effs[e->channel]);
466 break;
468 case SONG_EV_TEMPO:
470 /* just store the last tempo */
471 tempo = e->amount;
472 break;
474 case SONG_EV_SS_PITCH_STRETCH:
476 /* find the wave */
477 freq = ss_note_frequency(e->value);
478 w = ss_ins_find_layer(i, freq, NULL);
480 /* calculate optimal frequency */
481 freq = ss_pitch_from_tempo(w, tempo, e->len);
483 /* play the note */
484 if (ss_ins_play(i, freq, e->vol,
485 e->note_id, w) < 0 && verbose >= 1)
486 printf("ss_ins_play error: track %d freq %f\n",
487 e->trk_id, freq);
489 break;
491 case SONG_EV_SS_PRINT_WAVE_TEMPO:
493 /* find the wave */
494 freq = ss_note_frequency(e->value);
495 w = ss_ins_find_layer(i, freq, NULL);
497 /* print the optimal tempo */
498 printf("Optimal tempo: %lf\n",
499 ss_tempo_from_wave(w, e->value, e->len));
501 break;
503 case SONG_EV_SS_MASTER_VOLUME:
505 /* set master volume */
506 ss_master_volume = e->vol;
507 break;
509 case SONG_EV_SONG_INFO:
511 /* add a new song (track) */
512 cue_file_song_info(frame, e->str2, e->name);
513 break;
515 case SONG_EV_EOT:
517 /* end of track; trigger possible cleaning */
518 ss_ins_disable(i);
519 break;
521 case SONG_EV_END:
523 e = NULL;
524 break;
526 case SONG_EV_BACK:
527 case SONG_EV_MIDI_CHANNEL:
528 case SONG_EV_MIDI_PROGRAM:
529 case SONG_EV_METER:
530 case SONG_EV_MEASURE:
531 case SONG_EV_NOP:
533 /* never found in ss song streams */
534 break;
537 /* next event */
538 if (e)
539 e++;
542 frame++;
544 return e;
548 int ss_song_render(int skip_secs, const char *driver, const char *devfile)
550 int n, i = 0;
551 sample_t output[SS_MAX_CHANNELS];
552 int skip_frames;
553 int n_channels;
554 const struct song_ev *e = NULL;
556 /* convert the song to ss events */
557 ss_song_convert_events(&n_channels);
559 if (verbose >= 2)
560 printf("Tracks: %d Channels: %d Events: %d\n",
561 n_song_tracks, n_channels, n_ss_ev);
563 if (trace) {
564 printf("** SOFTWARE SYNTHESIZER EVENT DUMP **\n\n");
565 dump_song_events(ss_song, n_ss_ev);
566 return 0;
569 /* set the number of channels, unless forced */
570 if (ss_nchannels == -1)
571 ss_nchannels = n_channels > 0 ? n_channels : 2;
573 if (ss_output_open(driver, devfile) < 0) {
574 printf("Error: can't init driver\n");
575 return 2;
578 /* init the generators */
579 ss_gen_init();
581 /* init the instruments */
582 for (n = 0; n < n_song_tracks; n++)
583 ss_ins_init(&ss_song_ins[n]);
585 /* calculate the frame to start playing */
586 skip_frames = skip_secs * ss_frequency;
588 /* main loop */
589 do {
590 /* process all events in this frame */
591 e = process_this_frame_events(e, skip_frames);
593 /* reset frame samples */
594 ss_output_init_frame(output);
596 /* generate output from all instruments */
597 for (n = i = 0; n < n_song_tracks; n++)
598 i += ss_ins_frame(&ss_song_ins[n], output);
600 /* dump to sampling driver */
601 ss_output_write(output);
602 } while (i);
604 if (verbose >= 1)
605 printf("\n");
607 ss_output_close();
609 return 0;