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.

483 lines
16KB

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