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.

656 lines
22KB

  1. /*
  2. * Carla State utils
  3. * Copyright (C) 2012-2014 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. #include "CarlaStateUtils.hpp"
  18. #include "CarlaBackendUtils.hpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include <string>
  22. using juce::String;
  23. using juce::XmlElement;
  24. CARLA_BACKEND_START_NAMESPACE
  25. // -----------------------------------------------------------------------
  26. // getNewLineSplittedString
  27. static String getNewLineSplittedString(const String& string)
  28. {
  29. static const int kLineWidth = 120;
  30. int i=0;
  31. const int length=string.length();
  32. String newString;
  33. newString.preallocateBytes(static_cast<size_t>(length + length/120 + 2));
  34. for (; i+kLineWidth < length; i += kLineWidth)
  35. {
  36. newString += string.substring(i, i+kLineWidth);
  37. newString += "\n";
  38. }
  39. newString += string.substring(i);
  40. return newString;
  41. }
  42. // -----------------------------------------------------------------------
  43. // xmlSafeStringFast
  44. /* Based on some code by James Kanze from stackoverflow
  45. * https://stackoverflow.com/questions/7724011/in-c-whats-the-fastest-way-to-replace-all-occurrences-of-a-substring-within */
  46. static std::string replaceStdString(const std::string& original, const std::string& before, const std::string& after)
  47. {
  48. std::string::const_iterator current = original.begin(), end = original.end(), next;
  49. std::string retval;
  50. for (; (next = std::search(current, end, before.begin(), before.end())) != end;)
  51. {
  52. retval.append(current, next);
  53. retval.append(after);
  54. current = next + static_cast<std::ssize_t>(before.size());
  55. }
  56. retval.append(current, next);
  57. return retval;
  58. }
  59. static std::string xmlSafeStringFast(const char* const cstring, const bool toXml)
  60. {
  61. std::string string(cstring);
  62. if (toXml)
  63. {
  64. string = replaceStdString(string, "&","&amp;");
  65. string = replaceStdString(string, "<","&lt;");
  66. string = replaceStdString(string, ">","&gt;");
  67. string = replaceStdString(string, "'","&apos;");
  68. string = replaceStdString(string, "\"","&quot;");
  69. }
  70. else
  71. {
  72. string = replaceStdString(string, "&lt;","<");
  73. string = replaceStdString(string, "&gt;",">");
  74. string = replaceStdString(string, "&apos;","'");
  75. string = replaceStdString(string, "&quot;","\"");
  76. string = replaceStdString(string, "&amp;","&");
  77. }
  78. return string;
  79. }
  80. // -----------------------------------------------------------------------
  81. // xmlSafeStringCharDup
  82. static const char* xmlSafeStringCharDup(const String& string, const bool toXml)
  83. {
  84. return carla_strdup(xmlSafeString(string, toXml).toRawUTF8());
  85. }
  86. // -----------------------------------------------------------------------
  87. // StateParameter
  88. StateParameter::StateParameter() noexcept
  89. : isInput(true),
  90. index(-1),
  91. name(nullptr),
  92. symbol(nullptr),
  93. #ifndef BUILD_BRIDGE
  94. value(0.0f),
  95. midiChannel(0),
  96. midiCC(-1) {}
  97. #else
  98. value(0.0f) {}
  99. #endif
  100. StateParameter::~StateParameter() noexcept
  101. {
  102. if (name != nullptr)
  103. {
  104. delete[] name;
  105. name = nullptr;
  106. }
  107. if (symbol != nullptr)
  108. {
  109. delete[] symbol;
  110. symbol = nullptr;
  111. }
  112. }
  113. // -----------------------------------------------------------------------
  114. // StateCustomData
  115. StateCustomData::StateCustomData() noexcept
  116. : type(nullptr),
  117. key(nullptr),
  118. value(nullptr) {}
  119. StateCustomData::~StateCustomData() noexcept
  120. {
  121. if (type != nullptr)
  122. {
  123. delete[] type;
  124. type = nullptr;
  125. }
  126. if (key != nullptr)
  127. {
  128. delete[] key;
  129. key = nullptr;
  130. }
  131. if (value != nullptr)
  132. {
  133. delete[] value;
  134. value = nullptr;
  135. }
  136. }
  137. // -----------------------------------------------------------------------
  138. // StateSave
  139. StateSave::StateSave() noexcept
  140. : type(nullptr),
  141. name(nullptr),
  142. label(nullptr),
  143. binary(nullptr),
  144. uniqueId(0),
  145. #ifndef BUILD_BRIDGE
  146. active(false),
  147. dryWet(1.0f),
  148. volume(1.0f),
  149. balanceLeft(-1.0f),
  150. balanceRight(1.0f),
  151. panning(0.0f),
  152. ctrlChannel(-1),
  153. options(0x0),
  154. #endif
  155. currentProgramIndex(-1),
  156. currentProgramName(nullptr),
  157. currentMidiBank(-1),
  158. currentMidiProgram(-1),
  159. chunk(nullptr),
  160. parameters(),
  161. customData() {}
  162. StateSave::~StateSave() noexcept
  163. {
  164. clear();
  165. }
  166. void StateSave::clear() noexcept
  167. {
  168. if (type != nullptr)
  169. {
  170. delete[] type;
  171. type = nullptr;
  172. }
  173. if (name != nullptr)
  174. {
  175. delete[] name;
  176. name = nullptr;
  177. }
  178. if (label != nullptr)
  179. {
  180. delete[] label;
  181. label = nullptr;
  182. }
  183. if (binary != nullptr)
  184. {
  185. delete[] binary;
  186. binary = nullptr;
  187. }
  188. if (currentProgramName != nullptr)
  189. {
  190. delete[] currentProgramName;
  191. currentProgramName = nullptr;
  192. }
  193. if (chunk != nullptr)
  194. {
  195. delete[] chunk;
  196. chunk = nullptr;
  197. }
  198. uniqueId = 0;
  199. #ifndef BUILD_BRIDGE
  200. active = false;
  201. dryWet = 1.0f;
  202. volume = 1.0f;
  203. balanceLeft = -1.0f;
  204. balanceRight = 1.0f;
  205. panning = 0.0f;
  206. ctrlChannel = -1;
  207. options = 0x0;
  208. #endif
  209. currentProgramIndex = -1;
  210. currentMidiBank = -1;
  211. currentMidiProgram = -1;
  212. for (StateParameterItenerator it = parameters.begin(); it.valid(); it.next())
  213. {
  214. StateParameter* const stateParameter(it.getValue());
  215. delete stateParameter;
  216. }
  217. for (StateCustomDataItenerator it = customData.begin(); it.valid(); it.next())
  218. {
  219. StateCustomData* const stateCustomData(it.getValue());
  220. delete stateCustomData;
  221. }
  222. parameters.clear();
  223. customData.clear();
  224. }
  225. // -----------------------------------------------------------------------
  226. // fillFromXmlElement
  227. bool StateSave::fillFromXmlElement(const XmlElement* const xmlElement)
  228. {
  229. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  230. clear();
  231. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  232. {
  233. const String& tagName(elem->getTagName());
  234. // ---------------------------------------------------------------
  235. // Info
  236. if (tagName.equalsIgnoreCase("info"))
  237. {
  238. for (XmlElement* xmlInfo = elem->getFirstChildElement(); xmlInfo != nullptr; xmlInfo = xmlInfo->getNextElement())
  239. {
  240. const String& tag(xmlInfo->getTagName());
  241. const String text(xmlInfo->getAllSubText().trim());
  242. if (tag.equalsIgnoreCase("type"))
  243. type = xmlSafeStringCharDup(text, false);
  244. else if (tag.equalsIgnoreCase("name"))
  245. name = xmlSafeStringCharDup(text, false);
  246. else if (tag.equalsIgnoreCase("label") || tag.equalsIgnoreCase("identifier") || tag.equalsIgnoreCase("uri"))
  247. label = xmlSafeStringCharDup(text, false);
  248. else if (tag.equalsIgnoreCase("binary") || tag.equalsIgnoreCase("bundle") || tag.equalsIgnoreCase("filename"))
  249. binary = xmlSafeStringCharDup(text, false);
  250. else if (tag.equalsIgnoreCase("uniqueid"))
  251. uniqueId = text.getLargeIntValue();
  252. }
  253. }
  254. // ---------------------------------------------------------------
  255. // Data
  256. else if (tagName.equalsIgnoreCase("data"))
  257. {
  258. for (XmlElement* xmlData = elem->getFirstChildElement(); xmlData != nullptr; xmlData = xmlData->getNextElement())
  259. {
  260. const String& tag(xmlData->getTagName());
  261. const String text(xmlData->getAllSubText().trim());
  262. #ifndef BUILD_BRIDGE
  263. // -------------------------------------------------------
  264. // Internal Data
  265. if (tag.equalsIgnoreCase("active"))
  266. {
  267. active = (text.equalsIgnoreCase("yes") || text.equalsIgnoreCase("true"));
  268. }
  269. else if (tag.equalsIgnoreCase("drywet"))
  270. {
  271. dryWet = carla_fixValue(0.0f, 1.0f, text.getFloatValue());
  272. }
  273. else if (tag.equalsIgnoreCase("volume"))
  274. {
  275. volume = carla_fixValue(0.0f, 1.27f, text.getFloatValue());
  276. }
  277. else if (tag.equalsIgnoreCase("balanceleft") || tag.equalsIgnoreCase("balance-left"))
  278. {
  279. balanceLeft = carla_fixValue(-1.0f, 1.0f, text.getFloatValue());
  280. }
  281. else if (tag.equalsIgnoreCase("balanceright") || tag.equalsIgnoreCase("balance-right"))
  282. {
  283. balanceRight = carla_fixValue(-1.0f, 1.0f, text.getFloatValue());
  284. }
  285. else if (tag.equalsIgnoreCase("panning"))
  286. {
  287. panning = carla_fixValue(-1.0f, 1.0f, text.getFloatValue());
  288. }
  289. else if (tag.equalsIgnoreCase("controlchannel") || tag.equalsIgnoreCase("control-channel"))
  290. {
  291. if (! text.startsWithIgnoreCase("n"))
  292. {
  293. const int value(text.getIntValue());
  294. if (value >= 1 && value <= MAX_MIDI_CHANNELS)
  295. ctrlChannel = static_cast<int8_t>(value-1);
  296. }
  297. }
  298. else if (tag.equalsIgnoreCase("options"))
  299. {
  300. const int value(text.getHexValue32());
  301. if (value > 0)
  302. options = static_cast<uint>(value);
  303. }
  304. #else
  305. if (false) {}
  306. #endif
  307. // -------------------------------------------------------
  308. // Program (current)
  309. else if (tag.equalsIgnoreCase("currentprogramindex") || tag.equalsIgnoreCase("current-program-index"))
  310. {
  311. const int value(text.getIntValue());
  312. if (value >= 1)
  313. currentProgramIndex = value-1;
  314. }
  315. else if (tag.equalsIgnoreCase("currentprogramname") || tag.equalsIgnoreCase("current-program-name"))
  316. {
  317. currentProgramName = xmlSafeStringCharDup(text, false);
  318. }
  319. // -------------------------------------------------------
  320. // Midi Program (current)
  321. else if (tag.equalsIgnoreCase("currentmidibank") || tag.equalsIgnoreCase("current-midi-bank"))
  322. {
  323. const int value(text.getIntValue());
  324. if (value >= 1)
  325. currentMidiBank = value-1;
  326. }
  327. else if (tag.equalsIgnoreCase("currentmidiprogram") || tag.equalsIgnoreCase("current-midi-program"))
  328. {
  329. const int value(text.getIntValue());
  330. if (value >= 1)
  331. currentMidiProgram = value-1;
  332. }
  333. // -------------------------------------------------------
  334. // Parameters
  335. else if (tag.equalsIgnoreCase("parameter"))
  336. {
  337. StateParameter* const stateParameter(new StateParameter());
  338. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  339. {
  340. const String& pTag(xmlSubData->getTagName());
  341. const String pText(xmlSubData->getAllSubText().trim());
  342. if (pTag.equalsIgnoreCase("index"))
  343. {
  344. const int index(pText.getIntValue());
  345. if (index >= 0)
  346. stateParameter->index = index;
  347. }
  348. else if (pTag.equalsIgnoreCase("name"))
  349. {
  350. stateParameter->name = xmlSafeStringCharDup(pText, false);
  351. }
  352. else if (pTag.equalsIgnoreCase("symbol"))
  353. {
  354. stateParameter->symbol = xmlSafeStringCharDup(pText, false);
  355. }
  356. else if (pTag.equalsIgnoreCase("value"))
  357. {
  358. stateParameter->value = pText.getFloatValue();
  359. }
  360. #ifndef BUILD_BRIDGE
  361. else if (pTag.equalsIgnoreCase("midichannel") || pTag.equalsIgnoreCase("midi-channel"))
  362. {
  363. const int channel(pText.getIntValue());
  364. if (channel >= 1 && channel <= MAX_MIDI_CHANNELS)
  365. stateParameter->midiChannel = static_cast<uint8_t>(channel-1);
  366. }
  367. else if (pTag.equalsIgnoreCase("midicc") || pTag.equalsIgnoreCase("midi-cc"))
  368. {
  369. const int cc(pText.getIntValue());
  370. if (cc >= -1 && cc < MAX_MIDI_CONTROL)
  371. stateParameter->midiCC = static_cast<int16_t>(cc);
  372. }
  373. #endif
  374. }
  375. parameters.append(stateParameter);
  376. }
  377. // -------------------------------------------------------
  378. // Custom Data
  379. else if (tag.equalsIgnoreCase("customdata") || tag.equalsIgnoreCase("custom-data"))
  380. {
  381. StateCustomData* const stateCustomData(new StateCustomData());
  382. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  383. {
  384. const String& cTag(xmlSubData->getTagName());
  385. const String cText(xmlSubData->getAllSubText().trim());
  386. if (cTag.equalsIgnoreCase("type"))
  387. stateCustomData->type = xmlSafeStringCharDup(cText, false);
  388. else if (cTag.equalsIgnoreCase("key"))
  389. stateCustomData->key = xmlSafeStringCharDup(cText, false);
  390. else if (cTag.equalsIgnoreCase("value"))
  391. stateCustomData->value = carla_strdup(cText.toRawUTF8()); //xmlSafeStringCharDup(cText, false);
  392. }
  393. customData.append(stateCustomData);
  394. }
  395. // -------------------------------------------------------
  396. // Chunk
  397. else if (tag.equalsIgnoreCase("chunk"))
  398. {
  399. const String nText(text.replace("\n", ""));
  400. chunk = xmlSafeStringCharDup(nText, false);
  401. }
  402. }
  403. }
  404. }
  405. return true;
  406. }
  407. // -----------------------------------------------------------------------
  408. // fillXmlStringFromStateSave
  409. String StateSave::toString() const
  410. {
  411. String content;
  412. {
  413. String infoXml(" <Info>\n");
  414. infoXml << " <Type>" << String(type != nullptr ? type : "") << "</Type>\n";
  415. infoXml << " <Name>" << xmlSafeString(name, true) << "</Name>\n";
  416. switch (getPluginTypeFromString(type))
  417. {
  418. case PLUGIN_NONE:
  419. break;
  420. case PLUGIN_INTERNAL:
  421. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  422. break;
  423. case PLUGIN_LADSPA:
  424. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  425. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  426. infoXml << " <UniqueID>" << uniqueId << "</UniqueID>\n";
  427. break;
  428. case PLUGIN_DSSI:
  429. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  430. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  431. break;
  432. case PLUGIN_LV2:
  433. infoXml << " <URI>" << xmlSafeString(label, true) << "</URI>\n";
  434. break;
  435. case PLUGIN_VST:
  436. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  437. infoXml << " <UniqueID>" << uniqueId << "</UniqueID>\n";
  438. break;
  439. case PLUGIN_VST3:
  440. // TODO?
  441. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  442. infoXml << " <UniqueID>" << uniqueId << "</UniqueID>\n";
  443. break;
  444. case PLUGIN_AU:
  445. infoXml << " <Identifier>" << xmlSafeString(label, true) << "</Identifier>\n";
  446. break;
  447. case PLUGIN_GIG:
  448. case PLUGIN_SF2:
  449. infoXml << " <Filename>" << xmlSafeString(binary, true) << "</Filename>\n";
  450. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  451. break;
  452. case PLUGIN_SFZ:
  453. infoXml << " <Filename>" << xmlSafeString(binary, true) << "</Filename>\n";
  454. break;
  455. }
  456. infoXml << " </Info>\n\n";
  457. content << infoXml;
  458. }
  459. content << " <Data>\n";
  460. #ifndef BUILD_BRIDGE
  461. {
  462. String dataXml;
  463. dataXml << " <Active>" << (active ? "Yes" : "No") << "</Active>\n";
  464. if (! carla_compareFloats(dryWet, 1.0f))
  465. dataXml << " <DryWet>" << String(dryWet, 7) << "</DryWet>\n";
  466. if (! carla_compareFloats(volume, 1.0f))
  467. dataXml << " <Volume>" << String(volume, 7) << "</Volume>\n";
  468. if (! carla_compareFloats(balanceLeft, -1.0f))
  469. dataXml << " <Balance-Left>" << String(balanceLeft, 7) << "</Balance-Left>\n";
  470. if (! carla_compareFloats(balanceRight, 1.0f))
  471. dataXml << " <Balance-Right>" << String(balanceRight, 7) << "</Balance-Right>\n";
  472. if (! carla_compareFloats(panning, 0.0f))
  473. dataXml << " <Panning>" << String(panning, 7) << "</Panning>\n";
  474. if (ctrlChannel < 0)
  475. dataXml << " <ControlChannel>N</ControlChannel>\n";
  476. else
  477. dataXml << " <ControlChannel>" << int(ctrlChannel+1) << "</ControlChannel>\n";
  478. dataXml << " <Options>0x" << String::toHexString(static_cast<int>(options)) << "</Options>\n";
  479. content << dataXml;
  480. }
  481. #endif
  482. for (StateParameterItenerator it = parameters.begin(); it.valid(); it.next())
  483. {
  484. StateParameter* const stateParameter(it.getValue());
  485. String parameterXml("\n"" <Parameter>\n");
  486. parameterXml << " <Index>" << String(stateParameter->index) << "</Index>\n";
  487. parameterXml << " <Name>" << xmlSafeString(stateParameter->name, true) << "</Name>\n";
  488. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  489. parameterXml << " <Symbol>" << xmlSafeString(stateParameter->symbol, true) << "</Symbol>\n";
  490. if (stateParameter->isInput)
  491. parameterXml << " <Value>" << String(stateParameter->value, 15) << "</Value>\n";
  492. #ifndef BUILD_BRIDGE
  493. if (stateParameter->midiCC > 0)
  494. {
  495. parameterXml << " <MidiCC>" << stateParameter->midiCC << "</MidiCC>\n";
  496. parameterXml << " <MidiChannel>" << stateParameter->midiChannel+1 << "</MidiChannel>\n";
  497. }
  498. #endif
  499. parameterXml << " </Parameter>\n";
  500. content << parameterXml;
  501. }
  502. if (currentProgramIndex >= 0 && currentProgramName != nullptr && currentProgramName[0] != '\0')
  503. {
  504. // ignore 'default' program
  505. if (currentProgramIndex > 0 || ! String(currentProgramName).equalsIgnoreCase("default"))
  506. {
  507. String programXml("\n");
  508. programXml << " <CurrentProgramIndex>" << currentProgramIndex+1 << "</CurrentProgramIndex>\n";
  509. programXml << " <CurrentProgramName>" << xmlSafeString(currentProgramName, true) << "</CurrentProgramName>\n";
  510. content << programXml;
  511. }
  512. }
  513. if (currentMidiBank >= 0 && currentMidiProgram >= 0)
  514. {
  515. String midiProgramXml("\n");
  516. midiProgramXml << " <CurrentMidiBank>" << currentMidiBank+1 << "</CurrentMidiBank>\n";
  517. midiProgramXml << " <CurrentMidiProgram>" << currentMidiProgram+1 << "</CurrentMidiProgram>\n";
  518. content << midiProgramXml;
  519. }
  520. for (StateCustomDataItenerator it = customData.begin(); it.valid(); it.next())
  521. {
  522. StateCustomData* const stateCustomData(it.getValue());
  523. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->type != nullptr && stateCustomData->type[0] != '\0');
  524. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->key != nullptr && stateCustomData->key[0] != '\0');
  525. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->value != nullptr);
  526. String customDataXml("\n"" <CustomData>\n");
  527. customDataXml << " <Type>" << xmlSafeString(stateCustomData->type, true) << "</Type>\n";
  528. customDataXml << " <Key>" << xmlSafeString(stateCustomData->key, true) << "</Key>\n";
  529. if (std::strcmp(stateCustomData->type, CUSTOM_DATA_TYPE_CHUNK) == 0 || std::strlen(stateCustomData->value) >= 128)
  530. {
  531. customDataXml << " <Value>\n";
  532. customDataXml << xmlSafeStringFast(stateCustomData->value, true);
  533. customDataXml << "\n </Value>\n";
  534. }
  535. else
  536. {
  537. customDataXml << " <Value>";
  538. customDataXml << xmlSafeStringFast(stateCustomData->value, true);
  539. customDataXml << "</Value>\n";
  540. }
  541. customDataXml << " </CustomData>\n";
  542. content << customDataXml;
  543. }
  544. if (chunk != nullptr && chunk[0] != '\0')
  545. {
  546. String chunkXml("\n"" <Chunk>\n");
  547. chunkXml << getNewLineSplittedString(chunk) << "\n </Chunk>\n";
  548. content << chunkXml;
  549. }
  550. content << " </Data>\n";
  551. return content;
  552. }
  553. // -----------------------------------------------------------------------
  554. CARLA_BACKEND_END_NAMESPACE