2020-05-02 03:46:42 +00:00
|
|
|
|
//
|
|
|
|
|
// EnvelopeGenerator.hpp
|
|
|
|
|
// Clock Signal
|
|
|
|
|
//
|
|
|
|
|
// Created by Thomas Harte on 01/05/2020.
|
|
|
|
|
// Copyright © 2020 Thomas Harte. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef EnvelopeGenerator_h
|
|
|
|
|
#define EnvelopeGenerator_h
|
|
|
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
|
#include <functional>
|
2020-05-05 01:14:51 +00:00
|
|
|
|
#include "LowFrequencyOscillator.hpp"
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
namespace Yamaha {
|
|
|
|
|
namespace OPL {
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Models an OPL-style envelope generator.
|
|
|
|
|
|
|
|
|
|
Damping is optional; if damping is enabled then if there is a transition to key-on while
|
|
|
|
|
attenuation is less than maximum then attenuation will be quickly transitioned to maximum
|
|
|
|
|
before the attack phase can begin.
|
|
|
|
|
|
|
|
|
|
in real hardware damping is used by the envelope generators associated with
|
|
|
|
|
carriers, with phases being reset upon the transition from damping to attack.
|
|
|
|
|
|
|
|
|
|
This code considers application of tremolo to be a function of the envelope generator;
|
|
|
|
|
this is largely for logical conformity with the phase generator that necessarily has to
|
|
|
|
|
apply vibrato.
|
2020-05-08 22:40:49 +00:00
|
|
|
|
|
|
|
|
|
TODO: use envelope_precision.
|
2020-05-02 03:46:42 +00:00
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
template <int envelope_precision, int period_precision> class EnvelopeGenerator {
|
2020-05-02 03:46:42 +00:00
|
|
|
|
public:
|
|
|
|
|
/*!
|
|
|
|
|
Advances the envelope generator a single step, given the current state of the low-frequency oscillator, @c oscillator.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void update(const LowFrequencyOscillator &oscillator) {
|
|
|
|
|
// Apply tremolo, which is fairly easy.
|
|
|
|
|
tremolo_ = tremolo_enable_ * oscillator.tremolo << 4;
|
|
|
|
|
|
|
|
|
|
// Something something something...
|
|
|
|
|
const int key_scaling_rate = key_scale_rate_ >> key_scale_rate_shift_;
|
|
|
|
|
switch(phase_) {
|
|
|
|
|
case Phase::Damp:
|
2020-05-08 22:40:49 +00:00
|
|
|
|
update_decay(oscillator, 12 << 2);
|
2020-05-03 20:24:55 +00:00
|
|
|
|
if(attenuation_ == 511) {
|
2020-05-04 01:57:15 +00:00
|
|
|
|
(*will_attack_)();
|
2020-05-03 20:24:55 +00:00
|
|
|
|
phase_ = Phase::Attack;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Phase::Attack:
|
|
|
|
|
update_attack(oscillator, attack_rate_ + key_scaling_rate);
|
|
|
|
|
|
|
|
|
|
// Two possible terminating conditions: (i) the attack rate is 15; (ii) full volume has been reached.
|
2020-05-08 22:59:05 +00:00
|
|
|
|
if(attenuation_ <= 0) {
|
2020-05-03 20:24:55 +00:00
|
|
|
|
attenuation_ = 0;
|
|
|
|
|
phase_ = Phase::Decay;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Phase::Decay:
|
|
|
|
|
update_decay(oscillator, decay_rate_ + key_scaling_rate);
|
|
|
|
|
if(attenuation_ >= sustain_level_) {
|
|
|
|
|
attenuation_ = sustain_level_;
|
|
|
|
|
phase_ = use_sustain_level_ ? Phase::Sustain : Phase::Release;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Phase::Sustain:
|
|
|
|
|
// Nothing to do.
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Phase::Release:
|
|
|
|
|
update_decay(oscillator, release_rate_ + key_scaling_rate);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
2020-05-06 02:14:11 +00:00
|
|
|
|
@returns The current attenuation from this envelope generator. This is independent of the envelope precision.
|
2020-05-02 03:46:42 +00:00
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
int attenuation() const {
|
2020-05-10 04:43:46 +00:00
|
|
|
|
// TODO: if this envelope is fully released, should tremolo still be able to vocalise it?
|
|
|
|
|
return (attenuation_ << 3) + tremolo_;
|
2020-05-03 20:24:55 +00:00
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Enables or disables damping on this envelope generator. If damping is enabled then this envelope generator will
|
|
|
|
|
use the damping phase when necessary (i.e. when transitioning to key on if attenuation is not already at maximum)
|
|
|
|
|
and in any case will call @c will_attack before transitioning from any other state to attack.
|
|
|
|
|
|
|
|
|
|
@param will_attack Supply a will_attack callback to enable damping mode; supply nullopt to disable damping mode.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void set_should_damp(const std::optional<std::function<void(void)>> &will_attack) {
|
|
|
|
|
will_attack_ = will_attack;
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the current state of the key-on input.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void set_key_on(bool key_on) {
|
|
|
|
|
// Do nothing if this is not a leading or trailing edge.
|
|
|
|
|
if(key_on == key_on_) return;
|
|
|
|
|
key_on_ = key_on;
|
|
|
|
|
|
|
|
|
|
// Always transition to release upon a key off.
|
|
|
|
|
if(!key_on_) {
|
|
|
|
|
phase_ = Phase::Release;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// On key on: if this is an envelope generator with damping, and damping is required,
|
|
|
|
|
// schedule that. If damping is not required, announce a pending attack now and
|
|
|
|
|
// transition to attack.
|
|
|
|
|
if(will_attack_) {
|
|
|
|
|
if(attenuation_ != 511) {
|
|
|
|
|
phase_ = Phase::Damp;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 01:57:15 +00:00
|
|
|
|
(*will_attack_)();
|
2020-05-03 20:24:55 +00:00
|
|
|
|
}
|
|
|
|
|
phase_ = Phase::Attack;
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the attack rate, which should be in the range 0–15.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void set_attack_rate(int rate) {
|
|
|
|
|
attack_rate_ = rate << 2;
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the decay rate, which should be in the range 0–15.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void set_decay_rate(int rate) {
|
|
|
|
|
decay_rate_ = rate << 2;
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the release rate, which should be in the range 0–15.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void set_release_rate(int rate) {
|
|
|
|
|
release_rate_ = rate << 2;
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the sustain level, which should be in the range 0–15.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void set_sustain_level(int level) {
|
|
|
|
|
sustain_level_ = level << 3;
|
|
|
|
|
// TODO: verify the shift level here. Especially re: precision.
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Enables or disables use of the sustain level. If this is disabled, the envelope proceeds
|
|
|
|
|
directly from decay to release.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void set_use_sustain_level(bool use) {
|
|
|
|
|
use_sustain_level_ = use;
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Enables or disables key-rate scaling.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void set_key_scaling_rate_enabled(bool enabled) {
|
|
|
|
|
key_scale_rate_shift_ = int(enabled) * 2;
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Enables or disables application of the low-frequency oscillator's tremolo.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void set_tremolo_enabled(bool enabled) {
|
|
|
|
|
tremolo_enable_ = int(enabled);
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Sets the current period associated with the channel that owns this envelope generator;
|
|
|
|
|
this is used to select a key scaling rate if key-rate scaling is enabled.
|
|
|
|
|
*/
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void set_period(int period, int octave) {
|
|
|
|
|
key_scale_rate_ = (octave << 1) | (period >> (period_precision - 1));
|
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2020-05-03 20:24:55 +00:00
|
|
|
|
enum class Phase {
|
2020-05-02 03:46:42 +00:00
|
|
|
|
Attack, Decay, Sustain, Release, Damp
|
2020-05-06 04:13:01 +00:00
|
|
|
|
} phase_ = Phase::Release;
|
2020-05-03 20:24:55 +00:00
|
|
|
|
int attenuation_ = 511, tremolo_ = 0;
|
2020-05-02 03:46:42 +00:00
|
|
|
|
|
|
|
|
|
bool key_on_ = false;
|
|
|
|
|
std::optional<std::function<void(void)>> will_attack_;
|
2020-05-03 20:24:55 +00:00
|
|
|
|
|
|
|
|
|
int key_scale_rate_ = 0;
|
|
|
|
|
int key_scale_rate_shift_ = 0;
|
|
|
|
|
|
|
|
|
|
int tremolo_enable_ = 0;
|
|
|
|
|
|
|
|
|
|
int attack_rate_ = 0;
|
|
|
|
|
int decay_rate_ = 0;
|
|
|
|
|
int release_rate_ = 0;
|
|
|
|
|
int sustain_level_ = 0;
|
|
|
|
|
bool use_sustain_level_ = false;
|
|
|
|
|
|
2020-05-08 22:40:49 +00:00
|
|
|
|
static constexpr int dithering_patterns[4][8] = {
|
|
|
|
|
{0, 1, 0, 1, 0, 1, 0, 1},
|
|
|
|
|
{0, 1, 0, 1, 1, 1, 0, 1},
|
|
|
|
|
{0, 1, 1, 1, 0, 1, 1, 1},
|
|
|
|
|
{0, 1, 1, 1, 1, 1, 1, 1},
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-03 20:24:55 +00:00
|
|
|
|
void update_attack(const LowFrequencyOscillator &oscillator, int rate) {
|
2020-05-08 22:59:05 +00:00
|
|
|
|
// Special case: no attack.
|
|
|
|
|
if(rate < 4) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Special case: instant attack.
|
|
|
|
|
if(rate >= 60) {
|
|
|
|
|
attenuation_ = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Work out the number of cycles between each adjustment tick, and stop now
|
|
|
|
|
// if not at the next adjustment boundary.
|
|
|
|
|
const int shift_size = 13 - (std::min(rate, 52) >> 2);
|
|
|
|
|
if(oscillator.counter & ((1 << shift_size) - 1)) {
|
|
|
|
|
return;
|
2020-05-03 20:24:55 +00:00
|
|
|
|
}
|
2020-05-08 22:59:05 +00:00
|
|
|
|
|
|
|
|
|
// Apply dithered adjustment.
|
|
|
|
|
const int rate_shift = (rate > 55);
|
|
|
|
|
const int step = dithering_patterns[rate & 3][(oscillator.counter >> shift_size) & 7];
|
|
|
|
|
attenuation_ -= ((attenuation_ >> (3 - rate_shift)) + 1) * step;
|
2020-05-03 20:24:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void update_decay(const LowFrequencyOscillator &oscillator, int rate) {
|
2020-05-08 22:40:49 +00:00
|
|
|
|
// Special case: no decay.
|
|
|
|
|
if(rate < 4) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-03 20:24:55 +00:00
|
|
|
|
|
2020-05-08 22:40:49 +00:00
|
|
|
|
// Work out the number of cycles between each adjustment tick, and stop now
|
|
|
|
|
// if not at the next adjustment boundary.
|
|
|
|
|
const int shift_size = 13 - (std::min(rate, 52) >> 2);
|
|
|
|
|
if(oscillator.counter & ((1 << shift_size) - 1)) {
|
|
|
|
|
return;
|
2020-05-03 20:24:55 +00:00
|
|
|
|
}
|
2020-05-08 22:40:49 +00:00
|
|
|
|
|
|
|
|
|
// Apply dithered adjustment and clamp.
|
|
|
|
|
const int rate_shift = 1 + (rate > 59) + (rate > 55);
|
|
|
|
|
attenuation_ += dithering_patterns[rate & 3][(oscillator.counter >> shift_size) & 7] * (4 << rate_shift);
|
|
|
|
|
attenuation_ = std::min(attenuation_, 511);
|
2020-05-03 20:24:55 +00:00
|
|
|
|
}
|
2020-05-02 03:46:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* EnvelopeGenerator_h */
|