Skip to content

Commit

Permalink
adding instrument info to lua mod
Browse files Browse the repository at this point in the history
  • Loading branch information
samba committed Jul 16, 2024
1 parent cd2c290 commit ff510d1
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
Binary file added rendertests/references/modinstrumentinfo.sheet.mid
Binary file not shown.
26 changes: 26 additions & 0 deletions rendertests/tests/modinstrumentinfo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "/lua/com/com"

local function checkinstrument (instrument)
if instrument == nil then
return false
end
if instrument.name == nil then
return false
end
if instrument.children ~= nil then
for _, child in pairs(instrument.children) do
return checkinstrument(child)
end
end
if instrument.midiChannel == nil then
return false
end
return true
end

function perform(events, params, timeinfo)
if checkinstrument(events[1].instrument) == false then
return {}
end
return events
end
26 changes: 26 additions & 0 deletions rendertests/tests/modinstrumentinfo.sheet
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

using "./modinstrumentinfo.lua";

device: _setName=MyDevice _isType=midi _useDevice="FLUID Synth";
instrumentDef: _setName=piano _onDevice=MyDevice _ch=0 _pc=1 _cc=8;
instrumentConf: piano mod modinstrumentinfo;

instrumentDef: _setName=p1 _onDevice=MyDevice _ch=1 _pc=100;
instrumentDef: _setName=p2 _onDevice=MyDevice _ch=2 _pc=12;
instrumentSection: mySection p1 p2;
-- instrumentConf: mySection mod modinstrumentinfo; -- https://github.com/werckme/werckmeister/issues/424

[
instrument: piano;
{
c d e f | g a b c'
}
]

[
instrument: mySection;
{
/mod: modinstrumentinfo/
c, d, e, f, | g, a, b, c
}
]
74 changes: 74 additions & 0 deletions src/compiler/modification/LuaMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static const char *LUA_EVENT_TYPE_UNKNOWN = "unknown";
static const char *LUA_EVENT_PROPETRY_VELOCITY = "velocity";
static const char *LUA_EVENT_PROPETRY_DURATION = "duration";
static const char *LUA_EVENT_PROPETRY_TYING = "isTied";
static const char *LUA_EVENT_PROPETRY_INSTRUMENT = "instrument";
static const char *LUA_EVENT_PROPETRY_OFFSET = "offset";
static const char *LUA_EVENT_PROPETRY_CC_NR = "ccNr";
static const char *LUA_EVENT_PROPETRY_CC_VALUE = "ccValue";
Expand All @@ -33,6 +34,14 @@ static const char *LUA_EVENT_PROPETRY_TOAL_TIED_DURATION = "totalTiedDuration";
static const char *LUA_EVENT_PROPERTY_TIED_DURATION = "tiedDuration";
static const char* LUA_EVENT_PROPERTY_TRACK_ID = "trackId";
static const char* LUA_EVENT_PROPERTY_VOICE_ID = "voiceId";
static const char* LUA_INSTRUMENT_PROPERTY_NAME = "name";
static const char* LUA_INSTRUMENT_PROPERTY_VOLUME = "volume";
static const char* LUA_INSTRUMENT_PROPERTY_PAN = "pan";
static const char* LUA_INSTRUMENT_PROPERTY_MIDI_CHANNEL = "midiChannel";
static const char* LUA_INSTRUMENT_PROPERTY_MIDI_MSB = "midiMsb";
static const char* LUA_INSTRUMENT_PROPERTY_MIDI_LSB = "midiLsb";
static const char* LUA_INSTRUMENT_PROPERTY_MIDI_PC = "midiPc";
static const char* LUA_INSTRUMENT_PROPERTY_CHILDREN = "children";

namespace compiler
{
Expand All @@ -48,6 +57,7 @@ namespace compiler
void push(lua_State *L);
void pushPitches(lua_State *L);
void pushTags(lua_State *L);
void pushInstrument(lua_State *L, AInstrumentDefPtr instrument);
void pushPitchBendValue(lua_State *L, int top, const documentModel::Event &event);
const char *getTypename() const;
};
Expand All @@ -68,6 +78,10 @@ namespace compiler
lua_pushstring(L, LUA_EVENT_PITCH_PROPETRY_TAGS);
pushTags(L);
lua_settable(L, top);
// instrument
lua_pushstring(L, LUA_EVENT_PROPETRY_INSTRUMENT);
pushInstrument(L, ctx->currentInstrumentDef());
lua_settable(L, top);
// offset
lua::setTableValue(L, LUA_EVENT_PROPETRY_OFFSET, top, event->offset / com::PPQ);

Expand Down Expand Up @@ -145,6 +159,66 @@ namespace compiler
lua_settable(L, top);
}
}
void LuaEvent::pushInstrument(lua_State *L, AInstrumentDefPtr instrument)
{
lua_createtable(L, event->tags.size(), 0);
if (!instrument)
{
return;
}
auto top = lua_gettop(L);
//
lua_pushstring(L, LUA_INSTRUMENT_PROPERTY_NAME);
lua_pushstring(L, instrument->uname.c_str());
lua_settable(L, top);
//
lua_pushstring(L, LUA_INSTRUMENT_PROPERTY_VOLUME);
lua_pushnumber(L, instrument->volume);
lua_settable(L, top);
//
lua_pushstring(L, LUA_INSTRUMENT_PROPERTY_PAN);
lua_pushnumber(L, instrument->pan);
lua_settable(L, top);
//
auto instrumentSection = std::dynamic_pointer_cast<InstrumentSectionDef>(instrument);
if (instrumentSection)
{
lua_pushstring(L, LUA_INSTRUMENT_PROPERTY_CHILDREN);
lua_createtable(L, instrumentSection->instrumentNames.size(), 0);
auto childrenTop = lua_gettop(L);
int index = 1;
for(const auto& uname : instrumentSection->instrumentNames)
{
auto instrumentDef = ctx->getInstrumentDef(uname);
lua_pushinteger(L, index++);
pushInstrument(L, instrumentDef);
lua_settable(L, childrenTop);
}
lua_settable(L, top);
return;
}
//
auto midiInstrument = std::dynamic_pointer_cast<MidiInstrumentDef>(instrument);
if (!midiInstrument)
{
return;
}
lua_pushstring(L, LUA_INSTRUMENT_PROPERTY_MIDI_CHANNEL);
lua_pushinteger(L, midiInstrument->channel);
lua_settable(L, top);
//
lua_pushstring(L, LUA_INSTRUMENT_PROPERTY_MIDI_MSB);
lua_pushinteger(L, midiInstrument->bankMsb);
lua_settable(L, top);
//
lua_pushstring(L, LUA_INSTRUMENT_PROPERTY_MIDI_LSB);
lua_pushinteger(L, midiInstrument->bankLsb);
lua_settable(L, top);
//
lua_pushstring(L, LUA_INSTRUMENT_PROPERTY_MIDI_PC);
lua_pushinteger(L, midiInstrument->pc);
lua_settable(L, top);
}
const char *LuaEvent::getTypename() const
{
using namespace documentModel;
Expand Down

0 comments on commit ff510d1

Please sign in to comment.