1 |
/* |
2 |
* Copyright (c) 2014 - 2016 Christian Schoenebeck |
3 |
* |
4 |
* http://www.linuxsampler.org |
5 |
* |
6 |
* This file is part of LinuxSampler and released under the same terms. |
7 |
* See README file for details. |
8 |
*/ |
9 |
|
10 |
#include "../../common/global_private.h" |
11 |
#include "InstrumentScriptVM.h" |
12 |
#include "../AbstractEngineChannel.h" |
13 |
#include "../../common/global_private.h" |
14 |
#include "AbstractInstrumentManager.h" |
15 |
#include "MidiKeyboardManager.h" |
16 |
|
17 |
namespace LinuxSampler { |
18 |
|
19 |
/////////////////////////////////////////////////////////////////////// |
20 |
// class 'EventGroup' |
21 |
|
22 |
void EventGroup::insert(int eventID) { |
23 |
if (contains(eventID)) return; |
24 |
|
25 |
AbstractEngine* pEngine = m_script->pEngineChannel->pEngine; |
26 |
|
27 |
// before adding the new event ID, check if there are any dead events |
28 |
// and remove them in that case, before otherwise we might run in danger |
29 |
// to run out of free space on this group for event IDs if a lot of |
30 |
// events die before being removed explicitly from the group by script |
31 |
// |
32 |
// NOTE: or should we do this "dead ones" check only once in a while? |
33 |
int firstDead = -1; |
34 |
for (int i = 0; i < size(); ++i) { |
35 |
if (firstDead >= 0) { |
36 |
if (pEngine->EventByID(eventID)) { |
37 |
remove(firstDead, i - firstDead); |
38 |
firstDead = -1; |
39 |
} |
40 |
} else { |
41 |
if (!pEngine->EventByID(eventID)) firstDead = i; |
42 |
} |
43 |
} |
44 |
|
45 |
append(eventID); |
46 |
} |
47 |
|
48 |
void EventGroup::erase(int eventID) { |
49 |
int index = find(eventID); |
50 |
remove(index); |
51 |
} |
52 |
|
53 |
/////////////////////////////////////////////////////////////////////// |
54 |
// class 'InstrumentScript' |
55 |
|
56 |
InstrumentScript::InstrumentScript(AbstractEngineChannel* pEngineChannel) { |
57 |
parserContext = NULL; |
58 |
bHasValidScript = false; |
59 |
handlerInit = NULL; |
60 |
handlerNote = NULL; |
61 |
handlerRelease = NULL; |
62 |
handlerController = NULL; |
63 |
pEvents = NULL; |
64 |
for (int i = 0; i < 128; ++i) |
65 |
pKeyEvents[i] = NULL; |
66 |
this->pEngineChannel = pEngineChannel; |
67 |
for (int i = 0; i < INSTR_SCRIPT_EVENT_GROUPS; ++i) |
68 |
eventGroups[i].setScript(this); |
69 |
} |
70 |
|
71 |
InstrumentScript::~InstrumentScript() { |
72 |
resetAll(); |
73 |
if (pEvents) { |
74 |
for (int i = 0; i < 128; ++i) delete pKeyEvents[i]; |
75 |
delete pEvents; |
76 |
} |
77 |
} |
78 |
|
79 |
/** @brief Load real-time instrument script. |
80 |
* |
81 |
* Loads the real-time instrument script given by @a text on the engine |
82 |
* channel this InstrumentScript object belongs to (defined by |
83 |
* pEngineChannel member variable). The sampler engine's resource manager is |
84 |
* used to allocate and share equivalent scripts on multiple engine |
85 |
* channels. |
86 |
* |
87 |
* @param text - source code of script |
88 |
*/ |
89 |
void InstrumentScript::load(const String& text) { |
90 |
dmsg(1,("Loading real-time instrument script ... ")); |
91 |
|
92 |
// hand back old script reference and VM execution contexts |
93 |
// (if not done already) |
94 |
unload(); |
95 |
|
96 |
code = text; |
97 |
|
98 |
AbstractInstrumentManager* pManager = |
99 |
dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager()); |
100 |
|
101 |
// get new script reference |
102 |
parserContext = pManager->scripts.Borrow(text, pEngineChannel); |
103 |
if (!parserContext->errors().empty()) { |
104 |
std::vector<ParserIssue> errors = parserContext->errors(); |
105 |
std::cerr << "[ScriptVM] Could not load instrument script, there were " |
106 |
<< errors.size() << " parser errors:\n"; |
107 |
for (int i = 0; i < errors.size(); ++i) |
108 |
errors[i].dump(); |
109 |
return; // stop here if there were any parser errors |
110 |
} |
111 |
|
112 |
handlerInit = parserContext->eventHandlerByName("init"); |
113 |
handlerNote = parserContext->eventHandlerByName("note"); |
114 |
handlerRelease = parserContext->eventHandlerByName("release"); |
115 |
handlerController = parserContext->eventHandlerByName("controller"); |
116 |
bHasValidScript = |
117 |
handlerInit || handlerNote || handlerRelease || handlerController; |
118 |
|
119 |
// amount of script handlers each script event has to execute |
120 |
int handlerExecCount = 0; |
121 |
if (handlerNote || handlerRelease || handlerController) // only one of these are executed after "init" handler |
122 |
handlerExecCount++; |
123 |
|
124 |
// create script event pool (if it doesn't exist already) |
125 |
if (!pEvents) { |
126 |
pEvents = new Pool<ScriptEvent>(CONFIG_MAX_EVENTS_PER_FRAGMENT); |
127 |
for (int i = 0; i < 128; ++i) |
128 |
pKeyEvents[i] = new RTList<ScriptEvent>(pEvents); |
129 |
// reset RTAVLNode's tree node member variables after nodes are allocated |
130 |
// (since we can't use a constructor right now, we do that initialization here) |
131 |
while (!pEvents->poolIsEmpty()) { |
132 |
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
133 |
it->reset(); |
134 |
} |
135 |
pEvents->clear(); |
136 |
} |
137 |
|
138 |
// create new VM execution contexts for new script |
139 |
while (!pEvents->poolIsEmpty()) { |
140 |
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
141 |
it->execCtx = pEngineChannel->pEngine->pScriptVM->createExecContext( |
142 |
parserContext |
143 |
); |
144 |
it->handlers = new VMEventHandler*[handlerExecCount+1]; |
145 |
} |
146 |
pEvents->clear(); |
147 |
|
148 |
dmsg(1,("Done\n")); |
149 |
} |
150 |
|
151 |
/** @brief Unload real-time instrument script. |
152 |
* |
153 |
* Unloads the currently used real-time instrument script and frees all |
154 |
* resources allocated for that script. The sampler engine's resource manager |
155 |
* is used to share equivalent scripts among multiple sampler channels, and |
156 |
* to deallocate the parsed script once not used on any engine channel |
157 |
* anymore. |
158 |
* |
159 |
* Calling this method will however not clear the @c code member variable. |
160 |
* Thus, the script can be parsed again afterwards. |
161 |
*/ |
162 |
void InstrumentScript::unload() { |
163 |
//dmsg(1,("InstrumentScript::unload(this=0x%llx)\n", this)); |
164 |
|
165 |
if (parserContext) |
166 |
dmsg(1,("Unloading current instrument script.\n")); |
167 |
|
168 |
resetEvents(); |
169 |
|
170 |
// free allocated VM execution contexts |
171 |
if (pEvents) { |
172 |
pEvents->clear(); |
173 |
while (!pEvents->poolIsEmpty()) { |
174 |
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
175 |
if (it->execCtx) { |
176 |
// free VM execution context object |
177 |
delete it->execCtx; |
178 |
it->execCtx = NULL; |
179 |
// free C array of handler pointers |
180 |
delete [] it->handlers; |
181 |
} |
182 |
} |
183 |
pEvents->clear(); |
184 |
} |
185 |
// hand back VM representation of script |
186 |
if (parserContext) { |
187 |
AbstractInstrumentManager* pManager = |
188 |
dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager()); |
189 |
|
190 |
pManager->scripts.HandBack(parserContext, pEngineChannel); |
191 |
parserContext = NULL; |
192 |
handlerInit = NULL; |
193 |
handlerNote = NULL; |
194 |
handlerRelease = NULL; |
195 |
handlerController = NULL; |
196 |
} |
197 |
bHasValidScript = false; |
198 |
} |
199 |
|
200 |
/** |
201 |
* Same as unload(), but this one also empties the @c code member variable |
202 |
* to an empty string. |
203 |
*/ |
204 |
void InstrumentScript::resetAll() { |
205 |
unload(); |
206 |
code.clear(); |
207 |
} |
208 |
|
209 |
/** |
210 |
* Clears all currently active script events. This should be called |
211 |
* whenever the engine or engine channel was reset for some reason. |
212 |
*/ |
213 |
void InstrumentScript::resetEvents() { |
214 |
for (int i = 0; i < INSTR_SCRIPT_EVENT_GROUPS; ++i) |
215 |
eventGroups[i].clear(); |
216 |
|
217 |
for (int i = 0; i < 128; ++i) |
218 |
if (pKeyEvents[i]) |
219 |
pKeyEvents[i]->clear(); |
220 |
|
221 |
suspendedEvents.clear(); |
222 |
|
223 |
if (pEvents) pEvents->clear(); |
224 |
} |
225 |
|
226 |
/////////////////////////////////////////////////////////////////////// |
227 |
// class 'InstrumentScriptVM' |
228 |
|
229 |
InstrumentScriptVM::InstrumentScriptVM() : |
230 |
m_event(NULL), m_fnPlayNote(this), m_fnSetController(this), |
231 |
m_fnIgnoreEvent(this), m_fnIgnoreController(this), m_fnNoteOff(this), |
232 |
m_fnSetEventMark(this), m_fnDeleteEventMark(this), m_fnByMarks(this), |
233 |
m_fnChangeVol(this), m_fnChangeTune(this), m_fnChangePan(this), |
234 |
m_fnChangeCutoff(this), m_fnChangeReso(this), m_fnChangeAttack(this), |
235 |
m_fnChangeDecay(this), m_fnChangeRelease(this), m_fnEventStatus(this), |
236 |
m_fnWait2(this), m_fnStopWait(this), |
237 |
m_varEngineUptime(this), m_varCallbackID(this) |
238 |
{ |
239 |
m_CC.size = _MEMBER_SIZEOF(AbstractEngineChannel, ControllerTable); |
240 |
m_CC_NUM = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.CC.Controller); |
241 |
m_EVENT_ID = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, id); |
242 |
m_EVENT_NOTE = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.Note.Key); |
243 |
m_EVENT_VELOCITY = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.Note.Velocity); |
244 |
m_KEY_DOWN.size = 128; |
245 |
m_NI_CALLBACK_TYPE = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, handlerType); |
246 |
m_NKSP_IGNORE_WAIT = DECLARE_VMINT(m_event, class ScriptEvent, ignoreAllWaitCalls); |
247 |
} |
248 |
|
249 |
VMExecStatus_t InstrumentScriptVM::exec(VMParserContext* parserCtx, ScriptEvent* event) { |
250 |
AbstractEngineChannel* pEngineChannel = |
251 |
static_cast<AbstractEngineChannel*>(event->cause.pEngineChannel); |
252 |
|
253 |
// prepare built-in script variables for script execution |
254 |
m_event = event; |
255 |
m_CC.data = (int8_t*) &pEngineChannel->ControllerTable[0]; |
256 |
m_KEY_DOWN.data = &pEngineChannel->GetMidiKeyboardManager()->KeyDown[0]; |
257 |
|
258 |
// if script is in start condition, then do mandatory MIDI event |
259 |
// preprocessing tasks, which essentially means updating i.e. controller |
260 |
// table with new CC value in case of a controller event, because the |
261 |
// script might access the new CC value |
262 |
if (!event->executionSlices) { |
263 |
switch (event->cause.Type) { |
264 |
case Event::type_control_change: |
265 |
pEngineChannel->ControllerTable[event->cause.Param.CC.Controller] = |
266 |
event->cause.Param.CC.Value; |
267 |
break; |
268 |
case Event::type_channel_pressure: |
269 |
pEngineChannel->ControllerTable[CTRL_TABLE_IDX_AFTERTOUCH] = |
270 |
event->cause.Param.ChannelPressure.Value; |
271 |
break; |
272 |
case Event::type_pitchbend: |
273 |
pEngineChannel->ControllerTable[CTRL_TABLE_IDX_PITCHBEND] = |
274 |
event->cause.Param.Pitch.Pitch; |
275 |
break; |
276 |
default: |
277 |
; // noop |
278 |
} |
279 |
} |
280 |
|
281 |
// run the script handler(s) |
282 |
VMExecStatus_t res = VM_EXEC_NOT_RUNNING; |
283 |
for ( ; event->handlers[event->currentHandler]; event->currentHandler++) { |
284 |
res = ScriptVM::exec( |
285 |
parserCtx, event->execCtx, event->handlers[event->currentHandler] |
286 |
); |
287 |
event->executionSlices++; |
288 |
if (res & VM_EXEC_SUSPENDED || res & VM_EXEC_ERROR) return res; |
289 |
} |
290 |
|
291 |
return res; |
292 |
} |
293 |
|
294 |
std::map<String,VMIntRelPtr*> InstrumentScriptVM::builtInIntVariables() { |
295 |
// first get built-in integer variables of derived VM class |
296 |
std::map<String,VMIntRelPtr*> m = ScriptVM::builtInIntVariables(); |
297 |
|
298 |
// now add own built-in variables |
299 |
m["$CC_NUM"] = &m_CC_NUM; |
300 |
m["$EVENT_ID"] = &m_EVENT_ID; |
301 |
m["$EVENT_NOTE"] = &m_EVENT_NOTE; |
302 |
m["$EVENT_VELOCITY"] = &m_EVENT_VELOCITY; |
303 |
// m["$POLY_AT_NUM"] = &m_POLY_AT_NUM; |
304 |
m["$NI_CALLBACK_TYPE"] = &m_NI_CALLBACK_TYPE; |
305 |
m["$NKSP_IGNORE_WAIT"] = &m_NKSP_IGNORE_WAIT; |
306 |
|
307 |
return m; |
308 |
} |
309 |
|
310 |
std::map<String,VMInt8Array*> InstrumentScriptVM::builtInIntArrayVariables() { |
311 |
// first get built-in integer array variables of derived VM class |
312 |
std::map<String,VMInt8Array*> m = ScriptVM::builtInIntArrayVariables(); |
313 |
|
314 |
// now add own built-in variables |
315 |
m["%CC"] = &m_CC; |
316 |
m["%KEY_DOWN"] = &m_KEY_DOWN; |
317 |
//m["%POLY_AT"] = &m_POLY_AT; |
318 |
|
319 |
return m; |
320 |
} |
321 |
|
322 |
std::map<String,int> InstrumentScriptVM::builtInConstIntVariables() { |
323 |
// first get built-in integer variables of derived VM class |
324 |
std::map<String,int> m = ScriptVM::builtInConstIntVariables(); |
325 |
|
326 |
m["$EVENT_STATUS_INACTIVE"] = EVENT_STATUS_INACTIVE; |
327 |
m["$EVENT_STATUS_NOTE_QUEUE"] = EVENT_STATUS_NOTE_QUEUE; |
328 |
m["$VCC_MONO_AT"] = CTRL_TABLE_IDX_AFTERTOUCH; |
329 |
m["$VCC_PITCH_BEND"] = CTRL_TABLE_IDX_PITCHBEND; |
330 |
for (int i = 0; i < INSTR_SCRIPT_EVENT_GROUPS; ++i) { |
331 |
m["$MARK_" + ToString(i+1)] = i; |
332 |
} |
333 |
|
334 |
return m; |
335 |
} |
336 |
|
337 |
std::map<String,VMDynVar*> InstrumentScriptVM::builtInDynamicVariables() { |
338 |
// first get built-in dynamic variables of derived VM class |
339 |
std::map<String,VMDynVar*> m = ScriptVM::builtInDynamicVariables(); |
340 |
|
341 |
m["$ENGINE_UPTIME"] = &m_varEngineUptime; |
342 |
m["$NI_CALLBACK_ID"] = &m_varCallbackID; |
343 |
|
344 |
return m; |
345 |
} |
346 |
|
347 |
VMFunction* InstrumentScriptVM::functionByName(const String& name) { |
348 |
// built-in script functions of this class |
349 |
if (name == "play_note") return &m_fnPlayNote; |
350 |
else if (name == "set_controller") return &m_fnSetController; |
351 |
else if (name == "ignore_event") return &m_fnIgnoreEvent; |
352 |
else if (name == "ignore_controller") return &m_fnIgnoreController; |
353 |
else if (name == "note_off") return &m_fnNoteOff; |
354 |
else if (name == "set_event_mark") return &m_fnSetEventMark; |
355 |
else if (name == "delete_event_mark") return &m_fnDeleteEventMark; |
356 |
else if (name == "by_marks") return &m_fnByMarks; |
357 |
else if (name == "change_vol") return &m_fnChangeVol; |
358 |
else if (name == "change_tune") return &m_fnChangeTune; |
359 |
else if (name == "change_pan") return &m_fnChangePan; |
360 |
else if (name == "change_cutoff") return &m_fnChangeCutoff; |
361 |
else if (name == "change_reso") return &m_fnChangeReso; |
362 |
else if (name == "change_attack") return &m_fnChangeAttack; |
363 |
else if (name == "change_decay") return &m_fnChangeDecay; |
364 |
else if (name == "change_release") return &m_fnChangeRelease; |
365 |
else if (name == "event_status") return &m_fnEventStatus; |
366 |
else if (name == "wait") return &m_fnWait2; // override wait() core implementation |
367 |
else if (name == "stop_wait") return &m_fnStopWait; |
368 |
|
369 |
// built-in script functions of derived VM class |
370 |
return ScriptVM::functionByName(name); |
371 |
} |
372 |
|
373 |
} // namespace LinuxSampler |