Skip to content

Commit

Permalink
Add storage->reportError to WavetableScriptEvaluator.cpp (#7674)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuoun authored Jun 25, 2024
1 parent 9fea01a commit f0dc41c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Korridor <[email protected]>
Mario Krušelj a.k.a. EvilDragon <http://github.com/mkruselj>
muhl <https://github.com/mynameismuhl>
Marty Lake <[email protected]>
nuoun <https://github.com/nuoun>
Luna Langton <https://github.com/selenologist>
Jacky Ligon <http://jackyligon.com>
Erik-Jan Maalderink <[email protected]>
Expand Down
26 changes: 19 additions & 7 deletions src/common/dsp/WavetableScriptEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace Surge
{
namespace WavetableScript
{
std::vector<float> evaluateScriptAtFrame(const std::string &eqn, int resolution, int frame,
int nFrames)
std::vector<float> evaluateScriptAtFrame(SurgeStorage *storage, const std::string &eqn,
int resolution, int frame, int nFrames)
{
#if HAS_LUA
static lua_State *L = nullptr;
Expand Down Expand Up @@ -93,12 +93,24 @@ std::vector<float> evaluateScriptAtFrame(const std::string &eqn, int resolution,
lua_pop(L, 1);
}
}
lua_pop(L, 1);
}
else
{
// If pcr is not LUA_OK then lua pushes an error string onto the stack. Show this error
std::string luaerr = lua_tostring(L, -1);
if (storage)
storage->reportError(luaerr, "Wavetable Evaluator Runtime Error");
else
std::cerr << luaerr;
}
lua_pop(L, 1); // Error string or pcall result
}
else
{
std::cout << emsg << std::endl;
if (storage)
storage->reportError(emsg, "Wavetable Evaluator Syntax Error");
else
std::cerr << emsg;
lua_pop(L, 1);
}
return values;
Expand All @@ -107,8 +119,8 @@ std::vector<float> evaluateScriptAtFrame(const std::string &eqn, int resolution,
#endif
}

bool constructWavetable(const std::string &eqn, int resolution, int frames, wt_header &wh,
float **wavdata)
bool constructWavetable(SurgeStorage *storage, const std::string &eqn, int resolution, int frames,
wt_header &wh, float **wavdata)
{
auto wd = new float[frames * resolution];
wh.n_samples = resolution;
Expand All @@ -118,7 +130,7 @@ 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, frames);
auto v = evaluateScriptAtFrame(storage, eqn, resolution, i, frames);
memcpy(&(wd[i * resolution]), &(v[0]), resolution * sizeof(float));
}
return true;
Expand Down
8 changes: 4 additions & 4 deletions src/common/dsp/WavetableScriptEvaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ 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,
int nFrames);
std::vector<float> evaluateScriptAtFrame(SurgeStorage *storage, const std::string &eqn,
int resolution, int frame, int nFrames);

/*
* Generate all the data required to call BuildWT. The wavdata here is data you
* must free with delete[]
*/
bool constructWavetable(const std::string &eqn, int resolution, int frames, wt_header &wh,
float **wavdata);
bool constructWavetable(SurgeStorage *storage, const std::string &eqn, int resolution, int frames,
wt_header &wh, float **wavdata);

std::string defaultWavetableFormula();

Expand Down
3 changes: 2 additions & 1 deletion src/surge-testrunner/UnitTestsLUA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ TEST_CASE("Wavetable Script", "[formula]")
{
SECTION("Just The Sines")
{
SurgeStorage storage;
const std::string s = R"FN(
function generate(config)
res = config.xs
Expand All @@ -676,7 +677,7 @@ end
)FN";
for (int fno = 0; fno < 4; ++fno)
{
auto fr = Surge::WavetableScript::evaluateScriptAtFrame(s, 512, fno, 4);
auto fr = Surge::WavetableScript::evaluateScriptAtFrame(nullptr, s, 512, fno, 4);
REQUIRE(fr.size() == 512);
auto dp = 1.0 / (512 - 1);
for (int i = 0; i < 512; ++i)
Expand Down
6 changes: 3 additions & 3 deletions src/surge-xt/gui/overlays/LuaEditors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ void WavetableEquationEditor::rerenderFromUIState()
respt *= 2;

renderer->points = Surge::WavetableScript::evaluateScriptAtFrame(
mainDocument->getAllContent().toStdString(), respt, cfr, nfr);
storage, mainDocument->getAllContent().toStdString(), respt, cfr, nfr);
renderer->frameNumber = cfr;
renderer->repaint();
}
Expand All @@ -1142,8 +1142,8 @@ void WavetableEquationEditor::buttonClicked(juce::Button *button)

wt_header wh;
float *wd = nullptr;
Surge::WavetableScript::constructWavetable(mainDocument->getAllContent().toStdString(),
respt, nfr, wh, &wd);
Surge::WavetableScript::constructWavetable(
storage, mainDocument->getAllContent().toStdString(), respt, nfr, wh, &wd);
storage->waveTableDataMutex.lock();
osc->wt.BuildWT(wd, wh, wh.flags & wtf_is_sample);
osc->wavetable_display_name = "Scripted Wavetable";
Expand Down

0 comments on commit f0dc41c

Please sign in to comment.