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.

584 lines
21KB

  1. /*
  2. * Carla State utils
  3. * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_STATE_UTILS_HPP_INCLUDED
  18. #define CARLA_STATE_UTILS_HPP_INCLUDED
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "RtList.hpp"
  22. #include "juce_core.h"
  23. using juce::String;
  24. using juce::XmlElement;
  25. CARLA_BACKEND_START_NAMESPACE
  26. // -----------------------------------------------------------------------
  27. struct StateParameter {
  28. uint32_t index;
  29. const char* name;
  30. const char* symbol;
  31. float value;
  32. uint8_t midiChannel;
  33. int16_t midiCC;
  34. StateParameter() noexcept
  35. : index(0),
  36. name(nullptr),
  37. symbol(nullptr),
  38. value(0.0f),
  39. midiChannel(0),
  40. midiCC(-1) {}
  41. ~StateParameter()
  42. {
  43. if (name != nullptr)
  44. {
  45. delete[] name;
  46. name = nullptr;
  47. }
  48. if (symbol != nullptr)
  49. {
  50. delete[] symbol;
  51. symbol = nullptr;
  52. }
  53. }
  54. CARLA_DECLARE_NON_COPY_STRUCT(StateParameter)
  55. };
  56. struct StateCustomData {
  57. const char* type;
  58. const char* key;
  59. const char* value;
  60. StateCustomData() noexcept
  61. : type(nullptr),
  62. key(nullptr),
  63. value(nullptr) {}
  64. ~StateCustomData()
  65. {
  66. if (type != nullptr)
  67. {
  68. delete[] type;
  69. type = nullptr;
  70. }
  71. if (key != nullptr)
  72. {
  73. delete[] key;
  74. key = nullptr;
  75. }
  76. if (value != nullptr)
  77. {
  78. delete[] value;
  79. value = nullptr;
  80. }
  81. }
  82. CARLA_DECLARE_NON_COPY_STRUCT(StateCustomData)
  83. };
  84. typedef NonRtList<StateParameter*> StateParameterList;
  85. typedef NonRtList<StateCustomData*> StateCustomDataList;
  86. typedef NonRtList<StateParameter*>::Itenerator StateParameterItenerator;
  87. typedef NonRtList<StateCustomData*>::Itenerator StateCustomDataItenerator;
  88. struct SaveState {
  89. const char* type;
  90. const char* name;
  91. const char* label;
  92. const char* binary;
  93. long uniqueID;
  94. bool active;
  95. float dryWet;
  96. float volume;
  97. float balanceLeft;
  98. float balanceRight;
  99. float panning;
  100. int8_t ctrlChannel;
  101. int32_t currentProgramIndex;
  102. const char* currentProgramName;
  103. int32_t currentMidiBank;
  104. int32_t currentMidiProgram;
  105. const char* chunk;
  106. StateParameterList parameters;
  107. StateCustomDataList customData;
  108. SaveState() noexcept
  109. : type(nullptr),
  110. name(nullptr),
  111. label(nullptr),
  112. binary(nullptr),
  113. uniqueID(0),
  114. active(false),
  115. dryWet(1.0f),
  116. volume(1.0f),
  117. balanceLeft(-1.0f),
  118. balanceRight(1.0f),
  119. panning(0.0f),
  120. ctrlChannel(-1),
  121. currentProgramIndex(-1),
  122. currentProgramName(nullptr),
  123. currentMidiBank(-1),
  124. currentMidiProgram(-1),
  125. chunk(nullptr) {}
  126. ~SaveState()
  127. {
  128. reset();
  129. }
  130. void reset()
  131. {
  132. if (type != nullptr)
  133. {
  134. delete[] type;
  135. type = nullptr;
  136. }
  137. if (name != nullptr)
  138. {
  139. delete[] name;
  140. name = nullptr;
  141. }
  142. if (label != nullptr)
  143. {
  144. delete[] label;
  145. label = nullptr;
  146. }
  147. if (binary != nullptr)
  148. {
  149. delete[] binary;
  150. binary = nullptr;
  151. }
  152. if (currentProgramName != nullptr)
  153. {
  154. delete[] currentProgramName;
  155. currentProgramName = nullptr;
  156. }
  157. if (chunk != nullptr)
  158. {
  159. delete[] chunk;
  160. chunk = nullptr;
  161. }
  162. uniqueID = 0;
  163. active = false;
  164. dryWet = 1.0f;
  165. volume = 1.0f;
  166. balanceLeft = -1.0f;
  167. balanceRight = 1.0f;
  168. panning = 0.0f;
  169. ctrlChannel = -1;
  170. currentProgramIndex = -1;
  171. currentMidiBank = -1;
  172. currentMidiProgram = -1;
  173. for (StateParameterItenerator it = parameters.begin(); it.valid(); it.next())
  174. {
  175. StateParameter* const stateParameter(*it);
  176. delete stateParameter;
  177. }
  178. for (StateCustomDataItenerator it = customData.begin(); it.valid(); it.next())
  179. {
  180. StateCustomData* const stateCustomData(*it);
  181. delete stateCustomData;
  182. }
  183. parameters.clear();
  184. customData.clear();
  185. }
  186. CARLA_DECLARE_NON_COPY_STRUCT(SaveState)
  187. };
  188. // -----------------------------------------------------------------------
  189. static inline
  190. String xmlSafeString(const String& string, const bool toXml)
  191. {
  192. String newString(string);
  193. if (toXml)
  194. return newString.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;").replace("'","&apos;").replace("\"","&quot;");
  195. else
  196. return newString.replace("&amp;","&").replace("&lt;","<").replace("&gt;",">").replace("&apos;","'").replace("&quot;","\"");
  197. }
  198. static inline
  199. const char* xmlSafeStringCharDup(const String& string, const bool toXml)
  200. {
  201. return carla_strdup(xmlSafeString(string, toXml).toRawUTF8());
  202. }
  203. // -----------------------------------------------------------------------
  204. static inline
  205. void fillSaveStateFromXmlElement(SaveState& saveState, const XmlElement& xmlElement)
  206. {
  207. for (XmlElement* elem = xmlElement.getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  208. {
  209. // ---------------------------------------------------------------
  210. // Info
  211. if (elem->getTagName().equalsIgnoreCase("info"))
  212. {
  213. for (XmlElement* xmlInfo = elem->getFirstChildElement(); xmlInfo != nullptr; xmlInfo = xmlInfo->getNextElement())
  214. {
  215. const String& tag(xmlInfo->getTagName());
  216. const String text(xmlInfo->getAllSubText().trim());
  217. if (tag.equalsIgnoreCase("type"))
  218. saveState.type = xmlSafeStringCharDup(text, false);
  219. else if (tag.equalsIgnoreCase("name"))
  220. saveState.name = xmlSafeStringCharDup(text, false);
  221. else if (tag.equalsIgnoreCase("label") || tag.equalsIgnoreCase("uri"))
  222. saveState.label = xmlSafeStringCharDup(text, false);
  223. else if (tag.equalsIgnoreCase("binary") || tag.equalsIgnoreCase("filename"))
  224. saveState.binary = xmlSafeStringCharDup(text, false);
  225. else if (tag.equalsIgnoreCase("uniqueid"))
  226. saveState.uniqueID = text.getLargeIntValue();
  227. }
  228. }
  229. // ---------------------------------------------------------------
  230. // Data
  231. else if (elem->getTagName().equalsIgnoreCase("data"))
  232. {
  233. for (XmlElement* xmlData = elem->getFirstChildElement(); xmlData != nullptr; xmlData = xmlData->getNextElement())
  234. {
  235. const String& tag(xmlData->getTagName());
  236. const String text(xmlData->getAllSubText().trim());
  237. // -------------------------------------------------------
  238. // Internal Data
  239. if (tag.equalsIgnoreCase("active"))
  240. {
  241. saveState.active = (text.equalsIgnoreCase("yes"));
  242. }
  243. else if (tag.equalsIgnoreCase("drywet"))
  244. {
  245. saveState.dryWet = carla_fixValue(0.0f, 1.0f, text.getFloatValue());
  246. }
  247. else if (tag.equalsIgnoreCase("volume"))
  248. {
  249. saveState.volume = carla_fixValue(0.0f, 1.27f, text.getFloatValue());
  250. }
  251. else if (tag.equalsIgnoreCase("balanceleft") || tag.equalsIgnoreCase("balance-left"))
  252. {
  253. saveState.balanceLeft = carla_fixValue(-1.0f, 1.0f, text.getFloatValue());
  254. }
  255. else if (tag.equalsIgnoreCase("balanceright") || tag.equalsIgnoreCase("balance-right"))
  256. {
  257. saveState.balanceRight = carla_fixValue(-1.0f, 1.0f, text.getFloatValue());
  258. }
  259. else if (tag.equalsIgnoreCase("panning"))
  260. {
  261. saveState.panning = carla_fixValue(-1.0f, 1.0f, text.getFloatValue());
  262. }
  263. else if (tag.equalsIgnoreCase("controlchannel") || tag.equalsIgnoreCase("control-channel"))
  264. {
  265. const int value(text.getIntValue());
  266. if (value >= 1 && value <= MAX_MIDI_CHANNELS)
  267. saveState.ctrlChannel = static_cast<int8_t>(value-1);
  268. }
  269. // -------------------------------------------------------
  270. // Program (current)
  271. else if (tag.equalsIgnoreCase("currentprogramindex") || tag.equalsIgnoreCase("current-program-index"))
  272. {
  273. const int value(text.getIntValue());
  274. if (value >= 1)
  275. saveState.currentProgramIndex = value-1;
  276. }
  277. else if (tag.equalsIgnoreCase("currentprogramname") || tag.equalsIgnoreCase("current-program-name"))
  278. {
  279. saveState.currentProgramName = xmlSafeStringCharDup(text, false);
  280. }
  281. // -------------------------------------------------------
  282. // Midi Program (current)
  283. else if (tag.equalsIgnoreCase("currentmidibank") || tag.equalsIgnoreCase("current-midi-bank"))
  284. {
  285. const int value(text.getIntValue());
  286. if (value >= 1)
  287. saveState.currentMidiBank = value-1;
  288. }
  289. else if (tag.equalsIgnoreCase("currentmidiprogram") || tag.equalsIgnoreCase("current-midi-program"))
  290. {
  291. const int value(text.getIntValue());
  292. if (value >= 1)
  293. saveState.currentMidiProgram = value-1;
  294. }
  295. // -------------------------------------------------------
  296. // Parameters
  297. else if (tag.equalsIgnoreCase("parameter"))
  298. {
  299. StateParameter* const stateParameter(new StateParameter());
  300. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  301. {
  302. const String& pTag(xmlSubData->getTagName());
  303. const String pText(xmlSubData->getAllSubText().trim());
  304. if (pTag.equalsIgnoreCase("index"))
  305. {
  306. const int index(pText.getIntValue());
  307. if (index >= 0)
  308. stateParameter->index = static_cast<uint32_t>(index);
  309. }
  310. else if (pTag.equalsIgnoreCase("name"))
  311. {
  312. stateParameter->name = xmlSafeStringCharDup(pText, false);
  313. }
  314. else if (pTag.equalsIgnoreCase("symbol"))
  315. {
  316. stateParameter->symbol = xmlSafeStringCharDup(pText, false);
  317. }
  318. else if (pTag.equalsIgnoreCase("value"))
  319. {
  320. stateParameter->value = pText.getFloatValue();
  321. }
  322. else if (pTag.equalsIgnoreCase("midichannel") || pTag.equalsIgnoreCase("midi-channel"))
  323. {
  324. const int channel(pText.getIntValue());
  325. if (channel >= 1 && channel <= MAX_MIDI_CHANNELS)
  326. stateParameter->midiChannel = static_cast<uint8_t>(channel-1);
  327. }
  328. else if (pTag.equalsIgnoreCase("midicc") || pTag.equalsIgnoreCase("midi-cc"))
  329. {
  330. const int cc(pText.getIntValue());
  331. if (cc >= 1 && cc < 0x5F)
  332. stateParameter->midiCC = static_cast<int16_t>(cc);
  333. }
  334. }
  335. saveState.parameters.append(stateParameter);
  336. }
  337. // -------------------------------------------------------
  338. // Custom Data
  339. else if (tag.equalsIgnoreCase("customdata") || tag.equalsIgnoreCase("custom-data"))
  340. {
  341. StateCustomData* const stateCustomData(new StateCustomData());
  342. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  343. {
  344. const String& cTag(xmlSubData->getTagName());
  345. const String cText(xmlSubData->getAllSubText().trim());
  346. if (cTag.equalsIgnoreCase("type"))
  347. stateCustomData->type = xmlSafeStringCharDup(cText, false);
  348. else if (cTag.equalsIgnoreCase("key"))
  349. stateCustomData->key = xmlSafeStringCharDup(cText, false);
  350. else if (cTag.equalsIgnoreCase("value"))
  351. stateCustomData->value = xmlSafeStringCharDup(cText, false);
  352. }
  353. saveState.customData.append(stateCustomData);
  354. }
  355. // -------------------------------------------------------
  356. // Chunk
  357. else if (tag.equalsIgnoreCase("chunk"))
  358. {
  359. saveState.chunk = xmlSafeStringCharDup(text, false);
  360. }
  361. }
  362. }
  363. }
  364. }
  365. // -----------------------------------------------------------------------
  366. static inline
  367. void fillXmlStringFromSaveState(String& content, const SaveState& saveState)
  368. {
  369. {
  370. String info(" <Info>\n");
  371. info << " <Type>" << saveState.type << "</Type>\n";
  372. info << " <Name>" << xmlSafeString(saveState.name, true) << "</Name>\n";
  373. switch (getPluginTypeFromString(saveState.type))
  374. {
  375. case PLUGIN_NONE:
  376. break;
  377. case PLUGIN_INTERNAL:
  378. info << " <Label>" << xmlSafeString(saveState.label, true) << "</Label>\n";
  379. break;
  380. case PLUGIN_LADSPA:
  381. info << " <Binary>" << xmlSafeString(saveState.binary, true) << "</Binary>\n";
  382. info << " <Label>" << xmlSafeString(saveState.label, true) << "</Label>\n";
  383. info << " <UniqueID>" << saveState.uniqueID << "</UniqueID>\n";
  384. break;
  385. case PLUGIN_DSSI:
  386. info << " <Binary>" << xmlSafeString(saveState.binary, true) << "</Binary>\n";
  387. info << " <Label>" << xmlSafeString(saveState.label, true) << "</Label>\n";
  388. break;
  389. case PLUGIN_LV2:
  390. info << " <URI>" << xmlSafeString(saveState.label, true) << "</URI>\n";
  391. break;
  392. case PLUGIN_VST:
  393. info << " <Binary>" << xmlSafeString(saveState.binary, true) << "</Binary>\n";
  394. info << " <UniqueID>" << saveState.uniqueID << "</UniqueID>\n";
  395. break;
  396. case PLUGIN_AU:
  397. // TODO?
  398. info << " <Binary>" << xmlSafeString(saveState.binary, true) << "</Binary>\n";
  399. info << " <UniqueID>" << saveState.uniqueID << "</UniqueID>\n";
  400. break;
  401. case PLUGIN_CSOUND:
  402. case PLUGIN_GIG:
  403. case PLUGIN_SF2:
  404. case PLUGIN_SFZ:
  405. info << " <Filename>" << xmlSafeString(saveState.binary, true) << "</Filename>\n";
  406. info << " <Label>" << xmlSafeString(saveState.label, true) << "</Label>\n";
  407. break;
  408. }
  409. info << " </Info>\n\n";
  410. content << info;
  411. }
  412. {
  413. String data(" <Data>\n");
  414. data << " <Active>" << (saveState.active ? "Yes" : "No") << "</Active>\n";
  415. if (saveState.dryWet != 1.0f)
  416. data << " <DryWet>" << saveState.dryWet << "</DryWet>\n";
  417. if (saveState.volume != 1.0f)
  418. data << " <Volume>" << saveState.volume << "</Volume>\n";
  419. if (saveState.balanceLeft != -1.0f)
  420. data << " <Balance-Left>" << saveState.balanceLeft << "</Balance-Left>\n";
  421. if (saveState.balanceRight != 1.0f)
  422. data << " <Balance-Right>" << saveState.balanceRight << "</Balance-Right>\n";
  423. if (saveState.panning != 0.0f)
  424. data << " <Panning>" << saveState.panning << "</Panning>\n";
  425. if (saveState.ctrlChannel < 0)
  426. data << " <ControlChannel>N</ControlChannel>\n";
  427. else
  428. data << " <ControlChannel>" << saveState.ctrlChannel+1 << "</ControlChannel>\n";
  429. content << data;
  430. }
  431. for (StateParameterItenerator it = saveState.parameters.begin(); it.valid(); it.next())
  432. {
  433. StateParameter* const stateParameter(*it);
  434. String parameter("\n"" <Parameter>\n");
  435. parameter << " <Index>" << (long)stateParameter->index << "</Index>\n"; // FIXME
  436. parameter << " <Name>" << xmlSafeString(stateParameter->name, true) << "</Name>\n";
  437. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  438. parameter << " <Symbol>" << xmlSafeString(stateParameter->symbol, true) << "</Symbol>\n";
  439. parameter << " <Value>" << stateParameter->value << "</Value>\n";
  440. if (stateParameter->midiCC > 0)
  441. {
  442. parameter << " <MidiCC>" << stateParameter->midiCC << "</MidiCC>\n";
  443. parameter << " <MidiChannel>" << stateParameter->midiChannel+1 << "</MidiChannel>\n";
  444. }
  445. parameter << " </Parameter>\n";
  446. content << parameter;
  447. }
  448. if (saveState.currentProgramIndex >= 0 && saveState.currentProgramName != nullptr)
  449. {
  450. // ignore 'default' program
  451. #ifdef __USE_GNU
  452. if ((saveState.currentProgramIndex > 0 || strcasecmp(saveState.currentProgramName, "default") != 0))
  453. #else
  454. if ((saveState.currentProgramIndex > 0 || std::strcmp(saveState.currentProgramName, "Default") != 0))
  455. #endif
  456. {
  457. String program("\n");
  458. program << " <CurrentProgramIndex>" << saveState.currentProgramIndex+1 << "</CurrentProgramIndex>\n";
  459. program << " <CurrentProgramName>" << xmlSafeString(saveState.currentProgramName, true) << "</CurrentProgramName>\n";
  460. content << program;
  461. }
  462. }
  463. if (saveState.currentMidiBank >= 0 && saveState.currentMidiProgram >= 0)
  464. {
  465. String midiProgram("\n");
  466. midiProgram << " <CurrentMidiBank>" << saveState.currentMidiBank+1 << "</CurrentMidiBank>\n";
  467. midiProgram << " <CurrentMidiProgram>" << saveState.currentMidiProgram+1 << "</CurrentMidiProgram>\n";
  468. content << midiProgram;
  469. }
  470. for (StateCustomDataItenerator it = saveState.customData.begin(); it.valid(); it.next())
  471. {
  472. StateCustomData* const stateCustomData(*it);
  473. String customData("\n"" <CustomData>\n");
  474. customData << " <Type>" << xmlSafeString(stateCustomData->type, true) << "</Type>\n";
  475. customData << " <Key>" << xmlSafeString(stateCustomData->key, true) << "</Key>\n";
  476. if (std::strcmp(stateCustomData->type, CUSTOM_DATA_CHUNK) == 0 || std::strlen(stateCustomData->value) >= 128)
  477. customData << " <Value>\n" << xmlSafeString(stateCustomData->value, true) << "\n </Value>\n";
  478. else
  479. customData << " <Value>" << xmlSafeString(stateCustomData->value, true) << "</Value>\n";
  480. customData << " </CustomData>\n";
  481. content << customData;
  482. }
  483. if (saveState.chunk != nullptr && saveState.chunk[0] != '\0')
  484. {
  485. String chunk("\n"" <Chunk>\n");
  486. chunk << saveState.chunk << "\n </Chunk>\n";
  487. content << chunk;
  488. }
  489. content << " </Data>\n";
  490. }
  491. // -----------------------------------------------------------------------
  492. CARLA_BACKEND_END_NAMESPACE
  493. #endif // CARLA_STATE_UTILS_HPP_INCLUDED