Skip to content

Commit

Permalink
Change the WTScripter syntax (#4633)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
baconpaul authored Jun 2, 2021
1 parent 972cf9a commit 74ebdba
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
31 changes: 23 additions & 8 deletions src/common/dsp/WavetableScriptEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace Surge
{
namespace WavetableScript
{
std::vector<float> evaluateScriptAtFrame(const std::string &eqn, int resolution, int frame)
std::vector<float> evaluateScriptAtFrame(const std::string &eqn, int resolution, int frame,
int nFrames)
{
#if !HAS_LUAJIT
auto res = std::vector<float>();
Expand All @@ -44,9 +45,13 @@ std::vector<float> 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)
Expand All @@ -55,8 +60,18 @@ std::vector<float> 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))
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/common/dsp/WavetableScriptEvaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float> evaluateScriptAtFrame(const std::string &eqn, int resolution, int frame);
std::vector<float> 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
Expand Down
2 changes: 1 addition & 1 deletion src/gui/SurgeGUIEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/gui/overlays/LuaEditors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ WavetableEquationEditor::WavetableEquationEditor(SurgeGUIEditor *ed, SurgeStorag
currentFrame = std::make_unique<juce::Slider>("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());
}
Expand Down Expand Up @@ -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();
}
Expand Down
10 changes: 5 additions & 5 deletions src/headless/UnitTestsLUA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 74ebdba

Please sign in to comment.