From 24ef40bc07d212c56b11b266d683959932b5068d Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 20 Jun 2025 15:23:03 +0200 Subject: [PATCH] add constructor parameter controlling reservation of number of voices --- synth/midi_synth.h | 6 +++++- synth/voice_allocator.h | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/synth/midi_synth.h b/synth/midi_synth.h index cb38419..940752c 100644 --- a/synth/midi_synth.h +++ b/synth/midi_synth.h @@ -3,6 +3,7 @@ #include "ivoice.h" #include "midi_event.h" #include "voice_allocator.h" +#include #include #include @@ -13,9 +14,12 @@ namespace trnr { template class midi_synth : public voice_allocator { public: - midi_synth() + midi_synth(size_t voice_reserve = 1) : m_voices_active {false} { + // call base constructor with a reserve of 1 voice + voice_allocator::set_voice_count(voice_reserve); + // checks whether template derives from ivoice typedef t_voice assert_at_compile_time[is_convertible::value ? 1 : -1]; } diff --git a/synth/voice_allocator.h b/synth/voice_allocator.h index bcbf9ea..1bb9786 100644 --- a/synth/voice_allocator.h +++ b/synth/voice_allocator.h @@ -3,6 +3,7 @@ #include "ivoice.h" #include "midi_event.h" #include +#include #include #include #include @@ -14,12 +15,14 @@ class voice_allocator { public: std::vector> voice_ptrs; - voice_allocator() + voice_allocator(size_t voice_reserve = 1) { // checks whether template derives from ivoice typedef t_voice assert_at_compile_time[is_convertible::value ? 1 : -1]; - voice_ptrs.reserve(8); + assert(voice_reserve > 0 && "voice_reserve must be greater than 0"); + + voice_ptrs.reserve(voice_reserve); } void set_voice_count(const int& voice_count)