score-avnd-senior/Senior/SeniorModel.cpp

64 lines
1.4 KiB
C++
Raw Normal View History

#include <Gamma/scl.h>
2024-10-06 23:26:13 +01:00
#include "Senior.hpp"
2024-09-30 18:50:58 +01:00
2024-10-06 23:26:13 +01:00
namespace Ottobit
2024-09-30 18:50:58 +01:00
{
2024-10-06 23:26:13 +01:00
void Senior::operator()(tick t)
2024-09-30 18:50:58 +01:00
{
2024-10-01 00:43:19 +01:00
// Only compute bit_factor when the control changes
2024-09-30 22:30:26 +01:00
if (inputs.bits != previous_bits)
{
set_bit_factor();
previous_bits = inputs.bits;
}
2024-10-01 00:43:19 +01:00
// Only compute skipped_frames when the control changes
if (inputs.rate != previous_rate)
{
step_size = (1. - inputs.rate) * RATE;
previous_rate = inputs.rate;
}
2024-09-30 18:50:58 +01:00
// Process the input buffer
for(int i{0}; i < inputs.audio.channels; i++)
2024-09-30 18:50:58 +01:00
{
auto* in = inputs.audio[i];
2024-10-06 23:26:13 +01:00
auto* out = outputs.audio[i % outputs.audio.channels];
2024-09-30 18:50:58 +01:00
// Init current_frame for each channel
2024-10-06 23:26:13 +01:00
double current_frame{crush_frame(in[i])};
2024-10-01 00:43:19 +01:00
for(int j{0}; j < t.frames; j++)
2024-09-30 18:50:58 +01:00
{
if (skipped_frames[i] <= step_size)
skipped_frames[i]++;
2024-10-01 00:43:19 +01:00
else
2024-09-30 22:30:26 +01:00
{
skipped_frames[i] = 0;
current_frame = crush_frame(in[j]);
2024-09-30 22:30:26 +01:00
}
2024-10-06 23:26:13 +01:00
fms[i].delay(lfos[i].cos()*0.0025 + 0.05);
out[j] = fms[i](current_frame);
2024-09-30 18:50:58 +01:00
}
}
}
2024-09-30 22:30:26 +01:00
2024-10-06 23:26:13 +01:00
void Senior::set_bit_factor()
2024-09-30 22:30:26 +01:00
{
bit_factor = pow(2, inputs.bits) - 1;
}
2024-10-06 23:26:13 +01:00
double Senior::crush_frame(const double& f)
{
if (inputs.bits == BITS)
return f;
else
// Rounding using static_cast
// return static_cast<double>(static_cast<int>(f * bit_factor)) / bit_factor;
2024-10-06 23:26:13 +01:00
// Close, but prefer the use of roundMagic from Lance Putnam's Gama
return gam::scl::round<double>(f * bit_factor) / bit_factor;
}
2024-09-30 18:50:58 +01:00
}