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.

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