diff --git a/synth/triplex.h b/synth/triplex.h index 2975006..c673e10 100644 --- a/synth/triplex.h +++ b/synth/triplex.h @@ -327,8 +327,17 @@ struct tx_state { tx_operator op3; }; +inline void tx_voice_init(tx_state& s, double samplerate) +{ + tx_sineosc_init(s.feedback_osc, samplerate); + tx_envelope_init(s.pitch_env, samplerate); + tx_operator_init(s.op1, samplerate); + tx_operator_init(s.op2, samplerate); + tx_operator_init(s.op3, samplerate); +} + inline void tx_voice_process_block(tx_state& t, voice_state& s, float** audio, size_t num_frames, - const vector>& mods) + const vector>& mods = {}) { float frequency = midi_to_frequency(s.midi_note + t.pitch_mod + t.additional_pitch_mod); @@ -517,12 +526,20 @@ struct tx_synth { array voices; }; -inline void tx_synth_process_block(tx_synth& s, float** audio, size_t num_frames, const vector& midi_events, - const vector>& mods) +inline void tx_synth_init(tx_synth& s, double samplerate) { + voice_allocator_init(s.allocator); + for (int i = 0; i < MAX_VOICES; i++) { tx_voice_init(s.voices[i], samplerate); } +} + +inline void tx_synth_process_block(tx_synth& s, float** audio, size_t num_frames, const vector& midi_events, + const vector>& mods = {}) +{ + for (int i = 0; i < num_frames; i++) { audio[0][i] = audio[1][i] = 0.f; } // clear audio buffers + voice_allocator_process_block(s.allocator, midi_events); - for (int i = 0; i < MAX_VOICES; i++) { + for (int i = 0; i < s.allocator.active_voice_count; i++) { tx_voice_process_block(s.voices[i], s.allocator.voices[i], audio, num_frames, mods); } } @@ -726,4 +743,9 @@ inline void tx_apply_parameter_mapping(array& v, tx_parame } } +inline void tx_apply_parameter_mappings(array& v, std::vector& m, + float value) +{ + for (int i = 0; i < m.size(); i++) { tx_apply_parameter_mapping(v, m[i], value); } +} } // namespace trnr diff --git a/synth/voice_allocator.h b/synth/voice_allocator.h index c2c7ff3..23f17af 100644 --- a/synth/voice_allocator.h +++ b/synth/voice_allocator.h @@ -61,10 +61,10 @@ inline void make_mod_wheel(midi_event& ev, double _mod, int _offset = 0) struct voice_state { int midi_note; - bool is_busy; - bool gate; - bool trigger; - float velocity; + bool is_busy = false; + bool gate = false; + bool trigger = false; + float velocity = 0.0f; array events; size_t event_count = 0; }; @@ -132,7 +132,7 @@ inline void voice_process_event_for_frame(voice_state& v, size_t frame) { const midi_event* best_event = nullptr; - for (int i = 0; i < v.events.size(); i++) { + for (int i = 0; i < v.event_count; i++) { const midi_event& ev = v.events[i]; if (ev.offset == frame) { best_event = &ev;