1 |
/* |
/* |
2 |
* Copyright (c) 2014 - 2020 Christian Schoenebeck |
* Copyright (c) 2014 - 2021 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
42 |
if (!pEngine->EventByID(eventID)) firstDead = i; |
if (!pEngine->EventByID(eventID)) firstDead = i; |
43 |
} |
} |
44 |
} |
} |
45 |
|
if (firstDead >= 0) |
46 |
|
remove(firstDead, size() - firstDead); |
47 |
|
|
48 |
append(eventID); |
append(eventID); |
49 |
} |
} |
90 |
* channels. |
* channels. |
91 |
* |
* |
92 |
* @param text - source code of script |
* @param text - source code of script |
93 |
|
* @param patchVars - 'patch' variables being overridden by instrument |
94 |
*/ |
*/ |
95 |
void InstrumentScript::load(const String& text) { |
void InstrumentScript::load(const String& text, |
96 |
|
const std::map<String,String>& patchVars) |
97 |
|
{ |
98 |
dmsg(1,("Loading real-time instrument script ... ")); |
dmsg(1,("Loading real-time instrument script ... ")); |
99 |
|
|
100 |
// hand back old script reference and VM execution contexts |
// hand back old script reference and VM execution contexts |
106 |
AbstractInstrumentManager* pManager = |
AbstractInstrumentManager* pManager = |
107 |
dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager()); |
dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager()); |
108 |
|
|
109 |
// get new script reference |
/* |
110 |
parserContext = pManager->scripts.Borrow(text, pEngineChannel); |
Get new script reference. |
111 |
|
|
112 |
|
Note: every engine channel now has its own compiled script object |
113 |
|
(a.k.a. VMParserContext). Originally a compiled script was shared by |
114 |
|
multiple engine channels. This was wrong: we cannot share compiled |
115 |
|
script instances among multiple engine channels (parts), for two |
116 |
|
reasons: |
117 |
|
|
118 |
|
1. VMParserContext not only encompasses the compiled tree |
119 |
|
presentation of the requested script, but also global variables |
120 |
|
and we don't want those global variables to be modified by |
121 |
|
different sampler parts, as this would not be expected behaviour |
122 |
|
by instrument script authors. |
123 |
|
|
124 |
|
2. If there is more than one sampler engine instance (e.g. if there |
125 |
|
are multiple audio output device instances) this would even crash, |
126 |
|
because each sampler engine instance has its own ScriptVM |
127 |
|
instance, and a (VM)ParserContext is always tied to exactly one |
128 |
|
ScriptVM instance. |
129 |
|
|
130 |
|
We would not be buying much by sharing compiled scripts anyway, as a |
131 |
|
script usually compiles in couple microseconds and RAM usage is also |
132 |
|
neglectable. |
133 |
|
*/ |
134 |
|
parserContext = pManager->scripts.Borrow({ |
135 |
|
.code = text, |
136 |
|
.patchVars = patchVars, |
137 |
|
.engineChannel = pEngineChannel /* unique owner of script */ |
138 |
|
}, |
139 |
|
pEngineChannel /* who is asking to borrow */ |
140 |
|
); |
141 |
if (!parserContext->errors().empty()) { |
if (!parserContext->errors().empty()) { |
142 |
std::vector<ParserIssue> errors = parserContext->errors(); |
std::vector<ParserIssue> errors = parserContext->errors(); |
143 |
std::cerr << "[ScriptVM] Could not load instrument script, there were " |
std::cerr << "[ScriptVM] Could not load instrument script, there were " |
174 |
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
175 |
it->reset(); |
it->reset(); |
176 |
} |
} |
|
pEvents->clear(); |
|
177 |
} |
} |
178 |
|
pEvents->clear(); // outside of upper block, as loop below must always start from cleared list |
179 |
|
|
180 |
// create new VM execution contexts for new script |
// create new VM execution contexts for new script |
181 |
while (!pEvents->poolIsEmpty()) { |
while (!pEvents->poolIsEmpty()) { |
214 |
pEvents->clear(); |
pEvents->clear(); |
215 |
while (!pEvents->poolIsEmpty()) { |
while (!pEvents->poolIsEmpty()) { |
216 |
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
217 |
|
if (!it) break; |
218 |
if (it->execCtx) { |
if (it->execCtx) { |
219 |
// free VM execution context object |
// free VM execution context object |
220 |
delete it->execCtx; |
delete it->execCtx; |
221 |
it->execCtx = NULL; |
it->execCtx = NULL; |
222 |
// free C array of handler pointers |
// free C array of handler pointers |
223 |
delete [] it->handlers; |
delete [] it->handlers; |
224 |
|
it->handlers = NULL; |
225 |
} |
} |
226 |
} |
} |
227 |
pEvents->clear(); |
pEvents->clear(); |
274 |
|
|
275 |
InstrumentScriptVM::InstrumentScriptVM() : |
InstrumentScriptVM::InstrumentScriptVM() : |
276 |
m_event(NULL), m_fnPlayNote(this), m_fnSetController(this), |
m_event(NULL), m_fnPlayNote(this), m_fnSetController(this), |
277 |
|
m_fnSetRpn(this), m_fnSetNrpn(this), |
278 |
m_fnIgnoreEvent(this), m_fnIgnoreController(this), m_fnNoteOff(this), |
m_fnIgnoreEvent(this), m_fnIgnoreController(this), m_fnNoteOff(this), |
279 |
m_fnSetEventMark(this), m_fnDeleteEventMark(this), m_fnByMarks(this), |
m_fnSetEventMark(this), m_fnDeleteEventMark(this), m_fnByMarks(this), |
280 |
m_fnChangeVol(this), m_fnChangeVolTime(this), |
m_fnChangeVol(this), m_fnChangeVolTime(this), |
442 |
// built-in script functions of this class |
// built-in script functions of this class |
443 |
if (name == "play_note") return &m_fnPlayNote; |
if (name == "play_note") return &m_fnPlayNote; |
444 |
else if (name == "set_controller") return &m_fnSetController; |
else if (name == "set_controller") return &m_fnSetController; |
445 |
|
else if (name == "set_rpn") return &m_fnSetRpn; |
446 |
|
else if (name == "set_nrpn") return &m_fnSetNrpn; |
447 |
else if (name == "ignore_event") return &m_fnIgnoreEvent; |
else if (name == "ignore_event") return &m_fnIgnoreEvent; |
448 |
else if (name == "ignore_controller") return &m_fnIgnoreController; |
else if (name == "ignore_controller") return &m_fnIgnoreController; |
449 |
else if (name == "note_off") return &m_fnNoteOff; |
else if (name == "note_off") return &m_fnNoteOff; |