1 |
/* |
2 |
* Copyright (c) 2014-2020 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 "InstrumentScriptVM.h" |
12 |
#include "EngineChannel.h" |
13 |
|
14 |
namespace LinuxSampler { namespace gig { |
15 |
|
16 |
///////////////////////////////////////////////////////////////////////// |
17 |
// Function: |
18 |
// gig_set_dim_zone(event_id, dimension, zone) |
19 |
|
20 |
InstrumentScriptVMFunction_gig_set_dim_zone::InstrumentScriptVMFunction_gig_set_dim_zone(InstrumentScriptVM* parent) |
21 |
: m_vm(parent) |
22 |
{ |
23 |
} |
24 |
|
25 |
bool InstrumentScriptVMFunction_gig_set_dim_zone::acceptsArgType(vmint iArg, ExprType_t type) const { |
26 |
return type == INT_EXPR || type == INT_ARR_EXPR; |
27 |
} |
28 |
|
29 |
VMFnResult* InstrumentScriptVMFunction_gig_set_dim_zone::exec(VMFnArgs* args) { |
30 |
EngineChannel* pEngineChannel = |
31 |
static_cast<EngineChannel*>(m_vm->m_event->cause.pEngineChannel); |
32 |
|
33 |
vmint dim = args->arg(1)->asInt()->evalInt(); |
34 |
vmint zone = args->arg(2)->asInt()->evalInt(); |
35 |
|
36 |
if (args->arg(0)->exprType() == INT_EXPR) { |
37 |
const ScriptID id = args->arg(0)->asInt()->evalInt(); |
38 |
if (!id) { |
39 |
wrnMsg("gig_set_dim_zone(): note ID for argument 1 may not be zero"); |
40 |
return successResult(); |
41 |
} |
42 |
if (!id.isNoteID()) { |
43 |
wrnMsg("gig_set_dim_zone(): argument 1 is not a note ID"); |
44 |
return successResult(); |
45 |
} |
46 |
|
47 |
NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); |
48 |
if (!pNote) { |
49 |
dmsg(2,("gig_set_dim_zone(): no active note with ID %d\n", int(id))); |
50 |
return successResult(); |
51 |
} |
52 |
|
53 |
int note = pNote->cause.Param.Note.Key; |
54 |
|
55 |
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(note); |
56 |
if (!pRegion) { |
57 |
dmsg(2,("gig_set_dim_zone(): no region for key %d\n", note)); |
58 |
return successResult(); |
59 |
} |
60 |
|
61 |
int idx = -1, baseBits = 0; |
62 |
for (int i = 0; i < pRegion->Dimensions; ++i) { |
63 |
if (pRegion->pDimensionDefinitions[i].dimension == dim) { |
64 |
idx = i; |
65 |
break; |
66 |
} |
67 |
baseBits += pRegion->pDimensionDefinitions[i].bits; |
68 |
} |
69 |
if (idx < 0) { |
70 |
dmsg(2,("gig_set_dim_zone(): no such gig dimension %" PRId64 "\n", (int64_t)dim)); |
71 |
return successResult(); // no such dimension found |
72 |
} |
73 |
|
74 |
int bits = pRegion->pDimensionDefinitions[idx].bits; |
75 |
int mask = 0; |
76 |
for (int i = 0; i < bits; ++i) mask |= (1 << (baseBits + i)); |
77 |
|
78 |
pNote->Format.Gig.DimMask |= mask; |
79 |
pNote->Format.Gig.DimBits |= (zone << baseBits) & mask; |
80 |
|
81 |
dmsg(3,("gig_set_dim_zone(): success, mask=%d bits=%d\n", pNote->Format.Gig.DimMask, pNote->Format.Gig.DimBits)); |
82 |
} else if (args->arg(0)->exprType() == INT_ARR_EXPR) { |
83 |
VMIntArrayExpr* ids = args->arg(0)->asIntArray(); |
84 |
for (vmint i = 0; i < ids->arraySize(); ++i) { |
85 |
const ScriptID id = ids->evalIntElement(i); |
86 |
if (!id || !id.isNoteID()) continue; |
87 |
|
88 |
NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); |
89 |
if (!pNote) continue; |
90 |
|
91 |
int note = pNote->cause.Param.Note.Key; |
92 |
|
93 |
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(note); |
94 |
if (!pRegion) continue; |
95 |
|
96 |
int idx = -1, baseBits = 0; |
97 |
for (int i = 0; i < pRegion->Dimensions; ++i) { |
98 |
if (pRegion->pDimensionDefinitions[i].dimension == dim) { |
99 |
idx = i; |
100 |
break; |
101 |
} |
102 |
baseBits += pRegion->pDimensionDefinitions[i].bits; |
103 |
} |
104 |
if (idx < 0) continue; |
105 |
|
106 |
int bits = pRegion->pDimensionDefinitions[idx].bits; |
107 |
int mask = 0; |
108 |
for (int i = 0; i < bits; ++i) mask |= (1 << (baseBits + i)); |
109 |
|
110 |
pNote->Format.Gig.DimMask |= mask; |
111 |
pNote->Format.Gig.DimBits |= (zone << baseBits) & mask; |
112 |
|
113 |
dmsg(3,("gig_set_dim_zone(): success, mask=%d bits=%d\n", pNote->Format.Gig.DimMask, pNote->Format.Gig.DimBits)); |
114 |
} |
115 |
} |
116 |
|
117 |
return successResult(); |
118 |
} |
119 |
|
120 |
///////////////////////////////////////////////////////////////////////// |
121 |
// Function: |
122 |
// same_region(key1, key2) |
123 |
|
124 |
InstrumentScriptVMFunction_same_region::InstrumentScriptVMFunction_same_region(InstrumentScriptVM* parent) |
125 |
: m_vm(parent) |
126 |
{ |
127 |
} |
128 |
|
129 |
VMFnResult* InstrumentScriptVMFunction_same_region::exec(VMFnArgs* args) { |
130 |
EngineChannel* pEngineChannel = |
131 |
static_cast<EngineChannel*>(m_vm->m_event->cause.pEngineChannel); |
132 |
|
133 |
vmint key1 = args->arg(0)->asInt()->evalInt(); |
134 |
vmint key2 = args->arg(1)->asInt()->evalInt(); |
135 |
|
136 |
if (key1 < 0 || key1 > 127) { |
137 |
wrnMsg("same_region(): key number for argument 1 out of range"); |
138 |
return successResult(-1); |
139 |
} |
140 |
if (key2 < 0 || key2 > 127) { |
141 |
wrnMsg("same_region(): key number for argument 2 out of range"); |
142 |
return successResult(-1); |
143 |
} |
144 |
|
145 |
::gig::Region* pRgn1 = pEngineChannel->pInstrument->GetRegion((int)key1); |
146 |
::gig::Region* pRgn2 = pEngineChannel->pInstrument->GetRegion((int)key2); |
147 |
|
148 |
if (!pRgn1 && !pRgn2) |
149 |
return successResult(5); |
150 |
if (pRgn1 == pRgn2) |
151 |
return successResult(1); |
152 |
if (pRgn1 && !pRgn2) |
153 |
return successResult(3); |
154 |
if (!pRgn1 && pRgn2) |
155 |
return successResult(4); |
156 |
if (pRgn1->KeyRange.overlaps(pRgn2->KeyRange)) |
157 |
return successResult(2); |
158 |
return successResult(0); |
159 |
} |
160 |
|
161 |
}} // namespace LinuxSampler::gig |