Skip to content

Commit

Permalink
Formula voicestate changes:
Browse files Browse the repository at this point in the history
- Rename voiceMax -> polylimit
- Rename voiceid -> voiceOrderAtCreate
- Add constexpr const char definitions for "surge" and "shared" table names
- Replace "return rows;" with "continue;" in createDebugDataOfModState() error condition
  • Loading branch information
nuoun committed Aug 7, 2024
1 parent 1ee52c6 commit 464afe2
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/common/LuaSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ bool Surge::LuaSupport::setSurgeFunctionEnvironment(lua_State *L)
return true;
}

bool Surge::LuaSupport::loadSurgePrelude(lua_State *s)
bool Surge::LuaSupport::loadSurgePrelude(lua_State *s, const char *surgeTableName)
{
#if HAS_LUA
auto guard = SGLD("loadPrologue", s);
Expand All @@ -203,7 +203,7 @@ bool Surge::LuaSupport::loadSurgePrelude(lua_State *s)
auto lua_size = lua_script.size();
auto load_stat = luaL_loadbuffer(s, lua_script.c_str(), lua_size, lua_script.c_str());
auto pcall = lua_pcall(s, 0, 1, 0);
lua_setglobal(s, "surge");
lua_setglobal(s, surgeTableName);
#endif
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/LuaSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ int parseStringDefiningMultipleFunctions(lua_State *s, const std::string &defini
bool setSurgeFunctionEnvironment(lua_State *s);

/*
* Call this function with a LUA state and it will introduce the global
* Call this function with a LUA state and a tablename and it will introduce the global
* 'surge' which is the surge prelude
*/
bool loadSurgePrelude(lua_State *s);
bool loadSurgePrelude(lua_State *s, const char *surgeTableName);

/*
* Call this function to get a string representation of the prelude
Expand Down
2 changes: 1 addition & 1 deletion src/common/dsp/SurgeVoice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ SurgeVoice::SurgeVoice(SurgeStorage *storage, SurgeSceneStorage *oscene, pdata *
state.channel = channel;

state.voiceOrderAtCreate = voiceOrder;
state.voiceMax = storage->getPatch().polylimit.val.i;
state.polylimit = storage->getPatch().polylimit.val.i;

state.velocity = velocity;
state.fvel = velocity / 127.f;
Expand Down
2 changes: 1 addition & 1 deletion src/common/dsp/SurgeVoiceState.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct SurgeVoiceState
MidiKeyState *keyState;
MidiChannelState *mainChannelState;
MidiChannelState *voiceChannelState;
int key, velocity, channel, scene_id, releasevelocity, voiceMax;
int key, velocity, channel, scene_id, releasevelocity, polylimit;
float portasrc_key, portaphase;
bool porta_doretrigger;

Expand Down
24 changes: 12 additions & 12 deletions src/common/dsp/modulators/FormulaModulationHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ bool prepareForEvaluation(SurgeStorage *storage, FormulaModulatorStorage *fs, Ev

if (firstTimeThrough)
{
// setup global table
// setup shared table
lua_newtable(s.L);
lua_setglobal(s.L, "shared");
lua_setglobal(s.L, sharedTableName);

// setup prelude
Surge::LuaSupport::loadSurgePrelude(s.L);
Surge::LuaSupport::loadSurgePrelude(s.L, surgeTableName);
auto reserved0 = std::string(R"FN(
function surge_reserved_formula_error_stub(m)
return 0;
Expand Down Expand Up @@ -485,10 +485,10 @@ void valueAt(int phaseIntPart, float phaseFracPart, SurgeStorage *storage,
lua_setfield(s->L, -2, "cycle");

// fake voice count for display calls
int vcount = 1;
int voiceCount = 1;
if (storage->voiceCount != 0)
vcount = storage->voiceCount;
lua_pushinteger(s->L, vcount);
voiceCount = storage->voiceCount;
lua_pushinteger(s->L, voiceCount);
lua_setfield(s->L, -2, "voice_count");

auto addn = [s](const char *q, float f) {
Expand Down Expand Up @@ -532,8 +532,8 @@ void valueAt(int phaseIntPart, float phaseFracPart, SurgeStorage *storage,
addn("channel", s->channel);
addb("released", s->released);

addn("voice_id", s->voiceid);
addn("voice_max", s->voicemax);
addn("voice_id", s->voiceOrderAtCreate);
addn("voice_polylimit", s->polylimit);

addn("poly_at", s->polyat);
addn("mpe_bend", s->mpebend);
Expand Down Expand Up @@ -820,15 +820,15 @@ std::vector<DebugRow> createDebugDataOfModState(const EvaluatorState &es)
}
};

std::vector<std::string> tablesList = {es.stateName, "shared"};
std::vector<std::string> tablesList = {es.stateName, sharedTableName};
for (const auto &t : tablesList)
{
lua_getglobal(es.L, t.c_str());
if (!lua_istable(es.L, -1))
{
lua_pop(es.L, -1);
rows.emplace_back(0, "Error", "Not a Table");
return rows;
continue;
}
rec(0, false);
lua_pop(es.L, -1);
Expand Down Expand Up @@ -916,8 +916,8 @@ void setupEvaluatorStateFrom(EvaluatorState &s, const SurgeVoice *v)
s.velocity = v->state.velocity;
s.releasevelocity = v->state.releasevelocity;

s.voiceid = v->state.voiceOrderAtCreate;
s.voicemax = v->state.voiceMax;
s.voiceOrderAtCreate = v->state.voiceOrderAtCreate;
s.polylimit = v->state.polylimit;

s.polyat =
v->storage
Expand Down
6 changes: 4 additions & 2 deletions src/common/dsp/modulators/FormulaModulationHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ struct GlobalData
};

static constexpr int max_formula_outputs{max_lfo_indices};
static constexpr const char *surgeTableName{"surge"};
static constexpr const char *sharedTableName{"shared"};

struct EvaluatorState
{
Expand All @@ -68,8 +70,8 @@ struct EvaluatorState

// voice features
bool isVoice;
int key{60}, channel{0}, velocity{0}, releasevelocity{0}, voicemax{1}, mpebendrange{24};
int64_t voiceid{1L};
int key{60}, channel{0}, velocity{0}, releasevelocity{0}, polylimit{1}, mpebendrange{24};
int64_t voiceOrderAtCreate{1L};
float polyat{0}, mpebend{0}, mpetimbre{0}, mpepressure{0};

// scene features
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 @@ -159,7 +159,8 @@ TEST_CASE("Surge Prelude", "[lua]")
REQUIRE(L);
luaL_openlibs(L);

REQUIRE(Surge::LuaSupport::loadSurgePrelude(L));
static constexpr const char *surgeTableName{"surge"};
REQUIRE(Surge::LuaSupport::loadSurgePrelude(L, surgeTableName));

std::string emsg;
REQUIRE(Surge::LuaSupport::parseStringDefiningFunction(
Expand Down

0 comments on commit 464afe2

Please sign in to comment.