clang format
This commit is contained in:
@@ -2,24 +2,24 @@
|
||||
|
||||
namespace trnr {
|
||||
struct ivoice {
|
||||
virtual ~ivoice() = default;
|
||||
virtual float process_sample() = 0;
|
||||
virtual bool is_busy() = 0;
|
||||
virtual void set_samplerate(double samplerate) = 0;
|
||||
virtual void note_on(int _note, float _velocity) = 0;
|
||||
virtual void note_off() = 0;
|
||||
virtual void modulate_pitch(float _pitch) = 0;
|
||||
virtual ~ivoice() = default;
|
||||
virtual float process_sample() = 0;
|
||||
virtual bool is_busy() = 0;
|
||||
virtual void set_samplerate(double samplerate) = 0;
|
||||
virtual void note_on(int _note, float _velocity) = 0;
|
||||
virtual void note_off() = 0;
|
||||
virtual void modulate_pitch(float _pitch) = 0;
|
||||
};
|
||||
|
||||
// check if a template derives from ivoice
|
||||
template <class derived>
|
||||
struct is_convertible {
|
||||
template <class T>
|
||||
static char test(T*);
|
||||
template <class T>
|
||||
static char test(T*);
|
||||
|
||||
template <class T>
|
||||
static double test(...);
|
||||
template <class T>
|
||||
static double test(...);
|
||||
|
||||
static const bool value = sizeof(test<ivoice>(static_cast<derived*>(0))) == 1;
|
||||
static const bool value = sizeof(test<ivoice>(static_cast<derived*>(0))) == 1;
|
||||
};
|
||||
}
|
||||
} // namespace trnr
|
||||
@@ -3,46 +3,46 @@
|
||||
namespace trnr {
|
||||
|
||||
enum midi_event_type {
|
||||
note_on = 0,
|
||||
note_off,
|
||||
pitch_wheel,
|
||||
mod_wheel
|
||||
note_on = 0,
|
||||
note_off,
|
||||
pitch_wheel,
|
||||
mod_wheel
|
||||
};
|
||||
|
||||
class midi_event {
|
||||
public:
|
||||
midi_event_type type;
|
||||
int offset = 0;
|
||||
int midi_note = 0;
|
||||
float velocity = 1.f;
|
||||
double data = 0;
|
||||
midi_event_type type;
|
||||
int offset = 0;
|
||||
int midi_note = 0;
|
||||
float velocity = 1.f;
|
||||
double data = 0;
|
||||
|
||||
void make_note_on(int _midi_note, float _velocity, int _offset = 0)
|
||||
{
|
||||
type = midi_event_type::note_on;
|
||||
midi_note = _midi_note;
|
||||
velocity = _velocity;
|
||||
offset = _offset;
|
||||
}
|
||||
void make_note_on(int _midi_note, float _velocity, int _offset = 0)
|
||||
{
|
||||
type = midi_event_type::note_on;
|
||||
midi_note = _midi_note;
|
||||
velocity = _velocity;
|
||||
offset = _offset;
|
||||
}
|
||||
|
||||
void make_note_off(int _midi_note, float _velocity, int _offset = 0)
|
||||
{
|
||||
type = midi_event_type::note_off;
|
||||
midi_note = _midi_note;
|
||||
velocity = _velocity;
|
||||
offset = _offset;
|
||||
}
|
||||
void make_note_off(int _midi_note, float _velocity, int _offset = 0)
|
||||
{
|
||||
type = midi_event_type::note_off;
|
||||
midi_note = _midi_note;
|
||||
velocity = _velocity;
|
||||
offset = _offset;
|
||||
}
|
||||
|
||||
void make_pitch_wheel(double _pitch, int _offset = 0)
|
||||
{
|
||||
type = midi_event_type::pitch_wheel;
|
||||
data = _pitch;
|
||||
}
|
||||
void make_pitch_wheel(double _pitch, int _offset = 0)
|
||||
{
|
||||
type = midi_event_type::pitch_wheel;
|
||||
data = _pitch;
|
||||
}
|
||||
|
||||
void make_mod_wheel(double _mod, int _offset = 0)
|
||||
{
|
||||
type = midi_event_type::pitch_wheel;
|
||||
data = _mod;
|
||||
}
|
||||
void make_mod_wheel(double _mod, int _offset = 0)
|
||||
{
|
||||
type = midi_event_type::pitch_wheel;
|
||||
data = _mod;
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace trnr
|
||||
|
||||
@@ -12,82 +12,78 @@ namespace trnr {
|
||||
template <typename t_voice, typename t_sample>
|
||||
class midi_synth : public voice_allocator<t_voice, t_sample> {
|
||||
public:
|
||||
midi_synth(int _n_voices)
|
||||
: m_voices_active { false }
|
||||
{
|
||||
// checks whether template derives from ivoice
|
||||
typedef t_voice assert_at_compile_time[is_convertible<t_voice>::value ? 1 : -1];
|
||||
}
|
||||
midi_synth(int _n_voices)
|
||||
: m_voices_active {false}
|
||||
{
|
||||
// checks whether template derives from ivoice
|
||||
typedef t_voice assert_at_compile_time[is_convertible<t_voice>::value ? 1 : -1];
|
||||
}
|
||||
|
||||
void set_samplerate_blocksize(double _samplerate, int _block_size)
|
||||
{
|
||||
m_block_size = _block_size;
|
||||
voice_allocator<t_voice, t_sample>::set_samplerate(_samplerate);
|
||||
}
|
||||
void set_samplerate_blocksize(double _samplerate, int _block_size)
|
||||
{
|
||||
m_block_size = _block_size;
|
||||
voice_allocator<t_voice, t_sample>::set_samplerate(_samplerate);
|
||||
}
|
||||
|
||||
void process_block(t_sample** _outputs, int _n_frames)
|
||||
{
|
||||
// sample accurate event handling based on the iPlug2 synth by Oli Larkin
|
||||
if (m_voices_active || !m_event_queue.empty()) {
|
||||
int block_size = m_block_size;
|
||||
int samples_remaining = _n_frames;
|
||||
int start_index = 0;
|
||||
void process_block(t_sample** _outputs, int _n_frames)
|
||||
{
|
||||
// sample accurate event handling based on the iPlug2 synth by Oli Larkin
|
||||
if (m_voices_active || !m_event_queue.empty()) {
|
||||
int block_size = m_block_size;
|
||||
int samples_remaining = _n_frames;
|
||||
int start_index = 0;
|
||||
|
||||
while (samples_remaining > 0) {
|
||||
while (samples_remaining > 0) {
|
||||
|
||||
if (samples_remaining < block_size)
|
||||
block_size = samples_remaining;
|
||||
if (samples_remaining < block_size) block_size = samples_remaining;
|
||||
|
||||
while (!m_event_queue.empty()) {
|
||||
midi_event event = m_event_queue.front();
|
||||
while (!m_event_queue.empty()) {
|
||||
midi_event event = m_event_queue.front();
|
||||
|
||||
// we assume the messages are in chronological order. If we find one later than the current block we are done.
|
||||
if (event.offset > start_index + block_size)
|
||||
break;
|
||||
// we assume the messages are in chronological order. If we find one later than the current block we
|
||||
// are done.
|
||||
if (event.offset > start_index + block_size) break;
|
||||
|
||||
// send performance messages to the voice allocator
|
||||
// message offset is relative to the start of this process_samples() block
|
||||
event.offset -= start_index;
|
||||
voice_allocator<t_voice, t_sample>::add_event(event);
|
||||
// send performance messages to the voice allocator
|
||||
// message offset is relative to the start of this process_samples() block
|
||||
event.offset -= start_index;
|
||||
voice_allocator<t_voice, t_sample>::add_event(event);
|
||||
|
||||
m_event_queue.erase(m_event_queue.begin());
|
||||
}
|
||||
m_event_queue.erase(m_event_queue.begin());
|
||||
}
|
||||
|
||||
voice_allocator<t_voice, t_sample>::process_samples(_outputs, start_index, block_size);
|
||||
voice_allocator<t_voice, t_sample>::process_samples(_outputs, start_index, block_size);
|
||||
|
||||
samples_remaining -= block_size;
|
||||
start_index += block_size;
|
||||
}
|
||||
samples_remaining -= block_size;
|
||||
start_index += block_size;
|
||||
}
|
||||
|
||||
m_voices_active = voice_allocator<t_voice, t_sample>::voices_active();
|
||||
m_voices_active = voice_allocator<t_voice, t_sample>::voices_active();
|
||||
|
||||
flush_event_queue(_n_frames);
|
||||
} else {
|
||||
for (int s = 0; s < _n_frames; s++) {
|
||||
_outputs[0][s] = 0.;
|
||||
_outputs[1][s] = 0.;
|
||||
}
|
||||
}
|
||||
}
|
||||
flush_event_queue(_n_frames);
|
||||
} else {
|
||||
for (int s = 0; s < _n_frames; s++) {
|
||||
_outputs[0][s] = 0.;
|
||||
_outputs[1][s] = 0.;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void add_event(midi_event event)
|
||||
{
|
||||
if (event.type == midi_event_type::note_on)
|
||||
m_voices_active = true;
|
||||
void add_event(midi_event event)
|
||||
{
|
||||
if (event.type == midi_event_type::note_on) m_voices_active = true;
|
||||
|
||||
m_event_queue.push_back(event);
|
||||
}
|
||||
m_event_queue.push_back(event);
|
||||
}
|
||||
|
||||
void flush_event_queue(int frames)
|
||||
{
|
||||
for (int i = 0; i < m_event_queue.size(); i++) {
|
||||
m_event_queue.at(i).offset -= frames;
|
||||
}
|
||||
}
|
||||
void flush_event_queue(int frames)
|
||||
{
|
||||
for (int i = 0; i < m_event_queue.size(); i++) { m_event_queue.at(i).offset -= frames; }
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<midi_event> m_event_queue;
|
||||
int m_block_size;
|
||||
bool m_voices_active;
|
||||
std::vector<midi_event> m_event_queue;
|
||||
int m_block_size;
|
||||
bool m_voices_active;
|
||||
};
|
||||
}
|
||||
} // namespace trnr
|
||||
|
||||
@@ -4,275 +4,235 @@
|
||||
namespace trnr {
|
||||
|
||||
enum env_state {
|
||||
idle = 0,
|
||||
attack1,
|
||||
attack2,
|
||||
hold,
|
||||
decay1,
|
||||
decay2,
|
||||
sustain,
|
||||
release1,
|
||||
release2
|
||||
idle = 0,
|
||||
attack1,
|
||||
attack2,
|
||||
hold,
|
||||
decay1,
|
||||
decay2,
|
||||
sustain,
|
||||
release1,
|
||||
release2
|
||||
};
|
||||
|
||||
class tx_envelope {
|
||||
public:
|
||||
env_state state = idle;
|
||||
float attack1_rate = 0;
|
||||
float attack1_level = 0;
|
||||
float attack2_rate = 0;
|
||||
float hold_rate = 0;
|
||||
float decay1_rate = 0;
|
||||
float decay1_level = 0;
|
||||
float decay2_rate = 0;
|
||||
float sustain_level = 0;
|
||||
float release1_rate = 0;
|
||||
float release1_level = 0;
|
||||
float release2_rate = 0;
|
||||
env_state state = idle;
|
||||
float attack1_rate = 0;
|
||||
float attack1_level = 0;
|
||||
float attack2_rate = 0;
|
||||
float hold_rate = 0;
|
||||
float decay1_rate = 0;
|
||||
float decay1_level = 0;
|
||||
float decay2_rate = 0;
|
||||
float sustain_level = 0;
|
||||
float release1_rate = 0;
|
||||
float release1_level = 0;
|
||||
float release2_rate = 0;
|
||||
|
||||
tx_envelope(bool _retrigger = false)
|
||||
: retrigger { _retrigger }
|
||||
{
|
||||
}
|
||||
tx_envelope(bool _retrigger = false)
|
||||
: retrigger {_retrigger}
|
||||
{
|
||||
}
|
||||
|
||||
float process_sample(bool gate, bool trigger) {
|
||||
float process_sample(bool gate, bool trigger) { return process_sample<float>(gate, trigger, 0, 0); }
|
||||
|
||||
return process_sample<float>(gate, trigger, 0, 0);
|
||||
}
|
||||
template <typename t_sample>
|
||||
float process_sample(bool gate, bool trigger, t_sample _attack_mod, t_sample _decay_mod)
|
||||
{
|
||||
|
||||
template <typename t_sample>
|
||||
float process_sample(bool gate, bool trigger, t_sample _attack_mod, t_sample _decay_mod) {
|
||||
size_t attack_mid_x1 = ms_to_samples(attack1_rate + (float)_attack_mod);
|
||||
size_t attack_mid_x2 = ms_to_samples(attack2_rate + (float)_attack_mod);
|
||||
size_t hold_samp = ms_to_samples(hold_rate);
|
||||
size_t decay_mid_x1 = ms_to_samples(decay1_rate + (float)_decay_mod);
|
||||
size_t decay_mid_x2 = ms_to_samples(decay2_rate + (float)_decay_mod);
|
||||
size_t release_mid_x1 = ms_to_samples(release1_rate + (float)_decay_mod);
|
||||
size_t release_mid_x2 = ms_to_samples(release2_rate + (float)_decay_mod);
|
||||
|
||||
size_t attack_mid_x1 = ms_to_samples(attack1_rate + (float)_attack_mod);
|
||||
size_t attack_mid_x2 = ms_to_samples(attack2_rate + (float)_attack_mod);
|
||||
size_t hold_samp = ms_to_samples(hold_rate);
|
||||
size_t decay_mid_x1 = ms_to_samples(decay1_rate + (float)_decay_mod);
|
||||
size_t decay_mid_x2 = ms_to_samples(decay2_rate + (float)_decay_mod);
|
||||
size_t release_mid_x1 = ms_to_samples(release1_rate + (float)_decay_mod);
|
||||
size_t release_mid_x2 = ms_to_samples(release2_rate + (float)_decay_mod);
|
||||
// if note on is triggered, transition to attack phase
|
||||
if (trigger) {
|
||||
if (retrigger) start_level = 0.f;
|
||||
else 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, (float)attack_mid_x1, attack1_level, (float)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, (float)attack_mid_x2, 1, (float)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, (float)decay_mid_x1, decay1_level, (float)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, (float)decay_mid_x2, sustain_level, (float)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, (float)release_mid_x1, release1_level, (float)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, (float)release_mid_x2, 0, (float)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;
|
||||
}
|
||||
}
|
||||
|
||||
// if note on is triggered, transition to attack phase
|
||||
if (trigger) {
|
||||
if (retrigger)
|
||||
start_level = 0.f;
|
||||
else
|
||||
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, (float)attack_mid_x1, attack1_level, (float)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, (float)attack_mid_x2, 1, (float)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, (float)decay_mid_x1, decay1_level, (float)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, (float)decay_mid_x2, sustain_level, (float)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, (float)release_mid_x1, release1_level, (float)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, (float)release_mid_x2, 0, (float)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);
|
||||
}
|
||||
|
||||
return smooth(level);
|
||||
}
|
||||
bool is_busy() { return state != 0; }
|
||||
|
||||
bool is_busy() { return state != 0; }
|
||||
void set_samplerate(double sampleRate) { this->samplerate = sampleRate; }
|
||||
|
||||
void set_samplerate(double sampleRate) {
|
||||
this->samplerate = sampleRate;
|
||||
}
|
||||
// converts the x/y coordinates of the envelope points as a list for graphical representation.
|
||||
std::array<float, 18> calc_coordinates(float _max_attack, float _max_decay, float _max_release)
|
||||
{
|
||||
|
||||
// converts the x/y coordinates of the envelope points as a list for graphical representation.
|
||||
std::array<float, 18> calc_coordinates(float _max_attack, float _max_decay, float _max_release) {
|
||||
auto scale = [](float _value, float _max) { return powf(_value / _max, 0.25) * _max; };
|
||||
|
||||
auto scale = [](float _value, float _max) {
|
||||
return powf(_value / _max, 0.25) * _max;
|
||||
};
|
||||
float a_x = 0;
|
||||
float a_y = 0;
|
||||
|
||||
float a_x = 0;
|
||||
float a_y = 0;
|
||||
float b_x = scale(attack1_rate, _max_attack / 2);
|
||||
float b_y = attack1_level;
|
||||
|
||||
float b_x = scale(attack1_rate, _max_attack / 2);
|
||||
float b_y = attack1_level;
|
||||
float c_x = b_x + scale(attack2_rate, _max_attack / 2);
|
||||
float c_y = 1;
|
||||
|
||||
float c_x = b_x + scale(attack2_rate, _max_attack / 2);
|
||||
float c_y = 1;
|
||||
float d_x = c_x + hold_rate;
|
||||
float d_y = 1;
|
||||
|
||||
float d_x = c_x + hold_rate;
|
||||
float d_y = 1;
|
||||
|
||||
float e_x = d_x + scale(decay1_rate, _max_decay / 2);
|
||||
float e_y = decay1_level;
|
||||
float e_x = d_x + scale(decay1_rate, _max_decay / 2);
|
||||
float e_y = decay1_level;
|
||||
|
||||
float f_x = e_x + scale(decay2_rate, _max_decay / 2);
|
||||
float f_y = sustain_level;
|
||||
float f_x = e_x + scale(decay2_rate, _max_decay / 2);
|
||||
float f_y = sustain_level;
|
||||
|
||||
float g_x = _max_attack + _max_decay;
|
||||
float g_y = sustain_level;
|
||||
float g_x = _max_attack + _max_decay;
|
||||
float g_y = sustain_level;
|
||||
|
||||
float h_x = g_x + scale(release1_rate, _max_decay / 2);
|
||||
float h_y = release1_level;
|
||||
float h_x = g_x + scale(release1_rate, _max_decay / 2);
|
||||
float h_y = release1_level;
|
||||
|
||||
float i_x = h_x + scale(release2_rate, _max_decay / 2);
|
||||
float i_y = 0;
|
||||
float i_x = h_x + scale(release2_rate, _max_decay / 2);
|
||||
float i_y = 0;
|
||||
|
||||
float total = _max_attack + _max_decay + _max_release;
|
||||
float total = _max_attack + _max_decay + _max_release;
|
||||
|
||||
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};
|
||||
}
|
||||
|
||||
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 = 44100.;
|
||||
size_t phase = 0;
|
||||
float level = 0.f;
|
||||
float start_level = 0.f;
|
||||
float h1 = 0.f;
|
||||
float h2 = 0.f;
|
||||
float h3 = 0.f;
|
||||
bool retrigger;
|
||||
double samplerate = 44100.;
|
||||
size_t phase = 0;
|
||||
float level = 0.f;
|
||||
float start_level = 0.f;
|
||||
float h1 = 0.f;
|
||||
float h2 = 0.f;
|
||||
float h3 = 0.f;
|
||||
bool retrigger;
|
||||
|
||||
float lerp(float x1, float y1, float x2, float y2, float x) { return y1 + (((x - x1) * (y2 - y1)) / (x2 - x1)); }
|
||||
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;
|
||||
float smooth(float sample)
|
||||
{
|
||||
h3 = h2;
|
||||
h2 = h1;
|
||||
h1 = sample;
|
||||
|
||||
return (h1 + h2 + h3) / 3.f;
|
||||
}
|
||||
return (h1 + h2 + h3) / 3.f;
|
||||
}
|
||||
|
||||
size_t ms_to_samples(float ms) {
|
||||
return static_cast<size_t>(ms * samplerate / 1000.f);
|
||||
}
|
||||
size_t ms_to_samples(float ms) { return static_cast<size_t>(ms * samplerate / 1000.f); }
|
||||
};
|
||||
}
|
||||
} // namespace trnr
|
||||
@@ -1,37 +1,40 @@
|
||||
#pragma once
|
||||
#include "tx_sineosc.h"
|
||||
#include "tx_envelope.h"
|
||||
#include "tx_sineosc.h"
|
||||
|
||||
namespace trnr {
|
||||
class tx_operator {
|
||||
public:
|
||||
tx_operator()
|
||||
: ratio { 1 }
|
||||
, amplitude { 1.0f }
|
||||
{
|
||||
}
|
||||
tx_operator()
|
||||
: ratio {1}
|
||||
, amplitude {1.0f}
|
||||
{
|
||||
}
|
||||
|
||||
tx_envelope envelope;
|
||||
tx_sineosc oscillator;
|
||||
float ratio;
|
||||
float amplitude;
|
||||
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 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);
|
||||
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.;
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
void set_samplerate(double samplerate)
|
||||
{
|
||||
this->envelope.set_samplerate(samplerate);
|
||||
this->oscillator.set_samplerate(samplerate);
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace trnr
|
||||
|
||||
@@ -5,91 +5,84 @@ namespace trnr {
|
||||
|
||||
class tx_sineosc {
|
||||
public:
|
||||
bool phase_reset;
|
||||
bool phase_reset;
|
||||
|
||||
tx_sineosc()
|
||||
: samplerate { 44100 }
|
||||
, phase_resolution { 16.f }
|
||||
, phase { 0. }
|
||||
, history { 0. }
|
||||
, phase_reset { false }
|
||||
{
|
||||
}
|
||||
tx_sineosc()
|
||||
: samplerate {44100}
|
||||
, phase_resolution {16.f}
|
||||
, phase {0.}
|
||||
, history {0.}
|
||||
, phase_reset {false}
|
||||
{
|
||||
}
|
||||
|
||||
void set_phase_resolution(float res) {
|
||||
phase_resolution = powf(2, res);
|
||||
}
|
||||
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 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);
|
||||
float lookup_phase = phase + phase_modulation;
|
||||
wrap(lookup_phase);
|
||||
phase += frequency / samplerate;
|
||||
wrap(phase);
|
||||
|
||||
redux(lookup_phase);
|
||||
redux(lookup_phase);
|
||||
|
||||
float output = sine(lookup_phase * 4096.);
|
||||
float output = sine(lookup_phase * 4096.);
|
||||
|
||||
filter(output);
|
||||
return output;
|
||||
}
|
||||
filter(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
void set_samplerate(double _samplerate) {
|
||||
this->samplerate = _samplerate;
|
||||
}
|
||||
void set_samplerate(double _samplerate) { this->samplerate = _samplerate; }
|
||||
|
||||
private:
|
||||
double samplerate;
|
||||
float phase_resolution;
|
||||
float phase;
|
||||
float history;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
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.;
|
||||
float wrap(float& phase)
|
||||
{
|
||||
while (phase < 0.) phase += 1.;
|
||||
|
||||
while (phase >= 1.)
|
||||
phase -= 1.;
|
||||
while (phase >= 1.) phase -= 1.;
|
||||
|
||||
return phase;
|
||||
}
|
||||
return phase;
|
||||
}
|
||||
|
||||
float filter(float& value) {
|
||||
value = 0.5 * (value + history);
|
||||
history = value;
|
||||
return value;
|
||||
}
|
||||
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;
|
||||
}
|
||||
float redux(float& value)
|
||||
{
|
||||
value = static_cast<int>(value * phase_resolution) / phase_resolution;
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace trnr
|
||||
285
synth/tx_voice.h
285
synth/tx_voice.h
@@ -9,183 +9,180 @@ namespace trnr {
|
||||
|
||||
class tx_voice : public ivoice {
|
||||
public:
|
||||
tx_voice()
|
||||
: algorithm { 0 }
|
||||
, pitch_env_amt { 0.f }
|
||||
, feedback_amt { 0.f }
|
||||
, bit_resolution(12.f)
|
||||
{
|
||||
}
|
||||
tx_voice()
|
||||
: algorithm {0}
|
||||
, pitch_env_amt {0.f}
|
||||
, feedback_amt {0.f}
|
||||
, bit_resolution(12.f)
|
||||
{
|
||||
}
|
||||
|
||||
bool gate = false;
|
||||
bool trigger = false;
|
||||
int midi_note = 0;
|
||||
float velocity = 1.f;
|
||||
float additional_pitch_mod = 0.f; // modulates pitch in frequency
|
||||
bool gate = false;
|
||||
bool trigger = false;
|
||||
int midi_note = 0;
|
||||
float velocity = 1.f;
|
||||
float additional_pitch_mod = 0.f; // modulates pitch in frequency
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
void note_on(int _note, float _velocity) override
|
||||
{
|
||||
this->gate = true;
|
||||
this->trigger = true;
|
||||
midi_note = _note;
|
||||
velocity = _velocity;
|
||||
}
|
||||
void note_on(int _note, float _velocity) override
|
||||
{
|
||||
this->gate = true;
|
||||
this->trigger = true;
|
||||
midi_note = _note;
|
||||
velocity = _velocity;
|
||||
}
|
||||
|
||||
void note_off() override
|
||||
{
|
||||
this->gate = false;
|
||||
}
|
||||
void note_off() override { this->gate = false; }
|
||||
|
||||
// modulates the pitch in semitones
|
||||
void modulate_pitch(float _pitch) override
|
||||
{
|
||||
this->pitch_mod = _pitch;
|
||||
}
|
||||
// modulates the pitch in semitones
|
||||
void modulate_pitch(float _pitch) override { this->pitch_mod = _pitch; }
|
||||
|
||||
float process_sample() override
|
||||
{
|
||||
float pitch_env_signal = pitch_env.process_sample(gate, trigger) * pitch_env_amt;
|
||||
float pitched_freq = midi_to_frequency(midi_note + pitch_mod + additional_pitch_mod) + pitch_env_signal;
|
||||
float process_sample() override
|
||||
{
|
||||
float pitch_env_signal = pitch_env.process_sample(gate, trigger) * pitch_env_amt;
|
||||
float pitched_freq = midi_to_frequency(midi_note + pitch_mod + additional_pitch_mod) + pitch_env_signal;
|
||||
|
||||
float output = 0.f;
|
||||
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;
|
||||
}
|
||||
// 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;
|
||||
// reset trigger
|
||||
trigger = false;
|
||||
|
||||
return redux(output, bit_resolution);
|
||||
}
|
||||
return redux(output, bit_resolution);
|
||||
}
|
||||
|
||||
bool is_busy() override { return gate || op1.envelope.is_busy() || op2.envelope.is_busy() || op3.envelope.is_busy(); }
|
||||
bool is_busy() override
|
||||
{
|
||||
return gate || op1.envelope.is_busy() || op2.envelope.is_busy() || op3.envelope.is_busy();
|
||||
}
|
||||
|
||||
void set_samplerate(double samplerate) override
|
||||
{
|
||||
pitch_env.set_samplerate(samplerate);
|
||||
feedback_osc.set_samplerate(samplerate);
|
||||
op1.set_samplerate(samplerate);
|
||||
op2.set_samplerate(samplerate);
|
||||
op3.set_samplerate(samplerate);
|
||||
}
|
||||
void set_samplerate(double samplerate) override
|
||||
{
|
||||
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;
|
||||
}
|
||||
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 pitch_mod = 0.f; // modulates pitch in semi-tones
|
||||
const float MOD_INDEX_COEFF = 4.f;
|
||||
float pitch_mod = 0.f; // modulates pitch in semi-tones
|
||||
|
||||
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 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 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 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 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 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 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 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;
|
||||
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;
|
||||
}
|
||||
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 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 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 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;
|
||||
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;
|
||||
}
|
||||
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 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 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 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 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;
|
||||
float redux(float& value, float resolution)
|
||||
{
|
||||
float res = powf(2, resolution);
|
||||
value = roundf(value * res) / res;
|
||||
|
||||
return value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace trnr
|
||||
@@ -8,156 +8,136 @@ namespace trnr {
|
||||
template <typename t_voice, typename t_sample>
|
||||
class voice_allocator {
|
||||
public:
|
||||
std::vector<t_voice> voices;
|
||||
std::vector<t_voice> voices;
|
||||
|
||||
voice_allocator()
|
||||
: voices(4, t_voice())
|
||||
{
|
||||
// checks whether template derives from ivoice
|
||||
typedef t_voice assert_at_compile_time[is_convertible<t_voice>::value ? 1 : -1];
|
||||
}
|
||||
voice_allocator()
|
||||
: voices(4, t_voice())
|
||||
{
|
||||
// checks whether template derives from ivoice
|
||||
typedef t_voice assert_at_compile_time[is_convertible<t_voice>::value ? 1 : -1];
|
||||
}
|
||||
|
||||
void set_voice_count(const int& voice_count)
|
||||
{
|
||||
voices.resize(voice_count, voices.at(0));
|
||||
}
|
||||
void set_voice_count(const int& voice_count) { voices.resize(voice_count, voices.at(0)); }
|
||||
|
||||
void note_on(const midi_event& event)
|
||||
{
|
||||
t_voice* voice = get_free_voice(event.midi_note);
|
||||
void note_on(const midi_event& event)
|
||||
{
|
||||
t_voice* voice = get_free_voice(event.midi_note);
|
||||
|
||||
if (voice == nullptr) {
|
||||
voice = steal_voice();
|
||||
}
|
||||
if (voice == nullptr) { voice = steal_voice(); }
|
||||
|
||||
if (voice != nullptr) {
|
||||
voice->note_on(event.midi_note, event.velocity);
|
||||
}
|
||||
}
|
||||
if (voice != nullptr) { voice->note_on(event.midi_note, event.velocity); }
|
||||
}
|
||||
|
||||
void note_off(const midi_event& event)
|
||||
{
|
||||
for (auto it = voices.begin(); it != voices.end(); it++) {
|
||||
if ((*it).midi_note == event.midi_note) {
|
||||
(*it).note_off();
|
||||
}
|
||||
}
|
||||
}
|
||||
void note_off(const midi_event& event)
|
||||
{
|
||||
for (auto it = voices.begin(); it != voices.end(); it++) {
|
||||
if ((*it).midi_note == event.midi_note) { (*it).note_off(); }
|
||||
}
|
||||
}
|
||||
|
||||
void access(std::function<void(t_voice&)> f)
|
||||
{
|
||||
std::for_each(voices.begin(), voices.end(), f);
|
||||
}
|
||||
void access(std::function<void(t_voice&)> f) { std::for_each(voices.begin(), voices.end(), f); }
|
||||
|
||||
void process_samples(t_sample** _outputs, int _start_index, int _block_size)
|
||||
{
|
||||
for (int s = _start_index; s < _start_index + _block_size; s++) {
|
||||
void process_samples(t_sample** _outputs, int _start_index, int _block_size)
|
||||
{
|
||||
for (int s = _start_index; s < _start_index + _block_size; s++) {
|
||||
|
||||
process_events(s);
|
||||
process_events(s);
|
||||
|
||||
float voices_signal = 0.;
|
||||
float voices_signal = 0.;
|
||||
|
||||
std::for_each(voices.begin(), voices.end(), [&voices_signal](t_voice& voice) {
|
||||
voices_signal += (voice.process_sample() / 3.);
|
||||
});
|
||||
std::for_each(voices.begin(), voices.end(),
|
||||
[&voices_signal](t_voice& voice) { voices_signal += (voice.process_sample() / 3.); });
|
||||
|
||||
_outputs[0][s] = voices_signal;
|
||||
_outputs[1][s] = voices_signal;
|
||||
}
|
||||
}
|
||||
_outputs[0][s] = voices_signal;
|
||||
_outputs[1][s] = voices_signal;
|
||||
}
|
||||
}
|
||||
|
||||
void add_event(midi_event event)
|
||||
{
|
||||
input_queue.push_back(event);
|
||||
}
|
||||
void add_event(midi_event event) { input_queue.push_back(event); }
|
||||
|
||||
bool voices_active()
|
||||
{
|
||||
bool voices_active = false;
|
||||
bool voices_active()
|
||||
{
|
||||
bool voices_active = false;
|
||||
|
||||
for (auto it = voices.begin(); it != voices.end(); it++) {
|
||||
bool busy = (*it).is_busy();
|
||||
voices_active |= busy;
|
||||
}
|
||||
for (auto it = voices.begin(); it != voices.end(); it++) {
|
||||
bool busy = (*it).is_busy();
|
||||
voices_active |= busy;
|
||||
}
|
||||
|
||||
return voices_active;
|
||||
}
|
||||
return voices_active;
|
||||
}
|
||||
|
||||
void set_samplerate(double _samplerate)
|
||||
{
|
||||
for (int i = 0; i < voices.size(); i++) {
|
||||
voices.at(i).set_samplerate(_samplerate);
|
||||
}
|
||||
}
|
||||
void set_samplerate(double _samplerate)
|
||||
{
|
||||
for (int i = 0; i < voices.size(); i++) { voices.at(i).set_samplerate(_samplerate); }
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<midi_event> input_queue;
|
||||
int index_to_steal = 0;
|
||||
std::vector<midi_event> input_queue;
|
||||
int index_to_steal = 0;
|
||||
|
||||
t_voice* get_free_voice(float frequency)
|
||||
{
|
||||
t_voice* voice = nullptr;
|
||||
t_voice* get_free_voice(float frequency)
|
||||
{
|
||||
t_voice* voice = nullptr;
|
||||
|
||||
for (auto it = voices.begin(); it != voices.end(); it++) {
|
||||
if (!(*it).is_busy()) {
|
||||
voice = &*it;
|
||||
}
|
||||
}
|
||||
for (auto it = voices.begin(); it != voices.end(); it++) {
|
||||
if (!(*it).is_busy()) { voice = &*it; }
|
||||
}
|
||||
|
||||
return voice;
|
||||
}
|
||||
return voice;
|
||||
}
|
||||
|
||||
t_voice* steal_voice()
|
||||
{
|
||||
t_voice* free_voice = nullptr;
|
||||
t_voice* steal_voice()
|
||||
{
|
||||
t_voice* free_voice = nullptr;
|
||||
|
||||
for (auto it = voices.begin(); it != voices.end(); it++) {
|
||||
if (!(*it).gate) {
|
||||
free_voice = &*it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (auto it = voices.begin(); it != voices.end(); it++) {
|
||||
if (!(*it).gate) {
|
||||
free_voice = &*it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (free_voice == nullptr) {
|
||||
free_voice = &voices.at(index_to_steal);
|
||||
|
||||
if (index_to_steal < voices.size() - 1) {
|
||||
index_to_steal++;
|
||||
} else {
|
||||
index_to_steal = 0;
|
||||
}
|
||||
}
|
||||
if (free_voice == nullptr) {
|
||||
free_voice = &voices.at(index_to_steal);
|
||||
|
||||
return free_voice;
|
||||
}
|
||||
if (index_to_steal < voices.size() - 1) {
|
||||
index_to_steal++;
|
||||
} else {
|
||||
index_to_steal = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void process_events(int _start_index)
|
||||
{
|
||||
auto iterator = input_queue.begin();
|
||||
while (iterator != input_queue.end()) {
|
||||
return free_voice;
|
||||
}
|
||||
|
||||
midi_event& event = *iterator;
|
||||
if (event.offset == _start_index) {
|
||||
void process_events(int _start_index)
|
||||
{
|
||||
auto iterator = input_queue.begin();
|
||||
while (iterator != input_queue.end()) {
|
||||
|
||||
switch (event.type) {
|
||||
case midi_event_type::note_on:
|
||||
note_on(event);
|
||||
break;
|
||||
case midi_event_type::note_off:
|
||||
note_off(event);
|
||||
break;
|
||||
case midi_event_type::pitch_wheel:
|
||||
access([&event](t_voice& voice) { voice.modulate_pitch(event.data); });
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
midi_event& event = *iterator;
|
||||
if (event.offset == _start_index) {
|
||||
|
||||
iterator = input_queue.erase(iterator);
|
||||
} else {
|
||||
iterator++;
|
||||
}
|
||||
}
|
||||
}
|
||||
switch (event.type) {
|
||||
case midi_event_type::note_on:
|
||||
note_on(event);
|
||||
break;
|
||||
case midi_event_type::note_off:
|
||||
note_off(event);
|
||||
break;
|
||||
case midi_event_type::pitch_wheel:
|
||||
access([&event](t_voice& voice) { voice.modulate_pitch(event.data); });
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
iterator = input_queue.erase(iterator);
|
||||
} else {
|
||||
iterator++;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace trnr
|
||||
|
||||
Reference in New Issue
Block a user