-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.cc
429 lines (349 loc) · 13.8 KB
/
engine.cc
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "engine.h"
std::vector <Plugin *> *Engine::activePlugins = nullptr;
bool Engine::addPlugin(char* library, int pluginIndex, SharedLibrary::PluginType _type) {
IN
processor->bypass = true ;
SharedLibrary * sharedLibrary = new SharedLibrary (library, _type);
sharedLibrary ->setLibraryPath(std::string (libraryPath));
//~ sharedLibrary ->setLibraryPath(std::string ("libs/linux/x86_64"));
sharedLibrary ->lv2_config_path = std::string (assetPath).append ("/lv2");
sharedLibrary->load();
if (sharedLibrary->descriptors.size() == 0) {
LOGE("Unable to load shared library!") ;
processor->bypass = false ;
return false;
}
LOGD("loaded shared library [ok] ... now trying to load plugin\n");
Plugin * plugin = new Plugin (sharedLibrary->descriptors.at(pluginIndex), (long) sampleRate, _type);
plugin->sharedLibrary = sharedLibrary;
if (_type != SharedLibrary::LADSPA) {
plugin -> load () ;
}
if (activePlugins == nullptr) {
LOGD ("first run\n");
activePlugins = new std::vector <Plugin *> () ;
}
activePlugins ->push_back(plugin);
LOGD ("adding plugin to active chain %d\n", activePlugins->size ());
// todo
buildPluginChain();
processor->bypass = false ;
OUT
return true ;
}
bool Engine::openAudio () {
driver = new AudioDriver (processor);
bool val = driver->open ();
if (val) {
sampleRate = driver->get_sample_rate () ;
} else
sampleRate = 48000 ; // sane default
return val ;
}
Engine::Engine () {
IN
#ifdef __linux__
struct utsname name;
if (uname (&name) == -1)
wtf ("cannot get system name!\n") ;
#else
wtf ("[discover] running on something not linux");
wtf ("profile: %s", getenv ("USERPROFILE"));
#endif
# ifdef __linux__
home = std::string (getenv("HOME")).append ("/amprack/recordings");
# else
home = std::string (getenv("USERPROFILE")).append ("/amprack/recordings");
# endif
g_mkdir_with_parents (home.c_str (), 0777);
LOGD ("home dir: %s", home.c_str ());
# ifdef __linux__
std::string _p_ = std::string ("libs/linux/").append (name.machine).append ("/") ;
# else
std::string _p_ = std::string ("libs/win32/") ;
LOGD ("[engine] %d\n", GetCurrentThreadId());
# endif
libraryPath = strdup (_p_.c_str ());
wtf ("trying %s ...\n", libraryPath);
if (! std::filesystem::exists (libraryPath)) {
free(libraryPath);
# ifdef __linux__
std::string _p_ = std::string ("/usr/share/amprack/libs/linux/").append (name.machine).append ("/") ;
# else
std::string _p_ = std::string ("C:\\Program Files\\Amp Rack\\Libs") ;
# endif
libraryPath = strdup (_p_.c_str ());
wtf ("trying %s ...\n", libraryPath);
}
if (std::filesystem::exists ("assets"))
assetPath = std::string ("assets");
else if (std::filesystem::exists ("/usr/share/amprack/assets"))
assetPath = std::string ("/usr/share/amprack/assets");
else {
LOGD ("CANNOT FIND ASSETS!\n");
abort ();
}
if (! std::filesystem::exists (libraryPath)) {
free(libraryPath);
printf ("CANNOT FIND LIBRARIES!\n");
abort () ;
}
LOGD ("[engine] library path: %s\n", libraryPath);
LOGD ("[engine] assets path: %s\n", assetPath.c_str ());
processor = new Processor () ;
openAudio () ;
HERE LOGD ("processor status %d\n", processor->bypass);
//~ processor -> bypass = false ;
ladspaPlugins = new std::vector <std::string> ();
lv2Plugins = new std::vector <std::string> ();
amps = filename_to_json (std::string (assetPath).append ("/amps.json"));
lv2Json = filename_to_json (std::string (assetPath).append ("/lv2_plugins.json"));
ladspaJson = filename_to_json (std::string (assetPath).append ("/all_plugins.json"));
categories = filename_to_json (std::string (assetPath).append ("/plugins.json"));
creators = filename_to_json (std::string (assetPath).append ("/creator.json"));
knobs = filename_to_json (std::string (assetPath).append ("/knobs.json"));
//~ initLilv ();
queueManager = new LockFreeQueueManager ();
queueManager->init (driver -> get_buffer_size ());
fileWriter = new FileWriter ();
queueManager->add_function (fileWriter->disk_write);
processor->lockFreeQueueManager = queueManager ;
HERE LOGD ("processor status %d\n", processor->bypass);
//~ processor -> bypass = false ;
OUT
}
void Engine::buildPluginChain () {
IN
LOGD("building chain for %d plugins", activePlugins->size());
processor->activePlugins = 0;
for (int i = 0 ; i < activePlugins->size () ; i ++) {
Plugin *p = activePlugins->at (i);
if (!p->active)
continue;
processor->inputPorts [processor->activePlugins] = p->inputPort ;
processor->outputPorts [processor->activePlugins] = p->outputPort ;
processor->inputPorts2 [processor->activePlugins] = p->inputPort2 ;
processor->outputPorts2 [processor->activePlugins] = p->outputPort2 ;
if (p->type == SharedLibrary::LADSPA) {
processor->connect_port [processor->activePlugins] = p->descriptor->connect_port ;
processor->run [processor->activePlugins] = p->descriptor->run ;
processor->run_adding [processor->activePlugins] = p->descriptor->run_adding ;
processor->set_run_adding_gain [processor->activePlugins] = p->descriptor->set_run_adding_gain ;
processor->descriptor [processor->activePlugins] = p->descriptor;
} else /*if (p->type == SharedLibrary::LV2) */{
processor->connect_port [processor->activePlugins] = reinterpret_cast<void (*)(
LADSPA_Handle, unsigned long, LADSPA_Data *)>(p->lv2Descriptor->connect_port);
processor->run [processor->activePlugins] = reinterpret_cast<void (*)(
LADSPA_Handle, unsigned long)>(p->lv2Descriptor->run);
processor->descriptor [processor->activePlugins] = reinterpret_cast<const LADSPA_Descriptor *>(p->lv2Descriptor);
}
processor->run_adding_gain [processor->activePlugins] = p->run_adding_gain ;
processor->handle [processor->activePlugins] = p->handle ;
processor->activePlugins ++ ;
}
OUT
}
bool Engine::addPluginByName (char * pluginName) {
std::string stub = "";
if (lv2Map .contains (pluginName)) {
stub = lv2Map [pluginName].dump();
}
# ifdef __linux__
LILV_FOREACH (plugins, i, plugins) {
const LilvPlugin* p = (LilvPlugin* )lilv_plugins_get(plugins, i);
const char * name = lilv_node_as_string (lilv_plugin_get_name (p));
const char * uri = lilv_node_as_string (lilv_plugin_get_uri (p));
if (strcmp (pluginName, name) == 0) {
printf("[LV2] %s\n", name);
return addPlugin ((char *)uri, 0, SharedLibrary::PluginType::LILV);
}
std::string s (uri);
int x = s.find ("#");
if (x == -1)
x = s.find_last_of ("/");
s = s.substr (x + 1, s.size ());
if (s == stub) {
LOGD ("found mapped plugin %s -> %s\n", pluginName, stub);
return addPlugin ((char *)uri, 0, SharedLibrary::PluginType::LILV);
}
}
# endif
return false ;
}
json Engine::getPreset () {
IN
if (activePlugins == nullptr || activePlugins->size () == 0) {
LOGD ("no active plugins\n");
OUT
return json {};
}
json preset = {} ;
json plugins = {} ;
for (int i = 0 ; i < activePlugins->size () ; i ++) {
Plugin * plugin = activePlugins->at (i);
json p = {};
p ["name"] = plugin->lv2_name;
if (! plugin->loadedFileName.empty ()) {
p ["filename"] = plugin -> loadedFileName ;
p ["filetype"] = plugin -> loadedFileType ;
}
std::string controls = std::string () ;
for (int x = 0 ; x < plugin->pluginControls.size () ; x ++) {
controls.append (std::to_string (*plugin->pluginControls.at (x)->def));
if (x < (plugin->pluginControls.size () + 2))
controls.append (";");
}
p ["controls"] = controls ;
plugins [std::to_string (i)] = p ;
}
preset ["controls"] = plugins;
OUT
return preset ;
}
bool Engine::savePreset (std::string filename, std::string description) {
IN
LOGD ("[save preset] to file %s\n", filename.c_str ());
json preset = getPreset () ;
if (preset == NULL)
return false ;
preset ["desc"] = description ;
preset ["name"] = std::string (filename).substr (filename.find_last_of ("/") + 1, filename.size ()) ;
json_to_filename (preset, filename);
LOGD ("[preset save] %s\n", preset.dump ().c_str ());
OUT
return true ;
}
bool Engine::load_preset (json j) {
IN
auto plugins = j ["controls"];
for (auto p: plugins) {
}
OUT
return true;
}
void Engine::set_plugin_audio_file (int index, char * filename) {
IN
# ifdef __linux__
SoundFile * sf = snd_read (filename) ;
if (sf == NULL) {
LOGD ("file read failed!\n");
return ;
} else {
LOGD ("read %d bytes\n", * sf -> len);
}
LOGD ("file read ok! set plugin: %d [%d bytes]\n", index, * sf -> len);
# endif
//~ for (int i = 0 ; i < * sf -> len; i ++)
//~ LOGD ("[frame: %f]\n", sf -> data [i]);
//~ activePlugins->at (index)->lv2Descriptor->connect_port(
//~ activePlugins->at (index)->handle, 99, sf->len);
//~ activePlugins->at (index)->lv2Descriptor->connect_port(
//~ activePlugins->at (index)->handle, 100, sf->data);
//~ *sf -> len = * sf -> len / 2 ;
processor->bypass = true ;
# ifdef __linux__
activePlugins->at (index)->setBuffer (sf ->data, * sf -> len);
delete (sf) ;
# endif
processor->bypass = false ;
activePlugins->at (index)->loadedFileName = std::string (filename) ;
activePlugins->at (index)->loadedFileType = 0 ;
std::string path = std::string (filename) ;
# ifdef __linux__
std::string dir = std::string (getenv ("HOME")).append ("/amprack/models/").append (activePlugins->at (index)->lv2_name).append ("/");
# else
std::string dir = std::string (getenv ("USERPROFILE")).append ("/amprack/models/").append (activePlugins->at (index)->lv2_name).append ("/");
# endif
g_mkdir_with_parents (dir.c_str (), 0777) ;
copy_file (activePlugins->at (index)->loadedFileName, dir.append (path.substr(path.find_last_of("/") + 1)));
OUT
}
void Engine::set_plugin_file (int index, char * filename) {
IN
std::ifstream fJson(filename);
std::stringstream buffer;
buffer << fJson.rdbuf();
int size = buffer.str ().size () ;
processor->bypass = true ;
if (activePlugins->at (index)->lv2Descriptor != nullptr) {
wtf ("lv2 plugin ...\n");
//~ activePlugins->at (index)->setBuffer ((float *) buffer.str ().c_str (), size);
activePlugins->at (index)->lv2Descriptor->connect_port(
activePlugins->at (index)->handle, 99, & size);
activePlugins->at (index)->lv2Descriptor->connect_port(
activePlugins->at (index)->handle, 100, (void *)buffer.str().c_str ());
processor->bypass = false ;
} else {
wtf ("ladspa plugin ...\n");
activePlugins->at (index)->descriptor->connect_port(
activePlugins->at (index)->handle, 99, (float *)& size);
activePlugins->at (index)->descriptor->connect_port(
activePlugins->at (index)->handle, 100, (float *)buffer.str().c_str ());
processor->bypass = false ;
}
activePlugins->at (index)->loadedFileName = std::string (filename) ;
std::string path = std::string (filename) ;
activePlugins->at (index)->loadedFileType = 1 ;
# ifdef __linux__
std::string dir = std::string (getenv ("HOME")).append ("/amprack/models/").append (activePlugins->at (index)->lv2_name).append ("/");
# else
std::string dir = std::string (getenv ("USERPROFILE")).append ("/amprack/models/").append (activePlugins->at (index)->lv2_name).append ("/");
# endif
g_mkdir_with_parents (dir.c_str (), 0777) ;
copy_file (activePlugins->at (index)->loadedFileName, dir.append (path.substr(path.find_last_of("/") + 1)));
//~ printf ("%s\n", buffer.str().c_str ());
OUT
}
int Engine :: moveActivePluginDown (int _p) {
IN
if (_p == activePlugins->size()) {
OUT
return _p ;
}
auto it = activePlugins->begin() + _p;
std::rotate(it, it + 1, it + 2);
buildPluginChain();
OUT
return _p + 1 ;
}
int Engine :: moveActivePluginUp (int _p) {
IN
if (_p == 0) {
OUT
return _p ;
}
processor->bypass = true ;
LOGD ("[engine] move %d up\n", _p) ;
auto it = activePlugins->begin() + _p;
std::rotate(it - 1, it, it + 1);
buildPluginChain();
processor->bypass = false ;
OUT
return _p - 1 ;
}
void Engine::print () {
LOGD ("-------| rack |---------\n");
for (int i = 0 ; i < activePlugins->size(); i ++) {
LOGD ("[%d] %s\n", i, activePlugins->at (i)->lv2_name.c_str ());
}
LOGD ("------------------------\n");
}
void Engine::startRecording () {
IN
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "/%d-%m-%Y %H-%M-%S");
auto str = std::string (home).append (oss.str());
fileWriter->setFileName (str);
fileWriter->setSampleRate (driver->get_sample_rate ());
fileWriter->startRecording ();
processor->recording = true ;
OUT
}
void Engine::stopRecording () {
IN
processor->recording = false ;
fileWriter->stopRecording ();
OUT
}