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.

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