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.

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