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.

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