diff --git a/synth/midi_synth.h b/synth/midi_synth.h index 030a6c1..9329e65 100644 --- a/synth/midi_synth.h +++ b/synth/midi_synth.h @@ -11,7 +11,8 @@ template class midi_synth : public voice_allocator { public: midi_synth(int _n_voices, double _samplerate, int _block_size) - : m_samplerate { _samplerate } + : voice_allocator { _samplerate } + , m_samplerate { _samplerate } , m_block_size { _block_size } , m_voices_active { false } { @@ -58,13 +59,12 @@ public: start_index += block_size; } - voices_active = voice_allocator::voices_active(); + m_voices_active = voice_allocator::voices_active(); flush_event_queue(_n_frames); } else { for (int s = 0; s < _n_frames; s++) { - outputs[0][s] = 0.; - outputs[1][s] = 0.; + _output[s] = 0.; } } } diff --git a/synth/tx_envelope.h b/synth/tx_envelope.h index fb160aa..17a5952 100644 --- a/synth/tx_envelope.h +++ b/synth/tx_envelope.h @@ -17,6 +17,7 @@ enum env_state { class tx_envelope { public: + env_state state; float attack1_rate; float attack1_level; float attack2_rate; @@ -259,7 +260,6 @@ private: double samplerate; int phase; float level; - env_state state; float start_level; float h1; float h2; diff --git a/synth/tx_voice.h b/synth/tx_voice.h index 2f06038..198e7a6 100644 --- a/synth/tx_voice.h +++ b/synth/tx_voice.h @@ -2,6 +2,9 @@ #include "tx_sineosc.h" #include "tx_envelope.h" #include "tx_operator.h" +#include "../util/audio_math.h" + +using namespace trnr::lib::util; namespace trnr::lib::synth { @@ -22,7 +25,7 @@ public: bool gate = false; bool trigger = false; - float frequency = 100.f; + int midi_note = 0; float velocity = 1.f; int algorithm; @@ -35,9 +38,20 @@ public: tx_operator op2; tx_operator op3; + void note_on(int _note, float _velocity) { + this->gate = true; + this->trigger = true; + midi_note = _note; + velocity = _velocity; + } + + void note_off() { + this->gate = false; + } + float process_sample() { float pitch_env_signal = pitch_env.process_sample(gate, trigger) * pitch_env_amt; - float pitched_freq = frequency + pitch_env_signal; + float pitched_freq = midi_to_frequency(midi_note) + pitch_env_signal; float output = 0.f; diff --git a/synth/voice_allocator.h b/synth/voice_allocator.h index 97bc375..24203eb 100644 --- a/synth/voice_allocator.h +++ b/synth/voice_allocator.h @@ -36,7 +36,7 @@ public: void note_off(const note_event& event) { for (auto it = voices.begin(); it != voices.end(); it++) { - if ((*it).MidiNote == event.midi_note) { + if ((*it).midi_note == event.midi_note) { (*it).note_off(); } } diff --git a/util/audio_math.h b/util/audio_math.h index 13efef5..f7ddee2 100644 --- a/util/audio_math.h +++ b/util/audio_math.h @@ -8,4 +8,8 @@ namespace trnr::lib::util { static inline double db_2_lin(double db) { return pow(10, db/20); } + + static inline float midi_to_frequency(int midi_note) { + return 440.0 * powf(2.0, ((float)midi_note - 69.0) / 12.0); + } } \ No newline at end of file