Skip to content

Commit

Permalink
nit: fix compilation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Segfault1602 committed Aug 5, 2024
1 parent 319765f commit a5bd4f1
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 135 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"request": "launch",
"program": "${workspaceFolder}/build/tests/perf/Debug/perf_tests.exe",
"args": [
"-tc=Aligned"
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
Expand Down
2 changes: 1 addition & 1 deletion include/buchla_lpg.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BuchlaLPG
/// @param in The input buffer
/// @param out The output buffer. Can be the same as `in`
/// @param size The size of the input and output buffer
void ProcessBlock(const float* cv_in, const float* in, float* out, size_t size);
void ProcessBlock(float* cv_in, const float* in, float* out, size_t size);

void ProcessCurrent(const float* vc_in, float* vc_out, size_t size);

Expand Down
1 change: 0 additions & 1 deletion src/basic_oscillators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <cassert>
#include <cmath>
#include <cstdlib>

#include "dsp_utils.h"
#include "sin_table.h"
Expand Down
84 changes: 9 additions & 75 deletions src/buchla_lpg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,77 +89,11 @@ void BuchlaLPG::Init(float samplerate)
f_inv_ = 1.f / f_;
}

void BuchlaLPG::ProcessBlock(const float* cv_in, const float* in, float* out, size_t size)
void BuchlaLPG::ProcessBlock(float* cv_in, const float* in, float* out, size_t size)
{
for (size_t i = 0; i < size; ++i)
{
// Process the CV
const float current = GetCurrent(cv_in[i], kOffset, /*smoothed=*/true);
int8_t dir = sign(current - prev_current_);
if (dir != prev_dir_)
{
switch (dir)
{
case 1:
wc_ = kWcOn * (1.f - kRhoOn + kRhoOn * current / 0.040f);
break;
case -1:
wc_ = kWcOff * (1.f + kRhoOff - (kRhoOff) * (yc_) / 0.040f);
break;
case 0:
break;
default:
assert(false);
break;
}
}

const float g = wc_ * dt_ * 0.5f;
const float vc = (current - sc_) * g / (1.f + g);
yc_ = vc + sc_;
sc_ = yc_ + vc;

prev_current_ = current;
prev_dir_ = dir;

const float processed_current = std::max(yc_, 1.01e-5f);
const float rf = 1.f / (std::pow(processed_current, 1.4f)) * kAVac + kBVac;

// Process the audio
// const float amax = (2.f * kC1 * kRa + (kC2 + kC3) * (kRa + rf)) / (kC3 * kRa);
const float a = 0.f; // kNormalizedFb * amax;
const float a1 = 1.f / (rf * kC1);
const float a2 = -1.f / kC1 * (1 / rf + 1.f / kRa);
const float b1 = 1.f / (rf * kC2);
const float b2 = -2.f / (rf * kC2);
const float b3 = b1;
const float d1 = a;

const float yi = in[i];
const float d_o = 1.f / (1.f - f_ * a2);
const float d_x = 1.f / (1.f - f_ * b2);

constexpr float kD2 = -1.f;

if (non_lin_)
{
}
else
{
float yx = (sx_ + f_ * b1 * yi + f_ * b3 * d_o * so_ + f_ * kB4 * sd_ + kB4 * d1 * d_o * so_) * d_x;
yx = yx / (1 - d_x * (f_ * f_ * b3 * d_o * a1 + kB4 * f_ * d1 * d_o * a1 + kB4 * kD2));
yo_ = (so_ + f_ * a1 * yx) * d_o;
const float yd = sd_ + (1.f / f_) * (d1 * yo_ + kD2 * yx);

sx_ = sx_ + 2.f * f_ * (b1 * yi + b2 * yx + b3 * yo_ + kB4 * yd);

so_ = so_ + 2.f * f_ * (a1 * yx + a2 * yo_);

sd_ = -sd_ - (2.f / f_) * (d1 * yo_ + kD2 * yx);
}

out[i] = yo_;
}
sfdsp::GetCurrent(cv_in, cv_in, size, /*smoothed=*/false);
ProcessCurrent(cv_in, cv_in, size);
ProcessAudio(cv_in, in, out, size);
}

