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 number */
67 float vol
; /* volume */
70 struct ss_ev_ss_note_on_by_time
72 song_ev_type type
; /* SONG_EV_SS_NOTE_ON_BY_TIME */
75 int note_id
; /* note id */
76 int note
; /* MIDI-like note (to find the wave) */
77 double len
; /* note length (1: whole) */
78 float vol
; /* volume */
81 struct ss_ev_ss_sustain
83 song_ev_type type
; /* SONG_EV_SS_SUSTAIN */
86 double sustain
; /* sustain time (in frames) */
89 struct ss_ev_ss_set_channel
91 song_ev_type type
; /* SONG_EV_SS_SET_CHANNEL */
94 int channel
; /* channel */
95 float vol
; /* volume */
100 song_ev_type type
; /* SONG_EV_SS_WAV */
103 char * file
; /* path to .wav file */
104 int base
; /* MIDI-like base note */
105 int min
; /* MIDI-like minimum note */
106 int max
; /* MIDI-like maximum note */
107 double loop_start
; /* loop start */
108 double loop_end
; /* loop end */
113 song_ev_type type
; /* SONG_EV_SS_PAT */
116 char * file
; /* path to .pat file */
121 song_ev_type type
; /* effect type */
124 int channel
; /* channel */
125 double size
; /* size of effect */
126 float gain
; /* gain */
127 double depth
; /* depth */
128 double freq
; /* freq */
129 double phase
; /* phase */
130 float initial
; /* initial vol */
131 float final
; /* final vol */
136 struct ss_ev_generic generic
;
137 struct ss_ev_note_on note_on
;
138 struct ss_ev_note_off note_off
;
139 struct ss_ev_ss_note_on_by_time ss_note_on_by_time
;
140 struct ss_ev_ss_sustain ss_sustain
;
141 struct ss_ev_ss_set_channel ss_set_channel
;
142 struct ss_ev_ss_wav ss_wav
;
143 struct ss_ev_ss_pat ss_pat
;
144 struct ss_ev_ss_eff ss_eff
;
147 /* the softsynth song stream */
149 static union ss_ev
* ss_song
=NULL
;
150 static int n_ss_ev
=0;
153 /* the instruments */
155 #define SONG_INS_NUM 256
156 struct ss_ins song_ins
[SONG_INS_NUM
];
161 ********************/
163 static void add_ss_ev(union ss_ev
* e
)
164 /* adds a softsynth song event */
167 ss_song
=(union ss_ev
*)realloc(ss_song
,
168 (n_ss_ev
+ 1) * sizeof(union ss_ev
));
171 memcpy(&ss_song
[n_ss_ev
], e
, sizeof(union ss_ev
));
177 static int ss_ev_cmp(const void * v1
, const void * v2
)
178 /* softsynth song event compare function for qsort() */
180 struct ss_ev_generic
* e1
;
181 struct ss_ev_generic
* e2
;
183 e1
=(struct ss_ev_generic
*)v1
; e2
=(struct ss_ev_generic
*)v2
;
185 if(e1
->frame
== e2
->frame
)
186 return(e1
->type
- e2
->type
);
188 return(e1
->frame
- e2
->frame
);
192 static int ss_song_convert_events(void)
193 /* converts generic song_ev events to softsynth events */
204 /* resets the ss stream */
221 /* travels the song events generating softsynth song events */
222 for(n
=0;n
< n_song_ev
;n
++)
224 /* gets the song event */
227 /* calculates the frame */
228 frame
=((e
->generic
.time
- time_ac
) * fpw
) + frame_ac
;
230 /* generic event data */
231 sse
.generic
.type
=e
->generic
.type
;
232 sse
.generic
.frame
=frame
;
233 sse
.generic
.trk_id
=e
->generic
.trk_id
;
235 /* account the biggest track seen */
236 if(b_track
< e
->generic
.trk_id
) b_track
=e
->generic
.trk_id
;
238 switch(e
->generic
.type
)
242 /* updates accumulations */
244 time_ac
+= e
->generic
.time
;
246 /* calculates frames-per-whole based on new tempo */
247 fpw
=(double) ss_frequency
* 60.0;
248 fpw
/= e
->tempo
.tempo
/ 4.0;
254 /* just store the values */
260 case SONG_EV_MEASURE
:
262 printf("measure boundary check (must be 0): %d\n",
263 ((int) e
->generic
.time
* num
) % den
);
269 /* convert to note on / off pairs */
271 sse
.note_on
.type
=SONG_EV_NOTE_ON
;
272 sse
.note_on
.note_id
=note_id
++;
273 sse
.note_on
.note
=e
->note
.note
;
274 sse
.note_on
.vol
=e
->note
.vol
;
278 frame
+= (int)(e
->note
.len
* fpw
);
280 sse
.note_off
.type
=SONG_EV_NOTE_OFF
;
281 sse
.note_off
.frame
=frame
;
286 case SONG_EV_SS_NOTE_ON_BY_TIME
:
288 sse
.ss_note_on_by_time
.note_id
=note_id
++;
289 sse
.ss_note_on_by_time
.note
=e
->ss_note_by_time
.note
;
290 sse
.ss_note_on_by_time
.len
=e
->ss_note_by_time
.len
;
291 sse
.ss_note_on_by_time
.vol
=e
->ss_note_by_time
.vol
;
295 frame
+= (int)(e
->ss_note_by_time
.len
* fpw
);
297 sse
.note_off
.type
=SONG_EV_NOTE_OFF
;
298 sse
.note_off
.frame
=frame
;
305 sse
.ss_wav
.file
=e
->ss_wav
.file
;
306 sse
.ss_wav
.base
=e
->ss_wav
.base
;
307 sse
.ss_wav
.min
=e
->ss_wav
.min
;
308 sse
.ss_wav
.max
=e
->ss_wav
.max
;
309 sse
.ss_wav
.loop_start
=e
->ss_wav
.loop_start
;
310 sse
.ss_wav
.loop_end
=e
->ss_wav
.loop_end
;
317 sse
.ss_pat
.file
=e
->ss_pat
.file
;
322 case SONG_EV_SS_SUSTAIN
:
324 sse
.ss_sustain
.sustain
=e
->ss_sustain
.sustain
;
329 case SONG_EV_SS_SET_CHANNEL
:
333 case SONG_EV_SS_EFF_DELAY
:
334 case SONG_EV_SS_EFF_ECHO
:
335 case SONG_EV_SS_EFF_COMB
:
336 case SONG_EV_SS_EFF_ALLPASS
:
337 case SONG_EV_SS_EFF_FLANGER
:
338 case SONG_EV_SS_EFF_WOBBLE
:
339 case SONG_EV_SS_EFF_SQWOBBLE
:
340 case SONG_EV_SS_EFF_FADER
:
341 case SONG_EV_SS_EFF_REVERB
:
343 sse
.ss_eff
.channel
=e
->ss_eff
.channel
;
344 sse
.ss_eff
.size
=e
->ss_eff
.size
;
345 sse
.ss_eff
.gain
=e
->ss_eff
.gain
;
346 sse
.ss_eff
.depth
=e
->ss_eff
.depth
;
347 sse
.ss_eff
.freq
=e
->ss_eff
.freq
;
348 sse
.ss_eff
.phase
=e
->ss_eff
.phase
;
349 sse
.ss_eff
.initial
=e
->ss_eff
.initial
;
350 sse
.ss_eff
.final
=e
->ss_eff
.final
;
355 case SONG_EV_MIDI_CHANNEL
:
356 case SONG_EV_MIDI_PROGRAM
:
361 case SONG_EV_NOTE_ON
:
362 case SONG_EV_NOTE_OFF
:
365 /* never found in generic song streams */
370 /* generates an end of event mark, a time after the last one */
371 sse
.generic
.type
=SONG_EV_END
;
372 sse
.generic
.frame
=frame
+ ss_frequency
;
376 qsort(ss_song
, n_ss_ev
, sizeof(union ss_ev
), ss_ev_cmp
);
378 /* return the number of tracks */
383 int ss_song_render(void)
389 float output
[SS_MAX_CHANNELS
];
393 /* convert the song to ss events */
394 n_tracks
=ss_song_convert_events();
400 /* init the instruments */
401 for(n
=0;n
< n_tracks
;n
++)
402 ss_ins_init(&song_ins
[n
]);
404 /* loop the events */
407 /* process all events for this exact frame */
408 while(e
->generic
.frame
== frame
)
410 /* take the instrument */
411 if(e
->generic
.trk_id
== -1)
414 i
=&song_ins
[e
->generic
.trk_id
];
416 switch(e
->generic
.type
)
418 case SONG_EV_NOTE_ON
:
420 ss_ins_note_on(i
, e
->note_on
.note
,
421 e
->note_on
.vol
, e
->note_on
.note_id
);
425 case SONG_EV_SS_NOTE_ON_BY_TIME
:
429 case SONG_EV_NOTE_OFF
:
431 ss_ins_note_off(i
, e
->note_off
.note_id
);
435 case SONG_EV_SS_SUSTAIN
:
437 ss_ins_set_sustain(i
, e
->ss_sustain
.sustain
);
441 case SONG_EV_SS_SET_CHANNEL
:
450 w
=ss_load_wav_file(e
->ss_wav
.file
,
451 ss_note_frequency(e
->ss_wav
.base
),
452 ss_note_frequency(e
->ss_wav
.min
),
453 ss_note_frequency(e
->ss_wav
.max
),
454 e
->ss_wav
.loop_start
,
457 ss_ins_add_layer(i
, w
);
464 ss_load_pat_file(i
, e
->ss_pat
.file
);
467 case SONG_EV_SS_EFF_DELAY
:
469 ss_eff_delay(&i
->effs
[e
->ss_eff
.channel
],
473 case SONG_EV_SS_EFF_ECHO
:
475 ss_eff_echo(&i
->effs
[e
->ss_eff
.channel
],
476 e
->ss_eff
.size
, e
->ss_eff
.gain
);
479 case SONG_EV_SS_EFF_COMB
:
481 ss_eff_comb(&i
->effs
[e
->ss_eff
.channel
],
482 e
->ss_eff
.size
, e
->ss_eff
.gain
);
485 case SONG_EV_SS_EFF_ALLPASS
:
487 ss_eff_allpass(&i
->effs
[e
->ss_eff
.channel
],
488 e
->ss_eff
.size
, e
->ss_eff
.gain
);
491 case SONG_EV_SS_EFF_FLANGER
:
493 ss_eff_flanger(&i
->effs
[e
->ss_eff
.channel
],
494 e
->ss_eff
.size
, e
->ss_eff
.gain
,
495 e
->ss_eff
.depth
, e
->ss_eff
.freq
,
499 case SONG_EV_SS_EFF_WOBBLE
:
501 ss_eff_wobble(&i
->effs
[e
->ss_eff
.channel
],
502 e
->ss_eff
.freq
, e
->ss_eff
.phase
);
506 case SONG_EV_SS_EFF_SQWOBBLE
:
508 ss_eff_square_wobble(&i
->effs
[e
->ss_eff
.channel
],
509 e
->ss_eff
.freq
, e
->ss_eff
.phase
);
513 case SONG_EV_SS_EFF_FADER
:
515 ss_eff_fader(&i
->effs
[e
->ss_eff
.channel
],
516 e
->ss_eff
.size
, e
->ss_eff
.initial
,
520 case SONG_EV_SS_EFF_REVERB
:
522 ss_eff_reverb(&i
->effs
[e
->ss_eff
.channel
]);
530 case SONG_EV_MIDI_CHANNEL
:
531 case SONG_EV_MIDI_PROGRAM
:
535 case SONG_EV_MEASURE
:
537 /* never found in ss song streams */
545 /* reset frame samples */
546 ss_output_init_frame(output
);
548 /* generate output from all instruments */
549 for(n
=0;n
< n_tracks
;n
++)
550 ss_ins_frame(&song_ins
[n
], output
);
552 /* dump to sampling driver */
553 ss_output_write(output
);