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
39 /* the softsynth song stream */
41 static struct song_ev
*ss_song
= NULL
;
42 static int n_ss_ev
= 0;
46 struct ss_ins ss_song_ins
[SS_MAX_INSTRUMENTS
];
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() */
66 e1
= (struct song_ev
*) v1
;
67 e2
= (struct song_ev
*) v2
;
69 ret
= e1
->frame
- e2
->frame
;
72 ret
= e1
->type
- e2
->type
;
75 ret
= e1
->event_id
- e2
->event_id
;
81 static void ss_song_convert_events(int *n_channels
)
82 /* converts generic song_ev events to softsynth events */
86 int frame
, frame_ac
, f_frame
;
87 double fpw
, time_ac
, time_ac_m
;
93 /* resets the ss stream */
94 if (ss_song
!= NULL
) {
105 frame
= frame_ac
= f_frame
= 0;
106 time_ac
= time_ac_m
= 0;
109 /* travels the song events generating softsynth song events */
110 for (n
= 0; n
< n_song_ev
; n
++) {
111 /* gets the song event */
114 /* calculates the frame */
115 frame
= ((e
->time
- time_ac
) * fpw
) + frame_ac
;
121 /* updates accumulations */
125 /* calculates frames-per-whole based on new tempo */
126 fpw
= (double) ss_frequency
* 60.0;
127 fpw
/= e
->amount
/ 4.0;
135 /* just store the values */
142 case SONG_EV_MEASURE
:
144 song_test_measure_boundary(e
->time
- time_ac_m
,
150 /* assign a note id */
151 e
->note_id
= note_id
++;
156 /* copy again, but set as NOTE_OFF */
158 e
->type
= SONG_EV_NOTE_OFF
;
160 frame
+= (int) (e
->len
* fpw
);
167 /* move the cursor back */
169 frame_ac
-= (int) (e
->len
* fpw
);
173 case SONG_EV_SS_PITCH_STRETCH
:
175 /* assign a note id */
176 e
->note_id
= note_id
++;
181 /* and copy again, as a note off */
183 e
->type
= SONG_EV_NOTE_OFF
;
185 frame
+= (int) (e
->len
* fpw
);
190 case SONG_EV_SS_PRINT_WAVE_TEMPO
:
193 case SONG_EV_SS_SUSTAIN
:
194 case SONG_EV_SS_ATTACK
:
195 case SONG_EV_SS_VIBRATO
:
196 case SONG_EV_SS_PORTAMENTO
:
197 case SONG_EV_SS_EFF_DELAY
:
198 case SONG_EV_SS_EFF_ECHO
:
199 case SONG_EV_SS_EFF_COMB
:
200 case SONG_EV_SS_EFF_ALLPASS
:
201 case SONG_EV_SS_EFF_FLANGER
:
202 case SONG_EV_SS_EFF_WOBBLE
:
203 case SONG_EV_SS_EFF_SQWOBBLE
:
204 case SONG_EV_SS_EFF_HFWOBBLE
:
205 case SONG_EV_SS_EFF_FADER
:
206 case SONG_EV_SS_EFF_REVERB
:
207 case SONG_EV_SS_EFF_FOLDBACK
:
208 case SONG_EV_SS_EFF_ATAN
:
209 case SONG_EV_SS_EFF_DISTORT
:
210 case SONG_EV_SS_EFF_OVERDRIVE
:
211 case SONG_EV_SS_EFF_OFF
:
212 case SONG_EV_SONG_INFO
:
219 case SONG_EV_SS_CHANNEL
:
222 if (*n_channels
< e
->channel
)
223 *n_channels
= e
->channel
;
229 case SONG_EV_MIDI_CHANNEL
:
230 case SONG_EV_MIDI_PROGRAM
:
235 case SONG_EV_NOTE_OFF
:
238 /* never found in generic song streams */
242 /* store the further frame seen */
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
;
255 qsort(ss_song
, n_ss_ev
, sizeof(struct song_ev
), frame_type_eventid_cmp
);
262 static struct song_ev
*process_this_frame_events(struct song_ev
*e
, int skip_frames
)
263 /* process the events attached to this frame */
265 static double tempo
= 120.0;
266 static int frame
= 0;
268 /* from the beginning? */
275 if (verbose
>= 1 && frame
% ss_frequency
== 0) {
276 int m
= frame
/ ss_frequency
;
277 printf("[%02d:%02d]\r", m
/ 60, m
% 60);
281 while (e
!= NULL
&& e
->frame
== frame
) {
286 if (e
->type
== SONG_EV_NOTE
||
287 e
->type
== SONG_EV_NOTE_OFF
||
288 e
->type
== SONG_EV_SS_PITCH_STRETCH
) {
289 if (frame
< skip_frames
) {
296 /* take the instrument */
300 i
= &ss_song_ins
[e
->trk_id
];
305 if (ss_ins_note_on(i
, e
->value
, e
->vol
, e
->note_id
) < 0 &&
307 printf("ss_ins_note_on error: track %d note %d\n",
308 e
->trk_id
, e
->value
);
312 case SONG_EV_NOTE_OFF
:
314 ss_ins_note_off(i
, e
->note_id
);
318 case SONG_EV_SS_SUSTAIN
:
320 ss_ins_set_sustain(i
, e
->amount
);
324 case SONG_EV_SS_ATTACK
:
326 ss_ins_set_attack(i
, e
->amount
);
330 case SONG_EV_SS_VIBRATO
:
332 ss_ins_set_vibrato(i
, e
->depth
, e
->freq
);
336 case SONG_EV_SS_PORTAMENTO
:
338 ss_ins_set_portamento(i
, (e
->amount
* 44100.0)
339 / ((double) ss_frequency
* 1000000.0));
343 case SONG_EV_SS_CHANNEL
:
345 ss_ins_set_channel(i
, e
->channel
, e
->vol
);
351 w
= ss_load_wav_file(e
->name
,
352 ss_note_frequency(e
->value
),
353 ss_note_frequency(e
->min
),
354 ss_note_frequency(e
->max
),
356 e
->channel
, e
->skip_channels
);
358 /* fail if can't open wav */
360 printf("Can't load wav '%s'\n", e
->name
);
364 ss_ins_add_layer(i
, w
);
370 if (ss_load_pat_file(i
, e
->name
) < 0) {
371 printf("Can't load pat '%s'\n", e
->name
);
377 case SONG_EV_SS_EFF_DELAY
:
379 ss_eff_delay(&i
->effs
[e
->channel
], e
->len
);
382 case SONG_EV_SS_EFF_ECHO
:
384 ss_eff_echo(&i
->effs
[e
->channel
], e
->len
, e
->vol
);
387 case SONG_EV_SS_EFF_COMB
:
389 ss_eff_comb(&i
->effs
[e
->channel
], e
->len
, e
->vol
);
392 case SONG_EV_SS_EFF_ALLPASS
:
394 ss_eff_allpass(&i
->effs
[e
->channel
], e
->len
, e
->vol
);
397 case SONG_EV_SS_EFF_FLANGER
:
399 ss_eff_flanger(&i
->effs
[e
->channel
],
400 e
->len
, e
->vol
, e
->depth
, e
->freq
, e
->phase
);
403 case SONG_EV_SS_EFF_WOBBLE
:
405 ss_eff_wobble(&i
->effs
[e
->channel
], e
->freq
, e
->phase
, e
->vol
);
409 case SONG_EV_SS_EFF_SQWOBBLE
:
411 ss_eff_square_wobble(&i
->effs
[e
->channel
], e
->freq
, e
->phase
);
415 case SONG_EV_SS_EFF_HFWOBBLE
:
417 ss_eff_half_wobble(&i
->effs
[e
->channel
], e
->freq
, e
->phase
);
421 case SONG_EV_SS_EFF_FADER
:
423 ss_eff_fader(&i
->effs
[e
->channel
], e
->len
, e
->initial
, e
->final
);
426 case SONG_EV_SS_EFF_REVERB
:
428 ss_eff_reverb(&i
->effs
[e
->channel
]);
431 case SONG_EV_SS_EFF_FOLDBACK
:
433 ss_eff_foldback(&i
->effs
[e
->channel
], e
->vol
);
436 case SONG_EV_SS_EFF_ATAN
:
438 ss_eff_atan(&i
->effs
[e
->channel
], e
->vol
);
441 case SONG_EV_SS_EFF_DISTORT
:
443 ss_eff_distort(&i
->effs
[e
->channel
], e
->vol
);
446 case SONG_EV_SS_EFF_OVERDRIVE
:
448 ss_eff_overdrive(&i
->effs
[e
->channel
], e
->vol
);
451 case SONG_EV_SS_EFF_OFF
:
453 ss_eff_off(&i
->effs
[e
->channel
]);
458 /* just store the last tempo */
462 case SONG_EV_SS_PITCH_STRETCH
:
465 freq
= ss_note_frequency(e
->value
);
466 w
= ss_ins_find_layer(i
, freq
, NULL
);
468 /* calculate optimal frequency */
469 freq
= ss_pitch_from_tempo(w
, tempo
, e
->len
);
472 if (ss_ins_play(i
, freq
, e
->vol
,
473 e
->note_id
, w
) < 0 && verbose
>= 1)
474 printf("ss_ins_play error: track %d freq %f\n",
479 case SONG_EV_SS_PRINT_WAVE_TEMPO
:
482 freq
= ss_note_frequency(e
->value
);
483 w
= ss_ins_find_layer(i
, freq
, NULL
);
485 /* print the optimal tempo */
486 printf("Optimal tempo: %lf\n",
487 ss_tempo_from_wave(w
, e
->value
, e
->len
));
491 case SONG_EV_SONG_INFO
:
493 /* add a new song (track) */
494 cue_file_song_info(frame
, e
->author
, e
->name
);
499 /* end of track; trigger possible cleaning */
509 case SONG_EV_MIDI_CHANNEL
:
510 case SONG_EV_MIDI_PROGRAM
:
512 case SONG_EV_MEASURE
:
515 /* never found in ss song streams */
530 int ss_song_render(int skip_secs
, char *driver
, char *devfile
)
533 sample_t output
[SS_MAX_CHANNELS
];
536 struct song_ev
*e
= NULL
;
538 /* convert the song to ss events */
539 ss_song_convert_events(&n_channels
);
542 printf("Tracks: %d Channels: %d Events: %d\n",
543 n_song_tracks
, n_channels
, n_ss_ev
);
546 printf("** SOFTWARE SYNTHESIZER EVENT DUMP **\n\n");
547 dump_song_events(ss_song
, n_ss_ev
);
551 /* set the number of channels, unless forced */
552 if (ss_nchannels
== -1)
553 ss_nchannels
= n_channels
> 0 ? n_channels
: 2;
555 if (ss_output_open(driver
, devfile
) < 0) {
556 printf("Error: can't init driver\n");
560 /* init the generators */
563 /* init the instruments */
564 for (n
= 0; n
< n_song_tracks
; n
++)
565 ss_ins_init(&ss_song_ins
[n
]);
567 /* calculate the frame to start playing */
568 skip_frames
= skip_secs
* ss_frequency
;
572 /* process all events in this frame */
573 e
= process_this_frame_events(e
, skip_frames
);
575 /* reset frame samples */
576 ss_output_init_frame(output
);
578 /* generate output from all instruments */
579 for (n
= i
= 0; n
< n_song_tracks
; n
++)
580 i
+= ss_ins_frame(&ss_song_ins
[n
], output
);
582 /* dump to sampling driver */
583 ss_output_write(output
);