void BuchlaLPG::ProcessCurrent(const float* vc_in, float* vc_out, size_t size)
Expand Down Expand Up @@ -205,13 +139,13 @@ void BuchlaLPG::ProcessAudio(const float* vc_in, const float* in, float* out, si
{
const float rf = vc_in[i];

const float a = 0.f; // kNormalizedFb * amax;
constexpr float a = 0.f; // kNormalizedFb * amax;
const float a1 = 1.f / (rf * kC1);
const float a2 = -1.f / kC1 * (1 / rf + 1.f / kRa);
const float b1 = 1.f / (rf * kC2);
const float b2 = -2.f / (rf * kC2);
const float b3 = b1;
const float d1 = a;
constexpr float d1 = a;

const float yi = in[i];
const float d_o = 1.f / (1.f - f_ * a2);
Expand All @@ -224,16 +158,16 @@ void BuchlaLPG::ProcessAudio(const float* vc_in, const float* in, float* out, si
}
else
{
float yx = (sx_ + f_ * b1 * yi + f_ * b3 * d_o * so_ + f_ * kB4 * sd_ + kB4 * d1 * d_o * so_) * d_x;
float yx = (sx_ + f_ * (b1 * yi + b3 * d_o * so_ + kB4 * sd_) + kB4 * d1 * d_o * so_) * d_x;
yx = yx / (1 - d_x * (f_ * f_ * b3 * d_o * a1 + kB4 * f_ * d1 * d_o * a1 + kB4 * kD2));
yo_ = (so_ + f_ * a1 * yx) * d_o;
const float yd = sd_ + (1.f / f_) * (d1 * yo_ + kD2 * yx);
const float yd = sd_ + f_inv_ * (d1 * yo_ + kD2 * yx);

sx_ = sx_ + 2.f * f_ * (b1 * yi + b2 * yx + b3 * yo_ + kB4 * yd);

so_ = so_ + 2.f * f_ * (a1 * yx + a2 * yo_);

sd_ = -sd_ - (2.f / f_) * (d1 * yo_ + kD2 * yx);
sd_ = -sd_ - 2.f * f_inv_ * (d1 * yo_ + kD2 * yx);
}

out[i] = yo_;
Expand Down
2 changes: 1 addition & 1 deletion src/waveguide_gate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void WaveguideGate::Process(Delayline& left_traveling_line, Delayline& right_tra

void WaveguideGate::Process(Waveguide& wave)
{
return Process(wave.left_traveling_line_, wave.right_traveling_line_);
Process(wave.left_traveling_line_, wave.right_traveling_line_);
}

} // namespace sfdsp
5 changes: 5 additions & 0 deletions tests/buchla_lpg_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ TEST(BuchlaLPGTestPerf, Current)
lpg.ProcessCurrent(cv_out.get(), cv_out.get(), kSize);
lpg.ProcessAudio(cv_out.get(), audio_buffer.get(), audio_buffer.get(), kSize);

// for (size_t i = 0; i < kSize; ++i)
// {
// audio_buffer[i] = 0.01f * cv_out[i];
// }

SF_INFO info;
info.channels = 1;
info.samplerate = kSamplerate;
Expand Down
3 changes: 2 additions & 1 deletion tests/perf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_executable(perf_tests
perf_tests.cpp
buchla_lpg_perf.cpp
basicosc_perf.cpp
phaseshaper_perf.cpp)
phaseshaper_perf.cpp
aligned_perf.cpp)
target_include_directories(perf_tests PRIVATE ${doctest_SOURCE_DIR}/doctest)
target_link_libraries(perf_tests PRIVATE nanobench dsp doctest)
76 changes: 76 additions & 0 deletions tests/perf/aligned_perf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "doctest.h"
#include "nanobench.h"
#include <chrono>
#include <memory>

#include <iostream>
#include <stdlib.h>

using namespace ankerl;
using namespace std::chrono_literals;

TEST_CASE("Aligned")
{

constexpr size_t size = 1024 * 1024;

nanobench::Bench bench;
bench.title("BasicOscillator");
bench.relative(true);
bench.minEpochIterations(5);
bench.timeUnit(1ms, "ms");

float* data_in = new float[size];
float* data_out = new float[size];

bench.run("Not-Aligned", [&]() {
for (size_t i = 0; i < size; i++)
{
data_in[i] = i;
}

constexpr size_t block_size = 256;
const size_t block_count = size / block_size;
float* write_ptr = data_out;
for (size_t i = 0; i < block_count; ++i)
{
std::copy(data_in + i * block_size, data_in + (i + 1) * block_size, write_ptr);
write_ptr += block_size;
}
});

for (size_t i = 0; i < size; i++)
{
CHECK(data_in[i] == data_out[i]);
}

delete[] data_in;
delete[] data_out;

float* data_in_aligned = static_cast<float*>(_aligned_malloc(size * sizeof(float), sizeof(float) * 4));
float* data_out_aligned = static_cast<float*>(_aligned_malloc(size * sizeof(float), sizeof(float) * 4));

bench.run("Aligned", [&]() {
for (size_t i = 0; i < size; i++)
{
data_in_aligned[i] = i;
}

constexpr size_t block_size = 256;
const size_t block_count = size / block_size;
float* write_ptr = data_out_aligned;
for (size_t i = 0; i < block_count; ++i)
{
std::copy(data_in_aligned + i * block_size, data_in_aligned + (i + 1) * block_size, write_ptr);
write_ptr += block_size;
}
});

for (size_t i = 0; i < size; i++)
{
CHECK(data_in_aligned[i] == data_out_aligned[i]);
}

_aligned_free(data_in_aligned);
_aligned_free(data_out_aligned);
}
8 changes: 2 additions & 6 deletions tests/perf/buchla_lpg_perf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ TEST_CASE("BuchlaLPG")
std::unique_ptr<float[]> audio_out = std::make_unique<float[]>(kOutputSize);
constexpr size_t kBlockSize = 128;

bench.run("Buchla LPG", [&]() {
// lpg.ProcessBlock(cv_out.get(), audio_buffer.get(), audio_out.get(), kOutputSize);
sfdsp::GetCurrent(cv_out.get(), cv_out.get(), kOutputSize, false);
lpg.ProcessCurrent(cv_out.get(), cv_out.get(), kOutputSize);
lpg.ProcessAudio(cv_out.get(), audio_buffer.get(), audio_buffer.get(), kOutputSize);
});
bench.run("Buchla LPG",
[&]() { lpg.ProcessBlock(cv_out.get(), audio_buffer.get(), audio_out.get(), kOutputSize); });
}

TEST_CASE("BuchlaLPG_Detailed")
Expand Down
Loading

0 comments on commit a5bd4f1

Please sign in to comment.