From aa451427282d3052a86fa771e8c6311525ec0dde Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Tue, 14 Apr 2020 18:32:06 -0400 Subject: [PATCH] Endeavours to fix attenuation and add FM synthesis. I now definitely think my frequency counting is wrong. --- Components/OPL2/OPL2.cpp | 9 +++++---- Components/OPL2/OPL2.hpp | 21 +++++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Components/OPL2/OPL2.cpp b/Components/OPL2/OPL2.cpp index 912c98a8d..753ad9f7c 100644 --- a/Components/OPL2/OPL2.cpp +++ b/Components/OPL2/OPL2.cpp @@ -193,6 +193,7 @@ void OPLL::get_samples(std::size_t number_of_samples, std::int16_t *target) { } void OPLL::set_sample_volume_range(std::int16_t range) { + total_volume_ = range; } uint8_t OPLL::read(uint16_t address) { @@ -402,7 +403,7 @@ void Operator::update(OperatorState &state, bool key_on, int channel_frequency, }; // Update the raw phase. - const int octave_divider = 64 << channel_octave; + const int octave_divider = 128 << channel_octave; state.divider_ %= octave_divider; state.divider_ += multipliers[frequency_multiple] * channel_frequency; state.raw_phase_ += state.divider_ / octave_divider; @@ -509,11 +510,11 @@ void Operator::update(OperatorState &state, bool key_on, int channel_frequency, state.time_in_phase_ = 0; } - // TODO: probably there's no multiply here? + // Combine the ADSR attenuation and overall channel attenuation, clamping to the permitted range. if(overrides) { - state.attenuation = (state.adsr_attenuation_ * overrides->attenuation) >> 4; + state.attenuation = state.adsr_attenuation_ + (overrides->attenuation << 5); } else { - state.attenuation = (state.adsr_attenuation_ * attenuation_) >> 6; + state.attenuation = state.adsr_attenuation_ + (attenuation_ << 3); } } diff --git a/Components/OPL2/OPL2.hpp b/Components/OPL2/OPL2.hpp index b82f0abc1..65efe5e98 100644 --- a/Components/OPL2/OPL2.hpp +++ b/Components/OPL2/OPL2.hpp @@ -13,6 +13,7 @@ #include "../../Concurrency/AsyncTaskQueue.hpp" #include "../../Numeric/LFSR.hpp" +#include #include namespace Yamaha { @@ -25,8 +26,8 @@ namespace OPL { */ struct OperatorState { public: - int phase = 0; // Will be in the range [0, 1023], mapping into a 1024-unit sine curve. - int attenuation = 511; + int phase = 0; // Will be in the range [0, 1023], mapping into a 1024-unit sine curve. + int attenuation = 255; // Will be in the range [0, 1023]. private: int divider_ = 0; @@ -198,14 +199,16 @@ 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. - // 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); + // Specifically: use lookup tables. + const float carrier_volume = logf(float(carrier_state_.attenuation + 1)) / logf(1023.0); + const float modulator_volume = logf(float(modulator_state_.attenuation + 1)) / logf(1023.0); + + const float modulator_output = modulator_volume * sinf(float(modulator_state_.phase) / 1024.0f); + return int(carrier_volume * sinf(modulator_output + (float(carrier_state_.phase) / 1024.0f)) * 20000.0f); } /// @returns @c true if this channel is currently producing any audio; @c false otherwise; @@ -328,7 +331,7 @@ struct OPLL: public OPLBase { }; 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].level = (channels_[c].update() * total_volume_) >> 14; } } Channel channels_[9]; @@ -340,6 +343,8 @@ struct OPLL: public OPLBase { const int audio_divider_ = 1; int audio_offset_ = 0; + + std::atomic total_volume_; }; }