/[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 2594 by schoenebeck, Thu Jun 5 00:16:25 2014 UTC revision 2629 by schoenebeck, Thu Jun 12 18:25:11 2014 UTC
# Line 9  Line 9 
9    
10  #include "InstrumentScriptVM.h"  #include "InstrumentScriptVM.h"
11  #include "../AbstractEngineChannel.h"  #include "../AbstractEngineChannel.h"
12    #include "../../common/global_private.h"
13    #include "AbstractInstrumentManager.h"
14    #include "MidiKeyboardManager.h"
15    
16  namespace LinuxSampler {  namespace LinuxSampler {
17    
18      // circumvents a bug in GCC 4.4 which prevents the sizeof() expr to be used      ///////////////////////////////////////////////////////////////////////
19      // directly within the scrope of a class (would throw a compiler error with:      // class 'InstrumentScript'
20      // "object missing in reference to 'LinuxSampler::AbstractEngineChannel::ControllerTable'")  
21      static const int _AbstractEngineChannel_ControllerTable_size = sizeof(AbstractEngineChannel::ControllerTable);      /** @brief Load real-time instrument script.
22         *
23      InstrumentScriptVM::InstrumentScriptVM() : m_event(NULL) {       * Loads the real-time instrument script given by @a text on the engine
24          m_CC.size = _AbstractEngineChannel_ControllerTable_size;       * channel this InstrumentScript object belongs to (defined by
25          m_CC_NUM = DECLARE_VMINT(m_cause, class Event, Param.CC.Controller);       * pEngineChannel member variable). The sampler engine's resource manager is
26          m_EVENT_NOTE = DECLARE_VMINT(m_cause, class Event, Param.Note.Key);       * used to allocate and share equivalent scripts on multiple engine
27          m_EVENT_VELOCITY = DECLARE_VMINT(m_cause, class Event, Param.Note.Velocity);       * 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 this 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            //dmsg(1,("InstrumentScript::unload(this=0x%llx)\n", this));
96    
97            if (parserContext)
98                dmsg(1,("Unloading current instrument script.\n"));
99    
100            // free allocated VM execution contexts
101            if (pEvents) {
102                pEvents->clear();
103                while (!pEvents->poolIsEmpty()) {
104                    RTList<ScriptEvent>::Iterator it = pEvents->allocAppend();
105                    if (it->execCtx) {
106                        // free VM execution context object
107                        delete it->execCtx;
108                        it->execCtx = NULL;
109                        // free C array of handler pointers
110                        delete [] it->handlers;
111                    }
112                }
113                pEvents->clear();
114            }
115            // hand back VM representation of script
116            if (parserContext) {
117                AbstractInstrumentManager* pManager =
118                    dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager());
119    
120                pManager->scripts.HandBack(parserContext, pEngineChannel);
121                parserContext = NULL;
122                handlerInit = NULL;
123                handlerNote = NULL;
124                handlerRelease = NULL;
125                handlerController = NULL;
126            }
127            bHasValidScript = false;
128        }
129    
130        /**
131         * Same as unload(), but this one also empties the @c code member variable
132         * to an empty string.
133         */
134        void InstrumentScript::resetAll() {
135            unload();
136            code.clear();
137        }
138    
139        ///////////////////////////////////////////////////////////////////////
140        // class 'InstrumentScriptVM'
141    
142        InstrumentScriptVM::InstrumentScriptVM() :
143            m_event(NULL), m_fnPlayNote(this), m_fnSetController(this),
144            m_fnIgnoreEvent(this), m_fnIgnoreController(this), m_fnNoteOff(this)
145        {
146            m_CC.size = _MEMBER_SIZEOF(AbstractEngineChannel, ControllerTable);
147            m_CC_NUM = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.CC.Controller);
148            m_EVENT_ID = DECLARE_VMINT(m_event, class ScriptEvent, id);
149            m_EVENT_NOTE = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.Note.Key);
150            m_EVENT_VELOCITY = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.Note.Velocity);
151            m_KEY_DOWN.size = 128;
152      }      }
153    
154      VMExecStatus_t InstrumentScriptVM::exec(VMParserContext* parserCtx, ScriptEvent* event) {      VMExecStatus_t InstrumentScriptVM::exec(VMParserContext* parserCtx, ScriptEvent* event) {
# Line 29  namespace LinuxSampler { Line 156  namespace LinuxSampler {
156              static_cast<AbstractEngineChannel*>(event->cause.pEngineChannel);              static_cast<AbstractEngineChannel*>(event->cause.pEngineChannel);
157    
158          // prepare built-in script variables for script execution          // prepare built-in script variables for script execution
159          m_cause = &event->cause;          m_event = event;
160          m_CC.data = (int8_t*) &pEngineChannel->ControllerTable[0];          m_CC.data = (int8_t*) &pEngineChannel->ControllerTable[0];
161            m_KEY_DOWN.data = &pEngineChannel->GetMidiKeyboardManager()->KeyDown[0];
162    
163          // if script is in start condition, then do mandatory MIDI event          // if script is in start condition, then do mandatory MIDI event
164          // preprocessing tasks, which essentially means updating i.e. controller          // preprocessing tasks, which essentially means updating i.e. controller
165          // table with new CC value in case of a controller event, because the          // table with new CC value in case of a controller event, because the
166          // script might access the new CC value          // script might access the new CC value
167          if (!event->executionSlices) {          if (!event->executionSlices) {
168              switch (m_cause->Type) {              switch (event->cause.Type) {
169                  case Event::type_control_change:                  case Event::type_control_change:
170                      pEngineChannel->ControllerTable[m_cause->Param.CC.Controller] =                      pEngineChannel->ControllerTable[event->cause.Param.CC.Controller] =
171                          m_cause->Param.CC.Value;                          event->cause.Param.CC.Value;
172                      break;                      break;
173                  case Event::type_channel_pressure:                  case Event::type_channel_pressure:
174                      pEngineChannel->ControllerTable[CTRL_TABLE_IDX_AFTERTOUCH] =                      pEngineChannel->ControllerTable[CTRL_TABLE_IDX_AFTERTOUCH] =
175                          m_cause->Param.ChannelPressure.Value;                          event->cause.Param.ChannelPressure.Value;
176                      break;                      break;
177                  case Event::type_pitchbend:                  case Event::type_pitchbend:
178                      pEngineChannel->ControllerTable[CTRL_TABLE_IDX_PITCHBEND] =                      pEngineChannel->ControllerTable[CTRL_TABLE_IDX_PITCHBEND] =
179                          m_cause->Param.Pitch.Pitch;                          event->cause.Param.Pitch.Pitch;
180                      break;                      break;
181              }              }
182          }          }
183    
184          // run the script handler(s)          // run the script handler(s)
185          VMExecStatus_t res = VM_EXEC_NOT_RUNNING;          VMExecStatus_t res = VM_EXEC_NOT_RUNNING;
186          while (event->handlers[event->currentHandler]) {          for ( ; event->handlers[event->currentHandler]; event->currentHandler++) {
187              res = ScriptVM::exec(              res = ScriptVM::exec(
188                  parserCtx, event->execCtx, event->handlers[event->currentHandler++]                  parserCtx, event->execCtx, event->handlers[event->currentHandler]
189              );              );
190              event->executionSlices++;              event->executionSlices++;
191              if (res & VM_EXEC_SUSPENDED || res & VM_EXEC_ERROR) return res;              if (res & VM_EXEC_SUSPENDED || res & VM_EXEC_ERROR) return res;
# Line 72  namespace LinuxSampler { Line 200  namespace LinuxSampler {
200    
201          // now add own built-in variables          // now add own built-in variables
202          m["$CC_NUM"] = &m_CC_NUM;          m["$CC_NUM"] = &m_CC_NUM;
203            m["$EVENT_ID"] = &m_EVENT_ID;
204          m["$EVENT_NOTE"] = &m_EVENT_NOTE;          m["$EVENT_NOTE"] = &m_EVENT_NOTE;
205          m["$EVENT_VELOCITY"] = &m_EVENT_VELOCITY;          m["$EVENT_VELOCITY"] = &m_EVENT_VELOCITY;
206  //         m["$POLY_AT_NUM"] = &m_POLY_AT_NUM;  //         m["$POLY_AT_NUM"] = &m_POLY_AT_NUM;
# Line 85  namespace LinuxSampler { Line 214  namespace LinuxSampler {
214    
215          // now add own built-in variables          // now add own built-in variables
216          m["%CC"] = &m_CC;          m["%CC"] = &m_CC;
217          //m["%KEY_DOWN"] = &m_KEY_DOWN;          m["%KEY_DOWN"] = &m_KEY_DOWN;
218          //m["%POLY_AT"] = &m_POLY_AT;          //m["%POLY_AT"] = &m_POLY_AT;
219    
220          return m;          return m;
# Line 101  namespace LinuxSampler { Line 230  namespace LinuxSampler {
230          return m;          return m;
231      }      }
232    
233        VMFunction* InstrumentScriptVM::functionByName(const String& name) {
234            // built-in script functions of this class
235            if      (name == "play_note") return &m_fnPlayNote;
236            else if (name == "set_controller") return &m_fnSetController;
237            else if (name == "ignore_event") return &m_fnIgnoreEvent;
238            else if (name == "ignore_controller") return &m_fnIgnoreController;
239            else if (name == "note_off") return &m_fnNoteOff;
240    
241            // built-in script functions of derived VM class
242            return ScriptVM::functionByName(name);
243        }
244    
245  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.2594  
changed lines
  Added in v.2629

  ViewVC Help
Powered by ViewVC