diff --git a/Components/OPL2/OPL2.cpp b/Components/OPL2/OPL2.cpp index d0b0b6b72..912c98a8d 100644 --- a/Components/OPL2/OPL2.cpp +++ b/Components/OPL2/OPL2.cpp @@ -141,10 +141,10 @@ OPLL::OPLL(Concurrency::DeferringAsyncTaskQueue &task_queue, int audio_divider, } bool OPLL::is_zero_level() { - for(int c = 0; c < 9; ++c) { - if(channels_[c].is_audible()) return false; - } - return true; +// for(int c = 0; c < 9; ++c) { +// if(channels_[c].is_audible()) return false; +// } + return false; } void OPLL::get_samples(std::size_t number_of_samples, std::int16_t *target) { @@ -402,7 +402,7 @@ void Operator::update(OperatorState &state, bool key_on, int channel_frequency, }; // Update the raw phase. - const int octave_divider = 32 << channel_octave; + const int octave_divider = 64 << channel_octave; state.divider_ %= octave_divider; state.divider_ += multipliers[frequency_multiple] * channel_frequency; state.raw_phase_ += state.divider_ / octave_divider; @@ -449,7 +449,7 @@ void Operator::update(OperatorState &state, bool key_on, int channel_frequency, } 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; + state.adsr_attenuation_ = state.adsr_attenuation_ - (state.adsr_attenuation_ >> 3) - 1; } } @@ -460,8 +460,9 @@ void Operator::update(OperatorState &state, bool key_on, int channel_frequency, } } break; + case OperatorState::ADSRPhase::Release: case OperatorState::ADSRPhase::Decay: - case OperatorState::ADSRPhase::Release: { + { // Rules: // // (relative to a 511 scale) @@ -480,7 +481,7 @@ void Operator::update(OperatorState &state, bool key_on, int channel_frequency, case 1: state.adsr_attenuation_ += 4; break; case 2: state.adsr_attenuation_ += 2; break; default: { - const int sample_length = 1 << ((decrease_rate >> 2) - 3); + const int sample_length = 1 << ((decrease_rate >> 2) - 4); if(!(state.time_in_phase_ & (sample_length - 1))) { ++state.adsr_attenuation_; } @@ -492,8 +493,9 @@ void Operator::update(OperatorState &state, bool key_on, int channel_frequency, 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; + if(state.adsr_phase_ == OperatorState::ADSRPhase::Decay && state.adsr_attenuation_ >= (sustain_level_ << 5)) { + state.adsr_attenuation_ = sustain_level_ << 5; + state.adsr_phase_ = ((overrides && overrides->hold_sustain_level) || hold_sustain_level) ? OperatorState::ADSRPhase::Sustain : OperatorState::ADSRPhase::Release; } } break; @@ -507,7 +509,11 @@ void Operator::update(OperatorState &state, bool key_on, int channel_frequency, 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_; + // TODO: probably there's no multiply here? + if(overrides) { + state.attenuation = (state.adsr_attenuation_ * overrides->attenuation) >> 4; + } else { + state.attenuation = (state.adsr_attenuation_ * attenuation_) >> 6; + } } diff --git a/Components/OPL2/OPL2.hpp b/Components/OPL2/OPL2.hpp index 943dec57e..b82f0abc1 100644 --- a/Components/OPL2/OPL2.hpp +++ b/Components/OPL2/OPL2.hpp @@ -107,10 +107,12 @@ class Operator { void update(OperatorState &state, bool key_on, int channel_frequency, int channel_octave, OperatorOverrides *overrides = nullptr); bool is_audible(OperatorState &state, OperatorOverrides *overrides = nullptr) { - if(overrides) { - if(overrides->attenuation == 0xf) return false; - } else { - if(attenuation_ == 0x3f) return false; + if(state.adsr_phase_ == OperatorState::ADSRPhase::Release) { + if(overrides) { + if(overrides->attenuation == 0xf) return false; + } else { + if(attenuation_ == 0x3f) return false; + } } return state.adsr_attenuation_ != 511; } @@ -196,12 +198,14 @@ 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, OperatorOverrides *modulator_overrides = nullptr, OperatorOverrides *carrier_overrides = nullptr) { - modulator->update(modulator_state_, key_on, frequency << frequency_shift, octave, modulator_overrides); +// 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; - return int(sin(float(carrier_state_.phase) / 1024.0) * 20000.0); + // Specifically: use lookup tables, apply attenuation properly. + if(carrier_state_.attenuation == 511) return 0; + const float volume = 1.0f - float(carrier_state_.attenuation) / 511.0f; + return int(volume * sin(float(carrier_state_.phase) / 1024.0) * 20000.0); } /// @returns @c true if this channel is currently producing any audio; @c false otherwise;