initial commit
This commit is contained in:
280
synth/tx_envelope.h
Normal file
280
synth/tx_envelope.h
Normal file
@@ -0,0 +1,280 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
|
||||
namespace trnr::lib::synth {
|
||||
|
||||
enum env_state {
|
||||
idle = 0,
|
||||
attack1,
|
||||
attack2,
|
||||
hold,
|
||||
decay1,
|
||||
decay2,
|
||||
sustain,
|
||||
release1,
|
||||
release2
|
||||
};
|
||||
|
||||
class tx_envelope {
|
||||
public:
|
||||
float attack1_rate;
|
||||
float attack1_level;
|
||||
float attack2_rate;
|
||||
float hold_rate;
|
||||
float decay1_rate;
|
||||
float decay1_level;
|
||||
float decay2_rate;
|
||||
float sustain_level;
|
||||
float release1_rate;
|
||||
float release1_level;
|
||||
float release2_rate;
|
||||
|
||||
tx_envelope(double _samplerate)
|
||||
: samplerate { _samplerate }
|
||||
, attack1_rate { 0 }
|
||||
, attack1_level { 0 }
|
||||
, attack2_rate { 0 }
|
||||
, hold_rate { 0 }
|
||||
, decay1_rate { 0 }
|
||||
, decay1_level { 0 }
|
||||
, decay2_rate { 0 }
|
||||
, sustain_level { 0 }
|
||||
, release1_rate { 0 }
|
||||
, release1_level { 0 }
|
||||
, release2_rate { 0 }
|
||||
, level { 0.f }
|
||||
, phase { 0 }
|
||||
, state { idle }
|
||||
, start_level { 0.f }
|
||||
, h1 { 0. }
|
||||
, h2 { 0. }
|
||||
, h3 { 0. }
|
||||
{
|
||||
}
|
||||
|
||||
float process_sample(bool gate, bool trigger) {
|
||||
|
||||
int attack_mid_x1 = ms_to_samples(attack1_rate);
|
||||
int attack_mid_x2 = ms_to_samples(attack2_rate);
|
||||
int hold_samp = ms_to_samples(hold_rate);
|
||||
int decay_mid_x1 = ms_to_samples(decay1_rate);
|
||||
int decay_mid_x2 = ms_to_samples(decay2_rate);
|
||||
int release_mid_x1 = ms_to_samples(release1_rate);
|
||||
int release_mid_x2 = ms_to_samples(release2_rate);
|
||||
|
||||
// if note on is triggered, transition to attack phase
|
||||
if (trigger) {
|
||||
start_level = level;
|
||||
phase = 0;
|
||||
state = attack1;
|
||||
}
|
||||
// attack 1st half
|
||||
if (state == attack1) {
|
||||
// while in attack phase
|
||||
if (phase < attack_mid_x1) {
|
||||
level = lerp(0, start_level, attack_mid_x1, attack1_level, phase);
|
||||
phase += 1;
|
||||
}
|
||||
// reset phase if parameter was changed
|
||||
if (phase > attack_mid_x1) {
|
||||
phase = attack_mid_x1;
|
||||
}
|
||||
// if attack phase is done, transition to decay phase
|
||||
if (phase == attack_mid_x1) {
|
||||
state = attack2;
|
||||
phase = 0;
|
||||
}
|
||||
}
|
||||
// attack 2nd half
|
||||
if (state == attack2) {
|
||||
// while in attack phase
|
||||
if (phase < attack_mid_x2) {
|
||||
level = lerp(0, attack1_level, attack_mid_x2, 1, phase);
|
||||
phase += 1;
|
||||
}
|
||||
// reset phase if parameter was changed
|
||||
if (phase > attack_mid_x2) {
|
||||
phase = attack_mid_x2;
|
||||
}
|
||||
// if attack phase is done, transition to decay phase
|
||||
if (phase == attack_mid_x2) {
|
||||
state = hold;
|
||||
phase = 0;
|
||||
}
|
||||
}
|
||||
// hold
|
||||
if (state == hold) {
|
||||
if (phase < hold_samp) {
|
||||
level = 1.0;
|
||||
phase += 1;
|
||||
}
|
||||
if (phase > hold_samp) {
|
||||
phase = hold_samp;
|
||||
}
|
||||
if (phase == hold_samp) {
|
||||
state = decay1;
|
||||
phase = 0;
|
||||
}
|
||||
}
|
||||
// decay 1st half
|
||||
if (state == decay1) {
|
||||
// while in decay phase
|
||||
if (phase < decay_mid_x1) {
|
||||
level = lerp(0, 1, decay_mid_x1, decay1_level, phase);
|
||||
phase += 1;
|
||||
}
|
||||
// reset phase if parameter was changed
|
||||
if (phase > decay_mid_x1) {
|
||||
phase = decay_mid_x1;
|
||||
}
|
||||
// if decay phase is done, transition to sustain phase
|
||||
if (phase == decay_mid_x1) {
|
||||
state = decay2;
|
||||
phase = 0;
|
||||
}
|
||||
}
|
||||
// decay 2nd half
|
||||
if (state == decay2) {
|
||||
// while in decay phase
|
||||
if (phase < decay_mid_x2) {
|
||||
level = lerp(0, decay1_level, decay_mid_x2, sustain_level, phase);
|
||||
phase += 1;
|
||||
}
|
||||
// reset phase if parameter was changed
|
||||
if (phase > decay_mid_x2) {
|
||||
phase = decay_mid_x2;
|
||||
}
|
||||
// if decay phase is done, transition to sustain phase
|
||||
if (phase == decay_mid_x2) {
|
||||
state = sustain;
|
||||
phase = 0;
|
||||
level = sustain_level;
|
||||
}
|
||||
}
|
||||
// while sustain phase: if note off is triggered, transition to release phase
|
||||
if (state == sustain && !gate) {
|
||||
state = release1;
|
||||
level = sustain_level;
|
||||
}
|
||||
// release 1st half
|
||||
if (state == release1) {
|
||||
// while in release phase
|
||||
if (phase < release_mid_x1) {
|
||||
level = lerp(0, sustain_level, release_mid_x1, release1_level, phase);
|
||||
phase += 1;
|
||||
}
|
||||
// reset phase if parameter was changed
|
||||
if (phase > release_mid_x1) {
|
||||
phase = release_mid_x1;
|
||||
}
|
||||
// transition to 2nd release half
|
||||
if (phase == release_mid_x1) {
|
||||
phase = 0;
|
||||
state = release2;
|
||||
}
|
||||
}
|
||||
// release 2nd half
|
||||
if (state == release2) {
|
||||
// while in release phase
|
||||
if (phase < release_mid_x2) {
|
||||
level = lerp(0, release1_level, release_mid_x2, 0, phase);
|
||||
phase += 1;
|
||||
}
|
||||
// reset phase if parameter was changed
|
||||
if (phase > release_mid_x2) {
|
||||
phase = release_mid_x2;
|
||||
}
|
||||
// reset
|
||||
if (phase == release_mid_x2) {
|
||||
phase = 0;
|
||||
state = idle;
|
||||
level = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return smooth(level);
|
||||
}
|
||||
|
||||
bool is_busy() { return state != 0; }
|
||||
|
||||
void set_samplerate(double sampleRate) {
|
||||
this->samplerate = sampleRate;
|
||||
}
|
||||
|
||||
// returns the x/y coordinates of the envelope points as a list for graphical representation.
|
||||
std::array<float, 18> calc_coordinates() {
|
||||
|
||||
float a_x = 0;
|
||||
float a_y = 0;
|
||||
|
||||
float b_x = attack1_rate;
|
||||
float b_y = attack1_level;
|
||||
|
||||
float c_x = b_x + attack2_rate;
|
||||
float c_y = 1;
|
||||
|
||||
float d_x = c_x + hold_rate;
|
||||
float d_y = 1;
|
||||
|
||||
float e_x = d_x + decay1_rate;
|
||||
float e_y = decay1_level;
|
||||
|
||||
float f_x = e_x + decay2_rate;
|
||||
float f_y = sustain_level;
|
||||
|
||||
float g_x = f_x + 125;
|
||||
float g_y = sustain_level;
|
||||
|
||||
float h_x = g_x + release1_rate;
|
||||
float h_y = release1_level;
|
||||
|
||||
float i_x = h_x + release2_rate;
|
||||
float i_y = 0;
|
||||
|
||||
float total = i_x;
|
||||
|
||||
return {
|
||||
a_x,
|
||||
a_y,
|
||||
b_x / total,
|
||||
b_y,
|
||||
c_x / total,
|
||||
c_y,
|
||||
d_x / total,
|
||||
d_y,
|
||||
e_x / total,
|
||||
e_y,
|
||||
f_x / total,
|
||||
f_y,
|
||||
g_x / total,
|
||||
g_y,
|
||||
h_x / total,
|
||||
h_y,
|
||||
i_x / total,
|
||||
i_y
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
double samplerate;
|
||||
int phase;
|
||||
float level;
|
||||
env_state state;
|
||||
float start_level;
|
||||
float h1;
|
||||
float h2;
|
||||
float h3;
|
||||
|
||||
float lerp(float x1, float y1, float x2, float y2, float x) { return y1 + (((x - x1) * (y2 - y1)) / (x2 - x1)); }
|
||||
|
||||
float smooth(float sample) {
|
||||
h3 = h2;
|
||||
h2 = h1;
|
||||
h1 = sample;
|
||||
|
||||
return (h1 + h2 + h3) / 3.f;
|
||||
}
|
||||
|
||||
float ms_to_samples(float ms) { return ms * samplerate / 1000.f; }
|
||||
};
|
||||
}
|
||||
39
synth/tx_operator.h
Normal file
39
synth/tx_operator.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include "tx_sineosc.h"
|
||||
#include "tx_envelope.h"
|
||||
|
||||
namespace trnr::lib::synth {
|
||||
class tx_operator {
|
||||
public:
|
||||
tx_operator(double samplerate)
|
||||
: ratio { 1 }
|
||||
, amplitude { 1.0f }
|
||||
, envelope(samplerate)
|
||||
, oscillator(samplerate)
|
||||
{
|
||||
}
|
||||
|
||||
tx_envelope envelope;
|
||||
tx_sineosc oscillator;
|
||||
float ratio;
|
||||
float amplitude;
|
||||
|
||||
float process_sample(const bool& gate, const bool& trigger, const float& frequency, const float& velocity, const float& pm = 0) {
|
||||
|
||||
float env = envelope.process_sample(gate, trigger);
|
||||
|
||||
// drifts and sounds better!
|
||||
if (envelope.is_busy()) {
|
||||
double osc = oscillator.process_sample(trigger, frequency, pm);
|
||||
return osc * env * velocity;
|
||||
} else {
|
||||
return 0.;
|
||||
}
|
||||
}
|
||||
|
||||
void set_samplerate(double samplerate) {
|
||||
this->envelope.set_samplerate(samplerate);
|
||||
this->oscillator.set_samplerate(samplerate);
|
||||
}
|
||||
};
|
||||
}
|
||||
95
synth/tx_sineosc.h
Normal file
95
synth/tx_sineosc.h
Normal file
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
#include <cmath>
|
||||
|
||||
namespace trnr::lib::synth {
|
||||
|
||||
class tx_sineosc {
|
||||
public:
|
||||
bool phase_reset;
|
||||
|
||||
tx_sineosc(double _samplerate)
|
||||
: samplerate { _samplerate }
|
||||
, phase_resolution { 16.f }
|
||||
, phase { 0. }
|
||||
, history { 0. }
|
||||
, phase_reset { false }
|
||||
{
|
||||
}
|
||||
|
||||
void set_phase_resolution(float res) {
|
||||
phase_resolution = powf(2, res);
|
||||
}
|
||||
|
||||
float process_sample(bool trigger, float frequency, float phase_modulation = 0.f) {
|
||||
if (trigger && phase_reset) {
|
||||
phase = 0.0;
|
||||
}
|
||||
|
||||
float lookup_phase = phase + phase_modulation;
|
||||
wrap(lookup_phase);
|
||||
phase += frequency / samplerate;
|
||||
wrap(phase);
|
||||
|
||||
redux(lookup_phase);
|
||||
|
||||
float output = sine(lookup_phase * 4096.);
|
||||
|
||||
filter(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
void set_samplerate(double _samplerate) {
|
||||
this->samplerate = _samplerate;
|
||||
}
|
||||
|
||||
private:
|
||||
double samplerate;
|
||||
float phase_resolution;
|
||||
float phase;
|
||||
float history;
|
||||
|
||||
float sine(float x) {
|
||||
// x is scaled 0<=x<4096
|
||||
const float a = -0.40319426317E-08;
|
||||
const float b = 0.21683205691E+03;
|
||||
const float c = 0.28463350538E-04;
|
||||
const float d = -0.30774648337E-02;
|
||||
float y;
|
||||
|
||||
bool negate = false;
|
||||
if (x > 2048) {
|
||||
negate = true;
|
||||
x -= 2048;
|
||||
}
|
||||
if (x > 1024)
|
||||
x = 2048 - x;
|
||||
y = (a + x) / (b + c * x * x) + d * x;
|
||||
if (negate)
|
||||
return (float)(-y);
|
||||
else
|
||||
return (float)y;
|
||||
}
|
||||
|
||||
float wrap(float& phase) {
|
||||
while (phase < 0.)
|
||||
phase += 1.;
|
||||
|
||||
while (phase >= 1.)
|
||||
phase -= 1.;
|
||||
|
||||
return phase;
|
||||
}
|
||||
|
||||
float filter(float& value) {
|
||||
value = 0.5 * (value + history);
|
||||
history = value;
|
||||
return value;
|
||||
}
|
||||
|
||||
float redux(float& value)
|
||||
{
|
||||
value = static_cast<int>(value * phase_resolution) / phase_resolution;
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
166
synth/tx_voice.h
Normal file
166
synth/tx_voice.h
Normal file
@@ -0,0 +1,166 @@
|
||||
#pragma once
|
||||
#include "tx_sineosc.h"
|
||||
#include "tx_envelope.h"
|
||||
#include "tx_operator.h"
|
||||
|
||||
namespace trnr::lib::synth {
|
||||
|
||||
class tx_voice {
|
||||
public:
|
||||
tx_voice(double samplerate)
|
||||
: algorithm { 0 }
|
||||
, pitch_env_amt { 0.f }
|
||||
, feedback_amt { 0.f }
|
||||
, pitch_env(samplerate)
|
||||
, feedback_osc(samplerate)
|
||||
, op1(samplerate)
|
||||
, op2(samplerate)
|
||||
, op3(samplerate)
|
||||
, bit_resolution(12.f)
|
||||
{
|
||||
}
|
||||
|
||||
bool gate = false;
|
||||
bool trigger = false;
|
||||
float frequency = 100.f;
|
||||
float velocity = 1.f;
|
||||
|
||||
int algorithm;
|
||||
float pitch_env_amt;
|
||||
float feedback_amt;
|
||||
float bit_resolution;
|
||||
tx_sineosc feedback_osc;
|
||||
tx_envelope pitch_env;
|
||||
tx_operator op1;
|
||||
tx_operator op2;
|
||||
tx_operator op3;
|
||||
|
||||
float process_sample() {
|
||||
float pitch_env_signal = pitch_env.process_sample(gate, trigger) * pitch_env_amt;
|
||||
float pitched_freq = frequency + pitch_env_signal;
|
||||
|
||||
float output = 0.f;
|
||||
|
||||
// mix operator signals according to selected algorithm
|
||||
switch (algorithm) {
|
||||
case 0:
|
||||
output = calc_algo1(pitched_freq);
|
||||
break;
|
||||
case 1:
|
||||
output = calc_algo2(pitched_freq);
|
||||
break;
|
||||
case 2:
|
||||
output = calc_algo3(pitched_freq);
|
||||
break;
|
||||
case 3:
|
||||
output = calc_algo4(pitched_freq);
|
||||
break;
|
||||
default:
|
||||
output = calc_algo1(pitched_freq);
|
||||
break;
|
||||
}
|
||||
|
||||
// reset trigger
|
||||
trigger = false;
|
||||
|
||||
return redux(output, bit_resolution);
|
||||
}
|
||||
|
||||
bool is_busy() { return gate || op1.envelope.is_busy() || op2.envelope.is_busy() || op3.envelope.is_busy(); }
|
||||
|
||||
void set_samplerate(double samplerate) {
|
||||
pitch_env.set_samplerate(samplerate);
|
||||
feedback_osc.set_samplerate(samplerate);
|
||||
op1.set_samplerate(samplerate);
|
||||
op2.set_samplerate(samplerate);
|
||||
op3.set_samplerate(samplerate);
|
||||
}
|
||||
|
||||
void set_phase_reset(bool phase_reset) {
|
||||
op1.oscillator.phase_reset = phase_reset;
|
||||
op2.oscillator.phase_reset = phase_reset;
|
||||
op3.oscillator.phase_reset = phase_reset;
|
||||
feedback_osc.phase_reset = phase_reset;
|
||||
}
|
||||
|
||||
private:
|
||||
const float MOD_INDEX_COEFF = 4.f;
|
||||
|
||||
float calc_algo1(const float frequency) {
|
||||
float fb_freq = frequency * op3.ratio;
|
||||
float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF);
|
||||
float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index;
|
||||
|
||||
float op3_Freq = frequency * op3.ratio;
|
||||
float op3_mod_index = (op3.amplitude * MOD_INDEX_COEFF);
|
||||
float op3_signal = op3.process_sample(gate, trigger, op3_Freq, velocity, fb_signal) * op3_mod_index;
|
||||
|
||||
float op2_freq = frequency * op2.ratio;
|
||||
float op2_mod_index = (op2.amplitude * MOD_INDEX_COEFF);
|
||||
float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity, op3_signal) * op2_mod_index;
|
||||
|
||||
float op1_freq = frequency * op1.ratio;
|
||||
return op1.process_sample(gate, trigger, op1_freq, velocity, op2_signal) * op1.amplitude;
|
||||
}
|
||||
|
||||
float calc_algo2(const float frequency) {
|
||||
float fb_freq = frequency * op3.ratio;
|
||||
float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF);
|
||||
float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index;
|
||||
|
||||
float op3_freq = frequency * op3.ratio;
|
||||
float op3_signal = op3.process_sample(gate, trigger, op3_freq, velocity, fb_signal) * op3.amplitude;
|
||||
|
||||
float op2_freq = frequency * op2.ratio;
|
||||
float op2_mod_index = (op2.amplitude * MOD_INDEX_COEFF);
|
||||
float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity) * op2_mod_index;
|
||||
|
||||
float op1_freq = frequency * op1.ratio;
|
||||
float op1_signal = op1.process_sample(gate, trigger, op1_freq, velocity, op2_signal) * op1.amplitude;
|
||||
|
||||
return op1_signal + op3_signal;
|
||||
}
|
||||
|
||||
float calc_algo3(const float frequency) {
|
||||
float fb_freq = frequency * op3.ratio;
|
||||
float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF);
|
||||
float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index;
|
||||
|
||||
float op3_freq = frequency * op3.ratio;
|
||||
float op3_signal = op3.process_sample(gate, trigger, op3_freq, velocity, fb_signal) * op3.amplitude;
|
||||
|
||||
float op2_freq = frequency * op2.ratio;
|
||||
float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity) * op2.amplitude;
|
||||
|
||||
float op1_freq = frequency * op1.ratio;
|
||||
float op1_signal = op1.process_sample(gate, trigger, op1_freq, velocity) * op1.amplitude;
|
||||
|
||||
return op1_signal + op2_signal + op3_signal;
|
||||
}
|
||||
|
||||
float calc_algo4(const float frequency) {
|
||||
float fb_freq = frequency * op3.ratio;
|
||||
float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF);
|
||||
float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index;
|
||||
|
||||
float op3_freq = frequency * op3.ratio;
|
||||
float op3_mod_index = (op3.amplitude * MOD_INDEX_COEFF);
|
||||
float op3_signal = op3.process_sample(gate, trigger, op3_freq, velocity, fb_signal) * op3_mod_index;
|
||||
|
||||
float op2_freq = frequency * op2.ratio;
|
||||
float op2_mod_index = (op2.amplitude * MOD_INDEX_COEFF);
|
||||
float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity) * op2_mod_index;
|
||||
|
||||
float op1_freq = frequency * op1.ratio;
|
||||
return op1.process_sample(gate, trigger, op1_freq, velocity, op2_signal + op3_signal) * op1.amplitude;
|
||||
}
|
||||
|
||||
float redux(float& value, float resolution)
|
||||
{
|
||||
float res = powf(2, resolution);
|
||||
value = roundf(value * res) / res;
|
||||
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user