1 /*
2  * Copyright (c) 2016 - 2019 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 "InstrumentScriptVMFunctions.h"
11 #include "InstrumentScriptVMDynVars.h"
12 #include "InstrumentScriptVM.h"
13 #include "../AbstractEngineChannel.h"
14 #include "../EngineBase.h"
15 
16 namespace LinuxSampler {
17 
18     // built-in variable $ENGINE_UPTIME
19 
evalInt()20     vmint InstrumentScriptVMDynVar_ENGINE_UPTIME::evalInt() {
21 
22         AbstractEngineChannel* pEngineChannel =
23             static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
24 
25         AbstractEngine* pEngine =
26             static_cast<AbstractEngine*>(pEngineChannel->GetEngine());
27 
28         // engine's official playback time in milliseconds (offline bounce safe)
29         return vmint( double(pEngine->FrameTime + m_vm->m_event->cause.FragmentPos()) /
30                       double(pEngine->SampleRate) * 1000.0 );
31     }
32 
33     // built-in variable $NI_CALLBACK_ID
34 
evalInt()35     vmint InstrumentScriptVMDynVar_NI_CALLBACK_ID::evalInt() {
36 
37         AbstractEngineChannel* pEngineChannel =
38             static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
39 
40         return pEngineChannel->GetScriptCallbackID(m_vm->m_event);
41     }
42 
43     // built-in array variable %NKSP_CALLBACK_CHILD_ID[]
44 
InstrumentScriptVMDynVar_NKSP_CALLBACK_CHILD_ID(InstrumentScriptVM * parent)45     InstrumentScriptVMDynVar_NKSP_CALLBACK_CHILD_ID::InstrumentScriptVMDynVar_NKSP_CALLBACK_CHILD_ID(InstrumentScriptVM* parent)
46         : m_vm(parent)
47     {
48     }
49 
asIntArray() const50     VMIntArrayExpr* InstrumentScriptVMDynVar_NKSP_CALLBACK_CHILD_ID::asIntArray() const {
51         return const_cast<VMIntArrayExpr*>( dynamic_cast<const VMIntArrayExpr*>(this) );
52     }
53 
arraySize() const54     vmint InstrumentScriptVMDynVar_NKSP_CALLBACK_CHILD_ID::arraySize() const {
55         return m_vm->m_event->countChildHandlers();
56     }
57 
evalIntElement(vmuint i)58     vmint InstrumentScriptVMDynVar_NKSP_CALLBACK_CHILD_ID::evalIntElement(vmuint i) {
59         if (i >= arraySize()) return 0;
60         return m_vm->m_event->childHandlerID[i];
61     }
62 
63     // built-in variable %ALL_EVENTS
64 
InstrumentScriptVMDynVar_ALL_EVENTS(InstrumentScriptVM * parent)65     InstrumentScriptVMDynVar_ALL_EVENTS::InstrumentScriptVMDynVar_ALL_EVENTS(InstrumentScriptVM* parent)
66         : m_vm(parent), m_ids(NULL), m_numIDs(0)
67     {
68         m_ids = new note_id_t[GLOBAL_MAX_NOTES];
69         memset(&m_ids[0], 0, GLOBAL_MAX_NOTES * sizeof(note_id_t));
70     }
71 
~InstrumentScriptVMDynVar_ALL_EVENTS()72     InstrumentScriptVMDynVar_ALL_EVENTS::~InstrumentScriptVMDynVar_ALL_EVENTS() {
73         if (m_ids) delete[] m_ids;
74     }
75 
asIntArray() const76     VMIntArrayExpr* InstrumentScriptVMDynVar_ALL_EVENTS::asIntArray() const {
77         const_cast<InstrumentScriptVMDynVar_ALL_EVENTS*>(this)->updateNoteIDs();
78         return const_cast<VMIntArrayExpr*>( dynamic_cast<const VMIntArrayExpr*>(this) );
79     }
80 
arraySize() const81     vmint InstrumentScriptVMDynVar_ALL_EVENTS::arraySize() const {
82         return m_numIDs;
83     }
84 
evalIntElement(vmuint i)85     vmint InstrumentScriptVMDynVar_ALL_EVENTS::evalIntElement(vmuint i) {
86         if (i >= m_numIDs) return 0;
87         return m_ids[i];
88     }
89 
updateNoteIDs()90     void InstrumentScriptVMDynVar_ALL_EVENTS::updateNoteIDs() {
91 
92         AbstractEngineChannel* pEngineChannel =
93             static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
94 
95         m_numIDs = pEngineChannel->AllNoteIDs(&m_ids[0], GLOBAL_MAX_NOTES);
96 
97         // translate sampler engine internal note IDs to public script id scope
98         for (vmuint i = 0; i < m_numIDs; ++i)
99             m_ids[i] = ScriptID::fromNoteID(m_ids[i]);
100     }
101 
102 } // namespace LinuxSampler
103