/[svn]/linuxsampler/trunk/src/engines/common/InstrumentScriptVM.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/common/InstrumentScriptVM.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2645 by schoenebeck, Wed Jun 18 00:14:57 2014 UTC revision 3690 by schoenebeck, Fri Jan 3 10:18:21 2020 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 Christian Schoenebeck   * Copyright (c) 2014 - 2020 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 7  Line 7 
7   * See README file for details.   * See README file for details.
8   */   */
9    
10    #include "../../common/global_private.h"
11  #include "InstrumentScriptVM.h"  #include "InstrumentScriptVM.h"
12  #include "../AbstractEngineChannel.h"  #include "../AbstractEngineChannel.h"
13  #include "../../common/global_private.h"  #include "../../common/global_private.h"
14  #include "AbstractInstrumentManager.h"  #include "AbstractInstrumentManager.h"
15  #include "MidiKeyboardManager.h"  #include "MidiKeyboardManager.h"
16    #include "Fade.h"
17    
18  namespace LinuxSampler {  namespace LinuxSampler {
19    
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;
# Line 29  namespace LinuxSampler { Line 31  namespace LinuxSampler {
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);
# Line 44  namespace LinuxSampler { Line 46  namespace LinuxSampler {
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    
# Line 59  namespace LinuxSampler { Line 61  namespace LinuxSampler {
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;
# Line 112  namespace LinuxSampler { Line 116  namespace LinuxSampler {
116          handlerNote = parserContext->eventHandlerByName("note");          handlerNote = parserContext->eventHandlerByName("note");
117          handlerRelease = parserContext->eventHandlerByName("release");          handlerRelease = parserContext->eventHandlerByName("release");
118          handlerController = parserContext->eventHandlerByName("controller");          handlerController = parserContext->eventHandlerByName("controller");
119            handlerRpn = parserContext->eventHandlerByName("rpn");
120            handlerNrpn = parserContext->eventHandlerByName("nrpn");
121          bHasValidScript =          bHasValidScript =
122              handlerInit || handlerNote || handlerRelease || handlerController;              handlerInit || handlerNote || handlerRelease || handlerController ||
123                handlerRpn || handlerNrpn;
124    
125          // amount of script handlers each script event has to execute          // amount of script handlers each script event has to execute
126          int handlerExecCount = 0;          int handlerExecCount = 0;
127          if (handlerNote || handlerRelease || handlerController) // only one of these are executed after "init" handler          if (handlerNote || handlerRelease || handlerController || handlerRpn ||
128                handlerNrpn) // only one of these are executed after "init" handler
129              handlerExecCount++;              handlerExecCount++;
130    
131          // create script event pool (if it doesn't exist already)          // create script event pool (if it doesn't exist already)
# Line 125  namespace LinuxSampler { Line 133  namespace LinuxSampler {
133              pEvents = new Pool<ScriptEvent>(CONFIG_MAX_EVENTS_PER_FRAGMENT);              pEvents = new Pool<ScriptEvent>(CONFIG_MAX_EVENTS_PER_FRAGMENT);
134              for (int i = 0; i < 128; ++i)              for (int i = 0; i < 128; ++i)
135                  pKeyEvents[i] = new RTList<ScriptEvent>(pEvents);                  pKeyEvents[i] = new RTList<ScriptEvent>(pEvents);
136                // reset RTAVLNode's tree node member variables after nodes are allocated
137                // (since we can't use a constructor right now, we do that initialization here)
138                while (!pEvents->poolIsEmpty()) {
139                    RTList<ScriptEvent>::Iterator it = pEvents->allocAppend();
140                    it->reset();
141                }
142                pEvents->clear();
143          }          }
144    
145          // create new VM execution contexts for new script          // create new VM execution contexts for new script
# Line 157  namespace LinuxSampler { Line 172  namespace LinuxSampler {
172          if (parserContext)          if (parserContext)
173              dmsg(1,("Unloading current instrument script.\n"));              dmsg(1,("Unloading current instrument script.\n"));
174    
175            resetEvents();
176    
177          // free allocated VM execution contexts          // free allocated VM execution contexts
178          if (pEvents) {          if (pEvents) {
179              pEvents->clear();              pEvents->clear();
# Line 183  namespace LinuxSampler { Line 200  namespace LinuxSampler {
200              handlerNote = NULL;              handlerNote = NULL;
201              handlerRelease = NULL;              handlerRelease = NULL;
202              handlerController = NULL;              handlerController = NULL;
203                handlerRpn = NULL;
204                handlerNrpn = NULL;
205          }          }
206          bHasValidScript = false;          bHasValidScript = false;
207      }      }
# Line 195  namespace LinuxSampler { Line 214  namespace LinuxSampler {
214          unload();          unload();
215          code.clear();          code.clear();
216      }      }
217        
218        /**
219         * Clears all currently active script events. This should be called
220         * whenever the engine or engine channel was reset for some reason.
221         */
222        void InstrumentScript::resetEvents() {
223            for (int i = 0; i < INSTR_SCRIPT_EVENT_GROUPS; ++i)
224                eventGroups[i].clear();
225    
226            for (int i = 0; i < 128; ++i)
227                if (pKeyEvents[i])
228                    pKeyEvents[i]->clear();
229    
230            suspendedEvents.clear();
231    
232            if (pEvents) pEvents->clear();
233        }
234    
235      ///////////////////////////////////////////////////////////////////////      ///////////////////////////////////////////////////////////////////////
236      // class 'InstrumentScriptVM'      // class 'InstrumentScriptVM'
# Line 202  namespace LinuxSampler { Line 238  namespace LinuxSampler {
238      InstrumentScriptVM::InstrumentScriptVM() :      InstrumentScriptVM::InstrumentScriptVM() :
239          m_event(NULL), m_fnPlayNote(this), m_fnSetController(this),          m_event(NULL), m_fnPlayNote(this), m_fnSetController(this),
240          m_fnIgnoreEvent(this), m_fnIgnoreController(this), m_fnNoteOff(this),          m_fnIgnoreEvent(this), m_fnIgnoreController(this), m_fnNoteOff(this),
241          m_fnSetEventMark(this), m_fnDeleteEventMark(this), m_fnByMarks(this)          m_fnSetEventMark(this), m_fnDeleteEventMark(this), m_fnByMarks(this),
242            m_fnChangeVol(this), m_fnChangeVolTime(this),
243            m_fnChangeTune(this), m_fnChangeTuneTime(this), m_fnChangePan(this),
244            m_fnChangePanTime(this), m_fnChangePanCurve(this),
245            m_fnChangeCutoff(this), m_fnChangeReso(this),  m_fnChangeAttack(this),
246            m_fnChangeDecay(this), m_fnChangeSustain(this), m_fnChangeRelease(this),
247            m_fnChangeCutoffAttack(this), m_fnChangeCutoffDecay(this),
248            m_fnChangeCutoffSustain(this), m_fnChangeCutoffRelease(this),
249            m_fnChangeAmpLFODepth(this), m_fnChangeAmpLFOFreq(this),
250            m_fnChangeCutoffLFODepth(this), m_fnChangeCutoffLFOFreq(this),
251            m_fnChangePitchLFODepth(this), m_fnChangePitchLFOFreq(this),
252            m_fnChangeNote(this), m_fnChangeVelo(this), m_fnFork(this),
253            m_fnEventStatus(this), m_fnWait2(this), m_fnStopWait(this),
254            m_fnAbort(this), m_fnFadeIn(this), m_fnFadeOut(this),
255            m_fnChangeVolCurve(this), m_fnChangeTuneCurve(this),
256            m_fnGetEventPar(this), m_fnSetEventPar(this), m_fnChangePlayPos(this),
257            m_fnCallbackStatus(this),
258            m_varEngineUptime(this), m_varCallbackID(this), m_varAllEvents(this),
259            m_varCallbackChildID(this)
260      {      {
261          m_CC.size = _MEMBER_SIZEOF(AbstractEngineChannel, ControllerTable);          m_CC.size = _MEMBER_SIZEOF(AbstractEngineChannel, ControllerTable);
262          m_CC_NUM = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.CC.Controller);          m_CC_NUM = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.CC.Controller);
263          m_EVENT_ID = DECLARE_VMINT(m_event, class ScriptEvent, id);          m_EVENT_ID = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, id);
264          m_EVENT_NOTE = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.Note.Key);          m_EVENT_NOTE = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, cause.Param.Note.Key);
265          m_EVENT_VELOCITY = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.Note.Velocity);          m_EVENT_VELOCITY = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, cause.Param.Note.Velocity);
266            m_RPN_ADDRESS = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, cause.Param.RPN.Parameter);
267            m_RPN_VALUE = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, cause.Param.RPN.Value);
268          m_KEY_DOWN.size = 128;          m_KEY_DOWN.size = 128;
269            m_KEY_DOWN.readonly = true;
270            m_NI_CALLBACK_TYPE = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, handlerType);
271            m_NKSP_IGNORE_WAIT = DECLARE_VMINT(m_event, class ScriptEvent, ignoreAllWaitCalls);
272            m_NKSP_CALLBACK_PARENT_ID = DECLARE_VMINT_READONLY(m_event, class ScriptEvent, parentHandlerID);
273      }      }
274    
275      VMExecStatus_t InstrumentScriptVM::exec(VMParserContext* parserCtx, ScriptEvent* event) {      VMExecStatus_t InstrumentScriptVM::exec(VMParserContext* parserCtx, ScriptEvent* event) {
# Line 239  namespace LinuxSampler { Line 299  namespace LinuxSampler {
299                      pEngineChannel->ControllerTable[CTRL_TABLE_IDX_PITCHBEND] =                      pEngineChannel->ControllerTable[CTRL_TABLE_IDX_PITCHBEND] =
300                          event->cause.Param.Pitch.Pitch;                          event->cause.Param.Pitch.Pitch;
301                      break;                      break;
302                    default:
303                        ; // noop
304              }              }
305          }          }
306    
# Line 249  namespace LinuxSampler { Line 311  namespace LinuxSampler {
311                  parserCtx, event->execCtx, event->handlers[event->currentHandler]                  parserCtx, event->execCtx, event->handlers[event->currentHandler]
312              );              );
313              event->executionSlices++;              event->executionSlices++;
314                if (!(res & VM_EXEC_SUSPENDED)) { // if script terminated ...
315                    // check if this script handler instance has any forked children
316                    // to be auto aborted
317                    for (int iChild = 0; iChild < MAX_FORK_PER_SCRIPT_HANDLER &&
318                         event->childHandlerID[iChild]; ++iChild)
319                    {
320                        RTList<ScriptEvent>::Iterator itChild =
321                            pEngineChannel->ScriptCallbackByID(event->childHandlerID[iChild]);
322                        if (itChild && itChild->autoAbortByParent)
323                            itChild->execCtx->signalAbort();
324                    }
325                }
326              if (res & VM_EXEC_SUSPENDED || res & VM_EXEC_ERROR) return res;              if (res & VM_EXEC_SUSPENDED || res & VM_EXEC_ERROR) return res;
327          }          }
328    
329          return res;          return res;
330      }      }
331    
332      std::map<String,VMIntRelPtr*> InstrumentScriptVM::builtInIntVariables() {      std::map<String,VMIntPtr*> InstrumentScriptVM::builtInIntVariables() {
333          // first get buil-in integer variables of derived VM class          // first get built-in integer variables of derived VM class
334          std::map<String,VMIntRelPtr*> m = ScriptVM::builtInIntVariables();          std::map<String,VMIntPtr*> m = ScriptVM::builtInIntVariables();
335    
336          // now add own built-in variables          // now add own built-in variables
337          m["$CC_NUM"] = &m_CC_NUM;          m["$CC_NUM"] = &m_CC_NUM;
# Line 265  namespace LinuxSampler { Line 339  namespace LinuxSampler {
339          m["$EVENT_NOTE"] = &m_EVENT_NOTE;          m["$EVENT_NOTE"] = &m_EVENT_NOTE;
340          m["$EVENT_VELOCITY"] = &m_EVENT_VELOCITY;          m["$EVENT_VELOCITY"] = &m_EVENT_VELOCITY;
341  //         m["$POLY_AT_NUM"] = &m_POLY_AT_NUM;  //         m["$POLY_AT_NUM"] = &m_POLY_AT_NUM;
342            m["$RPN_ADDRESS"] = &m_RPN_ADDRESS; // used for both RPN and NRPN events
343            m["$RPN_VALUE"] = &m_RPN_VALUE;     // used for both RPN and NRPN events
344            m["$NI_CALLBACK_TYPE"] = &m_NI_CALLBACK_TYPE;
345            m["$NKSP_IGNORE_WAIT"] = &m_NKSP_IGNORE_WAIT;
346            m["$NKSP_CALLBACK_PARENT_ID"] = &m_NKSP_CALLBACK_PARENT_ID;
347    
348          return m;          return m;
349      }      }
350    
351      std::map<String,VMInt8Array*> InstrumentScriptVM::builtInIntArrayVariables() {      std::map<String,VMInt8Array*> InstrumentScriptVM::builtInIntArrayVariables() {
352          // first get buil-in integer array variables of derived VM class          // first get built-in integer array variables of derived VM class
353          std::map<String,VMInt8Array*> m = ScriptVM::builtInIntArrayVariables();          std::map<String,VMInt8Array*> m = ScriptVM::builtInIntArrayVariables();
354    
355          // now add own built-in variables          // now add own built-in variables
# Line 281  namespace LinuxSampler { Line 360  namespace LinuxSampler {
360          return m;          return m;
361      }      }
362    
363      std::map<String,int> InstrumentScriptVM::builtInConstIntVariables() {      std::map<String,vmint> InstrumentScriptVM::builtInConstIntVariables() {
364          // first get buil-in integer variables of derived VM class          // first get built-in integer variables of derived VM class
365          std::map<String,int> m = ScriptVM::builtInConstIntVariables();          std::map<String,vmint> m = ScriptVM::builtInConstIntVariables();
366    
367            m["$EVENT_STATUS_INACTIVE"] = EVENT_STATUS_INACTIVE;
368            m["$EVENT_STATUS_NOTE_QUEUE"] = EVENT_STATUS_NOTE_QUEUE;
369          m["$VCC_MONO_AT"] = CTRL_TABLE_IDX_AFTERTOUCH;          m["$VCC_MONO_AT"] = CTRL_TABLE_IDX_AFTERTOUCH;
370          m["$VCC_PITCH_BEND"] = CTRL_TABLE_IDX_PITCHBEND;          m["$VCC_PITCH_BEND"] = CTRL_TABLE_IDX_PITCHBEND;
371          for (int i = 0; i < INSTR_SCRIPT_EVENT_GROUPS; ++i) {          for (int i = 0; i < INSTR_SCRIPT_EVENT_GROUPS; ++i) {
372              m["$MARK_" + ToString(i+1)] = i;              m["$MARK_" + ToString(i+1)] = i;
373          }          }
374            m["$EVENT_PAR_NOTE"] = EVENT_PAR_NOTE;
375            m["$EVENT_PAR_VELOCITY"] = EVENT_PAR_VELOCITY;
376            m["$EVENT_PAR_VOLUME"] = EVENT_PAR_VOLUME;
377            m["$EVENT_PAR_TUNE"] = EVENT_PAR_TUNE;
378            m["$EVENT_PAR_0"] = EVENT_PAR_0;
379            m["$EVENT_PAR_1"] = EVENT_PAR_1;
380            m["$EVENT_PAR_2"] = EVENT_PAR_2;
381            m["$EVENT_PAR_3"] = EVENT_PAR_3;
382            m["$NKSP_LINEAR"] = FADE_CURVE_LINEAR;
383            m["$NKSP_EASE_IN_EASE_OUT"] = FADE_CURVE_EASE_IN_EASE_OUT;
384            m["$CALLBACK_STATUS_TERMINATED"] = CALLBACK_STATUS_TERMINATED;
385            m["$CALLBACK_STATUS_QUEUE"]      = CALLBACK_STATUS_QUEUE;
386            m["$CALLBACK_STATUS_RUNNING"]    = CALLBACK_STATUS_RUNNING;
387    
388            return m;
389        }
390    
391        std::map<String,VMDynVar*> InstrumentScriptVM::builtInDynamicVariables() {
392            // first get built-in dynamic variables of derived VM class
393            std::map<String,VMDynVar*> m = ScriptVM::builtInDynamicVariables();
394    
395            m["%ALL_EVENTS"] = &m_varAllEvents;
396            m["$ENGINE_UPTIME"] = &m_varEngineUptime;
397            m["$NI_CALLBACK_ID"] = &m_varCallbackID;
398            m["%NKSP_CALLBACK_CHILD_ID"] = &m_varCallbackChildID;
399    
400          return m;          return m;
401      }      }
# Line 304  namespace LinuxSampler { Line 410  namespace LinuxSampler {
410          else if (name == "set_event_mark") return &m_fnSetEventMark;          else if (name == "set_event_mark") return &m_fnSetEventMark;
411          else if (name == "delete_event_mark") return &m_fnDeleteEventMark;          else if (name == "delete_event_mark") return &m_fnDeleteEventMark;
412          else if (name == "by_marks") return &m_fnByMarks;          else if (name == "by_marks") return &m_fnByMarks;
413            else if (name == "change_vol") return &m_fnChangeVol;
414            else if (name == "change_vol_time") return &m_fnChangeVolTime;
415            else if (name == "change_tune") return &m_fnChangeTune;
416            else if (name == "change_tune_time") return &m_fnChangeTuneTime;
417            else if (name == "change_note") return &m_fnChangeNote;
418            else if (name == "change_velo") return &m_fnChangeVelo;
419            else if (name == "change_pan") return &m_fnChangePan;
420            else if (name == "change_pan_time") return &m_fnChangePanTime;
421            else if (name == "change_pan_curve") return &m_fnChangePanCurve;
422            else if (name == "change_cutoff") return &m_fnChangeCutoff;
423            else if (name == "change_reso") return &m_fnChangeReso;
424            else if (name == "change_attack") return &m_fnChangeAttack;
425            else if (name == "change_decay") return &m_fnChangeDecay;
426            else if (name == "change_sustain") return &m_fnChangeSustain;
427            else if (name == "change_release") return &m_fnChangeRelease;
428            else if (name == "change_cutoff_attack") return &m_fnChangeCutoffAttack;
429            else if (name == "change_cutoff_decay") return &m_fnChangeCutoffDecay;
430            else if (name == "change_cutoff_sustain") return &m_fnChangeCutoffSustain;
431            else if (name == "change_cutoff_release") return &m_fnChangeCutoffRelease;
432            else if (name == "change_amp_lfo_depth") return &m_fnChangeAmpLFODepth;
433            else if (name == "change_amp_lfo_freq") return &m_fnChangeAmpLFOFreq;
434            else if (name == "change_cutoff_lfo_depth") return &m_fnChangeCutoffLFODepth;
435            else if (name == "change_cutoff_lfo_freq") return &m_fnChangeCutoffLFOFreq;
436            else if (name == "change_pitch_lfo_depth") return &m_fnChangePitchLFODepth;
437            else if (name == "change_pitch_lfo_freq") return &m_fnChangePitchLFOFreq;
438            else if (name == "fade_in") return &m_fnFadeIn;
439            else if (name == "fade_out") return &m_fnFadeOut;
440            else if (name == "change_vol_curve") return &m_fnChangeVolCurve;
441            else if (name == "change_tune_curve") return &m_fnChangeTuneCurve;
442            else if (name == "change_play_pos") return &m_fnChangePlayPos;
443            else if (name == "get_event_par") return &m_fnGetEventPar;
444            else if (name == "set_event_par") return &m_fnSetEventPar;
445            else if (name == "event_status") return &m_fnEventStatus;
446            else if (name == "wait") return &m_fnWait2; // override wait() core implementation
447            else if (name == "stop_wait") return &m_fnStopWait;
448            else if (name == "abort") return &m_fnAbort;
449            else if (name == "fork") return &m_fnFork;
450            else if (name == "callback_status") return &m_fnCallbackStatus;
451    
452          // built-in script functions of derived VM class          // built-in script functions of derived VM class
453          return ScriptVM::functionByName(name);          return ScriptVM::functionByName(name);

Legend:
Removed from v.2645  
changed lines
  Added in v.3690

  ViewVC Help
Powered by ViewVC