3
TiMidity -- Experimental MIDI to WAVE converter
4
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
6
Suddenly, you realize that this program is free software; you get
7
an overwhelming urge to redistribute it and/or modify it under the
8
terms of the GNU General Public License as published by the Free
9
Software Foundation; either version 2 of the License, or (at your
10
option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received another copy of the GNU General Public
18
License along with this program; if not, write to the Free
19
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
I bet they'll be amazed.
38
/* Returns 1 if envelope runs out */
39
int recompute_envelope(int v)
43
stage = voice[v].envelope_stage;
46
if (voice[v].envelope_offset[stage] >> 23 > 127)
47
fprintf(stderr, "offset %d\n", voice[v].envelope_offset[stage]>>23);
52
/* Envelope ran out. */
53
if (!(voice[v].status & VOICE_FREE))
55
voice[v].status = VOICE_FREE;
61
/**if (voice[v].sample->modes & MODES_ENVELOPE)**/
62
if ((voice[v].sample->modes & MODES_ENVELOPE) && (voice[v].sample->modes & MODES_SUSTAIN))
64
if (voice[v].status & (VOICE_ON | VOICE_SUSTAINED))
68
/* Freeze envelope until note turns off. Trumpets want this. */
69
voice[v].envelope_increment=0;
74
voice[v].envelope_stage=stage+1;
77
fprintf(stderr, "v=%d stage %d, inc %ld[%ld], vol %ld[%ld], offset %ld[%ld]\n", v,
78
stage, voice[v].envelope_increment, voice[v].envelope_increment>>23,
79
voice[v].envelope_volume, voice[v].envelope_volume>>23,
80
voice[v].envelope_offset[stage], voice[v].envelope_offset[stage]>>23);
85
if (voice[v].envelope_volume==(int)voice[v].envelope_offset[stage] ||
86
(stage > 2 && voice[v].envelope_volume < (int)voice[v].envelope_offset[stage]))
88
if (voice[v].envelope_volume==voice[v].envelope_offset[stage])
92
if ( (voice[v].envelope_increment >= 0 && voice[v].envelope_volume >= voice[v].envelope_offset[stage])
93
|| (voice[v].envelope_increment < 0 && voice[v].envelope_volume <= voice[v].envelope_offset[stage]) )
95
return recompute_envelope(v);
96
voice[v].envelope_target=voice[v].envelope_offset[stage];
97
voice[v].envelope_increment = voice[v].envelope_rate[stage];
98
if ((int)voice[v].envelope_target<voice[v].envelope_volume)
99
voice[v].envelope_increment = -voice[v].envelope_increment;
102
fprintf(stderr, " cont:v=%d, stage %d, inc %ld[%ld], vol %ld[%ld], offset %ld[%ld]\n", v,
103
stage, voice[v].envelope_increment, voice[v].envelope_increment>>23,
104
voice[v].envelope_volume, voice[v].envelope_volume>>23,
105
voice[v].envelope_offset[stage], voice[v].envelope_offset[stage]>>23);
115
int apply_envelope_to_amp(int v)
117
void apply_envelope_to_amp(int v)
120
FLOAT_T lamp=voice[v].left_amp, ramp;
122
if (voice[v].panned == PANNED_MYSTERY)
124
ramp=voice[v].right_amp;
125
if (voice[v].tremolo_phase_increment)
127
lamp *= voice[v].tremolo_volume;
128
ramp *= voice[v].tremolo_volume;
130
if (voice[v].sample->modes & MODES_ENVELOPE)
132
lamp *= vol_table[voice[v].envelope_volume>>23];
133
ramp *= vol_table[voice[v].envelope_volume>>23];
135
if (voice[v].envelope_volume>>23 > 127)
136
fprintf(stderr,"env vol %d >>23 = %d\n", voice[v].envelope_volume,
137
voice[v].envelope_volume >> 23);
141
la = (int32)FRSCALE(lamp,AMP_BITS);
143
if (la>MAX_AMP_VALUE)
146
ra = (int32)FRSCALE(ramp,AMP_BITS);
147
if (ra>MAX_AMP_VALUE)
151
if ((voice[v].status & (VOICE_OFF | VOICE_DIE | VOICE_FREE | VOICE_SUSTAINED))
152
&& (la | ra) <= MIN_AMP_VALUE)
154
if (!(voice[v].status & VOICE_FREE))
156
voice[v].status = VOICE_FREE;
163
voice[v].left_mix=FINAL_VOLUME(la);
164
voice[v].right_mix=FINAL_VOLUME(ra);
168
if (voice[v].tremolo_phase_increment)
169
lamp *= voice[v].tremolo_volume;
170
if (voice[v].sample->modes & MODES_ENVELOPE)
171
lamp *= vol_table[voice[v].envelope_volume>>23];
173
la = (int32)FRSCALE(lamp,AMP_BITS);
175
if (la>MAX_AMP_VALUE)
179
if ( (voice[v].status & (VOICE_OFF | VOICE_DIE | VOICE_FREE | VOICE_SUSTAINED))
180
&& la <= MIN_AMP_VALUE)
182
if (!(voice[v].status & VOICE_FREE))
184
voice[v].status = VOICE_FREE;
191
voice[v].left_mix=FINAL_VOLUME(la);
198
static int update_envelope(int v)
200
voice[v].envelope_volume += voice[v].envelope_increment;
201
if (voice[v].envelope_volume<0) voice[v].envelope_volume = 0;
202
/* Why is there no ^^ operator?? */
203
if (((voice[v].envelope_increment < 0) &&
204
(voice[v].envelope_volume <= (int)voice[v].envelope_target)) ||
205
((voice[v].envelope_increment > 0) &&
206
(voice[v].envelope_volume >= (int)voice[v].envelope_target)))
208
voice[v].envelope_volume = voice[v].envelope_target;
209
if (recompute_envelope(v))
215
static void update_tremolo(int v)
217
int32 depth=voice[v].sample->tremolo_depth<<7;
219
if (voice[v].tremolo_sweep)
221
/* Update sweep position */
223
voice[v].tremolo_sweep_position += voice[v].tremolo_sweep;
224
if (voice[v].tremolo_sweep_position>=(1<<SWEEP_SHIFT))
225
voice[v].tremolo_sweep=0; /* Swept to max amplitude */
228
/* Need to adjust depth */
229
depth *= voice[v].tremolo_sweep_position;
230
depth >>= SWEEP_SHIFT;
234
voice[v].tremolo_phase += voice[v].tremolo_phase_increment;
236
/* if (voice[v].tremolo_phase >= (SINE_CYCLE_LENGTH<<RATE_SHIFT))
237
voice[v].tremolo_phase -= SINE_CYCLE_LENGTH<<RATE_SHIFT; */
239
voice[v].tremolo_volume =
240
1.0 - FRSCALENEG((sine(voice[v].tremolo_phase >> RATE_SHIFT) + 1.0)
241
* depth * TREMOLO_AMPLITUDE_TUNING,
244
/* I'm not sure about the +1.0 there -- it makes tremoloed voices'
245
volumes on average the lower the higher the tremolo amplitude. */
248
/* Returns 1 if the note died */
249
static int update_signal(int v)
251
if (voice[v].envelope_increment && update_envelope(v))
254
if (voice[v].tremolo_phase_increment)
258
return apply_envelope_to_amp(v);
260
apply_envelope_to_amp(v);
266
# define MIXATION(a) *lp++ += mixup[(a<<8) | (uint8)s];
268
# define MIXATION(a) *lp++ += (a)*s;
271
static void mix_mystery_signal(sample_t *sp, int32 *lp, int v, uint32 count)
273
Voice *vp = voice + v;
280
if (!(cc = vp->control_counter))
283
if (update_signal(v))
284
return; /* Envelope ran out */
286
right = vp->right_mix;
300
if (update_signal(v))
301
return; /* Envelope ran out */
303
right = vp->right_mix;
307
vp->control_counter = cc - count;
318
static void mix_center_signal(sample_t *sp, int32 *lp, int v, uint32 count)
320
Voice *vp = voice + v;
326
if (!(cc = vp->control_counter))
329
if (update_signal(v))
330
return; /* Envelope ran out */
345
if (update_signal(v))
346
return; /* Envelope ran out */
351
vp->control_counter = cc - count;
362
static void mix_single_signal(sample_t *sp, int32 *lp, int v, uint32 count)
364
Voice *vp = voice + v;
370
if (!(cc = vp->control_counter))
373
if (update_signal(v))
374
return; /* Envelope ran out */
389
if (update_signal(v))
390
return; /* Envelope ran out */
395
vp->control_counter = cc - count;
406
static void mix_mono_signal(sample_t *sp, int32 *lp, int v, uint32 count)
408
Voice *vp = voice + v;
414
if (!(cc = vp->control_counter))
417
if (update_signal(v))
418
return; /* Envelope ran out */
432
if (update_signal(v))
433
return; /* Envelope ran out */
438
vp->control_counter = cc - count;
448
static void mix_mystery(sample_t *sp, int32 *lp, int v, uint32 count)
451
left=voice[v].left_mix,
452
right=voice[v].right_mix;
463
static void mix_center(sample_t *sp, int32 *lp, int v, uint32 count)
466
left=voice[v].left_mix;
477
static void mix_single(sample_t *sp, int32 *lp, int v, uint32 count)
480
left=voice[v].left_mix;
491
static void mix_mono(sample_t *sp, int32 *lp, int v, uint32 count)
494
left=voice[v].left_mix;
504
/* Ramp a note out in c samples */
505
static void ramp_out(sample_t *sp, int32 *lp, int v, uint32 c)
508
/* should be final_volume_t, but uint8 gives trouble. */
509
int32 left, right, li, ri;
511
sample_t s=0; /* silly warning about uninitialized s */
513
left=voice[v].left_mix;
515
li=-(left/c); /*NB: c can be 0 here */
518
/* printf("Ramping out: left=%d, c=%d, li=%d\n", left, c, li); */
520
if (!(play_mode->encoding & PE_MONO))
522
if (voice[v].panned==PANNED_MYSTERY)
524
right=voice[v].right_mix;
539
else if (voice[v].panned==PANNED_CENTER)
551
else if (voice[v].panned==PANNED_LEFT)
563
else if (voice[v].panned==PANNED_RIGHT)
590
/**************** interface function ******************/
592
void mix_voice(int32 *buf, int v, uint32 c)
597
if (vp->status&VOICE_DIE)
599
/* this seems no longer useful: resample kill voices
602
if (count>=MAX_DIE_TIME)
604
sp=resample_voice(v, &count);
605
ramp_out(sp, buf, v, count);
606
vp->status=VOICE_FREE;
610
sp=resample_voice(v, &count);
613
vp->status=VOICE_FREE;
616
if (play_mode->encoding & PE_MONO)
619
if (vp->envelope_increment || vp->tremolo_phase_increment)
620
mix_mono_signal(sp, buf, v, count);
622
mix_mono(sp, buf, v, count);
626
if (vp->panned == PANNED_MYSTERY)
628
if (vp->envelope_increment || vp->tremolo_phase_increment)
629
mix_mystery_signal(sp, buf, v, count);
631
mix_mystery(sp, buf, v, count);
633
else if (vp->panned == PANNED_CENTER)
635
if (vp->envelope_increment || vp->tremolo_phase_increment)
636
mix_center_signal(sp, buf, v, count);
638
mix_center(sp, buf, v, count);
642
/* It's either full left or full right. In either case,
643
every other sample is 0. Just get the offset right: */
644
if (vp->panned == PANNED_RIGHT) buf++;
646
if (vp->envelope_increment || vp->tremolo_phase_increment)
647
mix_single_signal(sp, buf, v, count);
649
mix_single(sp, buf, v, count);