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.

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