block based processing

This commit is contained in:
Chris
2023-08-16 13:30:39 +02:00
parent 044f42374b
commit 9dc4dd5192
4 changed files with 73 additions and 59 deletions

View File

@@ -1,9 +1,11 @@
#pragma once
namespace trnr {
template <typename t_sample>
struct ivoice {
virtual ~ivoice() = default;
virtual float process_sample() = 0;
//virtual float process_sample() = 0;
virtual void process_samples(t_sample** _outputs, int _start_index, int _block_size) = 0;
virtual bool is_busy() = 0;
virtual void set_samplerate(double samplerate) = 0;
virtual void note_on(int _note, float _velocity) = 0;
@@ -12,7 +14,7 @@ struct ivoice {
};
// check if a template derives from ivoice
template <class derived>
template <class derived, typename sample>
struct is_convertible {
template <class T>
static char test(T*);
@@ -20,6 +22,6 @@ struct is_convertible {
template <class T>
static double test(...);
static const bool value = sizeof(test<ivoice>(static_cast<derived*>(0))) == 1;
static const bool value = sizeof(test<ivoice<sample>>(static_cast<derived*>(0))) == 1;
};
} // namespace trnr

View File

@@ -16,7 +16,7 @@ public:
: m_voices_active {false}
{
// checks whether template derives from ivoice
typedef t_voice assert_at_compile_time[is_convertible<t_voice>::value ? 1 : -1];
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)
@@ -27,6 +27,9 @@ public:
void process_block(t_sample** _outputs, int _n_frames)
{
// clear outputs
for (auto i = 0; i < 2; i++) { memset(_outputs[i], 0, _n_frames * sizeof(t_sample)); }
// sample accurate event handling based on the iPlug2 synth by Oli Larkin
if (m_voices_active || !m_event_queue.empty()) {
int block_size = m_block_size;

View File

@@ -7,7 +7,8 @@
namespace trnr {
class tx_voice : public ivoice {
template <typename t_sample>
class tx_voice : public ivoice<t_sample> {
public:
tx_voice()
: algorithm {0}
@@ -46,11 +47,13 @@ public:
// modulates the pitch in semitones
void modulate_pitch(float _pitch) override { this->pitch_mod = _pitch; }
float process_sample() override
void process_samples(t_sample** _outputs, int _start_index, int _block_size) override
{
float pitch_env_signal = pitch_env.process_sample(gate, trigger) * pitch_env_amt;
float pitched_freq = midi_to_frequency(midi_note + pitch_mod + additional_pitch_mod) + pitch_env_signal;
for (int s = _start_index; s < _start_index + _block_size; s++) {
float output = 0.f;
// mix operator signals according to selected algorithm
@@ -75,7 +78,11 @@ public:
// reset trigger
trigger = false;
return redux(output, bit_resolution);
redux(output, bit_resolution);
_outputs[0][s] += output / 3.;
_outputs[1][s] = _outputs[0][s];
}
}
bool is_busy() override

View File

@@ -14,7 +14,7 @@ public:
: voices(4, t_voice())
{
// checks whether template derives from ivoice
typedef t_voice assert_at_compile_time[is_convertible<t_voice>::value ? 1 : -1];
typedef t_voice assert_at_compile_time[is_convertible<t_voice, t_sample>::value ? 1 : -1];
}
void set_voice_count(const int& voice_count) { voices.resize(voice_count, voices.at(0)); }
@@ -39,17 +39,16 @@ public:
void process_samples(t_sample** _outputs, int _start_index, int _block_size)
{
for (int s = _start_index; s < _start_index + _block_size; s++) {
for (int b = _start_index; b < _start_index + _block_size; b += internal_block_size) {
process_events(s);
const int block_size = internal_block_size;
float voices_signal = 0.;
// process all events in the block (introduces potential inaccuracy of up to 16 samples)
process_events(b, block_size);
std::for_each(voices.begin(), voices.end(),
[&voices_signal](t_voice& voice) { voices_signal += (voice.process_sample() / 3.); });
_outputs[0][s] = voices_signal;
_outputs[1][s] = voices_signal;
std::for_each(voices.begin(), voices.end(), [&_outputs, &b, &block_size](t_voice& voice) {
voice.process_samples(_outputs, b, block_size);
});
}
}
@@ -75,6 +74,7 @@ public:
private:
std::vector<midi_event> input_queue;
int index_to_steal = 0;
const int internal_block_size = 16;
t_voice* get_free_voice(float frequency)
{
@@ -111,8 +111,9 @@ private:
return free_voice;
}
void process_events(int _start_index)
void process_events(int _start_index, int _block_size)
{
for (int s = _start_index; s < _start_index + _block_size; s++) {
auto iterator = input_queue.begin();
while (iterator != input_queue.end()) {
@@ -139,5 +140,6 @@ private:
}
}
}
}
};
} // namespace trnr