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.

777 lines
27KB

  1. /*
  2. * Carla State utils
  3. * Copyright (C) 2012-2023 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 "water/streams/MemoryOutputStream.h"
  22. #include "water/xml/XmlElement.h"
  23. #include <string>
  24. using water::MemoryOutputStream;
  25. using water::String;
  26. using water::XmlElement;
  27. CARLA_BACKEND_START_NAMESPACE
  28. // -----------------------------------------------------------------------
  29. // getNewLineSplittedString
  30. static void getNewLineSplittedString(MemoryOutputStream& stream, const String& string)
  31. {
  32. static const int kLineWidth = 120;
  33. int i = 0;
  34. const int length = string.length();
  35. const char* const raw = string.toUTF8();
  36. stream.preallocate(static_cast<std::size_t>(length + length/kLineWidth + 3));
  37. for (; i+kLineWidth < length; i += kLineWidth)
  38. {
  39. stream.write(raw+i, kLineWidth);
  40. stream.writeByte('\n');
  41. }
  42. stream << (raw+i);
  43. }
  44. // -----------------------------------------------------------------------
  45. // xmlSafeStringFast
  46. /* Based on some code by James Kanze from stackoverflow
  47. * https://stackoverflow.com/questions/7724011/in-c-whats-the-fastest-way-to-replace-all-occurrences-of-a-substring-within */
  48. static std::string replaceStdString(const std::string& original, const std::string& before, const std::string& after)
  49. {
  50. std::string::const_iterator current = original.begin(), end = original.end(), next;
  51. std::string retval;
  52. for (; (next = std::search(current, end, before.begin(), before.end())) != end;)
  53. {
  54. retval.append(current, next);
  55. retval.append(after);
  56. current = next + static_cast<ssize_t>(before.size());
  57. }
  58. retval.append(current, next);
  59. return retval;
  60. }
  61. static std::string xmlSafeStringFast(const char* const cstring, const bool toXml)
  62. {
  63. std::string string(cstring);
  64. if (toXml)
  65. {
  66. string = replaceStdString(string, "&","&amp;");
  67. string = replaceStdString(string, "<","&lt;");
  68. string = replaceStdString(string, ">","&gt;");
  69. string = replaceStdString(string, "'","&apos;");
  70. string = replaceStdString(string, "\"","&quot;");
  71. }
  72. else
  73. {
  74. string = replaceStdString(string, "&lt;","<");
  75. string = replaceStdString(string, "&gt;",">");
  76. string = replaceStdString(string, "&apos;","'");
  77. string = replaceStdString(string, "&quot;","\"");
  78. string = replaceStdString(string, "&amp;","&");
  79. }
  80. return string;
  81. }
  82. // -----------------------------------------------------------------------
  83. // xmlSafeStringCharDup
  84. /*
  85. static const char* xmlSafeStringCharDup(const char* const cstring, const bool toXml)
  86. {
  87. return carla_strdup(xmlSafeString(cstring, toXml).toRawUTF8());
  88. }
  89. */
  90. static const char* xmlSafeStringCharDup(const String& string, const bool toXml)
  91. {
  92. return carla_strdup(xmlSafeString(string, toXml).toRawUTF8());
  93. }
  94. // -----------------------------------------------------------------------
  95. // StateParameter
  96. CarlaStateSave::Parameter::Parameter() noexcept
  97. : dummy(true),
  98. index(-1),
  99. name(nullptr),
  100. symbol(nullptr),
  101. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  102. value(0.0f),
  103. mappedControlIndex(CONTROL_INDEX_NONE),
  104. midiChannel(0),
  105. mappedRangeValid(false),
  106. mappedMinimum(0.0f),
  107. mappedMaximum(1.0f) {}
  108. #else
  109. value(0.0f) {}
  110. #endif
  111. CarlaStateSave::Parameter::~Parameter() noexcept
  112. {
  113. if (name != nullptr)
  114. {
  115. delete[] name;
  116. name = nullptr;
  117. }
  118. if (symbol != nullptr)
  119. {
  120. delete[] symbol;
  121. symbol = nullptr;
  122. }
  123. }
  124. // -----------------------------------------------------------------------
  125. // StateCustomData
  126. CarlaStateSave::CustomData::CustomData() noexcept
  127. : type(nullptr),
  128. key(nullptr),
  129. value(nullptr) {}
  130. CarlaStateSave::CustomData::~CustomData() noexcept
  131. {
  132. if (type != nullptr)
  133. {
  134. delete[] type;
  135. type = nullptr;
  136. }
  137. if (key != nullptr)
  138. {
  139. delete[] key;
  140. key = nullptr;
  141. }
  142. if (value != nullptr)
  143. {
  144. delete[] value;
  145. value = nullptr;
  146. }
  147. }
  148. bool CarlaStateSave::CustomData::isValid() const noexcept
  149. {
  150. if (type == nullptr || type[0] == '\0') return false;
  151. if (key == nullptr || key [0] == '\0') return false;
  152. if (value == nullptr) return false;
  153. return true;
  154. }
  155. // -----------------------------------------------------------------------
  156. // StateSave
  157. CarlaStateSave::CarlaStateSave() noexcept
  158. : type(nullptr),
  159. name(nullptr),
  160. label(nullptr),
  161. binary(nullptr),
  162. uniqueId(0),
  163. options(PLUGIN_OPTIONS_NULL),
  164. temporary(false),
  165. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  166. active(false),
  167. dryWet(1.0f),
  168. volume(1.0f),
  169. balanceLeft(-1.0f),
  170. balanceRight(1.0f),
  171. panning(0.0f),
  172. ctrlChannel(-1),
  173. #endif
  174. currentProgramIndex(-1),
  175. currentProgramName(nullptr),
  176. currentMidiBank(-1),
  177. currentMidiProgram(-1),
  178. chunk(nullptr),
  179. parameters(),
  180. customData() {}
  181. CarlaStateSave::~CarlaStateSave() noexcept
  182. {
  183. clear();
  184. }
  185. void CarlaStateSave::clear() noexcept
  186. {
  187. if (type != nullptr)
  188. {
  189. delete[] type;
  190. type = nullptr;
  191. }
  192. if (name != nullptr)
  193. {
  194. delete[] name;
  195. name = nullptr;
  196. }
  197. if (label != nullptr)
  198. {
  199. delete[] label;
  200. label = nullptr;
  201. }
  202. if (binary != nullptr)
  203. {
  204. delete[] binary;
  205. binary = nullptr;
  206. }
  207. if (currentProgramName != nullptr)
  208. {
  209. delete[] currentProgramName;
  210. currentProgramName = nullptr;
  211. }
  212. if (chunk != nullptr)
  213. {
  214. delete[] chunk;
  215. chunk = nullptr;
  216. }
  217. uniqueId = 0;
  218. options = PLUGIN_OPTIONS_NULL;
  219. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  220. active = false;
  221. dryWet = 1.0f;
  222. volume = 1.0f;
  223. balanceLeft = -1.0f;
  224. balanceRight = 1.0f;
  225. panning = 0.0f;
  226. ctrlChannel = -1;
  227. #endif
  228. currentProgramIndex = -1;
  229. currentMidiBank = -1;
  230. currentMidiProgram = -1;
  231. for (ParameterItenerator it = parameters.begin2(); it.valid(); it.next())
  232. {
  233. Parameter* const stateParameter(it.getValue(nullptr));
  234. delete stateParameter;
  235. }
  236. for (CustomDataItenerator it = customData.begin2(); it.valid(); it.next())
  237. {
  238. CustomData* const stateCustomData(it.getValue(nullptr));
  239. delete stateCustomData;
  240. }
  241. parameters.clear();
  242. customData.clear();
  243. }
  244. // -----------------------------------------------------------------------
  245. // fillFromXmlElement
  246. bool CarlaStateSave::fillFromXmlElement(const XmlElement* const xmlElement)
  247. {
  248. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  249. clear();
  250. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  251. {
  252. const String& tagName(elem->getTagName());
  253. // ---------------------------------------------------------------
  254. // Info
  255. if (tagName == "Info")
  256. {
  257. for (XmlElement* xmlInfo = elem->getFirstChildElement(); xmlInfo != nullptr; xmlInfo = xmlInfo->getNextElement())
  258. {
  259. const String& tag(xmlInfo->getTagName());
  260. const String text(xmlInfo->getAllSubText().trim());
  261. /**/ if (tag == "Type")
  262. type = xmlSafeStringCharDup(text, false);
  263. else if (tag == "Name")
  264. name = xmlSafeStringCharDup(text, false);
  265. else if (tag == "Label" || tag == "URI" || tag == "Identifier" || tag == "Setup")
  266. label = xmlSafeStringCharDup(text, false);
  267. else if (tag == "Binary" || tag == "Filename")
  268. binary = xmlSafeStringCharDup(text, false);
  269. else if (tag == "UniqueID")
  270. uniqueId = text.getLargeIntValue();
  271. }
  272. }
  273. // ---------------------------------------------------------------
  274. // Data
  275. else if (tagName == "Data")
  276. {
  277. for (XmlElement* xmlData = elem->getFirstChildElement(); xmlData != nullptr; xmlData = xmlData->getNextElement())
  278. {
  279. const String& tag(xmlData->getTagName());
  280. const String text(xmlData->getAllSubText().trim());
  281. // -------------------------------------------------------
  282. // Internal Data
  283. /**/ if (tag == "Options")
  284. {
  285. const int value(text.getHexValue32());
  286. if (value > 0)
  287. options = static_cast<uint>(value);
  288. }
  289. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  290. else if (tag == "Active")
  291. {
  292. active = (text == "Yes");
  293. }
  294. else if (tag == "DryWet")
  295. {
  296. dryWet = carla_fixedValue(0.0f, 1.0f, text.getFloatValue());
  297. }
  298. else if (tag == "Volume")
  299. {
  300. volume = carla_fixedValue(0.0f, 1.27f, text.getFloatValue());
  301. }
  302. else if (tag == "Balance-Left")
  303. {
  304. balanceLeft = carla_fixedValue(-1.0f, 1.0f, text.getFloatValue());
  305. }
  306. else if (tag == "Balance-Right")
  307. {
  308. balanceRight = carla_fixedValue(-1.0f, 1.0f, text.getFloatValue());
  309. }
  310. else if (tag == "Panning")
  311. {
  312. panning = carla_fixedValue(-1.0f, 1.0f, text.getFloatValue());
  313. }
  314. else if (tag == "ControlChannel")
  315. {
  316. if (! text.startsWithIgnoreCase("n"))
  317. {
  318. const int value(text.getIntValue());
  319. if (value >= 1 && value <= MAX_MIDI_CHANNELS)
  320. ctrlChannel = static_cast<int8_t>(value-1);
  321. }
  322. }
  323. #endif
  324. // -------------------------------------------------------
  325. // Program (current)
  326. else if (tag == "CurrentProgramIndex")
  327. {
  328. const int value(text.getIntValue());
  329. if (value >= 1)
  330. currentProgramIndex = value-1;
  331. }
  332. else if (tag == "CurrentProgramName")
  333. {
  334. currentProgramName = xmlSafeStringCharDup(text, false);
  335. }
  336. // -------------------------------------------------------
  337. // Midi Program (current)
  338. else if (tag == "CurrentMidiBank")
  339. {
  340. const int value(text.getIntValue());
  341. if (value >= 1)
  342. currentMidiBank = value-1;
  343. }
  344. else if (tag == "CurrentMidiProgram")
  345. {
  346. const int value(text.getIntValue());
  347. if (value >= 1)
  348. currentMidiProgram = value-1;
  349. }
  350. // -------------------------------------------------------
  351. // Parameters
  352. else if (tag == "Parameter")
  353. {
  354. Parameter* const stateParameter(new Parameter());
  355. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  356. bool hasMappedMinimum = false, hasMappedMaximum = false;
  357. #endif
  358. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  359. {
  360. const String& pTag(xmlSubData->getTagName());
  361. const String pText(xmlSubData->getAllSubText().trim());
  362. /**/ if (pTag == "Index")
  363. {
  364. const int index(pText.getIntValue());
  365. if (index >= 0)
  366. stateParameter->index = index;
  367. }
  368. else if (pTag == "Name")
  369. {
  370. stateParameter->name = xmlSafeStringCharDup(pText, false);
  371. }
  372. else if (pTag == "Symbol" || pTag == "Identifier")
  373. {
  374. stateParameter->symbol = xmlSafeStringCharDup(pText, false);
  375. }
  376. else if (pTag == "Value")
  377. {
  378. stateParameter->dummy = false;
  379. stateParameter->value = pText.getFloatValue();
  380. }
  381. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  382. else if (pTag == "MidiChannel")
  383. {
  384. const int channel(pText.getIntValue());
  385. if (channel >= 1 && channel <= MAX_MIDI_CHANNELS)
  386. stateParameter->midiChannel = static_cast<uint8_t>(channel-1);
  387. }
  388. else if (pTag == "MidiCC")
  389. {
  390. const int cc(pText.getIntValue());
  391. if (cc > 0 && cc < MAX_MIDI_CONTROL)
  392. stateParameter->mappedControlIndex = static_cast<int16_t>(cc);
  393. }
  394. else if (pTag == "MappedControlIndex")
  395. {
  396. const int ctrl(pText.getIntValue());
  397. if (ctrl > CONTROL_INDEX_NONE && ctrl <= CONTROL_INDEX_MAX_ALLOWED)
  398. if (ctrl != CONTROL_INDEX_MIDI_LEARN)
  399. stateParameter->mappedControlIndex = static_cast<int16_t>(ctrl);
  400. }
  401. else if (pTag == "MappedMinimum")
  402. {
  403. hasMappedMinimum = true;
  404. stateParameter->mappedMinimum = pText.getFloatValue();
  405. }
  406. else if (pTag == "MappedMaximum")
  407. {
  408. hasMappedMaximum = true;
  409. stateParameter->mappedMaximum = pText.getFloatValue();
  410. }
  411. #endif
  412. }
  413. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  414. if (hasMappedMinimum && hasMappedMaximum)
  415. stateParameter->mappedRangeValid = true;
  416. #endif
  417. parameters.append(stateParameter);
  418. }
  419. // -------------------------------------------------------
  420. // Custom Data
  421. else if (tag == "CustomData")
  422. {
  423. CustomData* const stateCustomData(new CustomData());
  424. // find type first
  425. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  426. {
  427. const String& cTag(xmlSubData->getTagName());
  428. if (cTag != "Type")
  429. continue;
  430. stateCustomData->type = xmlSafeStringCharDup(xmlSubData->getAllSubText().trim(), false);
  431. break;
  432. }
  433. if (stateCustomData->type == nullptr || stateCustomData->type[0] == '\0')
  434. {
  435. carla_stderr("Reading CustomData type failed");
  436. delete stateCustomData;
  437. continue;
  438. }
  439. // now fill in key and value, knowing what the type is
  440. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  441. {
  442. const String& cTag(xmlSubData->getTagName());
  443. String cText(xmlSubData->getAllSubText());
  444. /**/ if (cTag == "Key")
  445. {
  446. stateCustomData->key = xmlSafeStringCharDup(cText.trim(), false);
  447. }
  448. else if (cTag == "Value")
  449. {
  450. // save operation adds a newline and newline+space around the string in some cases
  451. const int len = cText.length();
  452. if (std::strcmp(stateCustomData->type, CUSTOM_DATA_TYPE_CHUNK) == 0 || len >= 128+6)
  453. {
  454. CARLA_SAFE_ASSERT_CONTINUE(len >= 6);
  455. cText = cText.substring(1, len - 5);
  456. }
  457. stateCustomData->value = xmlSafeStringCharDup(cText, false);
  458. }
  459. }
  460. if (stateCustomData->isValid())
  461. {
  462. customData.append(stateCustomData);
  463. }
  464. else
  465. {
  466. carla_stderr("Reading CustomData property failed, missing data");
  467. delete stateCustomData;
  468. }
  469. }
  470. // -------------------------------------------------------
  471. // Chunk
  472. else if (tag == "Chunk")
  473. {
  474. chunk = carla_strdup(text.toRawUTF8());
  475. }
  476. }
  477. }
  478. }
  479. return true;
  480. }
  481. // -----------------------------------------------------------------------
  482. // fillXmlStringFromStateSave
  483. void CarlaStateSave::dumpToMemoryStream(MemoryOutputStream& content) const
  484. {
  485. const PluginType pluginType = getPluginTypeFromString(type);
  486. {
  487. MemoryOutputStream infoXml;
  488. infoXml << " <Info>\n";
  489. infoXml << " <Type>" << String(type != nullptr ? type : "") << "</Type>\n";
  490. infoXml << " <Name>" << xmlSafeString(name, true) << "</Name>\n";
  491. switch (pluginType)
  492. {
  493. case PLUGIN_NONE:
  494. break;
  495. case PLUGIN_INTERNAL:
  496. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  497. break;
  498. case PLUGIN_LADSPA:
  499. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  500. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  501. infoXml << " <UniqueID>" << water::int64(uniqueId) << "</UniqueID>\n";
  502. break;
  503. case PLUGIN_DSSI:
  504. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  505. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  506. break;
  507. case PLUGIN_LV2:
  508. infoXml << " <URI>" << xmlSafeString(label, true) << "</URI>\n";
  509. break;
  510. case PLUGIN_VST2:
  511. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  512. infoXml << " <UniqueID>" << water::int64(uniqueId) << "</UniqueID>\n";
  513. break;
  514. case PLUGIN_VST3:
  515. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  516. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  517. break;
  518. case PLUGIN_AU:
  519. infoXml << " <Identifier>" << xmlSafeString(label, true) << "</Identifier>\n";
  520. break;
  521. case PLUGIN_CLAP:
  522. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  523. infoXml << " <Identifier>" << xmlSafeString(label, true) << "</Identifier>\n";
  524. break;
  525. case PLUGIN_DLS:
  526. case PLUGIN_GIG:
  527. case PLUGIN_SF2:
  528. case PLUGIN_JSFX:
  529. infoXml << " <Filename>" << xmlSafeString(binary, true) << "</Filename>\n";
  530. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  531. break;
  532. case PLUGIN_SFZ:
  533. infoXml << " <Filename>" << xmlSafeString(binary, true) << "</Filename>\n";
  534. break;
  535. case PLUGIN_JACK:
  536. infoXml << " <Filename>" << xmlSafeString(binary, true) << "</Filename>\n";
  537. infoXml << " <Setup>" << xmlSafeString(label, true) << "</Setup>\n";
  538. break;
  539. case PLUGIN_TYPE_COUNT:
  540. break;
  541. }
  542. infoXml << " </Info>\n\n";
  543. content << infoXml;
  544. }
  545. content << " <Data>\n";
  546. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  547. {
  548. MemoryOutputStream dataXml;
  549. dataXml << " <Active>" << (active ? "Yes" : "No") << "</Active>\n";
  550. if (carla_isNotEqual(dryWet, 1.0f))
  551. dataXml << " <DryWet>" << String(dryWet, 7) << "</DryWet>\n";
  552. if (carla_isNotEqual(volume, 1.0f))
  553. dataXml << " <Volume>" << String(volume, 7) << "</Volume>\n";
  554. if (carla_isNotEqual(balanceLeft, -1.0f))
  555. dataXml << " <Balance-Left>" << String(balanceLeft, 7) << "</Balance-Left>\n";
  556. if (carla_isNotEqual(balanceRight, 1.0f))
  557. dataXml << " <Balance-Right>" << String(balanceRight, 7) << "</Balance-Right>\n";
  558. if (carla_isNotEqual(panning, 0.0f))
  559. dataXml << " <Panning>" << String(panning, 7) << "</Panning>\n";
  560. if (ctrlChannel < 0)
  561. dataXml << " <ControlChannel>N</ControlChannel>\n";
  562. else
  563. dataXml << " <ControlChannel>" << int(ctrlChannel+1) << "</ControlChannel>\n";
  564. dataXml << " <Options>0x" << String::toHexString(static_cast<int>(options)) << "</Options>\n";
  565. content << dataXml;
  566. }
  567. #endif
  568. for (ParameterItenerator it = parameters.begin2(); it.valid(); it.next())
  569. {
  570. Parameter* const stateParameter(it.getValue(nullptr));
  571. CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr);
  572. MemoryOutputStream parameterXml;
  573. parameterXml << "\n";
  574. parameterXml << " <Parameter>\n";
  575. parameterXml << " <Index>" << String(stateParameter->index) << "</Index>\n";
  576. parameterXml << " <Name>" << xmlSafeString(stateParameter->name, true) << "</Name>\n";
  577. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  578. {
  579. if (pluginType == PLUGIN_CLAP)
  580. parameterXml << " <Identifier>" << xmlSafeString(stateParameter->symbol, true) << "</Identifier>\n";
  581. else
  582. parameterXml << " <Symbol>" << xmlSafeString(stateParameter->symbol, true) << "</Symbol>\n";
  583. }
  584. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  585. if (stateParameter->mappedControlIndex > CONTROL_INDEX_NONE && stateParameter->mappedControlIndex <= CONTROL_INDEX_MAX_ALLOWED)
  586. {
  587. parameterXml << " <MidiChannel>" << stateParameter->midiChannel+1 << "</MidiChannel>\n";
  588. parameterXml << " <MappedControlIndex>" << stateParameter->mappedControlIndex << "</MappedControlIndex>\n";
  589. if (stateParameter->mappedRangeValid)
  590. {
  591. parameterXml << " <MappedMinimum>" << String(stateParameter->mappedMinimum, 15) << "</MappedMinimum>\n";
  592. parameterXml << " <MappedMaximum>" << String(stateParameter->mappedMaximum, 15) << "</MappedMaximum>\n";
  593. }
  594. // backwards compatibility for older carla versions
  595. if (stateParameter->mappedControlIndex > 0 && stateParameter->mappedControlIndex < MAX_MIDI_CONTROL)
  596. parameterXml << " <MidiCC>" << stateParameter->mappedControlIndex << "</MidiCC>\n";
  597. }
  598. #endif
  599. if (! stateParameter->dummy)
  600. parameterXml << " <Value>" << String(stateParameter->value, 15) << "</Value>\n";
  601. parameterXml << " </Parameter>\n";
  602. content << parameterXml;
  603. }
  604. if (currentProgramIndex >= 0 && currentProgramName != nullptr && currentProgramName[0] != '\0')
  605. {
  606. // ignore 'default' program
  607. if (currentProgramIndex > 0 || ! String(currentProgramName).equalsIgnoreCase("default"))
  608. {
  609. MemoryOutputStream programXml;
  610. programXml << "\n";
  611. programXml << " <CurrentProgramIndex>" << currentProgramIndex+1 << "</CurrentProgramIndex>\n";
  612. programXml << " <CurrentProgramName>" << xmlSafeString(currentProgramName, true) << "</CurrentProgramName>\n";
  613. content << programXml;
  614. }
  615. }
  616. if (currentMidiBank >= 0 && currentMidiProgram >= 0)
  617. {
  618. MemoryOutputStream midiProgramXml;
  619. midiProgramXml << "\n";
  620. midiProgramXml << " <CurrentMidiBank>" << currentMidiBank+1 << "</CurrentMidiBank>\n";
  621. midiProgramXml << " <CurrentMidiProgram>" << currentMidiProgram+1 << "</CurrentMidiProgram>\n";
  622. content << midiProgramXml;
  623. }
  624. for (CustomDataItenerator it = customData.begin2(); it.valid(); it.next())
  625. {
  626. CustomData* const stateCustomData(it.getValue(nullptr));
  627. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  628. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  629. MemoryOutputStream customDataXml;
  630. customDataXml << "\n";
  631. customDataXml << " <CustomData>\n";
  632. customDataXml << " <Type>" << xmlSafeString(stateCustomData->type, true) << "</Type>\n";
  633. customDataXml << " <Key>" << xmlSafeString(stateCustomData->key, true) << "</Key>\n";
  634. if (std::strcmp(stateCustomData->type, CUSTOM_DATA_TYPE_CHUNK) == 0 || std::strlen(stateCustomData->value) >= 128)
  635. {
  636. customDataXml << " <Value>\n";
  637. customDataXml << xmlSafeStringFast(stateCustomData->value, true);
  638. customDataXml << "\n </Value>\n";
  639. }
  640. else
  641. {
  642. customDataXml << " <Value>";
  643. customDataXml << xmlSafeStringFast(stateCustomData->value, true);
  644. customDataXml << "</Value>\n";
  645. }
  646. customDataXml << " </CustomData>\n";
  647. content << customDataXml;
  648. }
  649. if (chunk != nullptr && chunk[0] != '\0')
  650. {
  651. MemoryOutputStream chunkXml, chunkSplt;
  652. getNewLineSplittedString(chunkSplt, chunk);
  653. chunkXml << "\n <Chunk>\n";
  654. chunkXml << chunkSplt;
  655. chunkXml << "\n </Chunk>\n";
  656. content << chunkXml;
  657. }
  658. content << " </Data>\n";
  659. }
  660. // -----------------------------------------------------------------------
  661. CARLA_BACKEND_END_NAMESPACE