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

Annotation of /linuxsampler/trunk/src/engines/common/InstrumentScriptVM.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2871 - (hide annotations) (download) (as text)
Sun Apr 10 18:22:23 2016 UTC (2 years, 8 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 7563 byte(s)
* All engines: Implemented scheduler for delayed MIDI events and for
  suspended real-time instrument scripts.
* Real-Time instrument scripts: Implemented support for built-in "wait()"
  function's "duration-us" argument, thus scripts using this function are
  now correctly resumed after the requested amount of microseconds.
* Real-Time instrument scripts: Implemented support for built-in
  "play_note()" function's "duration-us" argument, thus notes triggered
  with this argument are now correctly released after the requested amount
  of microseconds.
* Real-Time instrument scripts: Fixed crash which happened when trying to
  reference an undeclared script variable.
* Real-Time instrument scripts: Script events were not cleared when
  engine channel was reset, potentially causing undefined behavior.
* All engines: Attempt to partly fix resetting engine channels vs.
  resetting engine, an overall cleanup of the Reset*(),
  ConnectAudioDevice(), DisconnectAudioDevice() API methods would still be
  desirable though, because the current situation is still inconsistent
  and error prone.
* Bumped version (2.0.0.svn2).

1 schoenebeck 2594 /*
2 schoenebeck 2871 * Copyright (c) 2014-2016 Christian Schoenebeck
3 schoenebeck 2594 *
4     * http://www.linuxsampler.org
5     *
6     * This file is part of LinuxSampler and released under the same terms.
7     * See README file for details.
8     */
9    
10     #ifndef LS_INSTRUMENT_SCRIPT_VM_H
11     #define LS_INSTRUMENT_SCRIPT_VM_H
12    
13     #include "../../common/global.h"
14 schoenebeck 2630 #include "../../common/ConstCapacityArray.h"
15 schoenebeck 2594 #include "../../scriptvm/ScriptVM.h"
16     #include "Event.h"
17 schoenebeck 2611 #include "../../common/Pool.h"
18 schoenebeck 2596 #include "InstrumentScriptVMFunctions.h"
19 schoenebeck 2594
20 schoenebeck 2630 #define INSTR_SCRIPT_EVENT_GROUPS 28
21    
22 schoenebeck 2594 namespace LinuxSampler {
23    
24 schoenebeck 2611 class AbstractEngineChannel;
25 schoenebeck 2630 class InstrumentScript;
26 schoenebeck 2611
27 schoenebeck 2630 /** @brief List of Event IDs.
28     *
29     * Used for built-in script functions:
30     * by_marks(), set_event_mark(), delete_event_mark()
31     */
32     class EventGroup : protected ConstCapacityArray<int> {
33     public:
34     EventGroup() : ConstCapacityArray<int>(CONFIG_MAX_EVENTS_PER_FRAGMENT), m_script(NULL) {}
35     void insert(int eventID);
36     void erase(int eventID);
37     void setScript(InstrumentScript* pScript) { m_script = pScript; }
38     inline int size() const { return ConstCapacityArray<int>::size(); }
39 schoenebeck 2871 inline void clear() { ConstCapacityArray<int>::clear(); }
40 schoenebeck 2630 inline int& operator[](uint index) { return ConstCapacityArray<int>::operator[](index); }
41     inline const int& operator[](uint index) const { return ConstCapacityArray<int>::operator[](index); }
42     protected:
43     InstrumentScript* m_script;
44     };
45    
46 schoenebeck 2611 /** @brief Real-time instrument script VM representation.
47     *
48     * Holds the VM representation of all event handlers of the currently loaded
49     * script, ready to be executed by the sampler engine.
50 schoenebeck 2645 *
51     * Even thought scripts (or to be more specific their event handler objects)
52     * are shared between sampler engine channels, the InstrumentScript struct
53     * instances though are not shared. Each sampler engine channel has its own
54     * instance of a InstrumentScript struct. That's important, because this
55     * struct also holds engine channel local informations, for example the
56     * events that occured on the respective engine channel.
57 schoenebeck 2611 */
58     struct InstrumentScript {
59     VMParserContext* parserContext; ///< VM represenation of the currently loaded script or NULL if not script was loaded. Note that it is also not NULL if parser errors occurred!
60     bool bHasValidScript; ///< True in case there is a valid script currently loaded, false if script processing shall be skipped.
61     VMEventHandler* handlerInit; ///< VM representation of script's initilization callback or NULL if current script did not define such an init handler.
62     VMEventHandler* handlerNote; ///< VM representation of script's MIDI note on callback or NULL if current script did not define such an event handler.
63     VMEventHandler* handlerRelease; ///< VM representation of script's MIDI note off callback or NULL if current script did not define such an event handler.
64     VMEventHandler* handlerController; ///< VM representation of script's MIDI controller callback or NULL if current script did not define such an event handler.
65 schoenebeck 2645 Pool<ScriptEvent>* pEvents; ///< Pool of all available script execution instances. ScriptEvents available to be allocated from the Pool are currently unused / not executiong, whereas the ScriptEvents allocated on the list are currently suspended / have not finished execution yet (@see pKeyEvents).
66     RTList<ScriptEvent>* pKeyEvents[128]; ///< Stores previously finished executed "note on" script events for the respective active note/key as long as the key/note is active. This is however only done if there is a "note" script event handler and a "release" script event handler defined in the script and both handlers use (reference) polyphonic variables. If that is not the case, then this list is not used at all. So the purpose of pKeyEvents is only to implement preserving/passing polyphonic variable data from "on note .. end on" script block to the respective "on release .. end on" script block.
67 schoenebeck 2871 RTAVLTree<ScriptEvent> suspendedEvents; ///< Contains pointers to all suspended events, sorted by time when those script events are to be resumed next.
68 schoenebeck 2611 AbstractEngineChannel* pEngineChannel;
69 schoenebeck 2612 String code; ///< Source code of the instrument script. Used in case the sampler engine is changed, in that case a new ScriptVM object is created for the engine and VMParserContext object for this script needs to be recreated as well. Thus the script is then parsed again by passing the source code to recreate the parser context.
70 schoenebeck 2630 EventGroup eventGroups[INSTR_SCRIPT_EVENT_GROUPS]; ///< Used for built-in script functions: by_event_marks(), set_event_mark(), delete_event_mark().
71 schoenebeck 2611
72 schoenebeck 2630 InstrumentScript(AbstractEngineChannel* pEngineChannel);
73 schoenebeck 2645 ~InstrumentScript();
74 schoenebeck 2611
75     void load(const String& text);
76 schoenebeck 2612 void unload();
77     void resetAll();
78 schoenebeck 2871 void resetEvents();
79 schoenebeck 2611 };
80    
81 schoenebeck 2594 /** @brief Real-time instrument script virtual machine.
82     *
83     * Extends the core ScriptVM implementation with MIDI specific built-in
84     * script functions and MIDI specific built-in script variables required
85     * for MIDI processing by instrument script for all sampler engine
86     * implementations (sampler formats) of this sampler.
87     *
88     * Note that this class is currently re-entrant safe, but @b not thread
89     * safe! See also comments of base class ScriptVM regarding this issue.
90     */
91     class InstrumentScriptVM : public ScriptVM {
92     public:
93     InstrumentScriptVM();
94     VMExecStatus_t exec(VMParserContext* parserCtx, ScriptEvent* event);
95 schoenebeck 2596 VMFunction* functionByName(const String& name) OVERRIDE;
96 schoenebeck 2594 std::map<String,VMIntRelPtr*> builtInIntVariables() OVERRIDE;
97     std::map<String,VMInt8Array*> builtInIntArrayVariables() OVERRIDE;
98     std::map<String,int> builtInConstIntVariables() OVERRIDE;
99     protected:
100     ScriptEvent* m_event; ///< The event currently executed by exec().
101    
102     // buil-in script variables
103     VMInt8Array m_CC;
104     VMInt8RelPtr m_CC_NUM;
105 schoenebeck 2598 VMIntRelPtr m_EVENT_ID;
106 schoenebeck 2594 VMInt8RelPtr m_EVENT_NOTE;
107     VMInt8RelPtr m_EVENT_VELOCITY;
108 schoenebeck 2613 VMInt8Array m_KEY_DOWN;
109 schoenebeck 2594 //VMIntArray m_POLY_AT; //TODO: ...
110     //int m_POLY_AT_NUM; //TODO: ...
111 schoenebeck 2596
112     // buil-in script functions
113     InstrumentScriptVMFunction_play_note m_fnPlayNote;
114 schoenebeck 2600 InstrumentScriptVMFunction_set_controller m_fnSetController;
115     InstrumentScriptVMFunction_ignore_event m_fnIgnoreEvent;
116     InstrumentScriptVMFunction_ignore_controller m_fnIgnoreController;
117 schoenebeck 2629 InstrumentScriptVMFunction_note_off m_fnNoteOff;
118 schoenebeck 2630 InstrumentScriptVMFunction_set_event_mark m_fnSetEventMark;
119     InstrumentScriptVMFunction_delete_event_mark m_fnDeleteEventMark;
120     InstrumentScriptVMFunction_by_marks m_fnByMarks;
121 schoenebeck 2596
122     friend class InstrumentScriptVMFunction_play_note;
123 schoenebeck 2600 friend class InstrumentScriptVMFunction_set_controller;
124 schoenebeck 2598 friend class InstrumentScriptVMFunction_ignore_event;
125     friend class InstrumentScriptVMFunction_ignore_controller;
126 schoenebeck 2629 friend class InstrumentScriptVMFunction_note_off;
127 schoenebeck 2630 friend class InstrumentScriptVMFunction_set_event_mark;
128     friend class InstrumentScriptVMFunction_delete_event_mark;
129     friend class InstrumentScriptVMFunction_by_marks;
130 schoenebeck 2594 };
131    
132     } // namespace LinuxSampler
133    
134     #endif // LS_INSTRUMENT_SCRIPT_VM_H

  ViewVC Help
Powered by ViewVC