-
Notifications
You must be signed in to change notification settings - Fork 2
/
RNBO_MaxPresetAdapter.cpp
166 lines (159 loc) · 4.48 KB
/
RNBO_MaxPresetAdapter.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
#include <RNBO.h>
#include "RNBO_MaxPresetAdapter.h"
#include <ext.h>
#include <atomic>
namespace MaxPresetAdapter {
using namespace RNBO;
//min's max-lib doesn't export _sym_nothing, so we do it ourselves
static t_symbol* _local_sym_nothing = gensym("");
void toDict(const Preset& preset, t_dictionary* presetDict) {
for (auto const& entry : preset) {
t_symbol *key = gensym(entry.first.c_str());
auto type = entry.second.getType();
switch (type) {
case ValueHolder::FLOAT: {
float value = (float)entry.second;
dictionary_appendfloat(presetDict, key, value);
break;
}
case ValueHolder::DOUBLE: {
double value = (double)entry.second;
dictionary_appendfloat(presetDict, key, value);
break;
}
case ValueHolder::UINT32: {
UInt32 value = (UInt32)entry.second;
dictionary_appendfloat(presetDict, key, value);
break;
}
case ValueHolder::UINT64: {
UInt64 value = (UInt64)entry.second;
dictionary_appendfloat(presetDict, key, value);
break;
}
case ValueHolder::STRING: {
const char* str = entry.second;
dictionary_appendstring(presetDict, key, str);
break;
}
case ValueHolder::LIST: {
const list& value = entry.second;
t_atom *av = nullptr;
long ac = 0;
char alloc;
atom_alloc_array(value.length, &ac, &av, &alloc);
if (alloc) {
t_atom *a = av;
for (long i = 0; i < value.length; i++) {
atom_setfloat(a, value[i]);
a++;
}
dictionary_appendatoms(presetDict, key, ac, av);
}
break;
}
case ValueHolder::SUBSTATE: {
t_dictionary* substate = dictionary_new();
const Preset& preset = entry.second;
toDict(preset, substate);
dictionary_appenddictionary(presetDict, key, (t_object *)substate);
break;
}
case ValueHolder::SUBSTATEMAP: {
long ac = 0;
t_atom *av = nullptr;
char alloc;
Index size = entry.second.getSubStateMapSize();
if (size) {
atom_alloc_array(size, &ac, &av, &alloc);
for (Index i = 0; i < size; i++) {
t_dictionary* substate = dictionary_new();
const Preset& preset = entry.second[i];
toDict(preset, substate);
atom_setobj(av + i, substate);
}
dictionary_appendatoms(presetDict, key, ac, av);
}
break;
}
default:
// we do only support numbers, lists and substates
RNBO_ASSERT(false);
}
}
}
void fromDict(t_dictionary* presetDict, Preset& preset) {
long numkeys = 0;
t_symbol** keys;
dictionary_getkeys(presetDict, &numkeys, &keys);
t_atom a;
for (long i = 0; i < numkeys; i++) {
t_symbol* symkey = keys[i];
const char * key = symkey->s_name;
if (dictionary_entryisdictionary(presetDict, symkey)) {
Preset value;
t_dictionary *subDict = nullptr;
dictionary_getdictionary(presetDict, symkey, (t_object **)&subDict);
Preset& substate = preset.getSubState(key);
fromDict(subDict, substate);
}
else if (dictionary_entryisatomarray(presetDict, symkey)) {
long ac = 0;
t_atom *av = nullptr;
dictionary_getatoms(presetDict, symkey, &ac, &av);
if (ac) {
auto type = atom_gettype(av);
if (type == A_FLOAT) {
list value;
for (long i = 0; i < ac; i++) {
value.push(atom_getfloat(av));
av++;
}
preset[key] = value;
}
else if (type == A_OBJ) {
for (long i = 0; i < ac; i++) {
t_dictionary* d = (t_dictionary *)atom_getobj(av);
Preset& subPreset = preset[key][i];
fromDict(d, subPreset);
}
}
}
}
else if (dictionary_getatom(presetDict, symkey, &a) == 0) {
auto t = atom_gettype(&a);
if (t == A_SYM) {
if (auto s = atom_getsym(&a)) {
preset[key] = s->s_name;
}
}
else if (t == A_FLOAT || t == A_LONG) {
number value = atom_getfloat(&a);
preset[key] = value;
}
}
}
}
void getObjectPreset(RNBO::CoreObject& o, t_dictionary *presetDict, bool async) {
ConstPresetPtr returnedPreset;
if (async) {
std::atomic_bool waiting(true);
o.getPreset(
[&returnedPreset, &waiting] (ConstPresetPtr preset) {
returnedPreset = preset;
waiting = false;
});
while (waiting) {
systhread_sleep(10);
}
} else {
returnedPreset = o.getPresetSync();
}
toDict(*returnedPreset, presetDict);
}
void setObjectPreset(RNBO::CoreObject& o, t_dictionary *presetDict) {
std::unique_ptr<Preset> preset = make_unique<Preset>();
fromDict(presetDict, *preset);
o.setPreset(std::move(preset));
}
}