From d55490d7d1db91697df86d923e4e4ca75817be54 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 10 Aug 2025 14:08:06 +0200 Subject: [PATCH] remove encapsulation from allocator + midi synth, parameter to disable stealing of non-gated voices --- synth/midi_synth.h | 14 +++++--------- synth/voice_allocator.h | 28 ++++++++++++---------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/synth/midi_synth.h b/synth/midi_synth.h index 4542b7d..a8a1829 100644 --- a/synth/midi_synth.h +++ b/synth/midi_synth.h @@ -1,6 +1,5 @@ #pragma once #include "audio_buffer.h" -#include "ivoice.h" #include "midi_event.h" #include "voice_allocator.h" #include @@ -14,12 +13,14 @@ namespace trnr { template class midi_synth : public voice_allocator { public: + std::vector m_event_queue; + int m_block_size; + bool m_voices_active; + midi_synth(size_t num_voices = 1) : m_voices_active {false} - , trnr::voice_allocator(num_voices) + , voice_allocator(num_voices) { - // checks whether template derives from ivoice - typedef t_voice assert_at_compile_time[is_convertible::value ? 1 : -1]; } void set_samplerate_blocksize(double _samplerate, int _block_size) @@ -86,10 +87,5 @@ public: { for (int i = 0; i < m_event_queue.size(); i++) { m_event_queue.at(i).offset -= frames; } } - -private: - std::vector m_event_queue; - int m_block_size; - bool m_voices_active; }; } // namespace trnr diff --git a/synth/voice_allocator.h b/synth/voice_allocator.h index bedc619..6bb20df 100644 --- a/synth/voice_allocator.h +++ b/synth/voice_allocator.h @@ -1,6 +1,5 @@ #pragma once #include "audio_buffer.h" -#include "ivoice.h" #include "midi_event.h" #include @@ -15,14 +14,15 @@ template class voice_allocator { public: std::vector> voice_ptrs; + std::vector input_queue; + int index_to_steal = 0; + const int internal_block_size = 16; + size_t active_voice_count; + bool steal_non_gated = true; voice_allocator(size_t num_voices = 1) { - // checks whether template derives from ivoice - typedef t_voice assert_at_compile_time[is_convertible::value ? 1 : -1]; - - assert(num_voices > 0 && "voice_reserve must be greater than 0"); - + assert(num_voices > 0 && "number of voices must be greater than 0"); init_voice_ptrs(num_voices); } @@ -89,12 +89,6 @@ public: for (const auto& v : voice_ptrs) { v->set_samplerate(_samplerate); } } -private: - std::vector input_queue; - int index_to_steal = 0; - const int internal_block_size = 16; - size_t active_voice_count; - void init_voice_ptrs(size_t num_voices) { voice_ptrs.reserve(num_voices); @@ -114,13 +108,15 @@ private: std::shared_ptr steal_voice() { // Try to find a voice that is not gated (not playing a note) - for (size_t i = 0; i < active_voice_count; ++i) { - if (!voice_ptrs[i]->gate) { return voice_ptrs[i]; } - } + if (steal_non_gated) + for (size_t i = 0; i < active_voice_count; ++i) { + if (!voice_ptrs[i]->gate) { return voice_ptrs[i]; } + } // If all voices are gated, steal one round-robin auto voice = voice_ptrs[index_to_steal]; - index_to_steal = (index_to_steal + 1) % active_voice_count; + index_to_steal++; + if (index_to_steal >= active_voice_count) index_to_steal = 0; return voice; }