diff --git a/rendertests/references/modinstrumentinfo.sheet.mid b/rendertests/references/modinstrumentinfo.sheet.mid new file mode 100644 index 00000000..f70f4a1d Binary files /dev/null and b/rendertests/references/modinstrumentinfo.sheet.mid differ diff --git a/rendertests/tests/modinstrumentinfo.lua b/rendertests/tests/modinstrumentinfo.lua new file mode 100644 index 00000000..82569169 --- /dev/null +++ b/rendertests/tests/modinstrumentinfo.lua @@ -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 \ No newline at end of file diff --git a/rendertests/tests/modinstrumentinfo.sheet b/rendertests/tests/modinstrumentinfo.sheet new file mode 100644 index 00000000..588c9266 --- /dev/null +++ b/rendertests/tests/modinstrumentinfo.sheet @@ -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 +} +] \ No newline at end of file diff --git a/src/compiler/modification/LuaMod.cpp b/src/compiler/modification/LuaMod.cpp index a9533aab..19284647 100644 --- a/src/compiler/modification/LuaMod.cpp +++ b/src/compiler/modification/LuaMod.cpp @@ -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"; @@ -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 { @@ -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; }; @@ -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); @@ -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(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(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;