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.

603 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. options = text.getHexValue32();
  262. }
  263. // -------------------------------------------------------
  264. // Program (current)
  265. else if (tag.equalsIgnoreCase("currentprogramindex") || tag.equalsIgnoreCase("current-program-index"))
  266. {
  267. const int value(text.getIntValue());
  268. if (value >= 1)
  269. currentProgramIndex = value-1;
  270. }
  271. else if (tag.equalsIgnoreCase("currentprogramname") || tag.equalsIgnoreCase("current-program-name"))
  272. {
  273. currentProgramName = xmlSafeStringCharDup(text, false);
  274. }
  275. // -------------------------------------------------------
  276. // Midi Program (current)
  277. else if (tag.equalsIgnoreCase("currentmidibank") || tag.equalsIgnoreCase("current-midi-bank"))
  278. {
  279. const int value(text.getIntValue());
  280. if (value >= 1)
  281. currentMidiBank = value-1;
  282. }
  283. else if (tag.equalsIgnoreCase("currentmidiprogram") || tag.equalsIgnoreCase("current-midi-program"))
  284. {
  285. const int value(text.getIntValue());
  286. if (value >= 1)
  287. currentMidiProgram = value-1;
  288. }
  289. // -------------------------------------------------------
  290. // Parameters
  291. else if (tag.equalsIgnoreCase("parameter"))
  292. {
  293. StateParameter* const stateParameter(new StateParameter());
  294. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  295. {
  296. const String& pTag(xmlSubData->getTagName());
  297. const String pText(xmlSubData->getAllSubText().trim());
  298. if (pTag.equalsIgnoreCase("index"))
  299. {
  300. const int index(pText.getIntValue());
  301. if (index >= 0)
  302. stateParameter->index = index;
  303. }
  304. else if (pTag.equalsIgnoreCase("name"))
  305. {
  306. stateParameter->name = xmlSafeStringCharDup(pText, false);
  307. }
  308. else if (pTag.equalsIgnoreCase("symbol"))
  309. {
  310. stateParameter->symbol = xmlSafeStringCharDup(pText, false);
  311. }
  312. else if (pTag.equalsIgnoreCase("value"))
  313. {
  314. stateParameter->value = pText.getFloatValue();
  315. }
  316. else if (pTag.equalsIgnoreCase("midichannel") || pTag.equalsIgnoreCase("midi-channel"))
  317. {
  318. const int channel(pText.getIntValue());
  319. if (channel >= 1 && channel <= MAX_MIDI_CHANNELS)
  320. stateParameter->midiChannel = static_cast<uint8_t>(channel-1);
  321. }
  322. else if (pTag.equalsIgnoreCase("midicc") || pTag.equalsIgnoreCase("midi-cc"))
  323. {
  324. const int cc(pText.getIntValue());
  325. if (cc >= 1 && cc < 0x5F)
  326. stateParameter->midiCC = static_cast<int16_t>(cc);
  327. }
  328. }
  329. parameters.append(stateParameter);
  330. }
  331. // -------------------------------------------------------
  332. // Custom Data
  333. else if (tag.equalsIgnoreCase("customdata") || tag.equalsIgnoreCase("custom-data"))
  334. {
  335. StateCustomData* const stateCustomData(new StateCustomData());
  336. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  337. {
  338. const String& cTag(xmlSubData->getTagName());
  339. const String cText(xmlSubData->getAllSubText().trim());
  340. if (cTag.equalsIgnoreCase("type"))
  341. stateCustomData->type = xmlSafeStringCharDup(cText, false);
  342. else if (cTag.equalsIgnoreCase("key"))
  343. stateCustomData->key = xmlSafeStringCharDup(cText, false);
  344. else if (cTag.equalsIgnoreCase("value"))
  345. stateCustomData->value = xmlSafeStringCharDup(cText, false);
  346. }
  347. customData.append(stateCustomData);
  348. }
  349. // -------------------------------------------------------
  350. // Chunk
  351. else if (tag.equalsIgnoreCase("chunk"))
  352. {
  353. const String nText(text.replace("\n", ""));
  354. chunk = xmlSafeStringCharDup(nText, false);
  355. }
  356. }
  357. }
  358. }
  359. return true;
  360. }
  361. // -----------------------------------------------------------------------
  362. // fillXmlStringFromStateSave
  363. String StateSave::toString() const
  364. {
  365. String content;
  366. {
  367. String infoXml(" <Info>\n");
  368. infoXml << " <Type>" << String(type != nullptr ? type : "") << "</Type>\n";
  369. infoXml << " <Name>" << xmlSafeString(name, true) << "</Name>\n";
  370. switch (getPluginTypeFromString(type))
  371. {
  372. case PLUGIN_NONE:
  373. break;
  374. case PLUGIN_INTERNAL:
  375. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  376. break;
  377. case PLUGIN_LADSPA:
  378. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  379. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  380. infoXml << " <UniqueID>" << uniqueId << "</UniqueID>\n";
  381. break;
  382. case PLUGIN_DSSI:
  383. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  384. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  385. break;
  386. case PLUGIN_LV2:
  387. infoXml << " <Bundle>" << xmlSafeString(binary, true) << "</Bundle>\n";
  388. infoXml << " <URI>" << xmlSafeString(label, true) << "</URI>\n";
  389. break;
  390. case PLUGIN_VST:
  391. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  392. infoXml << " <UniqueID>" << uniqueId << "</UniqueID>\n";
  393. break;
  394. case PLUGIN_VST3:
  395. // TODO?
  396. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  397. infoXml << " <UniqueID>" << uniqueId << "</UniqueID>\n";
  398. break;
  399. case PLUGIN_AU:
  400. // TODO?
  401. infoXml << " <Binary>" << xmlSafeString(binary, true) << "</Binary>\n";
  402. infoXml << " <UniqueID>" << uniqueId << "</UniqueID>\n";
  403. break;
  404. case PLUGIN_GIG:
  405. case PLUGIN_SF2:
  406. infoXml << " <Filename>" << xmlSafeString(binary, true) << "</Filename>\n";
  407. infoXml << " <Label>" << xmlSafeString(label, true) << "</Label>\n";
  408. break;
  409. case PLUGIN_SFZ:
  410. infoXml << " <Filename>" << xmlSafeString(binary, true) << "</Filename>\n";
  411. break;
  412. }
  413. infoXml << " </Info>\n\n";
  414. content << infoXml;
  415. }
  416. content << " <Data>\n";
  417. {
  418. String dataXml;
  419. dataXml << " <Active>" << (active ? "Yes" : "No") << "</Active>\n";
  420. if (dryWet != 1.0f)
  421. dataXml << " <DryWet>" << String(dryWet, 7) << "</DryWet>\n";
  422. if (volume != 1.0f)
  423. dataXml << " <Volume>" << String(volume, 7) << "</Volume>\n";
  424. if (balanceLeft != -1.0f)
  425. dataXml << " <Balance-Left>" << String(balanceLeft, 7) << "</Balance-Left>\n";
  426. if (balanceRight != 1.0f)
  427. dataXml << " <Balance-Right>" << String(balanceRight, 7) << "</Balance-Right>\n";
  428. if (panning != 0.0f)
  429. dataXml << " <Panning>" << String(panning, 7) << "</Panning>\n";
  430. if (ctrlChannel < 0)
  431. dataXml << " <ControlChannel>N</ControlChannel>\n";
  432. else
  433. dataXml << " <ControlChannel>" << int(ctrlChannel+1) << "</ControlChannel>\n";
  434. dataXml << " <Options>0x" << String::toHexString(static_cast<int>(options)) << "</Options>\n";
  435. content << dataXml;
  436. }
  437. for (StateParameterItenerator it = parameters.begin(); it.valid(); it.next())
  438. {
  439. StateParameter* const stateParameter(it.getValue());
  440. String parameterXml("\n"" <Parameter>\n");
  441. parameterXml << " <Index>" << String(stateParameter->index) << "</Index>\n";
  442. parameterXml << " <Name>" << xmlSafeString(stateParameter->name, true) << "</Name>\n";
  443. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  444. parameterXml << " <Symbol>" << xmlSafeString(stateParameter->symbol, true) << "</Symbol>\n";
  445. if (stateParameter->isInput)
  446. parameterXml << " <Value>" << String(stateParameter->value, 15) << "</Value>\n";
  447. if (stateParameter->midiCC > 0)
  448. {
  449. parameterXml << " <MidiCC>" << stateParameter->midiCC << "</MidiCC>\n";
  450. parameterXml << " <MidiChannel>" << stateParameter->midiChannel+1 << "</MidiChannel>\n";
  451. }
  452. parameterXml << " </Parameter>\n";
  453. content << parameterXml;
  454. }
  455. if (currentProgramIndex >= 0 && currentProgramName != nullptr && currentProgramName[0] != '\0')
  456. {
  457. // ignore 'default' program
  458. if (currentProgramIndex > 0 || ! String(currentProgramName).equalsIgnoreCase("default"))
  459. {
  460. String programXml("\n");
  461. programXml << " <CurrentProgramIndex>" << currentProgramIndex+1 << "</CurrentProgramIndex>\n";
  462. programXml << " <CurrentProgramName>" << xmlSafeString(currentProgramName, true) << "</CurrentProgramName>\n";
  463. content << programXml;
  464. }
  465. }
  466. if (currentMidiBank >= 0 && currentMidiProgram >= 0)
  467. {
  468. String midiProgramXml("\n");
  469. midiProgramXml << " <CurrentMidiBank>" << currentMidiBank+1 << "</CurrentMidiBank>\n";
  470. midiProgramXml << " <CurrentMidiProgram>" << currentMidiProgram+1 << "</CurrentMidiProgram>\n";
  471. content << midiProgramXml;
  472. }
  473. for (StateCustomDataItenerator it = customData.begin(); it.valid(); it.next())
  474. {
  475. StateCustomData* const stateCustomData(it.getValue());
  476. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->type != nullptr && stateCustomData->type[0] != '\0');
  477. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->key != nullptr && stateCustomData->key[0] != '\0');
  478. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->value != nullptr);
  479. String customDataXml("\n"" <CustomData>\n");
  480. customDataXml << " <Type>" << xmlSafeString(stateCustomData->type, true) << "</Type>\n";
  481. customDataXml << " <Key>" << xmlSafeString(stateCustomData->key, true) << "</Key>\n";
  482. if (std::strcmp(stateCustomData->type, CUSTOM_DATA_TYPE_CHUNK) == 0 || std::strlen(stateCustomData->value) >= 128)
  483. {
  484. customDataXml << " <Value>\n";
  485. customDataXml << xmlSafeString(stateCustomData->value, true);
  486. customDataXml << "\n </Value>\n";
  487. }
  488. else
  489. {
  490. customDataXml << " <Value>";
  491. customDataXml << xmlSafeString(stateCustomData->value, true);
  492. customDataXml << "</Value>\n";
  493. }
  494. customDataXml << " </CustomData>\n";
  495. content << customDataXml;
  496. }
  497. if (chunk != nullptr && chunk[0] != '\0')
  498. {
  499. String chunkXml("\n"" <Chunk>\n");
  500. chunkXml << getNewLineSplittedString(chunk) << "\n </Chunk>\n";
  501. content << chunkXml;
  502. }
  503. content << " </Data>\n";
  504. return content;
  505. }
  506. // -----------------------------------------------------------------------
  507. CARLA_BACKEND_END_NAMESPACE