2008-04-11 04:59:23 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
TiMidity -- Experimental MIDI to WAVE converter
|
|
|
|
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
mix.c
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "timidity.h"
|
|
|
|
|
|
|
|
namespace Timidity
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Returns 1 if envelope runs out */
|
|
|
|
int recompute_envelope(Voice *v)
|
|
|
|
{
|
|
|
|
int stage;
|
|
|
|
|
|
|
|
stage = v->envelope_stage;
|
|
|
|
|
2008-04-16 05:41:03 +00:00
|
|
|
if (stage > RELEASEC)
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
|
|
|
/* Envelope ran out. */
|
|
|
|
v->status = VOICE_FREE;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-04-16 05:41:03 +00:00
|
|
|
if (v->sample->modes & PATCH_NO_SRELEASE)
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
|
|
|
if (v->status == VOICE_ON || v->status == VOICE_SUSTAINED)
|
|
|
|
{
|
|
|
|
if (stage > DECAY)
|
|
|
|
{
|
|
|
|
/* Freeze envelope until note turns off. Trumpets want this. */
|
|
|
|
v->envelope_increment = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v->envelope_stage = stage + 1;
|
|
|
|
|
|
|
|
if (v->envelope_volume == v->sample->envelope_offset[stage])
|
2008-04-16 05:41:03 +00:00
|
|
|
{
|
2008-04-11 04:59:23 +00:00
|
|
|
return recompute_envelope(v);
|
2008-04-16 05:41:03 +00:00
|
|
|
}
|
2008-04-11 04:59:23 +00:00
|
|
|
v->envelope_target = v->sample->envelope_offset[stage];
|
|
|
|
v->envelope_increment = v->sample->envelope_rate[stage];
|
|
|
|
if (v->envelope_target < v->envelope_volume)
|
|
|
|
v->envelope_increment = -v->envelope_increment;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void apply_envelope_to_amp(Voice *v)
|
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
float env_vol = v->attenuation;
|
|
|
|
float final_amp = v->sample->volume * FINAL_MIX_SCALE;
|
|
|
|
if (v->tremolo_phase_increment != 0)
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
env_vol *= v->tremolo_volume;
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
2008-04-19 05:57:09 +00:00
|
|
|
if (v->sample->modes & PATCH_NO_SRELEASE)
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
env_vol *= v->envelope_volume / float(1 << 30);
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
2008-04-19 05:57:09 +00:00
|
|
|
// Note: The pan offsets are negative.
|
|
|
|
v->left_mix = (float)calc_gf1_amp(env_vol + v->left_offset) * final_amp;
|
|
|
|
v->right_mix = (float)calc_gf1_amp(env_vol + v->right_offset) * final_amp;
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int update_envelope(Voice *v)
|
|
|
|
{
|
|
|
|
v->envelope_volume += v->envelope_increment;
|
2008-04-12 05:04:37 +00:00
|
|
|
if (((v->envelope_increment < 0) && (v->envelope_volume <= v->envelope_target)) ||
|
|
|
|
((v->envelope_increment > 0) && (v->envelope_volume >= v->envelope_target)))
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
|
|
|
v->envelope_volume = v->envelope_target;
|
|
|
|
if (recompute_envelope(v))
|
2008-04-16 05:41:03 +00:00
|
|
|
{
|
2008-04-11 04:59:23 +00:00
|
|
|
return 1;
|
2008-04-16 05:41:03 +00:00
|
|
|
}
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_tremolo(Voice *v)
|
|
|
|
{
|
|
|
|
int depth = v->sample->tremolo_depth << 7;
|
|
|
|
|
2008-04-16 05:41:03 +00:00
|
|
|
if (v->tremolo_sweep != 0)
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
|
|
|
/* Update sweep position */
|
|
|
|
|
|
|
|
v->tremolo_sweep_position += v->tremolo_sweep;
|
|
|
|
if (v->tremolo_sweep_position >= (1 << SWEEP_SHIFT))
|
|
|
|
{
|
|
|
|
/* Swept to max amplitude */
|
|
|
|
v->tremolo_sweep = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Need to adjust depth */
|
|
|
|
depth *= v->tremolo_sweep_position;
|
|
|
|
depth >>= SWEEP_SHIFT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v->tremolo_phase += v->tremolo_phase_increment;
|
|
|
|
|
|
|
|
v->tremolo_volume = (float)
|
|
|
|
(1.0 - FSCALENEG((sine(v->tremolo_phase >> RATE_SHIFT) + 1.0)
|
|
|
|
* depth * TREMOLO_AMPLITUDE_TUNING,
|
|
|
|
17));
|
|
|
|
|
|
|
|
/* I'm not sure about the +1.0 there -- it makes tremoloed voices'
|
|
|
|
volumes on average the lower the higher the tremolo amplitude. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns 1 if the note died */
|
|
|
|
static int update_signal(Voice *v)
|
|
|
|
{
|
2008-04-16 05:41:03 +00:00
|
|
|
if (v->envelope_increment != 0 && update_envelope(v))
|
|
|
|
{
|
2008-04-11 04:59:23 +00:00
|
|
|
return 1;
|
2008-04-16 05:41:03 +00:00
|
|
|
}
|
|
|
|
if (v->tremolo_phase_increment != 0)
|
|
|
|
{
|
2008-04-11 04:59:23 +00:00
|
|
|
update_tremolo(v);
|
2008-04-16 05:41:03 +00:00
|
|
|
}
|
2008-04-11 04:59:23 +00:00
|
|
|
apply_envelope_to_amp(v);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mix_mystery_signal(SDWORD control_ratio, const sample_t *sp, float *lp, Voice *v, int count)
|
|
|
|
{
|
|
|
|
final_volume_t
|
|
|
|
left = v->left_mix,
|
|
|
|
right = v->right_mix;
|
|
|
|
int cc;
|
|
|
|
sample_t s;
|
|
|
|
|
|
|
|
if (!(cc = v->control_counter))
|
|
|
|
{
|
|
|
|
cc = control_ratio;
|
|
|
|
if (update_signal(v))
|
|
|
|
return; /* Envelope ran out */
|
|
|
|
|
|
|
|
left = v->left_mix;
|
|
|
|
right = v->right_mix;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (count)
|
|
|
|
{
|
|
|
|
if (cc < count)
|
|
|
|
{
|
|
|
|
count -= cc;
|
|
|
|
while (cc--)
|
|
|
|
{
|
|
|
|
s = *sp++;
|
|
|
|
lp[0] += left * s;
|
|
|
|
lp[1] += right * s;
|
|
|
|
lp += 2;
|
|
|
|
}
|
|
|
|
cc = control_ratio;
|
|
|
|
if (update_signal(v))
|
|
|
|
return; /* Envelope ran out */
|
|
|
|
left = v->left_mix;
|
|
|
|
right = v->right_mix;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v->control_counter = cc - count;
|
|
|
|
while (count--)
|
|
|
|
{
|
|
|
|
s = *sp++;
|
|
|
|
lp[0] += left * s;
|
|
|
|
lp[1] += right * s;
|
|
|
|
lp += 2;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 05:57:09 +00:00
|
|
|
static void mix_single_signal(SDWORD control_ratio, const sample_t *sp, float *lp, Voice *v, float *ampat, int count)
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
final_volume_t amp;
|
2008-04-11 04:59:23 +00:00
|
|
|
int cc;
|
|
|
|
|
2008-04-19 05:57:09 +00:00
|
|
|
if (0 == (cc = v->control_counter))
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
|
|
|
cc = control_ratio;
|
|
|
|
if (update_signal(v))
|
2008-04-19 05:57:09 +00:00
|
|
|
return; /* Envelope ran out */
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
2008-04-19 05:57:09 +00:00
|
|
|
amp = *ampat;
|
2008-04-11 04:59:23 +00:00
|
|
|
|
|
|
|
while (count)
|
|
|
|
{
|
|
|
|
if (cc < count)
|
|
|
|
{
|
|
|
|
count -= cc;
|
|
|
|
while (cc--)
|
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
lp[0] += *sp++ * amp;
|
2008-04-11 04:59:23 +00:00
|
|
|
lp += 2;
|
|
|
|
}
|
|
|
|
cc = control_ratio;
|
|
|
|
if (update_signal(v))
|
|
|
|
return; /* Envelope ran out */
|
2008-04-19 05:57:09 +00:00
|
|
|
amp = *ampat;
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v->control_counter = cc - count;
|
|
|
|
while (count--)
|
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
lp[0] += *sp++ * amp;
|
2008-04-11 04:59:23 +00:00
|
|
|
lp += 2;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mix_single_left_signal(SDWORD control_ratio, const sample_t *sp, float *lp, Voice *v, int count)
|
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
mix_single_signal(control_ratio, sp, lp, v, &v->left_mix, count);
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mix_single_right_signal(SDWORD control_ratio, const sample_t *sp, float *lp, Voice *v, int count)
|
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
mix_single_signal(control_ratio, sp, lp + 1, v, &v->right_mix, count);
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mix_mono_signal(SDWORD control_ratio, const sample_t *sp, float *lp, Voice *v, int count)
|
|
|
|
{
|
|
|
|
final_volume_t
|
|
|
|
left = v->left_mix;
|
|
|
|
int cc;
|
|
|
|
|
|
|
|
if (!(cc = v->control_counter))
|
|
|
|
{
|
|
|
|
cc = control_ratio;
|
|
|
|
if (update_signal(v))
|
|
|
|
return; /* Envelope ran out */
|
|
|
|
left = v->left_mix;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (count)
|
|
|
|
{
|
|
|
|
if (cc < count)
|
|
|
|
{
|
|
|
|
count -= cc;
|
|
|
|
while (cc--)
|
|
|
|
{
|
|
|
|
*lp++ += *sp++ * left;
|
|
|
|
}
|
|
|
|
cc = control_ratio;
|
|
|
|
if (update_signal(v))
|
|
|
|
return; /* Envelope ran out */
|
|
|
|
left = v->left_mix;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v->control_counter = cc - count;
|
|
|
|
while (count--)
|
|
|
|
{
|
|
|
|
*lp++ += *sp++ * left;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mix_mystery(SDWORD control_ratio, const sample_t *sp, float *lp, Voice *v, int count)
|
|
|
|
{
|
|
|
|
final_volume_t
|
|
|
|
left = v->left_mix,
|
|
|
|
right = v->right_mix;
|
|
|
|
sample_t s;
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
{
|
|
|
|
s = *sp++;
|
|
|
|
lp[0] += s * left;
|
|
|
|
lp[1] += s * right;
|
|
|
|
lp += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 05:57:09 +00:00
|
|
|
static void mix_single(const sample_t *sp, float *lp, final_volume_t amp, int count)
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
|
|
|
while (count--)
|
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
lp[0] += *sp++ * amp;
|
2008-04-11 04:59:23 +00:00
|
|
|
lp += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mix_single_left(const sample_t *sp, float *lp, Voice *v, int count)
|
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
mix_single(sp, lp, v->left_mix, count);
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
|
|
|
static void mix_single_right(const sample_t *sp, float *lp, Voice *v, int count)
|
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
mix_single(sp, lp + 1, v->right_mix, count);
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mix_mono(const sample_t *sp, float *lp, Voice *v, int count)
|
|
|
|
{
|
|
|
|
final_volume_t
|
|
|
|
left = v->left_mix;
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
{
|
|
|
|
*lp++ += *sp++ * left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ramp a note out in c samples */
|
|
|
|
static void ramp_out(const sample_t *sp, float *lp, Voice *v, int c)
|
|
|
|
{
|
|
|
|
final_volume_t left, right, li, ri;
|
|
|
|
|
|
|
|
sample_t s = 0; /* silly warning about uninitialized s */
|
|
|
|
|
|
|
|
/* Fix by James Caldwell */
|
|
|
|
if ( c == 0 ) c = 1;
|
|
|
|
|
|
|
|
/* printf("Ramping out: left=%d, c=%d, li=%d\n", left, c, li); */
|
|
|
|
|
2008-04-19 05:57:09 +00:00
|
|
|
if (v->left_offset == 0) // All the way to the left
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
left = v->left_mix;
|
|
|
|
li = -(left/c);
|
|
|
|
if (li == 0) li = -1;
|
|
|
|
|
2008-04-11 04:59:23 +00:00
|
|
|
while (c--)
|
|
|
|
{
|
|
|
|
left += li;
|
|
|
|
if (left < 0)
|
|
|
|
return;
|
2008-04-19 05:57:09 +00:00
|
|
|
lp[0] += *sp++ * left;
|
2008-04-11 04:59:23 +00:00
|
|
|
lp += 2;
|
|
|
|
}
|
|
|
|
}
|
2008-04-19 05:57:09 +00:00
|
|
|
else if (v->right_offset == 0) // All the way to the right
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
right = v->right_mix;
|
|
|
|
ri = -(right/c);
|
|
|
|
if (ri == 0) ri = -1;
|
|
|
|
|
2008-04-11 04:59:23 +00:00
|
|
|
while (c--)
|
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
right += ri;
|
|
|
|
if (right < 0)
|
2008-04-11 04:59:23 +00:00
|
|
|
return;
|
2008-04-19 05:57:09 +00:00
|
|
|
s = *sp++;
|
|
|
|
lp[1] += *sp++ * right;
|
2008-04-11 04:59:23 +00:00
|
|
|
lp += 2;
|
|
|
|
}
|
|
|
|
}
|
2008-04-19 05:57:09 +00:00
|
|
|
else // Somewhere in the middle
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
left = v->left_mix;
|
|
|
|
li = -(left/c);
|
|
|
|
if (li == 0) li = -1;
|
|
|
|
right = v->right_mix;
|
|
|
|
ri = -(right/c);
|
|
|
|
if (ri == 0) ri = -1;
|
|
|
|
|
|
|
|
right = v->right_mix;
|
|
|
|
ri = -(right/c);
|
2008-04-11 04:59:23 +00:00
|
|
|
while (c--)
|
|
|
|
{
|
|
|
|
left += li;
|
2008-04-19 05:57:09 +00:00
|
|
|
right += ri;
|
2008-04-11 04:59:23 +00:00
|
|
|
if (left < 0)
|
2008-04-19 05:57:09 +00:00
|
|
|
{
|
|
|
|
if (right < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
else if (right < 0)
|
|
|
|
{
|
|
|
|
right = 0;
|
|
|
|
}
|
2008-04-11 04:59:23 +00:00
|
|
|
s = *sp++;
|
2008-04-19 05:57:09 +00:00
|
|
|
lp[0] += s * left;
|
|
|
|
lp[1] += s * right;
|
2008-04-11 04:59:23 +00:00
|
|
|
lp += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**************** interface function ******************/
|
|
|
|
|
|
|
|
void mix_voice(Renderer *song, float *buf, Voice *v, int c)
|
|
|
|
{
|
|
|
|
int count = c;
|
|
|
|
sample_t *sp;
|
|
|
|
if (c < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (v->status == VOICE_DIE)
|
|
|
|
{
|
|
|
|
if (count >= MAX_DIE_TIME)
|
|
|
|
count = MAX_DIE_TIME;
|
|
|
|
sp = resample_voice(song, v, &count);
|
|
|
|
ramp_out(sp, buf, v, count);
|
|
|
|
v->status = VOICE_FREE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sp = resample_voice(song, v, &count);
|
|
|
|
if (count < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-04-19 05:57:09 +00:00
|
|
|
if (v->left_offset == 0) // All the way to the left
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
if (v->envelope_increment != 0 || v->tremolo_phase_increment != 0)
|
|
|
|
{
|
|
|
|
mix_single_left_signal(song->control_ratio, sp, buf, v, count);
|
|
|
|
}
|
2008-04-11 04:59:23 +00:00
|
|
|
else
|
2008-04-19 05:57:09 +00:00
|
|
|
{
|
|
|
|
mix_single_left(sp, buf, v, count);
|
|
|
|
}
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
2008-04-19 05:57:09 +00:00
|
|
|
else if (v->right_offset == 0) // All the way to the right
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
if (v->envelope_increment != 0 || v->tremolo_phase_increment != 0)
|
|
|
|
{
|
|
|
|
mix_single_right_signal(song->control_ratio, sp, buf, v, count);
|
|
|
|
}
|
2008-04-11 04:59:23 +00:00
|
|
|
else
|
2008-04-19 05:57:09 +00:00
|
|
|
{
|
|
|
|
mix_single_right(sp, buf, v, count);
|
|
|
|
}
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
2008-04-19 05:57:09 +00:00
|
|
|
else // Somewhere in the middle
|
|
|
|
{
|
2008-04-11 04:59:23 +00:00
|
|
|
if (v->envelope_increment || v->tremolo_phase_increment)
|
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
mix_mystery_signal(song->control_ratio, sp, buf, v, count);
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
2008-04-19 05:57:09 +00:00
|
|
|
else
|
2008-04-11 04:59:23 +00:00
|
|
|
{
|
2008-04-19 05:57:09 +00:00
|
|
|
mix_mystery(song->control_ratio, sp, buf, v, count);
|
2008-04-11 04:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|