1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-10-05 23:56:33 +00:00

Takes a stab at per-operator ADSR.

Heavy caveats apply: no KSR is applied, non-ADSR attenuation isn't applied, attenuation isn't voiced in general.
This commit is contained in:
Thomas Harte 2020-04-13 21:39:06 -04:00
parent cb1970ebab
commit a1f80b5142
2 changed files with 168 additions and 48 deletions

View File

@ -226,7 +226,7 @@ void OPLL::write_register(uint8_t address, uint8_t value) {
switch(address & 0xf0) {
case 0x30:
// Select an instrument in the top nibble, set a channel volume in the lower.
channels_[index].overrides.output_level = value & 0xf;
channels_[index].overrides.attenuation = value & 0xf;
channels_[index].modulator = &operators_[(value >> 4) * 2];
break;
@ -385,3 +385,129 @@ uint8_t OPL2::read(uint16_t address) {
// b5 = timer 2 flag
return 0xff;
}
// MARK: - Operators
void Operator::update(OperatorState &state, bool key_on, int channel_frequency, int channel_octave, OperatorOverrides *overrides) {
// Per the documentation:
//
// Delta phase = ( [desired freq] * 2^19 / [input clock / 72] ) / 2 ^ (b - 1)
//
// After experimentation, I think this gives rate calculation as formulated below.
// This encodes the MUL -> multiple table given on page 12,
// multiplied by two.
constexpr int multipliers[] = {
1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
};
// Update the raw phase.
const int octave_divider = 32 << channel_octave;
state.divider_ %= octave_divider;
state.divider_ += multipliers[frequency_multiple] * channel_frequency;
state.raw_phase_ += state.divider_ / octave_divider;
// Hence calculate phase (TODO: by also taking account of vibrato).
constexpr int waveforms[4][4] = {
{1023, 1023, 1023, 1023}, // Sine: don't mask in any quadrant.
{511, 511, 0, 0}, // Half sine: keep the first half in tact, lock to 0 in the second half.
{511, 511, 511, 511}, // AbsSine: endlessly repeat the first half of the sine wave.
{255, 0, 255, 0}, // PulseSine: act as if the first quadrant is in the first and third; lock the other two to 0.
};
state.phase = state.raw_phase_ & waveforms[int(waveform)][(state.raw_phase_ >> 8) & 3];
// Key-on logic: any time it is false, be in the release state.
// On the leading edge of it becoming true, enter the attack state.
if(!key_on) {
state.adsr_phase_ = OperatorState::ADSRPhase::Release;
state.time_in_phase_ = 0;
} else if(!state.last_key_on_) {
state.adsr_phase_ = OperatorState::ADSRPhase::Attack;
state.time_in_phase_ = 0;
}
state.last_key_on_ = key_on;
// Adjust the ADSR attenuation appropriately;
// cf. http://forums.submarine.org.uk/phpBB/viewtopic.php?f=9&t=16 (primarily) for the source of the maths below.
// "An attack rate value of 52 (AR = 13) has 32 samples in the attack phase, an attack rate value of 48 (AR = 12)
// has 64 samples in the attack phase, but pairs of samples show the same envelope attenuation. I am however struggling to find a plausible algorithm to match the experimental results.
const auto current_phase = state.adsr_phase_;
switch(current_phase) {
case OperatorState::ADSRPhase::Attack: {
const int attack_rate = attack_rate_; // TODO: key scaling rate. Which I do not yet understand.
// Rules:
//
// An attack rate of '13' has 32 samples in the attack phase; a rate of '12' has the same 32 steps, but spread out over 64 samples, etc.
// An attack rate of '14' uses a divide by four instead of two.
// 15 is instantaneous.
if(attack_rate >= 56) {
state.adsr_attenuation_ = state.adsr_attenuation_ - (state.adsr_attenuation_ >> 2) - 1;
} else {
const int sample_length = 1 << (14 - (attack_rate >> 2)); // TODO: don't throw away KSR bits.
if(!(state.time_in_phase_ & (sample_length - 1))) {
state.adsr_attenuation_ = state.adsr_attenuation_ - (state.adsr_attenuation_ / 8) - 1;
}
}
// Two possible terminating conditions: (i) the attack rate is 15; (ii) full volume has been reached.
if(attack_rate > 60 || state.adsr_attenuation_ < 0) {
state.adsr_attenuation_ = 0;
state.adsr_phase_ = OperatorState::ADSRPhase::Decay;
}
} break;
case OperatorState::ADSRPhase::Decay:
case OperatorState::ADSRPhase::Release: {
// Rules:
//
// (relative to a 511 scale)
//
// A rate of 0 is no decay at all.
// A rate of 1 means increase 4 per cycle.
// A rate of 2 means increase 2 per cycle.
// A rate of 3 means increase 1 per cycle.
// A rate of 4 means increase 1 every other cycle.
// (etc)
const int decrease_rate = (state.adsr_phase_ == OperatorState::ADSRPhase::Decay) ? decay_rate_ : release_rate_; // TODO: again, key scaling rate.
if(decrease_rate) {
// TODO: don't throw away KSR bits.
switch(decrease_rate >> 2) {
case 1: state.adsr_attenuation_ += 4; break;
case 2: state.adsr_attenuation_ += 2; break;
default: {
const int sample_length = 1 << ((decrease_rate >> 2) - 3);
if(!(state.time_in_phase_ & (sample_length - 1))) {
++state.adsr_attenuation_;
}
} break;
}
}
// Clamp to the proper range.
state.adsr_attenuation_ = std::min(state.adsr_attenuation_, 511);
// Check for the decay exit condition.
if(state.adsr_phase_ == OperatorState::ADSRPhase::Decay && state.adsr_attenuation_ > (sustain_level_ << 5)) {
state.adsr_phase_ = hold_sustain_level ? OperatorState::ADSRPhase::Sustain : OperatorState::ADSRPhase::Release;
}
} break;
case OperatorState::ADSRPhase::Sustain:
// Nothing to do.
break;
}
if(state.adsr_phase_ == current_phase) {
++state.time_in_phase_;
} else {
state.time_in_phase_ = 0;
}
// TODO: calculate attenuation properly. Need to factor in channel attenuation, but presumably not through multiplication?
state.attenuation = state.adsr_attenuation_;
}

View File

@ -26,12 +26,19 @@ namespace OPL {
struct OperatorState {
public:
int phase = 0; // Will be in the range [0, 1023], mapping into a 1024-unit sine curve.
int volume = 0;
int attenuation = 511;
private:
int divider_ = 0;
int raw_phase_ = 0;
enum class ADSRPhase {
Attack, Decay, Sustain, Release
} adsr_phase_ = ADSRPhase::Attack;
int time_in_phase_ = 0;
int adsr_attenuation_ = 511;
bool last_key_on_ = false;
friend class Operator;
};
@ -41,7 +48,7 @@ struct OperatorState {
if the host is an OPLL or VRC7.
*/
struct OperatorOverrides {
int output_level = 0;
int attenuation = 0;
bool hold_sustain_level = false;
};
@ -66,20 +73,20 @@ class Operator {
public:
/// Sets this operator's attack rate as the top nibble of @c value, its decay rate as the bottom nibble.
void set_attack_decay(uint8_t value) {
attack_rate = (value & 0xf0) >> 2;
decay_rate = (value & 0x0f) << 2;
attack_rate_ = (value & 0xf0) >> 2;
decay_rate_ = (value & 0x0f) << 2;
}
/// Sets this operator's sustain level as the top nibble of @c value, its release rate as the bottom nibble.
void set_sustain_release(uint8_t value) {
sustain_level = (value & 0xf0) >> 2;
release_rate = (value & 0x0f) << 2;
sustain_level_ = (value & 0xf0) >> 4;
release_rate_ = (value & 0x0f) << 2;
}
/// Sets this operator's key scale level as the top two bits of @c value, its total output level as the low six bits.
void set_scaling_output(uint8_t value) {
scaling_level = value >> 6;
output_level = value & 0x3f;
attenuation_ = value & 0x3f;
}
/// Sets this operator's waveform using the low two bits of @c value.
@ -97,36 +104,15 @@ class Operator {
frequency_multiple = value & 0xf;
}
void update(OperatorState &state, int channel_frequency, int channel_octave, OperatorOverrides *overrides = nullptr) {
// Per the documentation:
//
// Delta phase = ( [desired freq] * 2^19 / [input clock / 72] ) / 2 ^ (b - 1)
//
// After experimentation, I think this gives rate calculation as formulated below.
void update(OperatorState &state, bool key_on, int channel_frequency, int channel_octave, OperatorOverrides *overrides = nullptr);
// This encodes the MUL -> multiple table given on page 12,
// multiplied by two.
constexpr int multipliers[] = {
1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
};
// Update the raw phase.
const int octave_divider = 32 << channel_octave;
state.divider_ %= octave_divider;
state.divider_ += multipliers[frequency_multiple] * channel_frequency;
state.raw_phase_ += state.divider_ / octave_divider;
// Hence calculate phase (TODO: by also taking account of vibrato).
constexpr int waveforms[4][4] = {
{1023, 1023, 1023, 1023}, // Sine: don't mask in any quadrant.
{511, 511, 0, 0}, // Half sine: keep the first half in tact, lock to 0 in the second half.
{511, 511, 511, 511}, // AbsSine: endlessly repeat the first half of the sine wave.
{255, 0, 255, 0}, // PulseSine: act as if the first quadrant is in the first and third; lock the other two to 0.
};
state.phase = state.raw_phase_ & waveforms[int(waveform)][(state.raw_phase_ >> 8) & 3];
// TODO: calculate output volume properly; apply: ADSR and amplitude modulation (tremolo, I assume?)
state.volume = output_level;
bool is_audible(OperatorState &state, OperatorOverrides *overrides = nullptr) {
if(overrides) {
if(overrides->attenuation == 0xf) return false;
} else {
if(attenuation_ == 0x3f) return false;
}
return state.adsr_attenuation_ != 511;
}
private:
@ -152,17 +138,17 @@ class Operator {
int frequency_multiple = 0;
/// Sets the current output level of this modulator, as an attenuation.
int output_level = 0;
int attenuation_ = 0;
/// Selects attenuation that is applied as a function of interval. Cf. p14.
int scaling_level = 0;
/// Sets the ADSR rates. These all provide the top four bits of a six-bit number;
/// the bottom two bits... are 'RL'?
int attack_rate = 0;
int decay_rate = 0;
int sustain_level = 0;
int release_rate = 0;
int attack_rate_ = 0;
int decay_rate_ = 0;
int sustain_level_ = 0;
int release_rate_ = 0;
/// Selects the generated waveform.
enum class Waveform {
@ -209,9 +195,9 @@ class Channel {
/// This should be called at a rate of around 49,716 Hz; it returns the current output level
/// level for this channel.
int update(Operator *modulator, Operator *carrier) {
modulator->update(modulator_state_, frequency << frequency_shift, octave);
carrier->update(carrier_state_, frequency << frequency_shift, octave);
int update(Operator *modulator, Operator *carrier, OperatorOverrides *modulator_overrides = nullptr, OperatorOverrides *carrier_overrides = nullptr) {
modulator->update(modulator_state_, key_on, frequency << frequency_shift, octave, modulator_overrides);
carrier->update(carrier_state_, key_on, frequency << frequency_shift, octave, carrier_overrides);
// TODO: almost everything else. This is a quick test.
if(!key_on) return 0;
@ -219,8 +205,8 @@ class Channel {
}
/// @returns @c true if this channel is currently producing any audio; @c false otherwise;
bool is_audible() {
return key_on; // TODO: this is a temporary hack in lieu of ADSR. Fix.
bool is_audible(Operator *carrier, OperatorOverrides *carrier_overrides = nullptr) {
return carrier->is_audible(carrier_state_, carrier_overrides);
}
private:
@ -324,13 +310,21 @@ struct OPLL: public OPLBase<OPLL> {
// three channels configured for rhythm generation.
struct Channel: public ::Yamaha::OPL::Channel {
int update() {
return Yamaha::OPL::Channel::update(modulator, modulator + 1, nullptr, &overrides);
}
bool is_audible() {
return Yamaha::OPL::Channel::is_audible(modulator + 1, &overrides);
}
Operator *modulator; // Implicitly, the carrier is modulator+1.
OperatorOverrides overrides;
int level = 0;
};
void update_all_chanels() {
for(int c = 0; c < 6; ++ c) { // Don't do anything with channels that might be percussion for now.
channels_[c].level = channels_[c].update(channels_[c].modulator, channels_[c].modulator + 1);
channels_[c].level = channels_[c].update();
}
}
Channel channels_[9];