From 74ebdba86a1006b1c50e85aaf3877bbfcfe7580f Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 2 Jun 2021 16:16:10 -0400 Subject: [PATCH] Change the WTScripter syntax (#4633) Basically make it a one arg function of a config structure so we can pass in mroe as time goes on and as we want. --- src/common/dsp/WavetableScriptEvaluator.cpp | 31 +++++++++++++++------ src/common/dsp/WavetableScriptEvaluator.h | 3 +- src/gui/SurgeGUIEditor.cpp | 2 +- src/gui/overlays/LuaEditors.cpp | 4 +-- src/headless/UnitTestsLUA.cpp | 10 +++---- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/common/dsp/WavetableScriptEvaluator.cpp b/src/common/dsp/WavetableScriptEvaluator.cpp index ea49dc42657..589e46ff751 100644 --- a/src/common/dsp/WavetableScriptEvaluator.cpp +++ b/src/common/dsp/WavetableScriptEvaluator.cpp @@ -20,7 +20,8 @@ namespace Surge { namespace WavetableScript { -std::vector evaluateScriptAtFrame(const std::string &eqn, int resolution, int frame) +std::vector evaluateScriptAtFrame(const std::string &eqn, int resolution, int frame, + int nFrames) { #if !HAS_LUAJIT auto res = std::vector(); @@ -44,9 +45,13 @@ std::vector evaluateScriptAtFrame(const std::string &eqn, int resolution, { Surge::LuaSupport::setSurgeFunctionEnvironment(L); /* - * Alright so we want the stack to be an array of 0...1 and a frame - * Right now the stack is just our generation function so + * Alright so we want the stack to be the config table which + * contains the xs, contains n, contains ntables, etc.. so */ + lua_createtable(L, 0, 10); + + // xs is an array of the x locations in phase space + lua_pushstring(L, "xs"); lua_createtable(L, resolution, 0); double dp = 1.0 / (resolution - 1); for (auto i = 0; i < resolution; ++i) @@ -55,8 +60,18 @@ std::vector evaluateScriptAtFrame(const std::string &eqn, int resolution, lua_pushnumber(L, i * dp); lua_settable(L, -3); } + lua_settable(L, -3); + + lua_pushstring(L, "n"); lua_pushinteger(L, frame); - auto pcr = lua_pcall(L, 2, 1, 0); + lua_settable(L, -3); + + lua_pushstring(L, "nTables"); + lua_pushinteger(L, nFrames); + lua_settable(L, -3); + + // So stack is now the table and the function + auto pcr = lua_pcall(L, 1, 1, 0); if (pcr == LUA_OK) { if (lua_istable(L, -1)) @@ -99,22 +114,22 @@ bool constructWavetable(const std::string &eqn, int resolution, int frames, wt_h for (int i = 0; i < frames; ++i) { - auto v = evaluateScriptAtFrame(eqn, resolution, i); + auto v = evaluateScriptAtFrame(eqn, resolution, i, frames); memcpy(&(wd[i * resolution]), &(v[0]), resolution * sizeof(float)); } return true; } std::string defaultWavetableFormula() { - return R"FN(function generate(xs,n) + return R"FN(function generate(config) --- This function was inserted as a guide, since the wavetable editor in this patch/oscillator has no --- generator function. The function takes an array of x values (xs) and a frame number (n) and --- generates the result as the n-th frame. The sample below generates a Fourier sine to saw --- which, remember, is: sum 2 / pi n * sin n x res = {} - for i,x in ipairs(xs) do + for i,x in ipairs(config.xs) do lv = 0 - for q = 1,(n+1) do + for q = 1,(config.n+1) do lv = lv + 2 * sin ( q * x * 2 * pi ) / ( pi * q ) end res[i] = lv diff --git a/src/common/dsp/WavetableScriptEvaluator.h b/src/common/dsp/WavetableScriptEvaluator.h index 5046cb06940..d5ea2ffdec8 100644 --- a/src/common/dsp/WavetableScriptEvaluator.h +++ b/src/common/dsp/WavetableScriptEvaluator.h @@ -41,7 +41,8 @@ namespace WavetableScript * not at the evaluation or synthesis time. As such I expect you call it from * one thread at a time and just you know generally be careful. */ -std::vector evaluateScriptAtFrame(const std::string &eqn, int resolution, int frame); +std::vector evaluateScriptAtFrame(const std::string &eqn, int resolution, int frame, + int nFrames); /* * Generate all the data required to call BuildWT. The wavdata here is data you diff --git a/src/gui/SurgeGUIEditor.cpp b/src/gui/SurgeGUIEditor.cpp index 2320a7e6e88..f2b6d1c7891 100644 --- a/src/gui/SurgeGUIEditor.cpp +++ b/src/gui/SurgeGUIEditor.cpp @@ -8255,7 +8255,7 @@ void SurgeGUIEditor::showWavetableScripter() c->takeOwnership(std::move(pt)); addEditorOverlay(c, "Wavetable Editor", FORMULA_EDITOR, CPoint(px, py), false, true, - [this]() {}); + [this]() { frame->juceComponent()->repaint(); }); } void SurgeGUIEditor::closeWavetableScripter() diff --git a/src/gui/overlays/LuaEditors.cpp b/src/gui/overlays/LuaEditors.cpp index e86a3b845a9..b0aa08745ad 100644 --- a/src/gui/overlays/LuaEditors.cpp +++ b/src/gui/overlays/LuaEditors.cpp @@ -258,7 +258,7 @@ WavetableEquationEditor::WavetableEquationEditor(SurgeGUIEditor *ed, SurgeStorag currentFrame = std::make_unique("currF"); currentFrame->setSliderStyle(juce::Slider::LinearVertical); currentFrame->setTextBoxStyle(juce::Slider::NoTextBox, true, 0, 0); - currentFrame->setMinAndMaxValues(0.0, 10.0); + currentFrame->setRange(0.0, 10.0); currentFrame->addListener(this); addAndMakeVisible(currentFrame.get()); } @@ -315,7 +315,7 @@ void WavetableEquationEditor::rerenderFromUIState() respt *= 2; renderer->points = Surge::WavetableScript::evaluateScriptAtFrame( - mainDocument->getAllContent().toStdString(), respt, cfr); + mainDocument->getAllContent().toStdString(), respt, cfr, nfr); renderer->frameNumber = cfr; renderer->repaint(); } diff --git a/src/headless/UnitTestsLUA.cpp b/src/headless/UnitTestsLUA.cpp index 675ac63a0c4..d3bc5457f0d 100644 --- a/src/headless/UnitTestsLUA.cpp +++ b/src/headless/UnitTestsLUA.cpp @@ -433,17 +433,17 @@ TEST_CASE("WavetableScript", "[formula]") SECTION("Just the Sins") { const std::string s = R"FN( -function generate(xs, n) - res = xs - for i,x in ipairs(xs) do - res[i] = math.sin(x * (n+1) * 2 * math.pi) +function generate(config) + res = config.xs + for i,x in ipairs(config.xs) do + res[i] = math.sin(x * (config.n+1) * 2 * math.pi) end return res end )FN"; for (int fno = 0; fno < 4; ++fno) { - auto fr = Surge::WavetableScript::evaluateScriptAtFrame(s, 512, fno); + auto fr = Surge::WavetableScript::evaluateScriptAtFrame(s, 512, fno, 4); REQUIRE(fr.size() == 512); auto dp = 1.0 / (512 - 1); for (int i = 0; i < 512; ++i)