remove encapsulation from allocator + midi synth, parameter to disable stealing of non-gated voices

This commit is contained in:
2025-08-10 14:08:06 +02:00
parent 2354be5896
commit d55490d7d1
2 changed files with 17 additions and 25 deletions

View File

@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "audio_buffer.h" #include "audio_buffer.h"
#include "ivoice.h"
#include "midi_event.h" #include "midi_event.h"
#include "voice_allocator.h" #include "voice_allocator.h"
#include <cstddef> #include <cstddef>
@@ -14,12 +13,14 @@ namespace trnr {
template <typename t_voice, typename t_sample> template <typename t_voice, typename t_sample>
class midi_synth : public voice_allocator<t_voice, t_sample> { class midi_synth : public voice_allocator<t_voice, t_sample> {
public: public:
std::vector<midi_event> m_event_queue;
int m_block_size;
bool m_voices_active;
midi_synth(size_t num_voices = 1) midi_synth(size_t num_voices = 1)
: m_voices_active {false} : m_voices_active {false}
, trnr::voice_allocator<t_voice, t_sample>(num_voices) , voice_allocator<t_voice, t_sample>(num_voices)
{ {
// checks whether template derives from ivoice
typedef t_voice assert_at_compile_time[is_convertible<t_voice, t_sample>::value ? 1 : -1];
} }
void set_samplerate_blocksize(double _samplerate, int _block_size) 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; } for (int i = 0; i < m_event_queue.size(); i++) { m_event_queue.at(i).offset -= frames; }
} }
private:
std::vector<midi_event> m_event_queue;
int m_block_size;
bool m_voices_active;
}; };
} // namespace trnr } // namespace trnr

View File

@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "audio_buffer.h" #include "audio_buffer.h"
#include "ivoice.h"
#include "midi_event.h" #include "midi_event.h"
#include <algorithm> #include <algorithm>
@@ -15,14 +14,15 @@ template <typename t_voice, typename t_sample>
class voice_allocator { class voice_allocator {
public: public:
std::vector<std::shared_ptr<t_voice>> voice_ptrs; std::vector<std::shared_ptr<t_voice>> voice_ptrs;
std::vector<midi_event> 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) voice_allocator(size_t num_voices = 1)
{ {
// checks whether template derives from ivoice assert(num_voices > 0 && "number of voices must be greater than 0");
typedef t_voice assert_at_compile_time[is_convertible<t_voice, t_sample>::value ? 1 : -1];
assert(num_voices > 0 && "voice_reserve must be greater than 0");
init_voice_ptrs(num_voices); init_voice_ptrs(num_voices);
} }
@@ -89,12 +89,6 @@ public:
for (const auto& v : voice_ptrs) { v->set_samplerate(_samplerate); } for (const auto& v : voice_ptrs) { v->set_samplerate(_samplerate); }
} }
private:
std::vector<midi_event> 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) void init_voice_ptrs(size_t num_voices)
{ {
voice_ptrs.reserve(num_voices); voice_ptrs.reserve(num_voices);
@@ -114,13 +108,15 @@ private:
std::shared_ptr<t_voice> steal_voice() std::shared_ptr<t_voice> steal_voice()
{ {
// Try to find a voice that is not gated (not playing a note) // Try to find a voice that is not gated (not playing a note)
for (size_t i = 0; i < active_voice_count; ++i) { if (steal_non_gated)
if (!voice_ptrs[i]->gate) { return voice_ptrs[i]; } 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 // If all voices are gated, steal one round-robin
auto voice = voice_ptrs[index_to_steal]; 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; return voice;
} }