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.

675 lines
23KB

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