1 |
/* |
/* |
2 |
* Copyright (c) 2014 - 2017 Christian Schoenebeck |
* Copyright (c) 2014 - 2020 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
20 |
/////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////// |
21 |
// class 'EventGroup' |
// class 'EventGroup' |
22 |
|
|
23 |
void EventGroup::insert(int eventID) { |
void EventGroup::insert(vmint eventID) { |
24 |
if (contains(eventID)) return; |
if (contains(eventID)) return; |
25 |
|
|
26 |
AbstractEngine* pEngine = m_script->pEngineChannel->pEngine; |
AbstractEngine* pEngine = m_script->pEngineChannel->pEngine; |
31 |
// events die before being removed explicitly from the group by script |
// events die before being removed explicitly from the group by script |
32 |
// |
// |
33 |
// NOTE: or should we do this "dead ones" check only once in a while? |
// NOTE: or should we do this "dead ones" check only once in a while? |
34 |
int firstDead = -1; |
ssize_t firstDead = -1; |
35 |
for (int i = 0; i < size(); ++i) { |
for (size_t i = 0; i < size(); ++i) { |
36 |
if (firstDead >= 0) { |
if (firstDead >= 0) { |
37 |
if (pEngine->EventByID(eventID)) { |
if (pEngine->EventByID(eventID)) { |
38 |
remove(firstDead, i - firstDead); |
remove(firstDead, i - firstDead); |
46 |
append(eventID); |
append(eventID); |
47 |
} |
} |
48 |
|
|
49 |
void EventGroup::erase(int eventID) { |
void EventGroup::erase(vmint eventID) { |
50 |
int index = find(eventID); |
size_t index = find(eventID); |
51 |
remove(index); |
remove(index); |
52 |
} |
} |
53 |
|
|
61 |
handlerNote = NULL; |
handlerNote = NULL; |
62 |
handlerRelease = NULL; |
handlerRelease = NULL; |
63 |
handlerController = NULL; |
handlerController = NULL; |
64 |
|
handlerRpn = NULL; |
65 |
|
handlerNrpn = NULL; |
66 |
pEvents = NULL; |
pEvents = NULL; |
67 |
for (int i = 0; i < 128; ++i) |
for (int i = 0; i < 128; ++i) |
68 |
pKeyEvents[i] = NULL; |
pKeyEvents[i] = NULL; |
88 |
* channels. |
* channels. |
89 |
* |
* |
90 |
* @param text - source code of script |
* @param text - source code of script |
91 |
|
* @param patchVars - 'patch' variables being overridden by instrument |
92 |
*/ |
*/ |
93 |
void InstrumentScript::load(const String& text) { |
void InstrumentScript::load(const String& text, |
94 |
|
const std::map<String,String>& patchVars) |
95 |
|
{ |
96 |
dmsg(1,("Loading real-time instrument script ... ")); |
dmsg(1,("Loading real-time instrument script ... ")); |
97 |
|
|
98 |
// hand back old script reference and VM execution contexts |
// hand back old script reference and VM execution contexts |
105 |
dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager()); |
dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager()); |
106 |
|
|
107 |
// get new script reference |
// get new script reference |
108 |
parserContext = pManager->scripts.Borrow(text, pEngineChannel); |
parserContext = pManager->scripts.Borrow( |
109 |
|
{ .code = text, .patchVars = patchVars }, pEngineChannel |
110 |
|
); |
111 |
if (!parserContext->errors().empty()) { |
if (!parserContext->errors().empty()) { |
112 |
std::vector<ParserIssue> errors = parserContext->errors(); |
std::vector<ParserIssue> errors = parserContext->errors(); |
113 |
std::cerr << "[ScriptVM] Could not load instrument script, there were " |
std::cerr << "[ScriptVM] Could not load instrument script, there were " |
121 |
handlerNote = parserContext->eventHandlerByName("note"); |
handlerNote = parserContext->eventHandlerByName("note"); |
122 |
handlerRelease = parserContext->eventHandlerByName("release"); |
handlerRelease = parserContext->eventHandlerByName("release"); |
123 |
handlerController = parserContext->eventHandlerByName("controller"); |
handlerController = parserContext->eventHandlerByName("controller"); |
124 |
|
handlerRpn = parserContext->eventHandlerByName("rpn"); |
125 |
|
handlerNrpn = parserContext->eventHandlerByName("nrpn"); |
126 |
bHasValidScript = |
bHasValidScript = |
127 |
handlerInit || handlerNote || handlerRelease || handlerController; |
handlerInit || handlerNote || handlerRelease || handlerController || |
128 |
|
handlerRpn || handlerNrpn; |
129 |
|
|
130 |
// amount of script handlers each script event has to execute |
// amount of script handlers each script event has to execute |
131 |
int handlerExecCount = 0; |
int handlerExecCount = 0; |
132 |
if (handlerNote || handlerRelease || handlerController) // only one of these are executed after "init" handler |
if (handlerNote || handlerRelease || handlerController || handlerRpn || |
133 |
|
handlerNrpn) // only one of these are executed after "init" handler |
134 |
handlerExecCount++; |
handlerExecCount++; |
135 |
|
|
136 |
// create script event pool (if it doesn't exist already) |
// create script event pool (if it doesn't exist already) |
205 |
handlerNote = NULL; |
handlerNote = NULL; |
206 |
handlerRelease = NULL; |
handlerRelease = NULL; |
207 |
handlerController = NULL; |
handlerController = NULL; |
208 |
|
handlerRpn = NULL; |
209 |
|
handlerNrpn = NULL; |
210 |
} |
} |
211 |
bHasValidScript = false; |
bHasValidScript = false; |
212 |
} |
} |
242 |
|
|
243 |
InstrumentScriptVM::InstrumentScriptVM() : |
InstrumentScriptVM::InstrumentScriptVM() : |
244 |
m_event(NULL), m_fnPlayNote(this), m_fnSetController(this), |
m_event(NULL), m_fnPlayNote(this), m_fnSetController(this), |
245 |
|
m_fnSetRpn(this), m_fnSetNrpn(this), |
246 |
m_fnIgnoreEvent(this), m_fnIgnoreController(this), m_fnNoteOff(this), |
m_fnIgnoreEvent(this), m_fnIgnoreController(this), m_fnNoteOff(this), |
247 |
m_fnSetEventMark(this), m_fnDeleteEventMark(this), m_fnByMarks(this), |
m_fnSetEventMark(this), m_fnDeleteEventMark(this), m_fnByMarks(this), |
248 |
m_fnChangeVol(this), m_fnChangeVolTime(this), |
m_fnChangeVol(this), m_fnChangeVolTime(this), |
249 |
m_fnChangeTune(this), m_fnChangeTuneTime(this), m_fnChangePan(this), |
m_fnChangeTune(this), m_fnChangeTuneTime(this), m_fnChangePan(this), |
250 |
|
m_fnChangePanTime(this), m_fnChangePanCurve(this), |
251 |
m_fnChangeCutoff(this), m_fnChangeReso(this), m_fnChangeAttack(this), |
m_fnChangeCutoff(this), m_fnChangeReso(this), m_fnChangeAttack(this), |
252 |
m_fnChangeDecay(this), m_fnChangeRelease(this), |
m_fnChangeDecay(this), m_fnChangeSustain(this), m_fnChangeRelease(this), |
253 |
|
m_fnChangeCutoffAttack(this), m_fnChangeCutoffDecay(this), |
254 |
|
m_fnChangeCutoffSustain(this), m_fnChangeCutoffRelease(this), |
255 |
m_fnChangeAmpLFODepth(this), m_fnChangeAmpLFOFreq(this), |
m_fnChangeAmpLFODepth(this), m_fnChangeAmpLFOFreq(this), |
256 |
|
m_fnChangeCutoffLFODepth(this), m_fnChangeCutoffLFOFreq(this), |
257 |
m_fnChangePitchLFODepth(this), m_fnChangePitchLFOFreq(this), |
m_fnChangePitchLFODepth(this), m_fnChangePitchLFOFreq(this), |
258 |
m_fnChangeNote(this), m_fnChangeVelo(this), m_fnFork(this), |
m_fnChangeNote(this), m_fnChangeVelo(this), m_fnFork(this), |
259 |
m_fnEventStatus(this), m_fnWait2(this), m_fnStopWait(this), |
m_fnEventStatus(this), m_fnWait2(this), m_fnStopWait(this), |
269 |
m_EVENT_ID = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, id); |
m_EVENT_ID = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, id); |
270 |
m_EVENT_NOTE = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, cause.Param.Note.Key); |
m_EVENT_NOTE = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, cause.Param.Note.Key); |
271 |
m_EVENT_VELOCITY = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, cause.Param.Note.Velocity); |
m_EVENT_VELOCITY = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, cause.Param.Note.Velocity); |
272 |
|
m_RPN_ADDRESS = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, cause.Param.RPN.Parameter); |
273 |
|
m_RPN_VALUE = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, cause.Param.RPN.Value); |
274 |
m_KEY_DOWN.size = 128; |
m_KEY_DOWN.size = 128; |
275 |
m_KEY_DOWN.readonly = true; |
m_KEY_DOWN.readonly = true; |
276 |
m_NI_CALLBACK_TYPE = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, handlerType); |
m_NI_CALLBACK_TYPE = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, handlerType); |
335 |
return res; |
return res; |
336 |
} |
} |
337 |
|
|
338 |
std::map<String,VMIntRelPtr*> InstrumentScriptVM::builtInIntVariables() { |
std::map<String,VMIntPtr*> InstrumentScriptVM::builtInIntVariables() { |
339 |
// first get built-in integer variables of derived VM class |
// first get built-in integer variables of derived VM class |
340 |
std::map<String,VMIntRelPtr*> m = ScriptVM::builtInIntVariables(); |
std::map<String,VMIntPtr*> m = ScriptVM::builtInIntVariables(); |
341 |
|
|
342 |
// now add own built-in variables |
// now add own built-in variables |
343 |
m["$CC_NUM"] = &m_CC_NUM; |
m["$CC_NUM"] = &m_CC_NUM; |
345 |
m["$EVENT_NOTE"] = &m_EVENT_NOTE; |
m["$EVENT_NOTE"] = &m_EVENT_NOTE; |
346 |
m["$EVENT_VELOCITY"] = &m_EVENT_VELOCITY; |
m["$EVENT_VELOCITY"] = &m_EVENT_VELOCITY; |
347 |
// m["$POLY_AT_NUM"] = &m_POLY_AT_NUM; |
// m["$POLY_AT_NUM"] = &m_POLY_AT_NUM; |
348 |
|
m["$RPN_ADDRESS"] = &m_RPN_ADDRESS; // used for both RPN and NRPN events |
349 |
|
m["$RPN_VALUE"] = &m_RPN_VALUE; // used for both RPN and NRPN events |
350 |
m["$NI_CALLBACK_TYPE"] = &m_NI_CALLBACK_TYPE; |
m["$NI_CALLBACK_TYPE"] = &m_NI_CALLBACK_TYPE; |
351 |
m["$NKSP_IGNORE_WAIT"] = &m_NKSP_IGNORE_WAIT; |
m["$NKSP_IGNORE_WAIT"] = &m_NKSP_IGNORE_WAIT; |
352 |
m["$NKSP_CALLBACK_PARENT_ID"] = &m_NKSP_CALLBACK_PARENT_ID; |
m["$NKSP_CALLBACK_PARENT_ID"] = &m_NKSP_CALLBACK_PARENT_ID; |
366 |
return m; |
return m; |
367 |
} |
} |
368 |
|
|
369 |
std::map<String,int> InstrumentScriptVM::builtInConstIntVariables() { |
std::map<String,vmint> InstrumentScriptVM::builtInConstIntVariables() { |
370 |
// first get built-in integer variables of derived VM class |
// first get built-in integer variables of derived VM class |
371 |
std::map<String,int> m = ScriptVM::builtInConstIntVariables(); |
std::map<String,vmint> m = ScriptVM::builtInConstIntVariables(); |
372 |
|
|
373 |
m["$EVENT_STATUS_INACTIVE"] = EVENT_STATUS_INACTIVE; |
m["$EVENT_STATUS_INACTIVE"] = EVENT_STATUS_INACTIVE; |
374 |
m["$EVENT_STATUS_NOTE_QUEUE"] = EVENT_STATUS_NOTE_QUEUE; |
m["$EVENT_STATUS_NOTE_QUEUE"] = EVENT_STATUS_NOTE_QUEUE; |
410 |
// built-in script functions of this class |
// built-in script functions of this class |
411 |
if (name == "play_note") return &m_fnPlayNote; |
if (name == "play_note") return &m_fnPlayNote; |
412 |
else if (name == "set_controller") return &m_fnSetController; |
else if (name == "set_controller") return &m_fnSetController; |
413 |
|
else if (name == "set_rpn") return &m_fnSetRpn; |
414 |
|
else if (name == "set_nrpn") return &m_fnSetNrpn; |
415 |
else if (name == "ignore_event") return &m_fnIgnoreEvent; |
else if (name == "ignore_event") return &m_fnIgnoreEvent; |
416 |
else if (name == "ignore_controller") return &m_fnIgnoreController; |
else if (name == "ignore_controller") return &m_fnIgnoreController; |
417 |
else if (name == "note_off") return &m_fnNoteOff; |
else if (name == "note_off") return &m_fnNoteOff; |
425 |
else if (name == "change_note") return &m_fnChangeNote; |
else if (name == "change_note") return &m_fnChangeNote; |
426 |
else if (name == "change_velo") return &m_fnChangeVelo; |
else if (name == "change_velo") return &m_fnChangeVelo; |
427 |
else if (name == "change_pan") return &m_fnChangePan; |
else if (name == "change_pan") return &m_fnChangePan; |
428 |
|
else if (name == "change_pan_time") return &m_fnChangePanTime; |
429 |
|
else if (name == "change_pan_curve") return &m_fnChangePanCurve; |
430 |
else if (name == "change_cutoff") return &m_fnChangeCutoff; |
else if (name == "change_cutoff") return &m_fnChangeCutoff; |
431 |
else if (name == "change_reso") return &m_fnChangeReso; |
else if (name == "change_reso") return &m_fnChangeReso; |
432 |
else if (name == "change_attack") return &m_fnChangeAttack; |
else if (name == "change_attack") return &m_fnChangeAttack; |
433 |
else if (name == "change_decay") return &m_fnChangeDecay; |
else if (name == "change_decay") return &m_fnChangeDecay; |
434 |
|
else if (name == "change_sustain") return &m_fnChangeSustain; |
435 |
else if (name == "change_release") return &m_fnChangeRelease; |
else if (name == "change_release") return &m_fnChangeRelease; |
436 |
|
else if (name == "change_cutoff_attack") return &m_fnChangeCutoffAttack; |
437 |
|
else if (name == "change_cutoff_decay") return &m_fnChangeCutoffDecay; |
438 |
|
else if (name == "change_cutoff_sustain") return &m_fnChangeCutoffSustain; |
439 |
|
else if (name == "change_cutoff_release") return &m_fnChangeCutoffRelease; |
440 |
else if (name == "change_amp_lfo_depth") return &m_fnChangeAmpLFODepth; |
else if (name == "change_amp_lfo_depth") return &m_fnChangeAmpLFODepth; |
441 |
else if (name == "change_amp_lfo_freq") return &m_fnChangeAmpLFOFreq; |
else if (name == "change_amp_lfo_freq") return &m_fnChangeAmpLFOFreq; |
442 |
|
else if (name == "change_cutoff_lfo_depth") return &m_fnChangeCutoffLFODepth; |
443 |
|
else if (name == "change_cutoff_lfo_freq") return &m_fnChangeCutoffLFOFreq; |
444 |
else if (name == "change_pitch_lfo_depth") return &m_fnChangePitchLFODepth; |
else if (name == "change_pitch_lfo_depth") return &m_fnChangePitchLFODepth; |
445 |
else if (name == "change_pitch_lfo_freq") return &m_fnChangePitchLFOFreq; |
else if (name == "change_pitch_lfo_freq") return &m_fnChangePitchLFOFreq; |
446 |
else if (name == "fade_in") return &m_fnFadeIn; |
else if (name == "fade_in") return &m_fnFadeIn; |