From bdce1c464a85e08777862bdac04fcc7062159e76 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Tue, 21 Apr 2020 00:09:42 -0400 Subject: [PATCH] Takes a shot at key-level scaling. Testing to come. --- Components/OPL2/Implementation/Operator.cpp | 51 ++++++++++++++++++++- Components/OPL2/Implementation/Operator.hpp | 2 +- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Components/OPL2/Implementation/Operator.cpp b/Components/OPL2/Implementation/Operator.cpp index d75aca14d..53c693dcf 100644 --- a/Components/OPL2/Implementation/Operator.cpp +++ b/Components/OPL2/Implementation/Operator.cpp @@ -27,7 +27,7 @@ void Operator::set_sustain_release(uint8_t value) { } void Operator::set_scaling_output(uint8_t value) { - level_key_scaling_ = value >> 6; + key_level_scaling_ = value >> 6; attenuation_ = value & 0x3f; } @@ -174,6 +174,55 @@ void Operator::update(OperatorState &state, bool key_on, int channel_period, int state.time_in_phase_ = 0; } + // Calculate key-level scaling. Table is as per p14 of the YM3812 application manual, + // converted into a fixed-point scheme. Compare with https://www.smspower.org/Development/RE12 + // and apologies for the highly ad hoc indentation. + constexpr int key_level_scales[4][8][16] = { +#define _ 0 + { + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + }, + { + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + {_, _, _, _, _, _, _, _, _, 1, 1, 2, 2, 3, 3, 4}, + {_, _, _, _, _, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 8}, + {_, _, _, 2, 4, 5, 6, 7, 8, 9, 9, 10, 10, 11, 11, 12}, + {_, _, 4, 6, 8, 9, 10, 11, 12, 13, 13, 14, 14, 15, 15, 16}, + {_, 4, 8, 10, 12, 13, 14, 15, 16, 17, 17, 18, 18, 19, 19, 20}, + {_, 8, 12, 14, 16, 17, 18, 19, 20, 21, 21, 22, 22, 23, 23, 24}, + {_, 12, 16, 18, 20, 21, 22, 23, 24, 25, 25, 26, 26, 27, 27, 28}, + }, + { + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + {_, _, _, _, _, _, _, _, _, 2, 3, 4, 5, 6, 7, 8}, + {_, _, _, _, _, 3, 5, 7, 8, 10, 11, 12, 13, 14, 15, 16}, + {_, _, _, 5, 8, 11, 13, 15, 16, 18, 19, 20, 21, 22, 23, 24}, + {_, _, 8, 13, 16, 19, 21, 23, 24, 26, 27, 28, 29, 30, 31, 32}, + {_, 8, 16, 21, 24, 27, 29, 31, 32, 34, 35, 36, 37, 38, 39, 40}, + {_, 16, 24, 29, 32, 35, 37, 39, 40, 42, 43, 44, 45, 46, 47, 48}, + {_, 24, 32, 37, 40, 43, 45, 47, 48, 50, 51, 52, 53, 54, 55, 56}, + }, + { + {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}, + {_, _, _, _, _, _, _, _, _, 4, 6, 8, 10, 12, 14, 16}, + {_, _, _, _, _, 6, 10, 14, 16, 20, 22, 24, 26, 28, 30, 32}, + {_, _, _, 10, 16, 22, 26, 30, 32, 36, 38, 40, 42, 44, 46, 48}, + {_, _, 16, 26, 32, 38, 42, 46, 48, 52, 54, 56, 58, 60, 62, 64}, + {_, 16, 32, 42, 48, 54, 58, 62, 64, 68, 70, 72, 74, 76, 78, 80}, + {_, 32, 48, 58, 64, 70, 74, 78, 80, 84, 86, 88, 90, 92, 94, 96}, + {_, 48, 64, 74, 80, 86, 90, 94, 96, 100, 102, 104, 106, 108, 110, 112}, + }, +#undef _ + }; + state.attenuation.log += key_level_scales[key_level_scaling_][channel_octave][channel_period >> 6] << 7; + // Combine the ADSR attenuation and overall channel attenuation. if(overrides) { // Overrides here represent per-channel volume on an OPLL. The bits are defined to represent diff --git a/Components/OPL2/Implementation/Operator.hpp b/Components/OPL2/Implementation/Operator.hpp index 0ec73a40e..22cb4f8c2 100644 --- a/Components/OPL2/Implementation/Operator.hpp +++ b/Components/OPL2/Implementation/Operator.hpp @@ -114,7 +114,7 @@ class Operator { bool key_scaling_rate_ = false; /// Selects attenuation that is applied as a function of interval. Cf. p14. - int level_key_scaling_ = 0; + int key_level_scaling_ = 0; /// Sets the ADSR rates. These all provide the top four bits of a six-bit number; /// the bottom two bits... are 'RL'?