3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 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 #include "ss_output.h"
47 song_ev_type type
; /* event type */
48 int frame
; /* frame number (time) */
49 int trk_id
; /* track id */
54 song_ev_type type
; /* SONG_EV_NOTE_OFF */
57 int note_id
; /* note id */
62 song_ev_type type
; /* SONG_EV_NOTE_ON */
65 int note_id
; /* note id */
66 int note
; /* MIDI-like note */
67 float vol
; /* volume */
70 struct ss_ev_ss_sustain
72 song_ev_type type
; /* SONG_EV_SS_SUSTAIN */
75 double sustain
; /* sustain time (in frames) */
78 struct ss_ev_ss_channel
80 song_ev_type type
; /* SONG_EV_SS_CHANNEL */
83 int channel
; /* channel */
84 float vol
; /* volume */
89 song_ev_type type
; /* SONG_EV_SS_WAV */
92 char * file
; /* path to .wav file */
93 int base
; /* MIDI-like base note */
94 int min
; /* MIDI-like minimum note */
95 int max
; /* MIDI-like maximum note */
96 double loop_start
; /* loop start */
97 double loop_end
; /* loop end */
102 song_ev_type type
; /* SONG_EV_SS_PAT */
105 char * file
; /* path to .pat file */
110 song_ev_type type
; /* SONG_EV_TEMPO */
113 double tempo
; /* tempo in bmp */
116 struct ss_ev_pitch_stretch
118 song_ev_type type
; /* SONG_EV_SS_PITCH_STRETCH */
121 int note_id
; /* note id */
122 int note
; /* MIDI-like note (to find the wave) */
123 double len
; /* note length (1: whole note) */
124 float vol
; /* note volume (1: full volume) */
129 song_ev_type type
; /* effect type */
132 int channel
; /* channel */
133 double size
; /* size of effect */
134 float gain
; /* gain */
135 double depth
; /* depth */
136 double freq
; /* freq */
137 double phase
; /* phase */
138 float initial
; /* initial vol */
139 float final
; /* final vol */
144 struct ss_ev_generic generic
;
145 struct ss_ev_note_on note_on
;
146 struct ss_ev_note_off note_off
;
147 struct ss_ev_ss_sustain ss_sustain
;
148 struct ss_ev_ss_channel ss_channel
;
149 struct ss_ev_ss_wav ss_wav
;
150 struct ss_ev_ss_pat ss_pat
;
151 struct ss_ev_ss_eff ss_eff
;
152 struct ss_ev_tempo tempo
;
153 struct ss_ev_pitch_stretch ss_pitch_stretch
;
156 /* the softsynth song stream */
158 static union ss_ev
* ss_song
=NULL
;
159 static int n_ss_ev
=0;
162 /* the instruments */
164 #define SONG_INS_NUM 256
165 struct ss_ins song_ins
[SONG_INS_NUM
];
170 ********************/
172 static void add_ss_ev(union ss_ev
* e
)
173 /* adds a softsynth song event */
176 ss_song
=(union ss_ev
*)realloc(ss_song
,
177 (n_ss_ev
+ 1) * sizeof(union ss_ev
));
180 memcpy(&ss_song
[n_ss_ev
], e
, sizeof(union ss_ev
));
186 static int ss_ev_cmp(const void * v1
, const void * v2
)
187 /* softsynth song event compare function for qsort() */
189 struct ss_ev_generic
* e1
;
190 struct ss_ev_generic
* e2
;
192 e1
=(struct ss_ev_generic
*)v1
; e2
=(struct ss_ev_generic
*)v2
;
194 if(e1
->frame
== e2
->frame
)
195 return(e1
->type
- e2
->type
);
197 return(e1
->frame
- e2
->frame
);
201 static int ss_song_convert_events(void)
202 /* converts generic song_ev events to softsynth events */
213 /* resets the ss stream */
230 /* travels the song events generating softsynth song events */
231 for(n
=0;n
< n_song_ev
;n
++)
233 /* gets the song event */
236 /* calculates the frame */
237 frame
=((e
->generic
.time
- time_ac
) * fpw
) + frame_ac
;
239 /* generic event data */
240 sse
.generic
.type
=e
->generic
.type
;
241 sse
.generic
.frame
=frame
;
242 sse
.generic
.trk_id
=e
->generic
.trk_id
;
244 /* account the biggest track seen */
245 if(b_track
< e
->generic
.trk_id
) b_track
=e
->generic
.trk_id
;
247 switch(e
->generic
.type
)
251 /* updates accumulations */
253 time_ac
+= e
->generic
.time
;
255 /* calculates frames-per-whole based on new tempo */
256 fpw
=(double) ss_frequency
* 60.0;
257 fpw
/= e
->tempo
.tempo
/ 4.0;
260 sse
.tempo
.tempo
=e
->tempo
.tempo
;
267 /* just store the values */
273 case SONG_EV_MEASURE
:
275 printf("measure boundary check (must be 0): %d\n",
276 ((int) e
->generic
.time
* num
) % den
);
282 /* convert to note on / off pairs */
284 sse
.note_on
.type
=SONG_EV_NOTE_ON
;
285 sse
.note_on
.note_id
=note_id
++;
286 sse
.note_on
.note
=e
->note
.note
;
287 sse
.note_on
.vol
=e
->note
.vol
;
291 frame
+= (int)(e
->note
.len
* fpw
);
293 sse
.note_off
.type
=SONG_EV_NOTE_OFF
;
294 sse
.note_off
.frame
=frame
;
299 case SONG_EV_SS_PITCH_STRETCH
:
301 sse
.ss_pitch_stretch
.note_id
=note_id
++;
302 sse
.ss_pitch_stretch
.note
=e
->ss_pitch_stretch
.note
;
303 sse
.ss_pitch_stretch
.len
=e
->ss_pitch_stretch
.len
;
304 sse
.ss_pitch_stretch
.vol
=e
->ss_pitch_stretch
.vol
;
308 frame
+= (int)(e
->ss_pitch_stretch
.len
* fpw
);
310 sse
.note_off
.type
=SONG_EV_NOTE_OFF
;
311 sse
.note_off
.frame
=frame
;
318 sse
.ss_wav
.file
=e
->ss_wav
.file
;
319 sse
.ss_wav
.base
=e
->ss_wav
.base
;
320 sse
.ss_wav
.min
=e
->ss_wav
.min
;
321 sse
.ss_wav
.max
=e
->ss_wav
.max
;
322 sse
.ss_wav
.loop_start
=e
->ss_wav
.loop_start
;
323 sse
.ss_wav
.loop_end
=e
->ss_wav
.loop_end
;
330 sse
.ss_pat
.file
=e
->ss_pat
.file
;
335 case SONG_EV_SS_SUSTAIN
:
337 sse
.ss_sustain
.sustain
=e
->ss_sustain
.sustain
;
342 case SONG_EV_SS_CHANNEL
:
344 sse
.ss_channel
.channel
=e
->ss_channel
.channel
;
345 sse
.ss_channel
.vol
=e
->ss_channel
.vol
;
350 case SONG_EV_SS_EFF_DELAY
:
351 case SONG_EV_SS_EFF_ECHO
:
352 case SONG_EV_SS_EFF_COMB
:
353 case SONG_EV_SS_EFF_ALLPASS
:
354 case SONG_EV_SS_EFF_FLANGER
:
355 case SONG_EV_SS_EFF_WOBBLE
:
356 case SONG_EV_SS_EFF_SQWOBBLE
:
357 case SONG_EV_SS_EFF_FADER
:
358 case SONG_EV_SS_EFF_REVERB
:
359 case SONG_EV_SS_EFF_OFF
:
361 sse
.ss_eff
.channel
=e
->ss_eff
.channel
;
362 sse
.ss_eff
.size
=e
->ss_eff
.size
;
363 sse
.ss_eff
.gain
=e
->ss_eff
.gain
;
364 sse
.ss_eff
.depth
=e
->ss_eff
.depth
;
365 sse
.ss_eff
.freq
=e
->ss_eff
.freq
;
366 sse
.ss_eff
.phase
=e
->ss_eff
.phase
;
367 sse
.ss_eff
.initial
=e
->ss_eff
.initial
;
368 sse
.ss_eff
.final
=e
->ss_eff
.final
;
373 case SONG_EV_MIDI_CHANNEL
:
374 case SONG_EV_MIDI_PROGRAM
:
379 case SONG_EV_NOTE_ON
:
380 case SONG_EV_NOTE_OFF
:
383 /* never found in generic song streams */
388 /* generates an end of event mark, a time after the last one */
389 sse
.generic
.type
=SONG_EV_END
;
390 sse
.generic
.frame
=frame
+ ss_frequency
;
394 qsort(ss_song
, n_ss_ev
, sizeof(union ss_ev
), ss_ev_cmp
);
396 /* return the number of tracks */
401 int ss_song_render(void)
407 float output
[SS_MAX_CHANNELS
];
412 /* convert the song to ss events */
413 n_tracks
=ss_song_convert_events();
419 /* init the instruments */
420 for(n
=0;n
< n_tracks
;n
++)
421 ss_ins_init(&song_ins
[n
]);
423 /* loop the events */
426 /* process all events for this exact frame */
427 while(e
->generic
.frame
== frame
)
429 /* take the instrument */
430 if(e
->generic
.trk_id
== -1)
433 i
=&song_ins
[e
->generic
.trk_id
];
435 switch(e
->generic
.type
)
437 case SONG_EV_NOTE_ON
:
439 ss_ins_note_on(i
, e
->note_on
.note
,
440 e
->note_on
.vol
, e
->note_on
.note_id
);
444 case SONG_EV_NOTE_OFF
:
446 ss_ins_note_off(i
, e
->note_off
.note_id
);
450 case SONG_EV_SS_SUSTAIN
:
452 ss_ins_set_sustain(i
, e
->ss_sustain
.sustain
);
456 case SONG_EV_SS_CHANNEL
:
458 ss_ins_set_channel(i
, e
->ss_channel
.channel
,
468 w
=ss_load_wav_file(e
->ss_wav
.file
,
469 ss_note_frequency(e
->ss_wav
.base
),
470 ss_note_frequency(e
->ss_wav
.min
),
471 ss_note_frequency(e
->ss_wav
.max
),
472 e
->ss_wav
.loop_start
,
475 ss_ins_add_layer(i
, w
);
482 ss_load_pat_file(i
, e
->ss_pat
.file
);
485 case SONG_EV_SS_EFF_DELAY
:
487 ss_eff_delay(&i
->effs
[e
->ss_eff
.channel
],
491 case SONG_EV_SS_EFF_ECHO
:
493 ss_eff_echo(&i
->effs
[e
->ss_eff
.channel
],
494 e
->ss_eff
.size
, e
->ss_eff
.gain
);
497 case SONG_EV_SS_EFF_COMB
:
499 ss_eff_comb(&i
->effs
[e
->ss_eff
.channel
],
500 e
->ss_eff
.size
, e
->ss_eff
.gain
);
503 case SONG_EV_SS_EFF_ALLPASS
:
505 ss_eff_allpass(&i
->effs
[e
->ss_eff
.channel
],
506 e
->ss_eff
.size
, e
->ss_eff
.gain
);
509 case SONG_EV_SS_EFF_FLANGER
:
511 ss_eff_flanger(&i
->effs
[e
->ss_eff
.channel
],
512 e
->ss_eff
.size
, e
->ss_eff
.gain
,
513 e
->ss_eff
.depth
, e
->ss_eff
.freq
,
517 case SONG_EV_SS_EFF_WOBBLE
:
519 ss_eff_wobble(&i
->effs
[e
->ss_eff
.channel
],
520 e
->ss_eff
.freq
, e
->ss_eff
.phase
);
524 case SONG_EV_SS_EFF_SQWOBBLE
:
526 ss_eff_square_wobble(&i
->effs
[e
->ss_eff
.channel
],
527 e
->ss_eff
.freq
, e
->ss_eff
.phase
);
531 case SONG_EV_SS_EFF_FADER
:
533 ss_eff_fader(&i
->effs
[e
->ss_eff
.channel
],
534 e
->ss_eff
.size
, e
->ss_eff
.initial
,
538 case SONG_EV_SS_EFF_REVERB
:
540 ss_eff_reverb(&i
->effs
[e
->ss_eff
.channel
]);
543 case SONG_EV_SS_EFF_OFF
:
545 ss_eff_off(&i
->effs
[e
->ss_eff
.channel
]);
550 /* just store the last tempo */
551 tempo
=e
->tempo
.tempo
;
554 case SONG_EV_SS_PITCH_STRETCH
:
562 freq
=ss_note_frequency(e
->ss_pitch_stretch
.note
);
563 w
=ss_ins_find_layer(i
, freq
, &n
);
565 /* calculate optimal frequency */
566 freq
=ss_pitch_from_tempo(w
, tempo
, e
->ss_pitch_stretch
.len
);
569 ss_ins_note_on_by_freq(i
, freq
,
570 e
->ss_pitch_stretch
.vol
,
571 e
->ss_pitch_stretch
.note_id
);
581 case SONG_EV_MIDI_CHANNEL
:
582 case SONG_EV_MIDI_PROGRAM
:
585 case SONG_EV_MEASURE
:
587 /* never found in ss song streams */
595 /* reset frame samples */
596 ss_output_init_frame(output
);
598 /* generate output from all instruments */
599 for(n
=0;n
< n_tracks
;n
++)
600 ss_ins_frame(&song_ins
[n
], output
);
602 /* dump to sampling driver */
603 ss_output_write(output
);