Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Last active May 21, 2025 14:14
Show Gist options
  • Save Xenakios/fbb8e4b69469cdb7e6f3de93009282b3 to your computer and use it in GitHub Desktop.
Save Xenakios/fbb8e4b69469cdb7e6f3de93009282b3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <print>
#include "sst/basic-blocks/simd/setup.h"
#include "include/sst/waveshapers.h"
#include <cmath>
#include <vector>
#include <random>
#include <algorithm>
int main()
{
std::vector<int> shapertypes;
for (int i = 1; i < 45; ++i)
{
shapertypes.push_back(i);
}
std::mt19937 rng{1003};
std::shuffle(shapertypes.begin(), shapertypes.end(), rng);
sst::waveshapers::QuadWaveshaperState wss{};
float R[sst::waveshapers::n_waveshaper_registers];
auto wsptr = sst::waveshapers::GetQuadWaveshaper(sst::waveshapers::WaveshaperType::wst_none);
int curtypeindex = -1;
int olen = 45 * 22050;
double sr = 44100.0 * 4;
for (int i = 0; i < olen; ++i)
{
if (i % 12001 == 0)
{
++curtypeindex;
if (curtypeindex == shapertypes.size())
curtypeindex = 0;
int shindex = shapertypes[curtypeindex];
auto wstype = sst::waveshapers::WaveshaperType(shindex);
std::cout << "switching to " << sst::waveshapers::wst_names[shindex] << "\n";
initializeWaveshaperRegister(wstype, R);
for (int i = 0; i < sst::waveshapers::n_waveshaper_registers; ++i)
{
wss.R[i] = _mm_set1_ps(R[i]);
}
wss.init = _mm_cmpneq_ps(_mm_setzero_ps(), _mm_setzero_ps());
wsptr = sst::waveshapers::GetQuadWaveshaper(wstype);
}
if (!wsptr)
return 1;
float insig = 1.0 * std::sin(M_PI * 2.0 / sr * 1000.0 * i);
float din alignas(16)[4] = {0, 0, 0, 0};
din[0] = insig;
din[1] = insig;
auto dat = _mm_load_ps(din);
auto drv = _mm_set1_ps(0.25);
dat = wsptr(&wss, dat, drv);
float res alignas(16)[4];
_mm_store_ps(res, dat);
if (res[0] < -2.0 || res[0] > 2.0 || std::isnan(res[0]) || std::isinf(res[0]) ||
res[1] < -2.0 || res[1] > 2.0 || std::isnan(res[1]) || std::isinf(res[1]))
{
std::print("bad sample produced by waveshaper {} {}\n", res[0], res[1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment