Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

498 lines
16KB

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