start method

void start(
  1. RegionPair region,
  2. int channel,
  3. int key,
  4. int velocity,
)

Implementation

void start(RegionPair region, int channel, int key, int velocity)
{
    _exclusiveClass = region.exclusiveClass();
    _channel = channel;
    _key = key;
    _velocity = velocity;

    if (velocity > 0)
    {
        // According to the Polyphone's implementation, the initial attenuation should be reduced to 40%.
        // I'm not sure why, but this indeed improves the loudness variability.
        var sampleAttenuation = 0.4 * region.initialAttenuation();
        var filterAttenuation = 0.5 * region.initialFilterQ();

        var decibels = 2 * SoundFontMath.linearToDecibels(velocity / 127.0) -
          sampleAttenuation - filterAttenuation;

        _noteGain = SoundFontMath.decibelsToLinear(decibels);
    }
    else
    {
        _noteGain = 0;
    }

    _cutoff = region.initialFilterCutoffFrequency();
    _resonance = SoundFontMath.decibelsToLinear(region.initialFilterQ());

    _vibLfoToPitch = 0.01 * region.vibratoLfoToPitch();
    _modLfoToPitch = 0.01 * region.modulationLfoToPitch();
    _modEnvToPitch = 0.01 * region.modulationEnvelopeToPitch();

    _modLfoToCutoff = region.modulationLfoToFilterCutoffFrequency();
    _modEnvToCutoff = region.modulationEnvelopeToFilterCutoffFrequency();

    _dynamicCutoff = _modLfoToCutoff != 0 || _modEnvToCutoff != 0;

    _modLfoToVolume = region.modulationLfoToVolume();
    _dynamicVolume = _modLfoToVolume > 0.05;

    _instrumentPan = region.pan().clamp(-50.0, 50.0);
    _instrumentReverb = 0.01 * region.reverbEffectsSend();
    _instrumentChorus = 0.01 * region.chorusEffectsSend();

    _volEnv.start2(region, key, velocity);
    _modEnv.start2(region, key, velocity);
    _vibLfo.startVibrato2(region, key, velocity);
    _modLfo.startModulation2(region, key, velocity);

    _oscillator.start2(synthesizer.soundFont.waveData, region);

    _filter.clearBuffer();
    _filter.setLowPassFilter(_cutoff, _resonance);

    _smoothedCutoff = _cutoff;

    _voiceState = VoiceState.playing;
    _voiceLength = 0;
}