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.

624 lines
21KB

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