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 "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(int 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 |
int dim = args->arg(1)->asInt()->evalInt(); |
34 |
int zone = args->arg(2)->asInt()->evalInt(); |
35 |
|
36 |
if (args->arg(0)->exprType() == INT_EXPR) { |
37 |
int id = args->arg(0)->asInt()->evalInt(); |
38 |
if (id < 0) { |
39 |
wrnMsg("gig_set_dim_zone(): argument 1 may not be a negative event ID"); |
40 |
return successResult(); |
41 |
} |
42 |
|
43 |
RTList<Event>::Iterator itEvent = pEngineChannel->pEngine->EventByID(id); |
44 |
if (!itEvent) { |
45 |
dmsg(2,("gig_set_dim_zone(): no active event with ID %d\n", id)); |
46 |
return successResult(); |
47 |
} |
48 |
|
49 |
int note = itEvent->Param.Note.Key; |
50 |
|
51 |
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(note); |
52 |
if (!pRegion) { |
53 |
dmsg(2,("gig_set_dim_zone(): no region for key %d\n", note)); |
54 |
return successResult(); |
55 |
} |
56 |
|
57 |
int idx = -1, baseBits = 0; |
58 |
for (int i = 0; i < pRegion->Dimensions; ++i) { |
59 |
if (pRegion->pDimensionDefinitions[i].dimension == dim) { |
60 |
idx = i; |
61 |
break; |
62 |
} |
63 |
baseBits += pRegion->pDimensionDefinitions[i].bits; |
64 |
} |
65 |
if (idx < 0) { |
66 |
dmsg(2,("gig_set_dim_zone(): no such gig dimension %d\n", dim)); |
67 |
return successResult(); // no such dimension found |
68 |
} |
69 |
|
70 |
int bits = pRegion->pDimensionDefinitions[idx].bits; |
71 |
int mask = 0; |
72 |
for (int i = 0; i < bits; ++i) mask |= (1 << (baseBits + i)); |
73 |
|
74 |
itEvent->Format.Gig.DimMask |= mask; |
75 |
itEvent->Format.Gig.DimBits |= (zone << baseBits) & mask; |
76 |
|
77 |
dmsg(3,("gig_set_dim_zone(): success, mask=%d bits=%d\n", itEvent->Format.Gig.DimMask, itEvent->Format.Gig.DimBits)); |
78 |
} else if (args->arg(0)->exprType() == INT_ARR_EXPR) { |
79 |
VMIntArrayExpr* ids = args->arg(0)->asIntArray(); |
80 |
for (int i = 0; i < ids->arraySize(); ++i) { |
81 |
int id = ids->evalIntElement(i); |
82 |
if (id < 0) continue; |
83 |
|
84 |
RTList<Event>::Iterator itEvent = pEngineChannel->pEngine->EventByID(id); |
85 |
if (!itEvent) continue; |
86 |
|
87 |
int note = itEvent->Param.Note.Key; |
88 |
|
89 |
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(note); |
90 |
if (!pRegion) continue; |
91 |
|
92 |
int idx = -1, baseBits = 0; |
93 |
for (int i = 0; i < pRegion->Dimensions; ++i) { |
94 |
if (pRegion->pDimensionDefinitions[i].dimension == dim) { |
95 |
idx = i; |
96 |
break; |
97 |
} |
98 |
baseBits += pRegion->pDimensionDefinitions[i].bits; |
99 |
} |
100 |
if (idx < 0) continue; |
101 |
|
102 |
int bits = pRegion->pDimensionDefinitions[idx].bits; |
103 |
int mask = 0; |
104 |
for (int i = 0; i < bits; ++i) mask |= (1 << (baseBits + i)); |
105 |
|
106 |
itEvent->Format.Gig.DimMask |= mask; |
107 |
itEvent->Format.Gig.DimBits |= (zone << baseBits) & mask; |
108 |
|
109 |
dmsg(3,("gig_set_dim_zone(): success, mask=%d bits=%d\n", itEvent->Format.Gig.DimMask, itEvent->Format.Gig.DimBits)); |
110 |
} |
111 |
} |
112 |
|
113 |
return successResult(); |
114 |
} |
115 |
|
116 |
}} // namespace LinuxSampler::gig |