block based processing
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
namespace trnr {
|
namespace trnr {
|
||||||
|
template <typename t_sample>
|
||||||
struct ivoice {
|
struct ivoice {
|
||||||
virtual ~ivoice() = default;
|
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 bool is_busy() = 0;
|
||||||
virtual void set_samplerate(double samplerate) = 0;
|
virtual void set_samplerate(double samplerate) = 0;
|
||||||
virtual void note_on(int _note, float _velocity) = 0;
|
virtual void note_on(int _note, float _velocity) = 0;
|
||||||
@@ -12,7 +14,7 @@ struct ivoice {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// check if a template derives from ivoice
|
// check if a template derives from ivoice
|
||||||
template <class derived>
|
template <class derived, typename sample>
|
||||||
struct is_convertible {
|
struct is_convertible {
|
||||||
template <class T>
|
template <class T>
|
||||||
static char test(T*);
|
static char test(T*);
|
||||||
@@ -20,6 +22,6 @@ struct is_convertible {
|
|||||||
template <class T>
|
template <class T>
|
||||||
static double test(...);
|
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
|
} // namespace trnr
|
||||||
@@ -16,7 +16,7 @@ public:
|
|||||||
: m_voices_active {false}
|
: m_voices_active {false}
|
||||||
{
|
{
|
||||||
// checks whether template derives from ivoice
|
// 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)
|
void set_samplerate_blocksize(double _samplerate, int _block_size)
|
||||||
@@ -27,6 +27,9 @@ public:
|
|||||||
|
|
||||||
void process_block(t_sample** _outputs, int _n_frames)
|
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
|
// sample accurate event handling based on the iPlug2 synth by Oli Larkin
|
||||||
if (m_voices_active || !m_event_queue.empty()) {
|
if (m_voices_active || !m_event_queue.empty()) {
|
||||||
int block_size = m_block_size;
|
int block_size = m_block_size;
|
||||||
|
|||||||
@@ -7,7 +7,8 @@
|
|||||||
|
|
||||||
namespace trnr {
|
namespace trnr {
|
||||||
|
|
||||||
class tx_voice : public ivoice {
|
template <typename t_sample>
|
||||||
|
class tx_voice : public ivoice<t_sample> {
|
||||||
public:
|
public:
|
||||||
tx_voice()
|
tx_voice()
|
||||||
: algorithm {0}
|
: algorithm {0}
|
||||||
@@ -46,11 +47,13 @@ public:
|
|||||||
// modulates the pitch in semitones
|
// modulates the pitch in semitones
|
||||||
void modulate_pitch(float _pitch) override { this->pitch_mod = _pitch; }
|
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 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;
|
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;
|
float output = 0.f;
|
||||||
|
|
||||||
// mix operator signals according to selected algorithm
|
// mix operator signals according to selected algorithm
|
||||||
@@ -75,7 +78,11 @@ public:
|
|||||||
// reset trigger
|
// reset trigger
|
||||||
trigger = false;
|
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
|
bool is_busy() override
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ public:
|
|||||||
: voices(4, t_voice())
|
: voices(4, 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>::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)); }
|
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)
|
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(),
|
std::for_each(voices.begin(), voices.end(), [&_outputs, &b, &block_size](t_voice& voice) {
|
||||||
[&voices_signal](t_voice& voice) { voices_signal += (voice.process_sample() / 3.); });
|
voice.process_samples(_outputs, b, block_size);
|
||||||
|
});
|
||||||
_outputs[0][s] = voices_signal;
|
|
||||||
_outputs[1][s] = voices_signal;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,6 +74,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
std::vector<midi_event> input_queue;
|
std::vector<midi_event> input_queue;
|
||||||
int index_to_steal = 0;
|
int index_to_steal = 0;
|
||||||
|
const int internal_block_size = 16;
|
||||||
|
|
||||||
t_voice* get_free_voice(float frequency)
|
t_voice* get_free_voice(float frequency)
|
||||||
{
|
{
|
||||||
@@ -111,8 +111,9 @@ private:
|
|||||||
return free_voice;
|
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();
|
auto iterator = input_queue.begin();
|
||||||
while (iterator != input_queue.end()) {
|
while (iterator != input_queue.end()) {
|
||||||
|
|
||||||
@@ -139,5 +140,6 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
} // namespace trnr
|
} // namespace trnr
|
||||||
|
|||||||
Reference in New Issue
Block a user