Skip to content

Commit

Permalink
fix #116871: add support for rt_decay
Browse files Browse the repository at this point in the history
  • Loading branch information
hpfmn committed Jun 30, 2016
1 parent 60bcbcb commit e1cb58a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
11 changes: 9 additions & 2 deletions zerberus/voice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void Voice::init()
// start
//---------------------------------------------------------

void Voice::start(Channel* c, int key, int v, const Zone* z)
void Voice::start(Channel* c, int key, int v, const Zone* z, double durSinceNoteOn)
{
_state = VoiceState::ATTACK;
//_state = VoiceState::PLAYING;
Expand All @@ -108,6 +108,7 @@ void Voice::start(Channel* c, int key, int v, const Zone* z)
_loopMode = z->loopMode;
_loopStart = z->loopStart;
_loopEnd = z->loopEnd;
_samplesSinceStart = 0;

_offMode = z->offMode;
_offBy = z->offBy;
Expand All @@ -120,8 +121,12 @@ void Voice::start(Channel* c, int key, int v, const Zone* z)
if (trigger == Trigger::CC)
_velocity = 127;
float curve = _velocity * _velocity / (127.0 * 127.0);

double rt_decay_value = 1.0;
if (trigger == Trigger::RELEASE)
rt_decay_value = pow(10, (-z->rtDecay * durSinceNoteOn)/20);
gain = z->volume * (offset + z->ampVeltrack * curve)
* .005 * c->gain();
* .005 * c->gain() * rt_decay_value;

phase.set(0);
float sr = float(s->sampleRate()) / _zerberus->sampleRate();
Expand Down Expand Up @@ -348,6 +353,7 @@ void Voice::process(int frames, float* p)
*p++ += v * _channel->panLeftGain();
*p++ += v * _channel->panRightGain();
phase += phaseIncr;
_samplesSinceStart++;
}
}
else {
Expand Down Expand Up @@ -408,6 +414,7 @@ void Voice::process(int frames, float* p)
*p++ += vl;
*p++ += vr;
phase += phaseIncr;
_samplesSinceStart++;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion zerberus/voice.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Voice {
int _loopStart;
int _loopEnd;
bool _looping;
int _samplesSinceStart;

float gain;

Expand Down Expand Up @@ -176,7 +177,7 @@ class Voice {
Voice* next() const { return _next; }
void setNext(Voice* v) { _next = v; }

void start(Channel* channel, int key, int velo, const Zone*);
void start(Channel* channel, int key, int velo, const Zone*, double durSinceNoteOn);
void updateEnvelopes();
void process(int frames, float*);
void updateLoop();
Expand All @@ -196,6 +197,7 @@ class Voice {
void off() { _state = VoiceState::OFF; }
const char* state() const;
LoopMode loopMode() const { return _loopMode; }
int getSamplesSinceStart() { return _samplesSinceStart; }

OffMode offMode() const { return _offMode; }
int offBy() const { return _offBy; }
Expand Down
11 changes: 6 additions & 5 deletions zerberus/zerberus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void Zerberus::programChange(int channel, int program)
// gui
//---------------------------------------------------------

void Zerberus::trigger(Channel* channel, int key, int velo, Trigger trigger, int cc, int ccVal)
void Zerberus::trigger(Channel* channel, int key, int velo, Trigger trigger, int cc, int ccVal, double durSinceNoteOn)
{
ZInstrument* i = channel->instrument();
double random = (double) rand() / (double) RAND_MAX;
Expand All @@ -102,7 +102,7 @@ void Zerberus::trigger(Channel* channel, int key, int velo, Trigger trigger, int
}
Voice* voice = freeVoices.pop();
Q_ASSERT(voice->isOff());
voice->start(channel, key, velo, z);
voice->start(channel, key, velo, z, durSinceNoteOn);
voice->setNext(activeVoices);
activeVoices = voice;

Expand Down Expand Up @@ -137,7 +137,8 @@ void Zerberus::processNoteOff(Channel* cp, int key)
if (cp->sustain() < 0x40) {
if (!v->isStopped())
v->stop();
trigger(cp, key, v->velocity(), Trigger::RELEASE, -1, -1);
double durSinceNoteOn = v->getSamplesSinceStart() / sampleRate();
trigger(cp, key, v->velocity(), Trigger::RELEASE, -1, -1, durSinceNoteOn);
}
else {
if (v->isPlaying())
Expand All @@ -162,7 +163,7 @@ void Zerberus::processNoteOn(Channel* cp, int key, int velo)
}
}
}
trigger(cp, key, velo, Trigger::ATTACK, -1, -1);
trigger(cp, key, velo, Trigger::ATTACK, -1, -1, 0);
}

//---------------------------------------------------------
Expand Down Expand Up @@ -196,7 +197,7 @@ void Zerberus::play(const Ms::PlayEvent& event)

case Ms::ME_CONTROLLER:
cp->controller(event.dataA(), event.dataB());
trigger(cp, -1, -1, Trigger::CC, event.dataA(), event.dataB());
trigger(cp, -1, -1, Trigger::CC, event.dataA(), event.dataB(), 0);
break;

default:
Expand Down
2 changes: 1 addition & 1 deletion zerberus/zerberus.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Zerberus : public Ms::Synthesizer {
bool _loadWasCanceled = false;

void programChange(int channel, int program);
void trigger(Channel*, int key, int velo, Trigger, int cc, int ccVal);
void trigger(Channel*, int key, int velo, Trigger, int cc, int ccVal, double durSinceNoteOn);
void processNoteOff(Channel*, int pitch);
void processNoteOn(Channel* cp, int key, int velo);

Expand Down

0 comments on commit e1cb58a

Please sign in to comment.