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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2598 - (show annotations) (download)
Fri Jun 6 12:38:54 2014 UTC (9 years, 10 months ago) by schoenebeck
File size: 4912 byte(s)
* ScriptVM (WIP): Built-in script function "play_note()" now returns the
  event ID of the triggered note.
* ScriptVM (WIP): Implemented built-in script int variable $EVENT_ID.
* ScriptVM (WIP): Implemented built-in script function "ignore_event()".
* ScriptVM (WIP): Implemented built-in script function
  "ignore_controller()" (accepts one and no argument).
* Bumped version (1.0.0.svn44).

1 /*
2 * Copyright (c) 2014 Christian Schoenebeck
3 *
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 #include "InstrumentScriptVM.h"
11 #include "../AbstractEngineChannel.h"
12
13 namespace LinuxSampler {
14
15 // circumvents a bug in GCC 4.x which causes a sizeof() expression applied
16 // on a class member to throw a compiler error, i.e. with GCC 4.4:
17 // "object missing in reference to 'LinuxSampler::AbstractEngineChannel::ControllerTable'")
18 // or with GCC 4.0:
19 // "invalid use of non-static data member 'LinuxSampler::AbstractEngineChannel::ControllerTable'"
20 #define _MEMBER_SIZEOF(T_Class, Member) sizeof(((T_Class*)NULL)->Member)
21
22 InstrumentScriptVM::InstrumentScriptVM() :
23 m_event(NULL), m_fnPlayNote(this), m_fnIgnoreEvent(this),
24 m_fnIgnoreController(this)
25 {
26 m_CC.size = _MEMBER_SIZEOF(AbstractEngineChannel, ControllerTable);
27 m_CC_NUM = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.CC.Controller);
28 m_EVENT_ID = DECLARE_VMINT(m_event, class ScriptEvent, id);
29 m_EVENT_NOTE = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.Note.Key);
30 m_EVENT_VELOCITY = DECLARE_VMINT(m_event, class ScriptEvent, cause.Param.Note.Velocity);
31 }
32
33 VMExecStatus_t InstrumentScriptVM::exec(VMParserContext* parserCtx, ScriptEvent* event) {
34 AbstractEngineChannel* pEngineChannel =
35 static_cast<AbstractEngineChannel*>(event->cause.pEngineChannel);
36
37 // prepare built-in script variables for script execution
38 m_event = event;
39 m_CC.data = (int8_t*) &pEngineChannel->ControllerTable[0];
40
41 // if script is in start condition, then do mandatory MIDI event
42 // preprocessing tasks, which essentially means updating i.e. controller
43 // table with new CC value in case of a controller event, because the
44 // script might access the new CC value
45 if (!event->executionSlices) {
46 switch (event->cause.Type) {
47 case Event::type_control_change:
48 pEngineChannel->ControllerTable[event->cause.Param.CC.Controller] =
49 event->cause.Param.CC.Value;
50 break;
51 case Event::type_channel_pressure:
52 pEngineChannel->ControllerTable[CTRL_TABLE_IDX_AFTERTOUCH] =
53 event->cause.Param.ChannelPressure.Value;
54 break;
55 case Event::type_pitchbend:
56 pEngineChannel->ControllerTable[CTRL_TABLE_IDX_PITCHBEND] =
57 event->cause.Param.Pitch.Pitch;
58 break;
59 }
60 }
61
62 // run the script handler(s)
63 VMExecStatus_t res = VM_EXEC_NOT_RUNNING;
64 for ( ; event->handlers[event->currentHandler]; event->currentHandler++) {
65 res = ScriptVM::exec(
66 parserCtx, event->execCtx, event->handlers[event->currentHandler]
67 );
68 event->executionSlices++;
69 if (res & VM_EXEC_SUSPENDED || res & VM_EXEC_ERROR) return res;
70 }
71
72 return res;
73 }
74
75 std::map<String,VMIntRelPtr*> InstrumentScriptVM::builtInIntVariables() {
76 // first get buil-in integer variables of derived VM class
77 std::map<String,VMIntRelPtr*> m = ScriptVM::builtInIntVariables();
78
79 // now add own built-in variables
80 m["$CC_NUM"] = &m_CC_NUM;
81 m["$EVENT_ID"] = &m_EVENT_ID;
82 m["$EVENT_NOTE"] = &m_EVENT_NOTE;
83 m["$EVENT_VELOCITY"] = &m_EVENT_VELOCITY;
84 // m["$POLY_AT_NUM"] = &m_POLY_AT_NUM;
85
86 return m;
87 }
88
89 std::map<String,VMInt8Array*> InstrumentScriptVM::builtInIntArrayVariables() {
90 // first get buil-in integer array variables of derived VM class
91 std::map<String,VMInt8Array*> m = ScriptVM::builtInIntArrayVariables();
92
93 // now add own built-in variables
94 m["%CC"] = &m_CC;
95 //m["%KEY_DOWN"] = &m_KEY_DOWN;
96 //m["%POLY_AT"] = &m_POLY_AT;
97
98 return m;
99 }
100
101 std::map<String,int> InstrumentScriptVM::builtInConstIntVariables() {
102 // first get buil-in integer variables of derived VM class
103 std::map<String,int> m = ScriptVM::builtInConstIntVariables();
104
105 m["$VCC_MONO_AT"] = CTRL_TABLE_IDX_AFTERTOUCH;
106 m["$VCC_PITCH_BEND"] = CTRL_TABLE_IDX_PITCHBEND;
107
108 return m;
109 }
110
111 VMFunction* InstrumentScriptVM::functionByName(const String& name) {
112 // built-in script functions of this class
113 if (name == "play_note") return &m_fnPlayNote;
114 else if (name == "ignore_event") return &m_fnIgnoreEvent;
115 else if (name == "ignore_controller") return &m_fnIgnoreController;
116
117 // built-in script functions of derived VM class
118 return ScriptVM::functionByName(name);
119 }
120
121 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC