LMMS
Loading...
Searching...
No Matches
PresetExtractor.cpp
Go to the documentation of this file.
1/*
2 ZynAddSubFX - a software synthesizer
3
4 PresetExtractor.cpp - Extract Presets from realtime data
5 Copyright (C) 2015 Mark McCurry
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11*/
12
14
15#include "../Misc/Master.h"
16#include "../Misc/Util.h"
17#include "../Misc/Allocator.h"
19#include "../Synth/OscilGen.h"
20#include "../Synth/Resonance.h"
24#include "../Params/LFOParams.h"
26#include "../Params/Presets.h"
29#include "../Misc/MiddleWare.h"
30#include "PresetExtractor.h"
31#include <rtosc/ports.h>
32#include <rtosc/port-sugar.h>
33#include <string>
34
35namespace zyncarla {
36
37using std::string;
38static void dummy(const char *, rtosc::RtData&) {}
39
41{
42 {"scan-for-presets:", 0, 0,
43 [](const char *, rtosc::RtData &d) {
44 MiddleWare &mw = *(MiddleWare*)d.obj;
45 assert(d.obj);
47 auto &pre = mw.getPresetsStore().presets;
48 d.reply(d.loc, "i", pre.size());
49 for(unsigned i=0; i<pre.size();++i)
50 d.reply(d.loc, "isss", i,
51 pre[i].file.c_str(),
52 pre[i].name.c_str(),
53 pre[i].type.c_str());
54
55 }},
56 {"copy:s:ss:si:ssi", 0, 0,
57 [](const char *msg, rtosc::RtData &d) {
58 MiddleWare &mw = *(MiddleWare*)d.obj;
59 assert(d.obj);
60 std::string args = rtosc_argument_string(msg);
61 d.reply(d.loc, "s", "clipboard copy...");
62 printf("\nClipboard Copy...\n");
63 if(args == "s")
64 presetCopy(mw, rtosc_argument(msg, 0).s, "");
65 else if(args == "ss")
67 rtosc_argument(msg, 1).s);
68 else if(args == "si")
70 rtosc_argument(msg, 1).i, "");
71 else if(args == "ssi")
74 else
75 assert(false && "bad arguments");
76 }},
77 {"paste:s:ss:si:ssi", 0, 0,
78 [](const char *msg, rtosc::RtData &d) {
79 MiddleWare &mw = *(MiddleWare*)d.obj;
80 assert(d.obj);
81 std::string args = rtosc_argument_string(msg);
82 d.reply(d.loc, "s", "clipboard paste...");
83 if(args == "s")
84 presetPaste(mw, rtosc_argument(msg, 0).s, "");
85 else if(args == "ss")
87 rtosc_argument(msg, 1).s);
88 else if(args == "si")
90 rtosc_argument(msg, 1).i, "");
91 else if(args == "ssi")
94 else
95 assert(false && "bad arguments");
96 }},
97 {"clipboard-type:", 0, 0,
98 [](const char *, rtosc::RtData &d) {
99 const MiddleWare &mw = *(MiddleWare*)d.obj;
100 assert(d.obj);
101 d.reply(d.loc, "s", mw.getPresetsStore().clipboard.type.c_str());
102 }},
103 {"delete:s", 0, 0,
104 [](const char *msg, rtosc::RtData &d) {
105 MiddleWare &mw = *(MiddleWare*)d.obj;
106 assert(d.obj);
108 }},
109
110};
111
112
114{
115 {"scan-for-presets:", rDoc("Scan For Presets"), 0, dummy},
116 {"copy:s:ss:si:ssi", rDoc("Copy (s)URL to (s) Name/Clipboard from subfield (i)"), 0, dummy},
117 {"paste:s:ss:si:ssi", rDoc("Paste (s) URL to (s) File-Name/Clipboard from subfield (i)"), 0, dummy},
118 {"clipboard-type:", rDoc("Type Stored In Clipboard"), 0, dummy},
119 {"delete:s", rDoc("Delete the given preset file"), 0, dummy},
120};
121
122//Relevant types to keep in mind
123//Effects/EffectMgr.cpp: setpresettype("Peffect");
124//Params/ADnoteParameters.cpp: setpresettype("Padsynth");
125//Params/EnvelopeParams.cpp: //setpresettype("Penvamplitude");
126//Params/EnvelopeParams.cpp: //setpresettype("Penvamplitude");
127//Params/EnvelopeParams.cpp: //setpresettype("Penvfrequency");
128//Params/EnvelopeParams.cpp: //setpresettype("Penvfilter");
129//Params/EnvelopeParams.cpp: //setpresettype("Penvbandwidth");
130//Params/FilterParams.cpp: //setpresettype("Pfilter");
131//Params/LFOParams.cpp: // setpresettype("Plfofrequency");
132//Params/LFOParams.cpp: // setpresettype("Plfoamplitude");
133//Params/LFOParams.cpp: // setpresettype("Plfofilter");
134//Params/PADnoteParameters.cpp: setpresettype("Ppadsynth");
135//Params/SUBnoteParameters.cpp: setpresettype("Psubsynth");
136//Synth/OscilGen.cpp: setpresettype("Poscilgen");
137//Synth/Resonance.cpp: setpresettype("Presonance");
138
139
140/*****************************************************************************
141 * Implementation Methods *
142 *****************************************************************************/
144{
145 public:
146 Capture(void *obj_)
147 {
148 matches = 0;
149 memset(locbuf, 0, sizeof(locbuf));
150 memset(msgbuf, 0, sizeof(msgbuf));
151 loc = locbuf;
152 loc_size = sizeof(locbuf);
153 obj = obj_;
154 }
155
156 virtual void reply(const char *path, const char *args, ...)
157 {
158 //printf("reply(%p)(%s)(%s)...\n", msgbuf, path, args);
159 //printf("size is %d\n", sizeof(msgbuf));
160 va_list va;
161 va_start(va,args);
162 char *buffer = msgbuf;
163 rtosc_vmessage(buffer,sizeof(msgbuf),path,args,va);
164 va_end(va);
165 }
166 char msgbuf[1024];
167 char locbuf[1024];
168};
169
170template <class T>
171T capture(Master *m, std::string url);
172
173template <>
174std::string capture(Master *m, std::string url)
175{
176 Capture c(m);
177 char query[1024];
178 rtosc_message(query, 1024, url.c_str(), "");
179 Master::ports.dispatch(query+1,c);
180 if(rtosc_message_length(c.msgbuf, sizeof(c.msgbuf))) {
181 if(rtosc_type(c.msgbuf, 0) == 's')
182 return rtosc_argument(c.msgbuf,0).s;
183 }
184
185 return "";
186}
187
188template <>
189void *capture(Master *m, std::string url)
190{
191 Capture c(m);
192 char query[1024];
193 rtosc_message(query, 1024, url.c_str(), "");
194 Master::ports.dispatch(query+1,c);
195 if(rtosc_message_length(c.msgbuf, sizeof(c.msgbuf))) {
196 if(rtosc_type(c.msgbuf, 0) == 'b' &&
197 rtosc_argument(c.msgbuf, 0).b.len == sizeof(void*))
198 return *(void**)rtosc_argument(c.msgbuf,0).b.data;
199 }
200
201 return NULL;
202}
203
204template<class T>
205std::string doCopy(MiddleWare &mw, string url, string name)
206{
207 XMLwrapper xml;
208 mw.doReadOnlyOp([&xml, url, name, &mw](){
209 Master *m = mw.spawnMaster();
210 //Get the pointer
211 //printf("capture at <%s>\n", (url+"self").c_str());
212 T *t = (T*)capture<void*>(m, url+"self");
213 assert(t);
214 //Extract Via mxml
215 //t->add2XML(&xml);
216 t->copy(mw.getPresetsStore(), name.empty()? NULL:name.c_str());
217 });
218
219 return "";//xml.getXMLdata();
220}
221
222template<class T, typename... Ts>
223void doPaste(MiddleWare &mw, string url, string type, XMLwrapper &xml, Ts&&... args)
224{
225 //Generate a new object
226 T *t = new T(std::forward<Ts>(args)...);
227
228 //Old workaround for LFO parameters
229 if(strstr(type.c_str(), "Plfo"))
230 type = "Plfo";
231
232 if(xml.enterbranch(type) == 0)
233 return;
234
235 t->getfromXML(xml);
236
237 //Send the pointer
238 string path = url+"paste";
239 char buffer[1024];
240 rtosc_message(buffer, 1024, path.c_str(), "b", sizeof(void*), &t);
241 if(!Master::ports.apropos(path.c_str()))
242 fprintf(stderr, "Warning: Missing Paste URL: '%s'\n", path.c_str());
243 //printf("Sending info to '%s'\n", buffer);
244 mw.transmitMsg(buffer);
245
246 //Let the pointer be reclaimed later
247}
248
249template<class T>
250std::string doArrayCopy(MiddleWare &mw, int field, string url, string name)
251{
252 XMLwrapper xml;
253 //printf("Getting info from '%s'<%d>\n", url.c_str(), field);
254 mw.doReadOnlyOp([&xml, url, field, name, &mw](){
255 Master *m = mw.spawnMaster();
256 //Get the pointer
257 T *t = (T*)capture<void*>(m, url+"self");
258 //Extract Via mxml
259 t->copy(mw.getPresetsStore(), field, name.empty()?NULL:name.c_str());
260 });
261
262 return "";//xml.getXMLdata();
263}
264
265template<class T, typename... Ts>
266void doArrayPaste(MiddleWare &mw, int field, string url, string type,
267 XMLwrapper &xml, Ts&&... args)
268{
269 //Generate a new object
270 T *t = new T(std::forward<Ts>(args)...);
271
272 if(xml.enterbranch(type+"n") == 0) {
273 delete t;
274 return;
275 }
276 t->defaults(field);
277 t->getfromXMLsection(xml, field);
278 xml.exitbranch();
279
280 //Send the pointer
281 string path = url+"paste-array";
282 char buffer[1024];
283 rtosc_message(buffer, 1024, path.c_str(), "bi", sizeof(void*), &t, field);
284 if(!Master::ports.apropos(path.c_str()))
285 fprintf(stderr, "Warning: Missing Paste URL: '%s'\n", path.c_str());
286 //printf("Sending info to '%s'<%d>\n", buffer, field);
287 mw.transmitMsg(buffer);
288
289 //Let the pointer be reclaimed later
290}
291
292/*
293 * Dispatch to class specific operators
294 *
295 * Oscilgen and PADnoteParameters have mixed RT/non-RT parameters and require
296 * extra handling.
297 * See MiddleWare.cpp for these specifics
298 */
299void doClassPaste(std::string type, std::string type_, MiddleWare &mw, string url, XMLwrapper &data)
300{
301 //printf("Class Paste\n");
302 if(type == "EnvelopeParams")
303 doPaste<EnvelopeParams>(mw, url, type_, data);
304 else if(type == "LFOParams")
305 doPaste<LFOParams>(mw, url, type_, data);
306 else if(type == "FilterParams")
307 doPaste<FilterParams>(mw, url, type_, data);
308 else if(type == "ADnoteParameters")
309 doPaste<ADnoteParameters>(mw, url, type_, data, mw.getSynth(), (FFTwrapper*)NULL);
310 else if(type == "PADnoteParameters")
311 doPaste<PADnoteParameters>(mw, url, type_, data, mw.getSynth(), (FFTwrapper*)NULL);
312 else if(type == "SUBnoteParameters")
313 doPaste<SUBnoteParameters>(mw, url, type_, data);
314 else if(type == "OscilGen")
315 doPaste<OscilGen>(mw, url, type_, data, mw.getSynth(), (FFTwrapper*)NULL, (Resonance*)NULL);
316 else if(type == "Resonance")
317 doPaste<Resonance>(mw, url, type_, data);
318 else if(type == "EffectMgr")
319 doPaste<EffectMgr>(mw, url, type_, data, DummyAlloc, mw.getSynth(), false);
320 else {
321 fprintf(stderr, "Warning: Unknown type<%s> from url<%s>\n", type.c_str(), url.c_str());
322 }
323}
324
325std::string doClassCopy(std::string type, MiddleWare &mw, string url, string name)
326{
327 //printf("doClassCopy(%p)\n", mw.spawnMaster()->uToB);
328 if(type == "EnvelopeParams")
329 return doCopy<EnvelopeParams>(mw, url, name);
330 else if(type == "LFOParams")
331 return doCopy<LFOParams>(mw, url, name);
332 else if(type == "FilterParams")
333 return doCopy<FilterParams>(mw, url, name);
334 else if(type == "ADnoteParameters")
335 return doCopy<ADnoteParameters>(mw, url, name);
336 else if(type == "PADnoteParameters")
337 return doCopy<PADnoteParameters>(mw, url, name);
338 else if(type == "SUBnoteParameters")
339 return doCopy<SUBnoteParameters>(mw, url, name);
340 else if(type == "OscilGen")
341 return doCopy<OscilGen>(mw, url, name);
342 else if(type == "Resonance")
343 return doCopy<Resonance>(mw, url, name);
344 else if(type == "EffectMgr")
345 doCopy<EffectMgr>(mw, url, name);
346 return "UNDEF";
347}
348
349void doClassArrayPaste(std::string type, std::string type_, int field, MiddleWare &mw, string url,
351{
352 if(type == "FilterParams")
353 doArrayPaste<FilterParams>(mw, field, url, type_, data);
354 else if(type == "ADnoteParameters")
355 doArrayPaste<ADnoteParameters>(mw, field, url, type_, data, mw.getSynth(), (FFTwrapper*)NULL);
356}
357
358std::string doClassArrayCopy(std::string type, int field, MiddleWare &mw, string url, string name)
359{
360 if(type == "FilterParams")
361 return doArrayCopy<FilterParams>(mw, field, url, name);
362 else if(type == "ADnoteParameters")
363 return doArrayCopy<ADnoteParameters>(mw, field, url, name);
364 return "UNDEF";
365}
366
367//This is an abuse of the readonly op, but one that might look reasonable from a
368//user perspective...
369std::string getUrlPresetType(std::string url, MiddleWare &mw)
370{
371 std::string result;
372 mw.doReadOnlyOp([url, &result, &mw](){
373 Master *m = mw.spawnMaster();
374 //Get the pointer
375 result = capture<std::string>(m, url+"preset-type");
376 });
377 //printf("preset type = %s\n", result.c_str());
378 return result;
379}
380
381std::string getUrlType(std::string url)
382{
383 assert(!url.empty());
384 //printf("Searching for '%s'\n", (url+"self").c_str());
385 auto self = Master::ports.apropos((url+"self").c_str());
386 if(!self)
387 fprintf(stderr, "Warning: URL Metadata Not Found For '%s'\n", url.c_str());
388
389 if(self)
390 return self->meta()["class"];
391 else
392 return "";
393}
394
395
396/*****************************************************************************
397 * API Stubs *
398 *****************************************************************************/
399
400#if 0
401Clipboard clipboardCopy(MiddleWare &mw, string url)
402{
403 //Identify The Self Type of the Object
404 string type = getUrlType(url);
405 printf("Copying a '%s' object", type.c_str());
406
407 //Copy The Object
408 string data = doClassCopy(type, mw, url);
409 printf("Object Information '%s'\n", data.c_str());
410
411 return {type, data};
412}
413
414void clipBoardPaste(const char *url, Clipboard clip)
415{
416 (void) url;
417 (void) clip;
418}
419#endif
420
421void presetCopy(MiddleWare &mw, std::string url, std::string name)
422{
423 (void) name;
424 doClassCopy(getUrlType(url), mw, url, name);
425 //printf("PresetCopy()\n");
426}
427void presetPaste(MiddleWare &mw, std::string url, std::string name)
428{
429 (void) name;
430 //printf("PresetPaste()\n");
431 string data = "";
432 XMLwrapper xml;
433 if(name.empty()) {
435 if(data.length() < 20)
436 return;
437 if(!xml.putXMLdata(data.c_str()))
438 return;
439 } else {
440 if(xml.loadXMLfile(name))
441 return;
442 }
443
444 doClassPaste(getUrlType(url), getUrlPresetType(url, mw), mw, url, xml);
445}
446void presetCopyArray(MiddleWare &mw, std::string url, int field, std::string name)
447{
448 (void) name;
449 //printf("PresetArrayCopy()\n");
450 doClassArrayCopy(getUrlType(url), field, mw, url, name);
451}
452void presetPasteArray(MiddleWare &mw, std::string url, int field, std::string name)
453{
454 (void) name;
455 //printf("PresetArrayPaste()\n");
456 string data = "";
457 XMLwrapper xml;
458 if(name.empty()) {
460 if(data.length() < 20)
461 return;
462 if(!xml.putXMLdata(data.c_str()))
463 return;
464 } else {
465 if(xml.loadXMLfile(name))
466 return;
467 }
468 //printf("Performing Paste...\n");
469 doClassArrayPaste(getUrlType(url), getUrlPresetType(url, mw), field, mw, url, xml);
470}
471#if 0
472void presetPaste(std::string url, int)
473{
474 printf("PresetPaste()\n");
475 doClassPaste(getUrlType(url), *middlewarepointer, url, presetsstore.clipboard.data);
476}
477#endif
479{
480 printf("PresetDelete()<UNIMPLEMENTED>\n");
481}
483{
484 printf("PresetRescan()<UNIMPLEMENTED>\n");
485}
487{
488 printf("PresetClipboardType()<UNIMPLEMENTED>\n");
489 return "dummy";
490}
492{
493 printf("PresetCheckClipboardType()<UNIMPLEMENTED>\n");
494 return true;
495}
496
497}
#define doPaste(x)
Definition SUBnoteParameters.cpp:466
#define NULL
Definition CarlaBridgeFormat.cpp:30
assert(0)
PresetsStore presetsstore
Definition PresetsStore.cpp:35
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
Definition PresetExtractor.cpp:144
Capture(void *obj_)
Definition PresetExtractor.cpp:146
char locbuf[1024]
Definition PresetExtractor.cpp:167
char msgbuf[1024]
Definition PresetExtractor.cpp:166
virtual void reply(const char *path, const char *args,...)
Definition PresetExtractor.cpp:156
Definition FFTwrapper.h:24
Definition Master.h:44
static const rtosc::Ports & ports
Definition Master.h:172
Definition MiddleWare.h:27
void doReadOnlyOp(std::function< void()>)
Definition MiddleWare.cpp:2039
const PresetsStore & getPresetsStore() const
Definition MiddleWare.cpp:2132
void transmitMsg(const char *)
Definition MiddleWare.cpp:2056
const SYNTH_T & getSynth(void) const
Definition MiddleWare.cpp:2119
class Master * spawnMaster(void)
Definition MiddleWare.cpp:1965
struct zyncarla::PresetsStore::@177034047276154346137141247104333070005043030335 clipboard
void deletepreset(unsigned int npreset)
Definition PresetsStore.cpp:165
std::string type
Definition PresetsStore.h:53
void scanforpresets()
Definition PresetsStore.cpp:79
std::vector< presetstruct > presets
Definition PresetsStore.h:47
std::string data
Definition PresetsStore.h:52
Definition Resonance.h:27
Definition XMLwrapper.h:47
bool putXMLdata(const char *xmldata)
Definition XMLwrapper.cpp:380
void exitbranch()
Definition XMLwrapper.cpp:441
int loadXMLfile(const std::string &filename)
Definition XMLwrapper.cpp:312
int enterbranch(const std::string &name)
unsigned * m
Definition inflate.c:1559
struct huft * t
Definition inflate.c:943
unsigned d
Definition inflate.c:940
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
static const char * name
Definition pugl.h:1582
JSAMPIMAGE data
Definition jpeglib.h:945
const char * msg
Definition missing_descriptor.c:20
Definition zynaddsubfx-src.cpp:569
void doArrayPaste(MiddleWare &mw, int field, string url, string type, XMLwrapper &xml, Ts &&... args)
Definition PresetExtractor.cpp:266
void presetRescan()
Definition PresetExtractor.cpp:482
void doClassArrayPaste(std::string type, std::string type_, int field, MiddleWare &mw, string url, XMLwrapper &data)
Definition PresetExtractor.cpp:349
void * data(next_t *n)
Definition Allocator.cpp:32
static void dummy(const char *, rtosc::RtData &)
Definition PresetExtractor.cpp:38
DummyAllocator DummyAlloc
Definition Allocator.cpp:23
std::string getUrlPresetType(std::string url, MiddleWare &mw)
Definition PresetExtractor.cpp:369
void presetPaste(MiddleWare &mw, std::string url, std::string name)
Definition PresetExtractor.cpp:427
bool presetCheckClipboardType()
Definition PresetExtractor.cpp:491
const rtosc::Ports preset_ports
Definition PresetExtractor.cpp:114
std::string getUrlType(std::string url)
Definition PresetExtractor.cpp:381
std::string presetClipboardType()
Definition PresetExtractor.cpp:486
void presetCopy(MiddleWare &mw, std::string url, std::string name)
Definition PresetExtractor.cpp:421
T capture(Master *m, std::string url)
std::string doClassCopy(std::string type, MiddleWare &mw, string url, string name)
Definition PresetExtractor.cpp:325
std::string doClassArrayCopy(std::string type, int field, MiddleWare &mw, string url, string name)
Definition PresetExtractor.cpp:358
const rtosc::Ports real_preset_ports
Definition PresetExtractor.cpp:40
Clipboard clipboardCopy(class MiddleWare &mw, std::string url)
std::string doArrayCopy(MiddleWare &mw, int field, string url, string name)
Definition PresetExtractor.cpp:250
void presetDelete(int)
Definition PresetExtractor.cpp:478
void doClassPaste(std::string type, std::string type_, MiddleWare &mw, string url, XMLwrapper &data)
Definition PresetExtractor.cpp:299
void presetCopyArray(MiddleWare &mw, std::string url, int field, std::string name)
Definition PresetExtractor.cpp:446
std::string doCopy(MiddleWare &mw, string url, string name)
Definition PresetExtractor.cpp:205
void presetPasteArray(MiddleWare &mw, std::string url, int field, std::string name)
Definition PresetExtractor.cpp:452
#define rDoc(doc)
Definition port-sugar.h:278
size_t rtosc_message_length(const char *msg, size_t len)
Definition rtosc.c:848
size_t rtosc_vmessage(char *buffer, size_t len, const char *address, const char *arguments, va_list ap)
Definition rtosc.c:497
const char * rtosc_argument_string(const char *msg)
Definition rtosc.c:11
size_t rtosc_message(char *buffer, size_t len, const char *address, const char *arguments,...)
Definition rtosc.c:161
char rtosc_type(const char *msg, unsigned nargument)
Definition rtosc.c:67
rtosc_arg_t rtosc_argument(const char *msg, unsigned idx)
Definition rtosc.c:732
Definition ports.h:161
data object for the dispatch routine
Definition ports.h:55
int matches
number of matches returned from dispatch routine
Definition ports.h:66
char * loc
Definition ports.h:63
size_t loc_size
Definition ports.h:64
void * obj
runtime object to dispatch this object to
Definition ports.h:65
uint8_t * data
Definition rtosc.h:43
Definition PresetExtractor.h:21
rtosc_blob_t b
Definition rtosc.h:55
const char * s
Definition rtosc.h:54
return c
Definition crypt.c:175
int query
Definition extract.c:1035
ZCONST char * pre
Definition fileio.c:2492
int result
Definition process.c:1455
#define void
Definition unzip.h:396