SIGSEGV crashes seem fixed (Closes: #1145).
[ahxm.git] / ss_song.c
blob698206459f806e5d14c4c906a90d7d9a3d1ca58e
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 /* FIXME: this *e2 thing is hiding a probably grave
153 problem in add_event() / copy_event() */
155 struct song_ev *e2;
157 /* assign a note id */
158 e->note_id = note_id++;
160 e2 = add_ss_ev(e);
161 e2->type = SONG_EV_NOTE_ON;
163 e2 = add_ss_ev(e);
164 e2->type = SONG_EV_NOTE_OFF;
166 frame += (int) (e->len * fpw);
167 e2->frame = frame;
170 break;
172 case SONG_EV_BACK:
174 /* move the cursor back */
176 frame_ac -= (int) (e->len * fpw);
178 break;
180 case SONG_EV_SS_PITCH_STRETCH:
182 /* assign a note id */
183 e->note_id = note_id++;
185 /* copy */
186 add_ss_ev(e);
188 /* and copy again, as a note off */
189 e = add_ss_ev(e);
190 e->type = SONG_EV_NOTE_OFF;
192 frame += (int) (e->len * fpw);
193 e->frame = frame;
195 break;
197 case SONG_EV_SS_PRINT_WAVE_TEMPO:
198 case SONG_EV_SS_WAV:
199 case SONG_EV_SS_PAT:
200 case SONG_EV_SS_SUSTAIN:
201 case SONG_EV_SS_ATTACK:
202 case SONG_EV_SS_VIBRATO:
203 case SONG_EV_SS_PORTAMENTO:
204 case SONG_EV_SS_EFF_DELAY:
205 case SONG_EV_SS_EFF_ECHO:
206 case SONG_EV_SS_EFF_COMB:
207 case SONG_EV_SS_EFF_ALLPASS:
208 case SONG_EV_SS_EFF_FLANGER:
209 case SONG_EV_SS_EFF_WOBBLE:
210 case SONG_EV_SS_EFF_SQWOBBLE:
211 case SONG_EV_SS_EFF_HFWOBBLE:
212 case SONG_EV_SS_EFF_FADER:
213 case SONG_EV_SS_EFF_REVERB:
214 case SONG_EV_SS_EFF_FOLDBACK:
215 case SONG_EV_SS_EFF_ATAN:
216 case SONG_EV_SS_EFF_DISTORT:
217 case SONG_EV_SS_EFF_OVERDRIVE:
218 case SONG_EV_SS_EFF_OFF:
219 case SONG_EV_SONG_INFO:
220 case SONG_EV_EOT:
222 /* just copy */
223 add_ss_ev(e);
224 break;
226 case SONG_EV_SS_CHANNEL:
228 /* count channels */
229 if (*n_channels < e->channel)
230 *n_channels = e->channel;
232 add_ss_ev(e);
233 break;
235 case SONG_EV_NOP:
236 case SONG_EV_MIDI_CHANNEL:
237 case SONG_EV_MIDI_PROGRAM:
239 /* ignored */
240 break;
242 case SONG_EV_NOTE_ON:
243 case SONG_EV_NOTE_OFF:
244 case SONG_EV_END:
246 /* never found in generic song streams */
247 break;
250 /* store the further frame seen */
251 if (f_frame < frame)
252 f_frame = frame;
255 /* generates an end of event mark, a time after the last one */
256 e = add_event(&ss_song, &n_ss_ev);
258 e->type = SONG_EV_END;
259 e->frame = f_frame + ss_frequency;
260 e->event_id = -1;
262 /* finally sort */
263 qsort(ss_song, n_ss_ev, sizeof(struct song_ev), frame_type_eventid_cmp);
265 /* count one more */
266 (*n_channels)++;
270 static struct song_ev *process_this_frame_events(struct song_ev *e, int skip_frames)
271 /* process the events attached to this frame */
273 static double tempo = 120.0;
274 static int frame = 0;
276 /* from the beginning? */
277 if (e == NULL) {
278 e = ss_song;
279 tempo = 120.0;
280 frame = 0;
283 if (verbose >= 1 && frame % ss_frequency == 0) {
284 int m = frame / ss_frequency;
285 printf("[%02d:%02d]\r", m / 60, m % 60);
286 fflush(stdout);
289 while (e != NULL && e->frame == frame) {
290 struct ss_ins *i;
291 struct ss_wave *w;
292 double freq;
294 if (e->type == SONG_EV_NOTE_ON ||
295 e->type == SONG_EV_NOTE_OFF ||
296 e->type == SONG_EV_SS_PITCH_STRETCH) {
297 if (frame < skip_frames) {
298 e++;
299 frame = e->frame;
300 continue;
304 /* take the instrument */
305 if (e->trk_id < 0)
306 i = NULL;
307 else
308 i = &ss_song_ins[e->trk_id];
310 switch (e->type) {
311 case SONG_EV_NOTE_ON:
313 if (ss_ins_note_on(i, e->value, e->vol, e->note_id) < 0 &&
314 verbose >= 1)
315 printf("ss_ins_note_on error: track %d note %d\n",
316 e->trk_id, e->value);
318 break;
320 case SONG_EV_NOTE_OFF:
322 ss_ins_note_off(i, e->note_id);
324 break;
326 case SONG_EV_SS_SUSTAIN:
328 ss_ins_set_sustain(i, e->amount);
330 break;
332 case SONG_EV_SS_ATTACK:
334 ss_ins_set_attack(i, e->amount);
336 break;
338 case SONG_EV_SS_VIBRATO:
340 ss_ins_set_vibrato(i, e->depth, e->freq);
342 break;
344 case SONG_EV_SS_PORTAMENTO:
346 ss_ins_set_portamento(i, (e->amount * 44100.0)
347 / ((double) ss_frequency * 1000000.0));
349 break;
351 case SONG_EV_SS_CHANNEL:
353 ss_ins_set_channel(i, e->channel, e->vol);
355 break;
357 case SONG_EV_SS_WAV:
359 w = ss_load_wav_file(e->name,
360 ss_note_frequency(e->value),
361 ss_note_frequency(e->min),
362 ss_note_frequency(e->max),
363 e->start, e->end,
364 e->channel, e->skip_channels);
366 /* fail if can't open wav */
367 if (w == NULL) {
368 printf("Can't load wav '%s'\n", e->name);
369 e = NULL;
371 else
372 ss_ins_add_layer(i, w);
374 break;
376 case SONG_EV_SS_PAT:
378 if (ss_load_pat_file(i, e->name) < 0) {
379 printf("Can't load pat '%s'\n", e->name);
380 e = NULL;
383 break;
385 case SONG_EV_SS_EFF_DELAY:
387 ss_eff_delay(&i->effs[e->channel], e->len);
388 break;
390 case SONG_EV_SS_EFF_ECHO:
392 ss_eff_echo(&i->effs[e->channel], e->len, e->vol);
393 break;
395 case SONG_EV_SS_EFF_COMB:
397 ss_eff_comb(&i->effs[e->channel], e->len, e->vol);
398 break;
400 case SONG_EV_SS_EFF_ALLPASS:
402 ss_eff_allpass(&i->effs[e->channel], e->len, e->vol);
403 break;
405 case SONG_EV_SS_EFF_FLANGER:
407 ss_eff_flanger(&i->effs[e->channel],
408 e->len, e->vol, e->depth, e->freq, e->phase);
409 break;
411 case SONG_EV_SS_EFF_WOBBLE:
413 ss_eff_wobble(&i->effs[e->channel], e->freq, e->phase, e->vol);
415 break;
417 case SONG_EV_SS_EFF_SQWOBBLE:
419 ss_eff_square_wobble(&i->effs[e->channel], e->freq, e->phase);
421 break;
423 case SONG_EV_SS_EFF_HFWOBBLE:
425 ss_eff_half_wobble(&i->effs[e->channel], e->freq, e->phase);
427 break;
429 case SONG_EV_SS_EFF_FADER:
431 ss_eff_fader(&i->effs[e->channel], e->len, e->initial, e->final);
432 break;
434 case SONG_EV_SS_EFF_REVERB:
436 ss_eff_reverb(&i->effs[e->channel]);
437 break;
439 case SONG_EV_SS_EFF_FOLDBACK:
441 ss_eff_foldback(&i->effs[e->channel], e->vol);
442 break;
444 case SONG_EV_SS_EFF_ATAN:
446 ss_eff_atan(&i->effs[e->channel], e->vol);
447 break;
449 case SONG_EV_SS_EFF_DISTORT:
451 ss_eff_distort(&i->effs[e->channel], e->vol);
452 break;
454 case SONG_EV_SS_EFF_OVERDRIVE:
456 ss_eff_overdrive(&i->effs[e->channel], e->vol);
457 break;
459 case SONG_EV_SS_EFF_OFF:
461 ss_eff_off(&i->effs[e->channel]);
462 break;
464 case SONG_EV_TEMPO:
466 /* just store the last tempo */
467 tempo = e->amount;
468 break;
470 case SONG_EV_SS_PITCH_STRETCH:
472 /* find the wave */
473 freq = ss_note_frequency(e->value);
474 w = ss_ins_find_layer(i, freq, NULL);
476 /* calculate optimal frequency */
477 freq = ss_pitch_from_tempo(w, tempo, e->len);
479 /* play the note */
480 if (ss_ins_play(i, freq, e->vol,
481 e->note_id, w) < 0 && verbose >= 1)
482 printf("ss_ins_play error: track %d freq %f\n",
483 e->trk_id, freq);
485 break;
487 case SONG_EV_SS_PRINT_WAVE_TEMPO:
489 /* find the wave */
490 freq = ss_note_frequency(e->value);
491 w = ss_ins_find_layer(i, freq, NULL);
493 /* print the optimal tempo */
494 printf("Optimal tempo: %lf\n",
495 ss_tempo_from_wave(w, e->value, e->len));
497 break;
499 case SONG_EV_SONG_INFO:
501 /* add a new song (track) */
502 cue_file_song_info(frame, e->author, e->name);
503 break;
505 case SONG_EV_EOT:
507 /* end of track; trigger possible cleaning */
508 ss_ins_disable(i);
509 break;
511 case SONG_EV_END:
513 e = NULL;
514 break;
516 case SONG_EV_BACK:
517 case SONG_EV_MIDI_CHANNEL:
518 case SONG_EV_MIDI_PROGRAM:
519 case SONG_EV_NOTE:
520 case SONG_EV_METER:
521 case SONG_EV_MEASURE:
522 case SONG_EV_NOP:
524 /* never found in ss song streams */
525 break;
528 /* next event */
529 if (e)
530 e++;
533 frame++;
535 return e;
539 int ss_song_render(int skip_secs, char *driver, char *devfile)
541 int n, i = 0;
542 sample_t output[SS_MAX_CHANNELS];
543 int skip_frames;
544 int n_channels;
545 struct song_ev *e = NULL;
547 /* convert the song to ss events */
548 ss_song_convert_events(&n_channels);
550 if (verbose >= 2)
551 printf("Tracks: %d Channels: %d Events: %d\n",
552 n_song_tracks, n_channels, n_ss_ev);
554 if (trace) {
555 printf("** SOFTWARE SYNTHESIZER EVENT DUMP **\n\n");
556 dump_song_events(ss_song, n_ss_ev);
557 return 0;
560 /* set the number of channels, unless forced */
561 if (ss_nchannels == -1)
562 ss_nchannels = n_channels > 0 ? n_channels : 2;
564 if (ss_output_open(driver, devfile) < 0) {
565 printf("Error: can't init driver\n");
566 return 2;
569 /* init the generators */
570 ss_gen_init();
572 /* init the instruments */
573 for (n = 0; n < n_song_tracks; n++)
574 ss_ins_init(&ss_song_ins[n]);
576 /* calculate the frame to start playing */
577 skip_frames = skip_secs * ss_frequency;
579 /* main loop */
580 do {
581 /* process all events in this frame */
582 e = process_this_frame_events(e, skip_frames);
584 /* reset frame samples */
585 ss_output_init_frame(output);
587 /* generate output from all instruments */
588 for (n = i = 0; n < n_song_tracks; n++)
589 i += ss_ins_frame(&ss_song_ins[n], output);
591 /* dump to sampling driver */
592 ss_output_write(output);
593 } while (i);
595 if (verbose >= 1)
596 printf("\n");
598 ss_output_close();
600 return 0;