-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMapper.cpp
254 lines (205 loc) · 6.69 KB
/
Mapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
MapperUGen - UGen for using libmapper with SuperCollider server
Copyright (C) 2020 Mathias Bredholt
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <mapper/mapper.h>
#include <atomic>
#include <functional>
#include <queue>
#include <thread>
#include "SC_PlugIn.h"
static InterfaceTable* ft;
static mpr_dev dev;
static std::atomic<bool> isReady;
static std::thread* libmapperThreadHandle;
void libmapperThread() {
// TODO(mb): Add option for specifying device name with Mapper.enable(name)
dev = mpr_dev_new("SuperCollider", 0);
while (!mpr_dev_get_is_ready(dev)) {
mpr_dev_poll(dev, 10);
}
isReady = true;
Print("Mapper: libmapper ready!\n");
while (dev) {
mpr_dev_poll(dev, 1);
}
}
struct MapperUnit : public Unit {
int signalNameSize;
char* signalName;
mpr_sig sig = nullptr;
float sigMin = 0;
float sigMax = 1;
float val = 0;
};
struct MapIn : public MapperUnit {};
struct MapOut : public MapperUnit {};
struct MapperEnabler : public Unit {};
struct MapperDisabler : public Unit {};
// Empty DSP function
static void Unit_next_nop(Unit* unit, int inNumSamples) {}
static void MapIn_Ctor(MapIn* unit);
static void MapIn_Dtor(MapIn* unit);
static void MapIn_next(MapIn* unit, int inNumSamples);
static void MapOut_Ctor(MapOut* unit);
static void MapOut_Dtor(MapOut* unit);
static void MapOut_next(MapOut* unit, int inNumSamples);
static void MapperEnabler_Ctor(MapperEnabler* unit);
static void MapperDisabler_Ctor(MapperDisabler* unit);
static void MapperUnit_bindToSignal(MapperUnit* unit, mpr_dir direction) {
// Search for existing output signal with same name
mpr_list sigs = mpr_dev_get_sigs(dev, direction);
sigs = mpr_list_filter(sigs, MPR_PROP_NAME, 0, 1, MPR_STR, unit->signalName,
MPR_OP_EQ);
if (sigs) {
// Signal exists, bind unit to signal
unit->sig = *sigs;
// Update signal metadata if signal range has changed
mpr_obj_set_prop(unit->sig, MPR_PROP_MIN, 0, 1, MPR_FLT, &unit->sigMin, 1);
mpr_obj_set_prop(unit->sig, MPR_PROP_MAX, 0, 1, MPR_FLT, &unit->sigMax, 1);
mpr_obj_push(unit->sig);
// Update maps containing signal
// mpr_list maps = mpr_sig_get_maps(unit->sig, MPR_DIR_ANY);
// while (maps) {
// // char expr[128];
// if (direction == MPR_DIR_IN) {
// mpr_obj_set_prop(*maps, MPR_PROP_EXTRA, "var@dMin", 1, MPR_FLT,
// &unit->sigMin, 1);
// mpr_obj_set_prop(*maps, MPR_PROP_EXTRA, "var@dMax", 1, MPR_FLT,
// &unit->sigMax, 1);
// } else {
// mpr_obj_set_prop(*maps, MPR_PROP_EXTRA, "var@sMin", 1, MPR_FLT,
// &unit->sigMin, 1);
// mpr_obj_set_prop(*maps, MPR_PROP_EXTRA, "var@sMax", 1, MPR_FLT,
// &unit->sigMax, 1);
// }
// mpr_obj_push(*maps);
// maps = mpr_list_get_next(maps);
// }
} else {
// Signal doesn't exist, create new
Print("Mapper: Creating signal '%s'\n", unit->signalName);
unit->sig = mpr_sig_new(dev, direction, unit->signalName, 1, MPR_FLT, 0,
&unit->sigMin, &unit->sigMax, 0, 0, 0);
}
}
// MapIn
void MapIn_Ctor(MapIn* unit) {
unit->val = 0;
SETCALC(MapIn_next);
unit->sigMin = IN0(0);
unit->sigMax = IN0(1);
// Set signal name
unit->signalNameSize = IN0(2);
const int signalNameAllocSize = (unit->signalNameSize + 1) * sizeof(char);
void* chunk = RTAlloc(unit->mWorld, signalNameAllocSize);
if (!chunk) {
Print("MapIn: RT memory allocation failed\n");
return;
}
unit->signalName = reinterpret_cast<char*>(chunk);
for (int i = 0; i < unit->signalNameSize; i++) {
unit->signalName[i] = static_cast<char>(IN0(3 + i));
}
unit->signalName[unit->signalNameSize] = 0;
// Bind to signal
if (dev) {
MapperUnit_bindToSignal(unit, MPR_DIR_IN);
} else {
Print("MapIn: libmapper not enabled\n");
}
MapIn_next(unit, 1);
}
void MapIn_Dtor(MapIn* unit) { RTFree(unit->mWorld, unit->signalName); }
void MapIn_next(MapIn* unit, int inNumSamples) {
float* out = OUT(0);
// Signal is not created yet
if (!unit->sig) {
*out = 0.f;
}
// Get signal value pointer
const float* val =
static_cast<const float*>(mpr_sig_get_value(unit->sig, 0, 0));
// Signal doesn't have a value yet
if (!val) {
*out = 0.f;
return;
}
// Set out value to signal value
*out = *val;
}
// MapOut
void MapOut_Ctor(MapOut* unit) {
unit->sigMin = IN0(1);
unit->sigMax = IN0(2);
// Set signal name
unit->signalNameSize = IN0(3);
const int signalNameAllocSize = (unit->signalNameSize + 1) * sizeof(char);
void* chunk = RTAlloc(unit->mWorld, signalNameAllocSize);
if (!chunk) {
Print("MapOut: RT memory allocation failed\n");
SETCALC(Unit_next_nop);
return;
}
unit->signalName = reinterpret_cast<char*>(chunk);
for (int i = 0; i < unit->signalNameSize; i++) {
unit->signalName[i] = static_cast<char>(IN0(4 + i));
}
unit->signalName[unit->signalNameSize] = 0;
if (isReady) {
MapperUnit_bindToSignal(unit, MPR_DIR_OUT);
} else {
Print("MapOut: libmapper not enabled\n");
SETCALC(Unit_next_nop);
}
SETCALC(MapOut_next);
MapOut_next(unit, 1);
}
void MapOut_Dtor(MapOut* unit) { RTFree(unit->mWorld, unit->signalName); }
void MapOut_next(MapOut* unit, int inNumSamples) {
// Signal is not ready yet
if (!unit->sig) {
return;
}
// Set output signal value
float val = IN0(0);
mpr_sig_set_value(unit->sig, 0, 1, MPR_FLT, &val);
}
// MapperEnabler
void MapperEnabler_Ctor(MapperEnabler* unit) {
if (!dev) {
// dev = mpr_dev_new("SuperCollider", 0);
libmapperThreadHandle = new std::thread(libmapperThread);
} else {
Print("Mapper: libmapper already enabled.\n");
}
SETCALC(Unit_next_nop);
}
// MapperDisabler
void MapperDisabler_Ctor(MapperDisabler* unit) {
if (isReady) {
mpr_dev_free(dev);
dev = nullptr;
libmapperThreadHandle->join();
}
SETCALC(Unit_next_nop);
}
PluginLoad(MapperUGens) {
ft = inTable;
DefineDtorUnit(MapIn);
DefineDtorUnit(MapOut);
DefineSimpleUnit(MapperEnabler);
DefineSimpleUnit(MapperDisabler);
}