1 |
/* |
2 |
* Copyright (c) 2014 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 "InstrumentScriptVM.h" |
11 |
#include "../AbstractEngineChannel.h" |
12 |
#include "../../common/global_private.h" |
13 |
#include "AbstractInstrumentManager.h" |
14 |
#include "MidiKeyboardManager.h" |
15 |
|
16 |
namespace LinuxSampler { |
17 |
|
18 |
/////////////////////////////////////////////////////////////////////// |
19 |
// class 'InstrumentScript' |
20 |
|
21 |
/** @brief Load real-time instrument script. |
22 |
* |
23 |
* Loads the real-time instrument script given by @a text on the engine |
24 |
* channel this InstrumentScript object belongs to (defined by |
25 |
* pEngineChannel member variable). The sampler engine's resource manager is |
26 |
* used to allocate and share equivalent scripts on multiple engine |
27 |
* channels. |
28 |
* |
29 |
* @param text - source code of script |
30 |
*/ |
31 |
void InstrumentScript::load(const String& text) { |
32 |
dmsg(1,("Loading real-time instrument script ... ")); |
33 |
|
34 |
// hand back old script reference and VM execution contexts |
35 |
// (if not done already) |
36 |
unload(); |
37 |
|
38 |
code = text; |
39 |
|
40 |
AbstractInstrumentManager* pManager = |
41 |
dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager()); |
42 |
|
43 |
// get new script reference |
44 |
parserContext = pManager->scripts.Borrow(text, pEngineChannel); |
45 |
if (!parserContext->errors().empty()) { |
46 |
std::vector<ParserIssue> errors = parserContext->errors(); |
47 |
std::cerr << "[ScriptVM] Could not load instrument script, there were " |
48 |
<< errors.size() << " parser errors:\n"; |
49 |
for (int i = 0; i < errors.size(); ++i) |
50 |
errors[i].dump(); |
51 |
return; // stop here if there were any parser errors |
52 |
} |
53 |
|
54 |
handlerInit = parserContext->eventHandlerByName("init"); |
55 |
handlerNote = parserContext->eventHandlerByName("note"); |
56 |
handlerRelease = parserContext->eventHandlerByName("release"); |
57 |
handlerController = parserContext->eventHandlerByName("controller"); |
58 |
bHasValidScript = |
59 |
handlerInit || handlerNote || handlerRelease || handlerController; |
60 |
|
61 |
// amount of script handlers each script event has to execute |
62 |
int handlerExecCount = 0; |
63 |
if (handlerNote || handlerRelease || handlerController) // only one of these are executed after "init" handler |
64 |
handlerExecCount++; |
65 |
|
66 |
// create script event pool (if it doesn't exist already) |
67 |
if (!pEvents) |
68 |
pEvents = new Pool<ScriptEvent>(CONFIG_MAX_EVENTS_PER_FRAGMENT); |
69 |
|
70 |
// create new VM execution contexts for new script |
71 |
while (!pEvents->poolIsEmpty()) { |
72 |
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
73 |
it->execCtx = pEngineChannel->pEngine->pScriptVM->createExecContext( |
74 |
parserContext |
75 |
); |
76 |
it->handlers = new VMEventHandler*[handlerExecCount+1]; |
77 |
} |
78 |
pEvents->clear(); |
79 |
|
80 |
dmsg(1,("Done\n")); |
81 |
} |
82 |
|
83 |
/** @brief Unload real-time instrument script. |
84 |
* |
85 |
* Unloads the currently used real-time instrument script and frees all |
86 |
* resources allocated for that script. The sampler engine's resource manager |
87 |
* is used to share equivalent scripts among multiple sampler channels, and |
88 |
* to deallocate the parsed script once not used on any engine channel |
89 |
* anymore. |
90 |
* |
91 |
* Calling thid method will however not clear the @c code member variable. |
92 |
* Thus, the script can be parsed again afterwards. |
93 |
*/ |
94 |
void InstrumentScript::unload() { |
95 |
if (parserContext) |
96 |
dmsg(1,("Unloading current instrument script.\n")); |
97 |
|
98 |
// free allocated VM execution contexts |
99 |
if (pEvents) { |
100 |
pEvents->clear(); |
101 |
while (!pEvents->poolIsEmpty()) { |
102 |
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
103 |
if (it->execCtx) { |
104 |
// free VM execution context object |
105 |
delete it->execCtx; |
106 |
it->execCtx = NULL; |
107 |
// free C array of handler pointers |
108 |
delete [] it->handlers; |
109 |
} |
110 |
} |
111 |
pEvents->clear(); |
112 |
} |
113 |
// hand back VM representation of script |
114 |
if (parserContext) { |
115 |
AbstractInstrumentManager* pManager = |
116 |
dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager()); |
117 |
|
118 |
pManager->scripts.HandBack(parserContext, pEngineChannel); |
119 |
parserContext = NULL; |
120 |
handlerInit = NULL; |
121 |
handlerNote = NULL; |
122 |
handlerRelease = NULL; |
123 |
handlerController = NULL; |
124 |
} |
125 |
bHasValidScript = false; |
126 |
} |
127 |
|
128 |
/** |
129 |
* Same as unload(), but this one also empties the @c code member variable |
130 |
* to an empty string. |
131 |
*/ |
132 |
void InstrumentScript::resetAll() { |
133 |
unload(); |
134 |
code.clear(); |
135 |
} |
136 |
|
137 |
/////////////////////////////////////////////////////////////////////// |
138 |
// class 'InstrumentScriptVM' |
139 |
|
140 |
InstrumentScriptVM::InstrumentScriptVM() : |
141 |
m_event(NULL), m_fnPlayNote(this), m_fnSetController(this), |
142 |
m_fnIgnoreEvent(this), m_fnIgnoreController(this) |
143 |
{ |
144 |
m_CC.size = _MEMBER_SIZEOF(AbstractEngineChannel, ControllerTable); |
145 |
m_CC_NUM = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.CC.Controller); |
146 |
m_EVENT_ID = DECLARE_VMINT(m_event, class ScriptEvent, id); |
147 |
m_EVENT_NOTE = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.Note.Key); |
148 |
m_EVENT_VELOCITY = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.Note.Velocity); |
149 |
m_KEY_DOWN.size = 128; |
150 |
} |
151 |
|
152 |
VMExecStatus_t InstrumentScriptVM::exec(VMParserContext* parserCtx, ScriptEvent* event) { |
153 |
AbstractEngineChannel* pEngineChannel = |
154 |
static_cast<AbstractEngineChannel*>(event->cause.pEngineChannel); |
155 |
|
156 |
// prepare built-in script variables for script execution |
157 |
m_event = event; |
158 |
m_CC.data = (int8_t*) &pEngineChannel->ControllerTable[0]; |
159 |
m_KEY_DOWN.data = &pEngineChannel->GetMidiKeyboardManager()->KeyDown[0]; |
160 |
|
161 |
// if script is in start condition, then do mandatory MIDI event |
162 |
// preprocessing tasks, which essentially means updating i.e. controller |
163 |
// table with new CC value in case of a controller event, because the |
164 |
// script might access the new CC value |
165 |
if (!event->executionSlices) { |
166 |
switch (event->cause.Type) { |
167 |
case Event::type_control_change: |
168 |
pEngineChannel->ControllerTable[event->cause.Param.CC.Controller] = |
169 |
event->cause.Param.CC.Value; |
170 |
break; |
171 |
case Event::type_channel_pressure: |
172 |
pEngineChannel->ControllerTable[CTRL_TABLE_IDX_AFTERTOUCH] = |
173 |
event->cause.Param.ChannelPressure.Value; |
174 |
break; |
175 |
case Event::type_pitchbend: |
176 |
pEngineChannel->ControllerTable[CTRL_TABLE_IDX_PITCHBEND] = |
177 |
event->cause.Param.Pitch.Pitch; |
178 |
break; |
179 |
} |
180 |
} |
181 |
|
182 |
// run the script handler(s) |
183 |
VMExecStatus_t res = VM_EXEC_NOT_RUNNING; |
184 |
for ( ; event->handlers[event->currentHandler]; event->currentHandler++) { |
185 |
res = ScriptVM::exec( |
186 |
parserCtx, event->execCtx, event->handlers[event->currentHandler] |
187 |
); |
188 |
event->executionSlices++; |
189 |
if (res & VM_EXEC_SUSPENDED || res & VM_EXEC_ERROR) return res; |
190 |
} |
191 |
|
192 |
return res; |
193 |
} |
194 |
|
195 |
std::map<String,VMIntRelPtr*> InstrumentScriptVM::builtInIntVariables() { |
196 |
// first get buil-in integer variables of derived VM class |
197 |
std::map<String,VMIntRelPtr*> m = ScriptVM::builtInIntVariables(); |
198 |
|
199 |
// now add own built-in variables |
200 |
m["$CC_NUM"] = &m_CC_NUM; |
201 |
m["$EVENT_ID"] = &m_EVENT_ID; |
202 |
m["$EVENT_NOTE"] = &m_EVENT_NOTE; |
203 |
m["$EVENT_VELOCITY"] = &m_EVENT_VELOCITY; |
204 |
// m["$POLY_AT_NUM"] = &m_POLY_AT_NUM; |
205 |
|
206 |
return m; |
207 |
} |
208 |
|
209 |
std::map<String,VMInt8Array*> InstrumentScriptVM::builtInIntArrayVariables() { |
210 |
// first get buil-in integer array variables of derived VM class |
211 |
std::map<String,VMInt8Array*> m = ScriptVM::builtInIntArrayVariables(); |
212 |
|
213 |
// now add own built-in variables |
214 |
m["%CC"] = &m_CC; |
215 |
m["%KEY_DOWN"] = &m_KEY_DOWN; |
216 |
//m["%POLY_AT"] = &m_POLY_AT; |
217 |
|
218 |
return m; |
219 |
} |
220 |
|
221 |
std::map<String,int> InstrumentScriptVM::builtInConstIntVariables() { |
222 |
// first get buil-in integer variables of derived VM class |
223 |
std::map<String,int> m = ScriptVM::builtInConstIntVariables(); |
224 |
|
225 |
m["$VCC_MONO_AT"] = CTRL_TABLE_IDX_AFTERTOUCH; |
226 |
m["$VCC_PITCH_BEND"] = CTRL_TABLE_IDX_PITCHBEND; |
227 |
|
228 |
return m; |
229 |
} |
230 |
|
231 |
VMFunction* InstrumentScriptVM::functionByName(const String& name) { |
232 |
// built-in script functions of this class |
233 |
if (name == "play_note") return &m_fnPlayNote; |
234 |
else if (name == "set_controller") return &m_fnSetController; |
235 |
else if (name == "ignore_event") return &m_fnIgnoreEvent; |
236 |
else if (name == "ignore_controller") return &m_fnIgnoreController; |
237 |
|
238 |
// built-in script functions of derived VM class |
239 |
return ScriptVM::functionByName(name); |
240 |
} |
241 |
|
242 |
} // namespace LinuxSampler |