10 |
#include "InstrumentScriptVM.h" |
#include "InstrumentScriptVM.h" |
11 |
#include "../AbstractEngineChannel.h" |
#include "../AbstractEngineChannel.h" |
12 |
#include "../../common/global_private.h" |
#include "../../common/global_private.h" |
13 |
|
#include "AbstractInstrumentManager.h" |
14 |
|
|
15 |
namespace LinuxSampler { |
namespace LinuxSampler { |
16 |
|
|
17 |
|
/////////////////////////////////////////////////////////////////////// |
18 |
|
// class 'InstrumentScript' |
19 |
|
|
20 |
|
/** @brief Load real-time instrument script. |
21 |
|
* |
22 |
|
* Loads the real-time instrument script given by @a text on the engine |
23 |
|
* channel this InstrumentScript object belongs to (defined by |
24 |
|
* pEngineChannel member variable). The sampler engine's resource manager is |
25 |
|
* used to allocate and share equivalent scripts on multiple engine |
26 |
|
* channels. |
27 |
|
* |
28 |
|
* @param text - source code of script |
29 |
|
*/ |
30 |
|
void InstrumentScript::load(const String& text) { |
31 |
|
dmsg(1,("Loading real-time instrument script ... ")); |
32 |
|
|
33 |
|
// hand back old script reference and VM execution contexts |
34 |
|
// (if not done already) |
35 |
|
reset(); |
36 |
|
|
37 |
|
AbstractInstrumentManager* pManager = |
38 |
|
dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager()); |
39 |
|
|
40 |
|
// get new script reference |
41 |
|
parserContext = pManager->scripts.Borrow(text, pEngineChannel); |
42 |
|
if (!parserContext->errors().empty()) { |
43 |
|
std::vector<ParserIssue> errors = parserContext->errors(); |
44 |
|
std::cerr << "[ScriptVM] Could not load instrument script, there were " |
45 |
|
<< errors.size() << " parser errors:\n"; |
46 |
|
for (int i = 0; i < errors.size(); ++i) |
47 |
|
errors[i].dump(); |
48 |
|
return; // stop here if there were any parser errors |
49 |
|
} |
50 |
|
|
51 |
|
handlerInit = parserContext->eventHandlerByName("init"); |
52 |
|
handlerNote = parserContext->eventHandlerByName("note"); |
53 |
|
handlerRelease = parserContext->eventHandlerByName("release"); |
54 |
|
handlerController = parserContext->eventHandlerByName("controller"); |
55 |
|
bHasValidScript = |
56 |
|
handlerInit || handlerNote || handlerRelease || handlerController; |
57 |
|
|
58 |
|
// amount of script handlers each script event has to execute |
59 |
|
int handlerExecCount = 0; |
60 |
|
if (handlerNote || handlerRelease || handlerController) // only one of these are executed after "init" handler |
61 |
|
handlerExecCount++; |
62 |
|
|
63 |
|
// create script event pool (if it doesn't exist already) |
64 |
|
if (!pEvents) |
65 |
|
pEvents = new Pool<ScriptEvent>(CONFIG_MAX_EVENTS_PER_FRAGMENT); |
66 |
|
|
67 |
|
// create new VM execution contexts for new script |
68 |
|
while (!pEvents->poolIsEmpty()) { |
69 |
|
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
70 |
|
it->execCtx = pEngineChannel->pEngine->pScriptVM->createExecContext( |
71 |
|
parserContext |
72 |
|
); |
73 |
|
it->handlers = new VMEventHandler*[handlerExecCount+1]; |
74 |
|
} |
75 |
|
pEvents->clear(); |
76 |
|
|
77 |
|
dmsg(1,("Done\n")); |
78 |
|
} |
79 |
|
|
80 |
|
/** @brief Unload real-time instrument script. |
81 |
|
* |
82 |
|
* Unloads the currently used real-time instrument script and frees all |
83 |
|
* resources allocated for that script. The sampler engine's resource manager |
84 |
|
* is used to share equivalent scripts among multiple sampler channels, and |
85 |
|
* to deallocate the parsed script once not used on any engine channel |
86 |
|
* anymore. |
87 |
|
*/ |
88 |
|
void InstrumentScript::reset() { |
89 |
|
if (parserContext) |
90 |
|
dmsg(1,("Unloading current instrument script.")); |
91 |
|
|
92 |
|
// free allocated VM execution contexts |
93 |
|
if (pEvents) { |
94 |
|
pEvents->clear(); |
95 |
|
while (!pEvents->poolIsEmpty()) { |
96 |
|
RTList<ScriptEvent>::Iterator it = pEvents->allocAppend(); |
97 |
|
if (it->execCtx) { |
98 |
|
// free VM execution context object |
99 |
|
delete it->execCtx; |
100 |
|
it->execCtx = NULL; |
101 |
|
// free C array of handler pointers |
102 |
|
delete [] it->handlers; |
103 |
|
} |
104 |
|
} |
105 |
|
pEvents->clear(); |
106 |
|
} |
107 |
|
// hand back VM representation of script |
108 |
|
if (parserContext) { |
109 |
|
AbstractInstrumentManager* pManager = |
110 |
|
dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager()); |
111 |
|
|
112 |
|
pManager->scripts.HandBack(parserContext, pEngineChannel); |
113 |
|
parserContext = NULL; |
114 |
|
handlerInit = NULL; |
115 |
|
handlerNote = NULL; |
116 |
|
handlerRelease = NULL; |
117 |
|
handlerController = NULL; |
118 |
|
} |
119 |
|
bHasValidScript = false; |
120 |
|
} |
121 |
|
|
122 |
|
/////////////////////////////////////////////////////////////////////// |
123 |
|
// class 'InstrumentScriptVM' |
124 |
|
|
125 |
InstrumentScriptVM::InstrumentScriptVM() : |
InstrumentScriptVM::InstrumentScriptVM() : |
126 |
m_event(NULL), m_fnPlayNote(this), m_fnSetController(this), |
m_event(NULL), m_fnPlayNote(this), m_fnSetController(this), |
127 |
m_fnIgnoreEvent(this), m_fnIgnoreController(this) |
m_fnIgnoreEvent(this), m_fnIgnoreController(this) |