From 1824fcd4d87c009c861a72ffb5705ede34dd949c Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 7 Aug 2023 13:44:24 +0200 Subject: [PATCH] optimized voice stealing --- synth/voice_allocator.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/synth/voice_allocator.h b/synth/voice_allocator.h index 0f85716..16f58ab 100644 --- a/synth/voice_allocator.h +++ b/synth/voice_allocator.h @@ -11,7 +11,7 @@ public: std::vector voices; voice_allocator() - : voices(8, t_voice()) + : voices(4, t_voice()) { // checks whether template derives from ivoice typedef t_voice assert_at_compile_time[is_convertible::value ? 1 : -1]; @@ -92,6 +92,7 @@ public: private: std::vector input_queue; + int index_to_steal = 0; t_voice* get_free_voice(float frequency) { @@ -118,7 +119,13 @@ private: } if (free_voice == nullptr) { - free_voice = &voices.at(0); + free_voice = &voices.at(index_to_steal); + + if (index_to_steal < voices.size() - 1) { + index_to_steal++; + } else { + index_to_steal = 0; + } } return free_voice; @@ -153,4 +160,4 @@ private: } } }; -} \ No newline at end of file +}