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.

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