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.

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