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.

589 lines
21KB

  1. /*
  2. * Carla State utils
  3. * Copyright (C) 2012-2013 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. #ifndef CARLA_STATE_UTILS_HPP_INCLUDED
  18. #define CARLA_STATE_UTILS_HPP_INCLUDED
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "List.hpp"
  22. #ifdef USE_JUCE
  23. # include "juce_core.h"
  24. #endif
  25. CARLA_BACKEND_START_NAMESPACE
  26. // -----------------------------------------------------------------------
  27. struct StateParameter {
  28. uint32_t index;
  29. const char* name;
  30. const char* symbol;
  31. float value;
  32. uint8_t midiChannel;
  33. int16_t midiCC;
  34. StateParameter() noexcept
  35. : index(0),
  36. name(nullptr),
  37. symbol(nullptr),
  38. value(0.0f),
  39. midiChannel(0),
  40. midiCC(-1) {}
  41. ~StateParameter()
  42. {
  43. if (name != nullptr)
  44. {
  45. delete[] name;
  46. name = nullptr;
  47. }
  48. if (symbol != nullptr)
  49. {
  50. delete[] symbol;
  51. symbol = nullptr;
  52. }
  53. }
  54. CARLA_DECLARE_NON_COPY_STRUCT(StateParameter)
  55. };
  56. struct StateCustomData {
  57. const char* type;
  58. const char* key;
  59. const char* value;
  60. StateCustomData() noexcept
  61. : type(nullptr),
  62. key(nullptr),
  63. value(nullptr) {}
  64. ~StateCustomData()
  65. {
  66. if (type != nullptr)
  67. {
  68. delete[] type;
  69. type = nullptr;
  70. }
  71. if (key != nullptr)
  72. {
  73. delete[] key;
  74. key = nullptr;
  75. }
  76. if (value != nullptr)
  77. {
  78. delete[] value;
  79. value = nullptr;
  80. }
  81. }
  82. CARLA_DECLARE_NON_COPY_STRUCT(StateCustomData)
  83. };
  84. typedef List<StateParameter*> StateParameterList;
  85. typedef List<StateCustomData*> StateCustomDataList;
  86. typedef List<StateParameter*>::Itenerator StateParameterItenerator;
  87. typedef List<StateCustomData*>::Itenerator StateCustomDataItenerator;
  88. struct SaveState {
  89. const char* type;
  90. const char* name;
  91. const char* label;
  92. const char* binary;
  93. long uniqueID;
  94. bool active;
  95. float dryWet;
  96. float volume;
  97. float balanceLeft;
  98. float balanceRight;
  99. float panning;
  100. int8_t ctrlChannel;
  101. int32_t currentProgramIndex;
  102. const char* currentProgramName;
  103. int32_t currentMidiBank;
  104. int32_t currentMidiProgram;
  105. const char* chunk;
  106. StateParameterList parameters;
  107. StateCustomDataList customData;
  108. SaveState() noexcept
  109. : type(nullptr),
  110. name(nullptr),
  111. label(nullptr),
  112. binary(nullptr),
  113. uniqueID(0),
  114. active(false),
  115. dryWet(1.0f),
  116. volume(1.0f),
  117. balanceLeft(-1.0f),
  118. balanceRight(1.0f),
  119. panning(0.0f),
  120. ctrlChannel(-1),
  121. currentProgramIndex(-1),
  122. currentProgramName(nullptr),
  123. currentMidiBank(-1),
  124. currentMidiProgram(-1),
  125. chunk(nullptr) {}
  126. ~SaveState()
  127. {
  128. reset();
  129. }
  130. void reset()
  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. currentProgramIndex = -1;
  171. currentMidiBank = -1;
  172. currentMidiProgram = -1;
  173. for (StateParameterItenerator it = parameters.begin(); it.valid(); it.next())
  174. {
  175. StateParameter* const stateParameter(*it);
  176. delete stateParameter;
  177. }
  178. for (StateCustomDataItenerator it = customData.begin(); it.valid(); it.next())
  179. {
  180. StateCustomData* const stateCustomData(*it);
  181. delete stateCustomData;
  182. }
  183. parameters.clear();
  184. customData.clear();
  185. }
  186. CARLA_DECLARE_NON_COPY_STRUCT(SaveState)
  187. };
  188. #ifdef USE_JUCE
  189. // -----------------------------------------------------------------------
  190. static inline
  191. juce::String xmlSafeString(const juce::String& string, const bool toXml)
  192. {
  193. juce::String newString(string);
  194. if (toXml)
  195. return newString.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;").replace("'","&apos;").replace("\"","&quot;");
  196. else
  197. return newString.replace("&amp;","&").replace("&lt;","<").replace("&gt;",">").replace("&apos;","'").replace("&quot;","\"");
  198. }
  199. static inline
  200. const char* xmlSafeStringCharDup(const juce::String& string, const bool toXml)
  201. {
  202. return carla_strdup(xmlSafeString(string, toXml).toRawUTF8());
  203. }
  204. // -----------------------------------------------------------------------
  205. static inline
  206. void fillSaveStateFromXmlElement(SaveState& saveState, const juce::XmlElement* const xmlElement)
  207. {
  208. using namespace juce;
  209. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  210. {
  211. // ---------------------------------------------------------------
  212. // Info
  213. if (elem->getTagName().equalsIgnoreCase("info"))
  214. {
  215. for (XmlElement* xmlInfo = elem->getFirstChildElement(); xmlInfo != nullptr; xmlInfo = xmlInfo->getNextElement())
  216. {
  217. const String& tag(xmlInfo->getTagName());
  218. const String text(xmlInfo->getAllSubText().trim());
  219. if (tag.equalsIgnoreCase("type"))
  220. saveState.type = xmlSafeStringCharDup(text, false);
  221. else if (tag.equalsIgnoreCase("name"))
  222. saveState.name = xmlSafeStringCharDup(text, false);
  223. else if (tag.equalsIgnoreCase("label") || tag.equalsIgnoreCase("uri"))
  224. saveState.label = xmlSafeStringCharDup(text, false);
  225. else if (tag.equalsIgnoreCase("binary") || tag.equalsIgnoreCase("filename"))
  226. saveState.binary = xmlSafeStringCharDup(text, false);
  227. else if (tag.equalsIgnoreCase("uniqueid"))
  228. saveState.uniqueID = text.getLargeIntValue();
  229. }
  230. }
  231. // ---------------------------------------------------------------
  232. // Data
  233. else if (elem->getTagName().equalsIgnoreCase("data"))
  234. {
  235. for (XmlElement* xmlData = elem->getFirstChildElement(); xmlData != nullptr; xmlData = xmlData->getNextElement())
  236. {
  237. const String& tag(xmlData->getTagName());
  238. const String text(xmlData->getAllSubText().trim());
  239. // -------------------------------------------------------
  240. // Internal Data
  241. if (tag.equalsIgnoreCase("active"))
  242. {
  243. saveState.active = (text.equalsIgnoreCase("yes"));
  244. }
  245. else if (tag.equalsIgnoreCase("drywet"))
  246. {
  247. saveState.dryWet = carla_fixValue(0.0f, 1.0f, text.getFloatValue());
  248. }
  249. else if (tag.equalsIgnoreCase("volume"))
  250. {
  251. saveState.volume = carla_fixValue(0.0f, 1.27f, text.getFloatValue());
  252. }
  253. else if (tag.equalsIgnoreCase("balanceleft") || tag.equalsIgnoreCase("balance-left"))
  254. {
  255. saveState.balanceLeft = carla_fixValue(-1.0f, 1.0f, text.getFloatValue());
  256. }
  257. else if (tag.equalsIgnoreCase("balanceright") || tag.equalsIgnoreCase("balance-right"))
  258. {
  259. saveState.balanceRight = carla_fixValue(-1.0f, 1.0f, text.getFloatValue());
  260. }
  261. else if (tag.equalsIgnoreCase("panning"))
  262. {
  263. saveState.panning = carla_fixValue(-1.0f, 1.0f, text.getFloatValue());
  264. }
  265. else if (tag.equalsIgnoreCase("controlchannel") || tag.equalsIgnoreCase("control-channel"))
  266. {
  267. const int value(text.getIntValue());
  268. if (value >= 1 && value <= MAX_MIDI_CHANNELS)
  269. saveState.ctrlChannel = static_cast<int8_t>(value-1);
  270. }
  271. // -------------------------------------------------------
  272. // Program (current)
  273. else if (tag.equalsIgnoreCase("currentprogramindex") || tag.equalsIgnoreCase("current-program-index"))
  274. {
  275. const int value(text.getIntValue());
  276. if (value >= 1)
  277. saveState.currentProgramIndex = value-1;
  278. }
  279. else if (tag.equalsIgnoreCase("currentprogramname") || tag.equalsIgnoreCase("current-program-name"))
  280. {
  281. saveState.currentProgramName = xmlSafeStringCharDup(text, false);
  282. }
  283. // -------------------------------------------------------
  284. // Midi Program (current)
  285. else if (tag.equalsIgnoreCase("currentmidibank") || tag.equalsIgnoreCase("current-midi-bank"))
  286. {
  287. const int value(text.getIntValue());
  288. if (value >= 1)
  289. saveState.currentMidiBank = value-1;
  290. }
  291. else if (tag.equalsIgnoreCase("currentmidiprogram") || tag.equalsIgnoreCase("current-midi-program"))
  292. {
  293. const int value(text.getIntValue());
  294. if (value >= 1)
  295. saveState.currentMidiProgram = value-1;
  296. }
  297. // -------------------------------------------------------
  298. // Parameters
  299. else if (tag.equalsIgnoreCase("parameter"))
  300. {
  301. StateParameter* const stateParameter(new StateParameter());
  302. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  303. {
  304. const String& pTag(xmlSubData->getTagName());
  305. const String pText(xmlSubData->getAllSubText().trim());
  306. if (pTag.equalsIgnoreCase("index"))
  307. {
  308. const int index(pText.getIntValue());
  309. if (index >= 0)
  310. stateParameter->index = static_cast<uint32_t>(index);
  311. }
  312. else if (pTag.equalsIgnoreCase("name"))
  313. {
  314. stateParameter->name = xmlSafeStringCharDup(pText, false);
  315. }
  316. else if (pTag.equalsIgnoreCase("symbol"))
  317. {
  318. stateParameter->symbol = xmlSafeStringCharDup(pText, false);
  319. }
  320. else if (pTag.equalsIgnoreCase("value"))
  321. {
  322. stateParameter->value = pText.getFloatValue();
  323. }
  324. else if (pTag.equalsIgnoreCase("midichannel") || pTag.equalsIgnoreCase("midi-channel"))
  325. {
  326. const int channel(pText.getIntValue());
  327. if (channel >= 1 && channel <= MAX_MIDI_CHANNELS)
  328. stateParameter->midiChannel = static_cast<uint8_t>(channel-1);
  329. }
  330. else if (pTag.equalsIgnoreCase("midicc") || pTag.equalsIgnoreCase("midi-cc"))
  331. {
  332. const int cc(pText.getIntValue());
  333. if (cc >= 1 && cc < 0x5F)
  334. stateParameter->midiCC = static_cast<int16_t>(cc);
  335. }
  336. }
  337. saveState.parameters.append(stateParameter);
  338. }
  339. // -------------------------------------------------------
  340. // Custom Data
  341. else if (tag.equalsIgnoreCase("customdata") || tag.equalsIgnoreCase("custom-data"))
  342. {
  343. StateCustomData* const stateCustomData(new StateCustomData());
  344. for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement())
  345. {
  346. const String& cTag(xmlSubData->getTagName());
  347. const String cText(xmlSubData->getAllSubText().trim());
  348. if (cTag.equalsIgnoreCase("type"))
  349. stateCustomData->type = xmlSafeStringCharDup(cText, false);
  350. else if (cTag.equalsIgnoreCase("key"))
  351. stateCustomData->key = xmlSafeStringCharDup(cText, false);
  352. else if (cTag.equalsIgnoreCase("value"))
  353. stateCustomData->value = xmlSafeStringCharDup(cText, false);
  354. }
  355. saveState.customData.append(stateCustomData);
  356. }
  357. // -------------------------------------------------------
  358. // Chunk
  359. else if (tag.equalsIgnoreCase("chunk"))
  360. {
  361. saveState.chunk = xmlSafeStringCharDup(text, false);
  362. }
  363. }
  364. }
  365. }
  366. }
  367. // -----------------------------------------------------------------------
  368. static inline
  369. void fillXmlStringFromSaveState(juce::String& content, const SaveState& saveState)
  370. {
  371. using namespace juce;
  372. {
  373. String info(" <Info>\n");
  374. info << " <Type>" << saveState.type << "</Type>\n";
  375. info << " <Name>" << xmlSafeString(saveState.name, true) << "</Name>\n";
  376. switch (getPluginTypeFromString(saveState.type))
  377. {
  378. case PLUGIN_NONE:
  379. break;
  380. case PLUGIN_INTERNAL:
  381. info << " <Label>" << xmlSafeString(saveState.label, true) << "</Label>\n";
  382. break;
  383. case PLUGIN_LADSPA:
  384. info << " <Binary>" << xmlSafeString(saveState.binary, true) << "</Binary>\n";
  385. info << " <Label>" << xmlSafeString(saveState.label, true) << "</Label>\n";
  386. info << " <UniqueID>" << saveState.uniqueID << "</UniqueID>\n";
  387. break;
  388. case PLUGIN_DSSI:
  389. info << " <Binary>" << xmlSafeString(saveState.binary, true) << "</Binary>\n";
  390. info << " <Label>" << xmlSafeString(saveState.label, true) << "</Label>\n";
  391. break;
  392. case PLUGIN_LV2:
  393. info << " <URI>" << xmlSafeString(saveState.label, true) << "</URI>\n";
  394. break;
  395. case PLUGIN_VST:
  396. info << " <Binary>" << xmlSafeString(saveState.binary, true) << "</Binary>\n";
  397. info << " <UniqueID>" << saveState.uniqueID << "</UniqueID>\n";
  398. break;
  399. case PLUGIN_AU:
  400. // TODO?
  401. info << " <Binary>" << xmlSafeString(saveState.binary, true) << "</Binary>\n";
  402. info << " <UniqueID>" << saveState.uniqueID << "</UniqueID>\n";
  403. break;
  404. case PLUGIN_CSOUND:
  405. case PLUGIN_GIG:
  406. case PLUGIN_SF2:
  407. case PLUGIN_SFZ:
  408. info << " <Filename>" << xmlSafeString(saveState.binary, true) << "</Filename>\n";
  409. info << " <Label>" << xmlSafeString(saveState.label, true) << "</Label>\n";
  410. break;
  411. }
  412. info << " </Info>\n\n";
  413. content << info;
  414. }
  415. {
  416. String data(" <Data>\n");
  417. data << " <Active>" << (saveState.active ? "Yes" : "No") << "</Active>\n";
  418. if (saveState.dryWet != 1.0f)
  419. data << " <DryWet>" << saveState.dryWet << "</DryWet>\n";
  420. if (saveState.volume != 1.0f)
  421. data << " <Volume>" << saveState.volume << "</Volume>\n";
  422. if (saveState.balanceLeft != -1.0f)
  423. data << " <Balance-Left>" << saveState.balanceLeft << "</Balance-Left>\n";
  424. if (saveState.balanceRight != 1.0f)
  425. data << " <Balance-Right>" << saveState.balanceRight << "</Balance-Right>\n";
  426. if (saveState.panning != 0.0f)
  427. data << " <Panning>" << saveState.panning << "</Panning>\n";
  428. if (saveState.ctrlChannel < 0)
  429. data << " <ControlChannel>N</ControlChannel>\n";
  430. else
  431. data << " <ControlChannel>" << saveState.ctrlChannel+1 << "</ControlChannel>\n";
  432. content << data;
  433. }
  434. for (StateParameterItenerator it = saveState.parameters.begin(); it.valid(); it.next())
  435. {
  436. StateParameter* const stateParameter(*it);
  437. String parameter("\n"" <Parameter>\n");
  438. parameter << " <Index>" << (long)stateParameter->index << "</Index>\n"; // FIXME
  439. parameter << " <Name>" << xmlSafeString(stateParameter->name, true) << "</Name>\n";
  440. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  441. parameter << " <Symbol>" << xmlSafeString(stateParameter->symbol, true) << "</Symbol>\n";
  442. parameter << " <Value>" << stateParameter->value << "</Value>\n";
  443. if (stateParameter->midiCC > 0)
  444. {
  445. parameter << " <MidiCC>" << stateParameter->midiCC << "</MidiCC>\n";
  446. parameter << " <MidiChannel>" << stateParameter->midiChannel+1 << "</MidiChannel>\n";
  447. }
  448. parameter << " </Parameter>\n";
  449. content << parameter;
  450. }
  451. if (saveState.currentProgramIndex >= 0 && saveState.currentProgramName != nullptr)
  452. {
  453. // ignore 'default' program
  454. #ifdef __USE_GNU
  455. if ((saveState.currentProgramIndex > 0 || strcasecmp(saveState.currentProgramName, "default") != 0))
  456. #else
  457. if ((saveState.currentProgramIndex > 0 || std::strcmp(saveState.currentProgramName, "Default") != 0))
  458. #endif
  459. {
  460. String program("\n");
  461. program << " <CurrentProgramIndex>" << saveState.currentProgramIndex+1 << "</CurrentProgramIndex>\n";
  462. program << " <CurrentProgramName>" << xmlSafeString(saveState.currentProgramName, true) << "</CurrentProgramName>\n";
  463. content << program;
  464. }
  465. }
  466. if (saveState.currentMidiBank >= 0 && saveState.currentMidiProgram >= 0)
  467. {
  468. String midiProgram("\n");
  469. midiProgram << " <CurrentMidiBank>" << saveState.currentMidiBank+1 << "</CurrentMidiBank>\n";
  470. midiProgram << " <CurrentMidiProgram>" << saveState.currentMidiProgram+1 << "</CurrentMidiProgram>\n";
  471. content << midiProgram;
  472. }
  473. for (StateCustomDataItenerator it = saveState.customData.begin(); it.valid(); it.next())
  474. {
  475. StateCustomData* const stateCustomData(*it);
  476. String customData("\n"" <CustomData>\n");
  477. customData << " <Type>" << xmlSafeString(stateCustomData->type, true) << "</Type>\n";
  478. customData << " <Key>" << xmlSafeString(stateCustomData->key, true) << "</Key>\n";
  479. if (std::strcmp(stateCustomData->type, CUSTOM_DATA_CHUNK) == 0 || std::strlen(stateCustomData->value) >= 128)
  480. customData << " <Value>\n" << xmlSafeString(stateCustomData->value, true) << "\n </Value>\n";
  481. else
  482. customData << " <Value>" << xmlSafeString(stateCustomData->value, true) << "</Value>\n";
  483. customData << " </CustomData>\n";
  484. content << customData;
  485. }
  486. if (saveState.chunk != nullptr && saveState.chunk[0] != '\0')
  487. {
  488. String chunk("\n"" <Chunk>\n");
  489. chunk << saveState.chunk << "\n </Chunk>\n";
  490. content << chunk;
  491. }
  492. content << " </Data>\n";
  493. }
  494. // -----------------------------------------------------------------------
  495. #endif
  496. CARLA_BACKEND_END_NAMESPACE
  497. #endif // CARLA_STATE_UTILS_HPP_INCLUDED