The new 'L' command has been documented (Closes: #1148).
[ahxm.git] / ss_song.c
blobba436e327a1fce87c1fb249f57fb5a8bbccba4d1
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"
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(const 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 /* assign a note id */
151 e->note_id = note_id++;
153 /* copy */
154 add_ss_ev(e);
156 /* copy again, but set as NOTE_OFF */
157 e = add_ss_ev(e);
158 e->type = SONG_EV_NOTE_OFF;
160 frame += (int) (e->len * fpw);
161 e->frame = frame;
163 break;
165 case SONG_EV_BACK:
167 /* move the cursor back */
169 frame_ac -= (int) (e->len * fpw);
171 break;
173 case SONG_EV_SS_PITCH_STRETCH:
175 /* assign a note id */
176 e->note_id = note_id++;
178 /* copy */
179 add_ss_ev(e);
181 /* and copy again, as a note off */
182 e = add_ss_ev(e);
183 e->type = SONG_EV_NOTE_OFF;
185 frame += (int) (e->len * fpw);
186 e->frame = frame;
188 break;
190 case SONG_EV_SS_PRINT_WAVE_TEMPO:
191 case SONG_EV_SS_WAV:
192 case SONG_EV_SS_PAT:
193 case SONG_EV_SS_SF2:
194 case SONG_EV_SS_SUSTAIN:
195 case SONG_EV_SS_ATTACK:
196 case SONG_EV_SS_VIBRATO:
197 case SONG_EV_SS_PORTAMENTO:
198 case SONG_EV_SS_EFF_DELAY:
199 case SONG_EV_SS_EFF_ECHO:
200 case SONG_EV_SS_EFF_COMB:
201 case SONG_EV_SS_EFF_ALLPASS:
202 case SONG_EV_SS_EFF_FLANGER:
203 case SONG_EV_SS_EFF_WOBBLE:
204 case SONG_EV_SS_EFF_SQWOBBLE:
205 case SONG_EV_SS_EFF_HFWOBBLE:
206 case SONG_EV_SS_EFF_FADER:
207 case SONG_EV_SS_EFF_REVERB:
208 case SONG_EV_SS_EFF_FOLDBACK:
209 case SONG_EV_SS_EFF_ATAN:
210 case SONG_EV_SS_EFF_DISTORT:
211 case SONG_EV_SS_EFF_OVERDRIVE:
212 case SONG_EV_SS_EFF_OFF:
213 case SONG_EV_SONG_INFO:
214 case SONG_EV_EOT:
216 /* just copy */
217 add_ss_ev(e);
218 break;
220 case SONG_EV_SS_CHANNEL:
222 /* count channels */
223 if (*n_channels < e->channel)
224 *n_channels = e->channel;
226 add_ss_ev(e);
227 break;
229 case SONG_EV_NOP:
230 case SONG_EV_MIDI_CHANNEL:
231 case SONG_EV_MIDI_PROGRAM:
233 /* ignored */
234 break;
236 case SONG_EV_NOTE_OFF:
237 case SONG_EV_END:
239 /* never found in generic song streams */
240 break;
243 /* store the further frame seen */
244 if (f_frame < frame)
245 f_frame = frame;
248 /* generates an end of event mark, a time after the last one */
249 e = add_event(&ss_song, &n_ss_ev);
251 e->type = SONG_EV_END;
252 e->frame = f_frame + ss_frequency;
253 e->event_id = -1;
255 /* finally sort */
256 qsort(ss_song, n_ss_ev, sizeof(struct song_ev), 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 *e, int skip_frames)
264 /* process the events attached to this frame */
266 static double tempo = 120.0;
267 static int frame = 0;
269 /* from the beginning? */
270 if (e == NULL) {
271 e = ss_song;
272 tempo = 120.0;
273 frame = 0;
276 if (verbose >= 1 && frame % ss_frequency == 0) {
277 int m = frame / ss_frequency;
278 printf("[%02d:%02d]\r", m / 60, m % 60);
279 fflush(stdout);
282 while (e != NULL && e->frame == frame) {
283 struct ss_ins *i;
284 struct ss_wave *w;
285 double freq;
287 if (e->type == SONG_EV_NOTE ||
288 e->type == SONG_EV_NOTE_OFF ||
289 e->type == SONG_EV_SS_PITCH_STRETCH) {
290 if (frame < skip_frames) {
291 e++;
292 frame = e->frame;
293 continue;
297 /* take the instrument */
298 if (e->trk_id < 0)
299 i = NULL;
300 else
301 i = &ss_song_ins[e->trk_id];
303 switch (e->type) {
304 case SONG_EV_NOTE:
306 if (ss_ins_note_on(i, e->value, e->vol, e->note_id) < 0 &&
307 verbose >= 1)
308 printf("ss_ins_note_on error: track %d note %d\n",
309 e->trk_id, e->value);
311 break;
313 case SONG_EV_NOTE_OFF:
315 ss_ins_note_off(i, e->note_id);
317 break;
319 case SONG_EV_SS_SUSTAIN:
321 ss_ins_set_sustain(i, e->amount);
323 break;
325 case SONG_EV_SS_ATTACK:
327 ss_ins_set_attack(i, e->amount);
329 break;
331 case SONG_EV_SS_VIBRATO:
333 ss_ins_set_vibrato(i, e->depth, e->freq);
335 break;
337 case SONG_EV_SS_PORTAMENTO:
339 ss_ins_set_portamento(i, (e->amount * 44100.0)
340 / ((double) ss_frequency * 1000000.0));
342 break;
344 case SONG_EV_SS_CHANNEL:
346 ss_ins_set_channel(i, e->channel, e->vol);
348 break;
350 case SONG_EV_SS_WAV:
352 w = ss_load_wav_file(e->name,
353 ss_note_frequency(e->value),
354 ss_note_frequency(e->min),
355 ss_note_frequency(e->max),
356 e->start, e->end,
357 e->channel, e->skip_channels);
359 /* fail if can't open wav */
360 if (w == NULL) {
361 printf("Can't load wav '%s'\n", e->name);
362 e = NULL;
364 else
365 ss_ins_add_layer(i, w);
367 break;
369 case SONG_EV_SS_PAT:
371 if (ss_load_pat_file(i, e->name) < 0) {
372 printf("Can't load pat '%s'\n", e->name);
373 e = NULL;
376 break;
378 case SONG_EV_SS_SF2:
380 if (ss_load_sf2_file(i, e->name, e->str2) < 0) {
381 printf("Can't load instrument from sf2 '%s'\n", e->name);
382 e = NULL;
385 break;
387 case SONG_EV_SS_EFF_DELAY:
389 ss_eff_delay(&i->effs[e->channel], e->len);
390 break;
392 case SONG_EV_SS_EFF_ECHO:
394 ss_eff_echo(&i->effs[e->channel], e->len, e->vol);
395 break;
397 case SONG_EV_SS_EFF_COMB:
399 ss_eff_comb(&i->effs[e->channel], e->len, e->vol);
400 break;
402 case SONG_EV_SS_EFF_ALLPASS:
404 ss_eff_allpass(&i->effs[e->channel], e->len, e->vol);
405 break;
407 case SONG_EV_SS_EFF_FLANGER:
409 ss_eff_flanger(&i->effs[e->channel],
410 e->len, e->vol, e->depth, e->freq, e->phase);
411 break;
413 case SONG_EV_SS_EFF_WOBBLE:
415 ss_eff_wobble(&i->effs[e->channel], e->freq, e->phase, e->vol);
417 break;
419 case SONG_EV_SS_EFF_SQWOBBLE:
421 ss_eff_square_wobble(&i->effs[e->channel], e->freq, e->phase);
423 break;
425 case SONG_EV_SS_EFF_HFWOBBLE:
427 ss_eff_half_wobble(&i->effs[e->channel], e->freq, e->phase);
429 break;
431 case SONG_EV_SS_EFF_FADER:
433 ss_eff_fader(&i->effs[e->channel], e->len, e->initial, e->final);
434 break;
436 case SONG_EV_SS_EFF_REVERB:
438 ss_eff_reverb(&i->effs[e->channel]);
439 break;
441 case SONG_EV_SS_EFF_FOLDBACK:
443 ss_eff_foldback(&i->effs[e->channel], e->vol);
444 break;
446 case SONG_EV_SS_EFF_ATAN:
448 ss_eff_atan(&i->effs[e->channel], e->vol);
449 break;
451 case SONG_EV_SS_EFF_DISTORT:
453 ss_eff_distort(&i->effs[e->channel], e->vol);
454 break;
456 case SONG_EV_SS_EFF_OVERDRIVE:
458 ss_eff_overdrive(&i->effs[e->channel], e->vol);
459 break;
461 case SONG_EV_SS_EFF_OFF:
463 ss_eff_off(&i->effs[e->channel]);
464 break;
466 case SONG_EV_TEMPO:
468 /* just store the last tempo */
469 tempo = e->amount;
470 break;
472 case SONG_EV_SS_PITCH_STRETCH:
474 /* find the wave */
475 freq = ss_note_frequency(e->value);
476 w = ss_ins_find_layer(i, freq, NULL);
478 /* calculate optimal frequency */
479 freq = ss_pitch_from_tempo(w, tempo, e->len);
481 /* play the note */
482 if (ss_ins_play(i, freq, e->vol,
483 e->note_id, w) < 0 && verbose >= 1)
484 printf("ss_ins_play error: track %d freq %f\n",
485 e->trk_id, freq);
487 break;
489 case SONG_EV_SS_PRINT_WAVE_TEMPO:
491 /* find the wave */
492 freq = ss_note_frequency(e->value);
493 w = ss_ins_find_layer(i, freq, NULL);
495 /* print the optimal tempo */
496 printf("Optimal tempo: %lf\n",
497 ss_tempo_from_wave(w, e->value, e->len));
499 break;
501 case SONG_EV_SONG_INFO:
503 /* add a new song (track) */
504 cue_file_song_info(frame, e->str2, e->name);
505 break;
507 case SONG_EV_EOT:
509 /* end of track; trigger possible cleaning */
510 ss_ins_disable(i);
511 break;
513 case SONG_EV_END:
515 e = NULL;
516 break;
518 case SONG_EV_BACK:
519 case SONG_EV_MIDI_CHANNEL:
520 case SONG_EV_MIDI_PROGRAM:
521 case SONG_EV_METER:
522 case SONG_EV_MEASURE:
523 case SONG_EV_NOP:
525 /* never found in ss song streams */
526 break;
529 /* next event */
530 if (e)
531 e++;
534 frame++;
536 return e;
540 int ss_song_render(int skip_secs, const char *driver, const char *devfile)
542 int n, i = 0;
543 sample_t output[SS_MAX_CHANNELS];
544 int skip_frames;
545 int n_channels;
546 const struct song_ev *e = NULL;
548 /* convert the song to ss events */
549 ss_song_convert_events(&n_channels);
551 if (verbose >= 2)
552 printf("Tracks: %d Channels: %d Events: %d\n",
553 n_song_tracks, n_channels, n_ss_ev);
555 if (trace) {
556 printf("** SOFTWARE SYNTHESIZER EVENT DUMP **\n\n");
557 dump_song_events(ss_song, n_ss_ev);
558 return 0;
561 /* set the number of channels, unless forced */
562 if (ss_nchannels == -1)
563 ss_nchannels = n_channels > 0 ? n_channels : 2;
565 if (ss_output_open(driver, devfile) < 0) {
566 printf("Error: can't init driver\n");
567 return 2;
570 /* init the generators */
571 ss_gen_init();
573 /* init the instruments */
574 for (n = 0; n < n_song_tracks; n++)
575 ss_ins_init(&ss_song_ins[n]);
577 /* calculate the frame to start playing */
578 skip_frames = skip_secs * ss_frequency;
580 /* main loop */
581 do {
582 /* process all events in this frame */
583 e = process_this_frame_events(e, skip_frames);
585 /* reset frame samples */
586 ss_output_init_frame(output);
588 /* generate output from all instruments */
589 for (n = i = 0; n < n_song_tracks; n++)
590 i += ss_ins_frame(&ss_song_ins[n], output);
592 /* dump to sampling driver */
593 ss_output_write(output);
594 } while (i);
596 if (verbose >= 1)
597 printf("\n");
599 ss_output_close();
601 return 0;