1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-04 18:29:40 +00:00

Attempts to use table-based maths.

This commit is contained in:
Thomas Harte 2020-04-17 23:23:16 -04:00
parent 4a295cd95e
commit 3948304172
3 changed files with 8 additions and 13 deletions

View File

@ -7,6 +7,7 @@
// //
#include "Channel.hpp" #include "Channel.hpp"
#include "Tables.h"
using namespace Yamaha::OPL; using namespace Yamaha::OPL;
@ -39,19 +40,15 @@ int Channel::update(Operator *modulator, Operator *carrier, OperatorOverrides *m
// TODO: almost everything. This is a quick test. // TODO: almost everything. This is a quick test.
// Specifically: use lookup tables. // Specifically: use lookup tables.
const auto modulator_level = level(modulator_state_, 0.0f); // TODO: what's the proper scaling on this? const auto modulator_level = level(modulator_state_, 0);
return int(level(carrier_state_, modulator_level) * 20'000.0f); return level(carrier_state_, modulator_level);
} }
bool Channel::is_audible(Operator *carrier, OperatorOverrides *carrier_overrides) { bool Channel::is_audible(Operator *carrier, OperatorOverrides *carrier_overrides) {
return carrier->is_audible(carrier_state_, carrier_overrides); return carrier->is_audible(carrier_state_, carrier_overrides);
} }
float Channel::level(OperatorState &state, float modulator_level) { int Channel::level(OperatorState &state, int modulator_level) {
const float phase = modulator_level + float(state.phase) / 1024.0f; const auto log_sin = negative_log_sin(modulator_level + state.phase);
const float phase_attenuation = logf(1.0f + sinf(float(M_PI) * 2.0f * phase)); return power_two(log_sin.logsin + state.attenuation) * log_sin.sign;
const float total_attenuation = phase_attenuation + float(state.attenuation) / 1023.0f;
const float result = expf(total_attenuation / 2.0f);
return result;
} }

View File

@ -11,8 +11,6 @@
#include "Operator.hpp" #include "Operator.hpp"
#include <cmath>
namespace Yamaha { namespace Yamaha {
namespace OPL { namespace OPL {
@ -48,7 +46,7 @@ class Channel {
bool is_audible(Operator *carrier, OperatorOverrides *carrier_overrides = nullptr); bool is_audible(Operator *carrier, OperatorOverrides *carrier_overrides = nullptr);
private: private:
float level(OperatorState &state, float modulator_level); int level(OperatorState &state, int modulator_level);
/// 'F-Num' in the spec; this plus the current octave determines channel frequency. /// 'F-Num' in the spec; this plus the current octave determines channel frequency.
int period_ = 0; int period_ = 0;

View File

@ -109,7 +109,7 @@ struct OPLL: public OPLBase<OPLL> {
for(int c = 0; c < 6; ++ c) { // Don't do anything with channels that might be percussion for now. 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() * total_volume_) >> 14; channels_[c].level = (channels_[c].update() * total_volume_) >> 14;
} }
channels_[2].level = (channels_[2].update() * total_volume_) >> 14; // channels_[2].level = (channels_[2].update() * total_volume_) >> 14;
} }
Channel channels_[9]; Channel channels_[9];