Skip to content

Commit

Permalink
Make it so you cannot add events too "early" in the block
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfd committed Jan 20, 2020
1 parent bc567c5 commit 864fe57
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/sfizz/Voice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ void sfz::Voice::registerCC(int delay, int ccNumber, uint8_t ccValue) noexcept
if (region->checkSustain && noteIsOff && ccNumber == config::sustainCC && ccValue < config::halfCCThreshold)
release(delay);

// TODO: gain-type events at time 0 may trigger sharp discontinuities; maybe add a minimum delay?
// Add a minimum delay for smoothing the envelopes
// TODO: this feels like a hack, revisit this along with the smoothed envelopes...
delay = max(delay, minEnvelopeDelay);

if (region->amplitudeCC && ccNumber == region->amplitudeCC->first) {
const float newGain { baseGain * normalizeCC(ccValue) * normalizePercents(region->amplitudeCC->second) };
amplitudeEnvelope.registerEvent(delay, Default::normalizedRange.clamp(newGain));
Expand All @@ -206,8 +209,8 @@ void sfz::Voice::registerCC(int delay, int ccNumber, uint8_t ccValue) noexcept
}

if (region->crossfadeCCInRange.contains(ccNumber) || region->crossfadeCCOutRange.contains(ccNumber)) {
const float crossfadeGain = region->getCrossfadeGain(midiState.getCCArray());
crossfadeEnvelope.registerEvent(delay, Default::normalizedRange.clamp(crossfadeGain));
const float crossfadeGain = region->getCrossfadeGain(midiState.getCCArray());
crossfadeEnvelope.registerEvent(delay, Default::normalizedRange.clamp(crossfadeGain));
}
}

Expand Down Expand Up @@ -237,6 +240,7 @@ void sfz::Voice::setSampleRate(float sampleRate) noexcept
void sfz::Voice::setSamplesPerBlock(int samplesPerBlock) noexcept
{
this->samplesPerBlock = samplesPerBlock;
this->minEnvelopeDelay = samplesPerBlock / 2;
tempBuffer1.resize(samplesPerBlock);
tempBuffer2.resize(samplesPerBlock);
tempBuffer3.resize(samplesPerBlock);
Expand Down
1 change: 1 addition & 0 deletions src/sfizz/Voice.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class Voice {
absl::Span<int> indexSpan { absl::MakeSpan(indexBuffer) };

int samplesPerBlock { config::defaultSamplesPerBlock };
int minEnvelopeDelay { config::defaultSamplesPerBlock / 2 };
float sampleRate { config::defaultSampleRate };

const MidiState& midiState;
Expand Down

0 comments on commit 864fe57

Please sign in to comment.