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.

689 lines
23KB

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