changed to vector of shared pointers

This commit is contained in:
Chris
2023-08-24 11:36:59 +02:00
parent d2d2fd1473
commit 8581a79659

View File

@@ -8,10 +8,9 @@ namespace trnr {
template <typename t_voice, typename t_sample> template <typename t_voice, typename t_sample>
class voice_allocator { class voice_allocator {
public: public:
std::vector<t_voice*> voicePtrs; std::vector<std::shared_ptr<t_voice>> voicePtrs;
voice_allocator() voice_allocator()
: voicePtrs(4, new t_voice())
{ {
// checks whether template derives from ivoice // checks whether template derives from ivoice
typedef t_voice assert_at_compile_time[is_convertible<t_voice, t_sample>::value ? 1 : -1]; typedef t_voice assert_at_compile_time[is_convertible<t_voice, t_sample>::value ? 1 : -1];
@@ -19,36 +18,50 @@ public:
voicePtrs.reserve(8); voicePtrs.reserve(8);
} }
void set_voice_count(const int& voice_count) { voicePtrs.resize(voice_count, voicePtrs.at(0)); } void set_voice_count(const int& voice_count)
{
if (voice_count > voicePtrs.size()) {
for (int i = voicePtrs.size(); i < voice_count; ++i) {
voicePtrs.emplace_back(std::make_shared<t_voice>());
}
} else if (voice_count < voicePtrs.size()) {
voicePtrs.resize(voice_count);
}
}
void note_on(const midi_event& event) void note_on(const midi_event& event)
{ {
t_voice* voice = get_free_voice(event.midi_note); auto voice = get_free_voice(event.midi_note);
if (voice == nullptr) { voice = steal_voice(); } if (!voice) { voice = steal_voice(); }
if (voice != nullptr) { voice->note_on(event.midi_note, event.velocity); } if (voice) { voice->note_on(event.midi_note, event.velocity); }
} }
void note_off(const midi_event& event) void note_off(const midi_event& event)
{ {
for (t_voice* v : voicePtrs) { for (const auto& v : voicePtrs) {
if (v->midi_note == event.midi_note) v->note_off(); if (v->midi_note == event.midi_note) v->note_off();
} }
} }
void access(std::function<void(t_voice*)> f) { std::for_each(voicePtrs.begin(), voicePtrs.end(), f); } void access(std::function<void(t_voice*)> f)
{
std::for_each(voicePtrs.begin(), voicePtrs.end(), [&](std::shared_ptr<t_voice> ptr) {
if (ptr) {
f(ptr.get()); // Call the function with the raw pointer
}
});
}
void process_samples(t_sample** _outputs, int _start_index, int _block_size) void process_samples(t_sample** _outputs, int _start_index, int _block_size)
{ {
for (int b = _start_index; b < _start_index + _block_size; b += internal_block_size) { for (int b = _start_index; b < _start_index + _block_size; b += internal_block_size) {
const int block_size = internal_block_size;
// process all events in the block (introduces potential inaccuracy of up to 16 samples) // process all events in the block (introduces potential inaccuracy of up to 16 samples)
process_events(b, block_size); process_events(b, internal_block_size);
for (t_voice* v : voicePtrs) { v->process_samples(_outputs, b, block_size); } for (const auto& v : voicePtrs) { v->process_samples(_outputs, b, internal_block_size); }
} }
} }
@@ -58,7 +71,7 @@ public:
{ {
bool voices_active = false; bool voices_active = false;
for (t_voice* v : voicePtrs) { for (const auto& v : voicePtrs) {
bool busy = v->is_busy(); bool busy = v->is_busy();
voices_active |= busy; voices_active |= busy;
} }
@@ -68,7 +81,7 @@ public:
void set_samplerate(double _samplerate) void set_samplerate(double _samplerate)
{ {
for (t_voice* v : voicePtrs) { v->set_samplerate(_samplerate); } for (const auto& v : voicePtrs) { v->set_samplerate(_samplerate); }
} }
private: private:
@@ -76,29 +89,31 @@ private:
int index_to_steal = 0; int index_to_steal = 0;
const int internal_block_size = 16; const int internal_block_size = 16;
t_voice* get_free_voice(float frequency) std::shared_ptr<t_voice> get_free_voice(float frequency)
{ {
t_voice* voice = nullptr; std::shared_ptr<t_voice> voice = nullptr;
for (const auto& v : voicePtrs) {
for (t_voice* v : voicePtrs) {
if (!v->is_busy()) voice = v; if (!v->is_busy()) voice = v;
break;
} }
return voice; return voice;
} }
t_voice* steal_voice() std::shared_ptr<t_voice> steal_voice()
{ {
t_voice* free_voice = nullptr; std::shared_ptr<t_voice> free_voice = nullptr;
for (t_voice* v : voicePtrs) { for (const auto& v : voicePtrs) {
if (!v->gate) { if (!v->gate) {
free_voice = v; free_voice = v;
break; break;
} }
} }
if (free_voice == nullptr) { if (!free_voice) {
free_voice = voicePtrs.at(index_to_steal); free_voice = voicePtrs.at(index_to_steal);
if (index_to_steal < voicePtrs.size() - 1) { if (index_to_steal < voicePtrs.size() - 1) {