/[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 2600 by schoenebeck, Sat Jun 7 00:16:03 2014 UTC revision 2612 by schoenebeck, Tue Jun 10 13:32:16 2014 UTC
# Line 10  Line 10 
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            unload();
36    
37            code = text;
38    
39            AbstractInstrumentManager* pManager =
40                dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager());
41    
42            // get new script reference
43            parserContext = pManager->scripts.Borrow(text, pEngineChannel);
44            if (!parserContext->errors().empty()) {
45                std::vector<ParserIssue> errors = parserContext->errors();
46                std::cerr << "[ScriptVM] Could not load instrument script, there were "
47                        << errors.size() << " parser errors:\n";
48                for (int i = 0; i < errors.size(); ++i)
49                    errors[i].dump();
50                return; // stop here if there were any parser errors
51            }
52    
53            handlerInit = parserContext->eventHandlerByName("init");
54            handlerNote = parserContext->eventHandlerByName("note");
55            handlerRelease = parserContext->eventHandlerByName("release");
56            handlerController = parserContext->eventHandlerByName("controller");
57            bHasValidScript =
58                handlerInit || handlerNote || handlerRelease || handlerController;
59    
60            // amount of script handlers each script event has to execute
61            int handlerExecCount = 0;
62            if (handlerNote || handlerRelease || handlerController) // only one of these are executed after "init" handler
63                handlerExecCount++;
64    
65            // create script event pool (if it doesn't exist already)
66            if (!pEvents)
67                pEvents = new Pool<ScriptEvent>(CONFIG_MAX_EVENTS_PER_FRAGMENT);
68    
69            // create new VM execution contexts for new script
70            while (!pEvents->poolIsEmpty()) {
71                RTList<ScriptEvent>::Iterator it = pEvents->allocAppend();
72                it->execCtx = pEngineChannel->pEngine->pScriptVM->createExecContext(
73                    parserContext
74                );
75                it->handlers = new VMEventHandler*[handlerExecCount+1];
76            }
77            pEvents->clear();
78    
79            dmsg(1,("Done\n"));
80        }
81    
82        /** @brief Unload real-time instrument script.
83         *
84         * Unloads the currently used real-time instrument script and frees all
85         * resources allocated for that script. The sampler engine's resource manager
86         * is used to share equivalent scripts among multiple sampler channels, and
87         * to deallocate the parsed script once not used on any engine channel
88         * anymore.
89         *
90         * Calling thid method will however not clear the @c code member variable.
91         * Thus, the script can be parsed again afterwards.
92         */
93        void InstrumentScript::unload() {
94            if (parserContext)
95                dmsg(1,("Unloading current instrument script.\n"));
96    
97            // free allocated VM execution contexts
98            if (pEvents) {
99                pEvents->clear();
100                while (!pEvents->poolIsEmpty()) {
101                    RTList<ScriptEvent>::Iterator it = pEvents->allocAppend();
102                    if (it->execCtx) {
103                        // free VM execution context object
104                        delete it->execCtx;
105                        it->execCtx = NULL;
106                        // free C array of handler pointers
107                        delete [] it->handlers;
108                    }
109                }
110                pEvents->clear();
111            }
112            // hand back VM representation of script
113            if (parserContext) {
114                AbstractInstrumentManager* pManager =
115                    dynamic_cast<AbstractInstrumentManager*>(pEngineChannel->pEngine->GetInstrumentManager());
116    
117                pManager->scripts.HandBack(parserContext, pEngineChannel);
118                parserContext = NULL;
119                handlerInit = NULL;
120                handlerNote = NULL;
121                handlerRelease = NULL;
122                handlerController = NULL;
123            }
124            bHasValidScript = false;
125        }
126    
127        /**
128         * Same as unload(), but this one also empties the @c code member variable
129         * to an empty string.
130         */
131        void InstrumentScript::resetAll() {
132            unload();
133            code.clear();
134        }
135    
136        ///////////////////////////////////////////////////////////////////////
137        // class 'InstrumentScriptVM'
138    
139      InstrumentScriptVM::InstrumentScriptVM() :      InstrumentScriptVM::InstrumentScriptVM() :
140          m_event(NULL), m_fnPlayNote(this), m_fnSetController(this),          m_event(NULL), m_fnPlayNote(this), m_fnSetController(this),
141          m_fnIgnoreEvent(this), m_fnIgnoreController(this)          m_fnIgnoreEvent(this), m_fnIgnoreController(this)

Legend:
Removed from v.2600  
changed lines
  Added in v.2612

  ViewVC Help
Powered by ViewVC