score-avnd-senior/Ottobit/OttobitModel.cpp

58 lines
1.2 KiB
C++

#include "Ottobit.hpp"
namespace Example
{
void Ottobit::operator()(tick t)
{
// Only compute bit_factor when the control changes
if (inputs.bits != previous_bits)
{
set_bit_factor();
previous_bits = inputs.bits;
}
// Only compute skipped_frames when the control changes
if (inputs.rate != previous_rate)
{
step_size = (1. - inputs.rate) * RATE;
previous_rate = inputs.rate;
qDebug() << "step size: " << step_size;
}
// Process the input buffer
for(int i = 0; i < inputs.audio.channels; i++)
{
auto* in = inputs.audio[i];
auto* out = outputs.audio[i];
// Re-init skipped_frames and current_frame for each channel
skipped_frames = 0;
current_frame = in[0];
for(int j = 0; j < t.frames; j++)
{
if (skipped_frames <= step_size)
skipped_frames++;
else
{
skipped_frames = 0;
if (inputs.bits == BITS)
current_frame = in[j];
else
{
int in_scaled{static_cast<int>((in[j] + 1) * bit_factor)};
current_frame = (static_cast<double>(in_scaled) / bit_factor) - 1;
}
}
out[j] = current_frame;
}
}
}
void Ottobit::set_bit_factor()
{
bit_factor = pow(2, inputs.bits) - 1;
}
}