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.

626 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 GPL.txt file
  16. */
  17. #ifndef __CARLA_STATE_UTILS_HPP__
  18. #define __CARLA_STATE_UTILS_HPP__
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaJuceUtils.hpp"
  21. #include "CarlaMIDI.h"
  22. #include <QtCore/QVector>
  23. #include <QtXml/QDomNode>
  24. CARLA_BACKEND_START_NAMESPACE
  25. // -------------------------------------------------
  26. struct StateParameter {
  27. uint32_t index;
  28. const char* name;
  29. const char* symbol;
  30. float value;
  31. uint8_t midiChannel;
  32. int16_t midiCC;
  33. StateParameter()
  34. : index(0),
  35. name(nullptr),
  36. symbol(nullptr),
  37. value(0.0f),
  38. midiChannel(0),
  39. midiCC(-1) {}
  40. ~StateParameter()
  41. {
  42. if (name != nullptr)
  43. delete[] name;
  44. if (symbol != nullptr)
  45. delete[] symbol;
  46. }
  47. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(StateParameter)
  48. };
  49. struct StateCustomData {
  50. const char* type;
  51. const char* key;
  52. const char* value;
  53. StateCustomData()
  54. : type(nullptr),
  55. key(nullptr),
  56. value(nullptr) {}
  57. ~StateCustomData()
  58. {
  59. if (type != nullptr)
  60. delete[] type;
  61. if (key != nullptr)
  62. delete[] key;
  63. if (value != nullptr)
  64. delete[] value;
  65. }
  66. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(StateCustomData)
  67. };
  68. typedef QVector<StateParameter*> StateParameterVector;
  69. typedef QVector<StateCustomData*> StateCustomDataVector;
  70. struct SaveState {
  71. const char* type;
  72. const char* name;
  73. const char* label;
  74. const char* binary;
  75. long uniqueID;
  76. bool active;
  77. float dryWet;
  78. float volume;
  79. float balanceLeft;
  80. float balanceRight;
  81. float panning;
  82. int8_t ctrlChannel;
  83. int32_t currentProgramIndex;
  84. const char* currentProgramName;
  85. int32_t currentMidiBank;
  86. int32_t currentMidiProgram;
  87. const char* chunk;
  88. StateParameterVector parameters;
  89. StateCustomDataVector customData;
  90. SaveState()
  91. : type(nullptr),
  92. name(nullptr),
  93. label(nullptr),
  94. binary(nullptr),
  95. uniqueID(0),
  96. active(false),
  97. dryWet(1.0f),
  98. volume(1.0f),
  99. balanceLeft(-1.0f),
  100. balanceRight(1.0f),
  101. panning(0.0f),
  102. ctrlChannel(0),
  103. currentProgramIndex(-1),
  104. currentProgramName(nullptr),
  105. currentMidiBank(-1),
  106. currentMidiProgram(-1),
  107. chunk(nullptr) {}
  108. ~SaveState()
  109. {
  110. reset();
  111. }
  112. void reset()
  113. {
  114. if (type != nullptr)
  115. {
  116. delete[] type;
  117. type = nullptr;
  118. }
  119. if (name != nullptr)
  120. {
  121. delete[] name;
  122. name = nullptr;
  123. }
  124. if (label != nullptr)
  125. {
  126. delete[] label;
  127. label = nullptr;
  128. }
  129. if (binary != nullptr)
  130. {
  131. delete[] binary;
  132. binary = nullptr;
  133. }
  134. if (currentProgramName != nullptr)
  135. {
  136. delete[] currentProgramName;
  137. currentProgramName = nullptr;
  138. }
  139. if (chunk != nullptr)
  140. {
  141. delete[] chunk;
  142. chunk = nullptr;
  143. }
  144. uniqueID = 0;
  145. active = false;
  146. dryWet = 1.0f;
  147. volume = 1.0f;
  148. balanceLeft = -1.0f;
  149. balanceRight = 1.0f;
  150. panning = 0.0f;
  151. ctrlChannel = 0;
  152. currentProgramIndex = -1;
  153. currentMidiBank = -1;
  154. currentMidiProgram = -1;
  155. for (auto it = parameters.begin(); it != parameters.end(); ++it)
  156. {
  157. StateParameter* const stateParameter(*it);
  158. delete stateParameter;
  159. }
  160. for (auto it = customData.begin(); it != customData.end(); ++it)
  161. {
  162. StateCustomData* const stateCustomData(*it);
  163. delete stateCustomData;
  164. }
  165. parameters.clear();
  166. customData.clear();
  167. }
  168. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(SaveState)
  169. };
  170. // -------------------------------------------------
  171. static inline
  172. QString xmlSafeString(const QString& string, const bool toXml)
  173. {
  174. QString newString(string);
  175. if (toXml)
  176. return newString.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;").replace("'","&apos;").replace("\"","&quot;");
  177. else
  178. return newString.replace("&amp;","&").replace("&lt;","<").replace("&gt;",">").replace("&apos;","'").replace("&quot;","\"");
  179. }
  180. static inline
  181. const char* xmlSafeStringChar(const QString& string, const bool toXml)
  182. {
  183. return carla_strdup(xmlSafeString(string, toXml).toUtf8().constData());
  184. }
  185. // -------------------------------------------------
  186. static inline
  187. const SaveState& getSaveStateDictFromXML(const QDomNode& xmlNode)
  188. {
  189. static SaveState saveState;
  190. saveState.reset();
  191. QDomNode node(xmlNode.firstChild());
  192. while (! node.isNull())
  193. {
  194. // ------------------------------------------------------
  195. // Info
  196. if (node.toElement().tagName().compare("Info", Qt::CaseInsensitive) == 0)
  197. {
  198. QDomNode xmlInfo(node.toElement().firstChild());
  199. while (! xmlInfo.isNull())
  200. {
  201. const QString tag(xmlInfo.toElement().tagName());
  202. const QString text(xmlInfo.toElement().text().trimmed());
  203. if (tag.compare("Type", Qt::CaseInsensitive) == 0)
  204. saveState.type = xmlSafeStringChar(text, false);
  205. else if (tag.compare("Name", Qt::CaseInsensitive) == 0)
  206. saveState.name = xmlSafeStringChar(text, false);
  207. else if (tag.compare("Label", Qt::CaseInsensitive) == 0 || tag.compare("URI", Qt::CaseInsensitive) == 0)
  208. saveState.label = xmlSafeStringChar(text, false);
  209. else if (tag.compare("Binary", Qt::CaseInsensitive) == 0)
  210. saveState.binary = xmlSafeStringChar(text, false);
  211. else if (tag.compare("UniqueID", Qt::CaseInsensitive) == 0)
  212. {
  213. bool ok;
  214. long uniqueID = text.toLong(&ok);
  215. if (ok) saveState.uniqueID = uniqueID;
  216. }
  217. xmlInfo = xmlInfo.nextSibling();
  218. }
  219. }
  220. // ------------------------------------------------------
  221. // Data
  222. else if (node.toElement().tagName().compare("Data", Qt::CaseInsensitive) == 0)
  223. {
  224. QDomNode xmlData(node.toElement().firstChild());
  225. while (! xmlData.isNull())
  226. {
  227. const QString tag(xmlData.toElement().tagName());
  228. const QString text(xmlData.toElement().text().trimmed());
  229. // ----------------------------------------------
  230. // Internal Data
  231. if (tag.compare("Active", Qt::CaseInsensitive) == 0)
  232. {
  233. saveState.active = (text.compare("Yes", Qt::CaseInsensitive) == 0);
  234. }
  235. else if (tag.compare("DryWet", Qt::CaseInsensitive) == 0)
  236. {
  237. bool ok;
  238. float value = text.toFloat(&ok);
  239. if (ok) saveState.dryWet = value;
  240. }
  241. else if (tag.compare("Volume", Qt::CaseInsensitive) == 0)
  242. {
  243. bool ok;
  244. float value = text.toFloat(&ok);
  245. if (ok) saveState.volume = value;
  246. }
  247. else if (tag.compare("Balance-Left", Qt::CaseInsensitive) == 0)
  248. {
  249. bool ok;
  250. float value = text.toFloat(&ok);
  251. if (ok) saveState.balanceLeft = value;
  252. }
  253. else if (tag.compare("Balance-Right", Qt::CaseInsensitive) == 0)
  254. {
  255. bool ok;
  256. float value = text.toFloat(&ok);
  257. if (ok) saveState.balanceRight = value;
  258. }
  259. else if (tag.compare("Panning", Qt::CaseInsensitive) == 0)
  260. {
  261. bool ok;
  262. float value = text.toFloat(&ok);
  263. if (ok) saveState.panning = value;
  264. }
  265. else if (tag.compare("ControlChannel", Qt::CaseInsensitive) == 0)
  266. {
  267. bool ok;
  268. short value = text.toShort(&ok);
  269. if (ok && value >= 1 && value < INT8_MAX)
  270. saveState.ctrlChannel = static_cast<int8_t>(value-1);
  271. }
  272. // ----------------------------------------------
  273. // Program (current)
  274. else if (tag.compare("CurrentProgramIndex", Qt::CaseInsensitive) == 0)
  275. {
  276. bool ok;
  277. int value = text.toInt(&ok);
  278. if (ok && value >= 1)
  279. saveState.currentProgramIndex = value-1;
  280. }
  281. else if (tag.compare("CurrentProgramName", Qt::CaseInsensitive) == 0)
  282. {
  283. saveState.currentProgramName = xmlSafeStringChar(text, false);
  284. }
  285. // ----------------------------------------------
  286. // Midi Program (current)
  287. else if (tag.compare("CurrentMidiBank", Qt::CaseInsensitive) == 0)
  288. {
  289. bool ok;
  290. int value = text.toInt(&ok);
  291. if (ok && value >= 1)
  292. saveState.currentMidiBank = value-1;
  293. }
  294. else if (tag.compare("CurrentMidiProgram", Qt::CaseInsensitive) == 0)
  295. {
  296. bool ok;
  297. int value = text.toInt(&ok);
  298. if (ok && value >= 1)
  299. saveState.currentMidiProgram = value-1;
  300. }
  301. // ----------------------------------------------
  302. // Parameters
  303. else if (tag.compare("Parameter", Qt::CaseInsensitive) == 0)
  304. {
  305. StateParameter* const stateParameter(new StateParameter());
  306. QDomNode xmlSubData(xmlData.toElement().firstChild());
  307. while (! xmlSubData.isNull())
  308. {
  309. const QString pTag(xmlSubData.toElement().tagName());
  310. const QString pText(xmlSubData.toElement().text().trimmed());
  311. if (pTag.compare("Index", Qt::CaseInsensitive) == 0)
  312. {
  313. bool ok;
  314. uint index = pText.toUInt(&ok);
  315. if (ok) stateParameter->index = index;
  316. }
  317. else if (pTag.compare("Name", Qt::CaseInsensitive) == 0)
  318. {
  319. stateParameter->name = xmlSafeStringChar(pText, false);
  320. }
  321. else if (pTag.compare("Symbol", Qt::CaseInsensitive) == 0)
  322. {
  323. stateParameter->symbol = xmlSafeStringChar(pText, false);
  324. }
  325. else if (pTag.compare("Value", Qt::CaseInsensitive) == 0)
  326. {
  327. bool ok;
  328. float value = pText.toFloat(&ok);
  329. if (ok) stateParameter->value = value;
  330. }
  331. else if (pTag.compare("MidiChannel", Qt::CaseInsensitive) == 0)
  332. {
  333. bool ok;
  334. ushort channel = pText.toUShort(&ok);
  335. if (ok && channel >= 1 && channel < MAX_MIDI_CHANNELS)
  336. stateParameter->midiChannel = static_cast<uint8_t>(channel-1);
  337. }
  338. else if (pTag.compare("MidiCC", Qt::CaseInsensitive) == 0)
  339. {
  340. bool ok;
  341. int cc = pText.toInt(&ok);
  342. if (ok && cc >= 1 && cc < INT16_MAX)
  343. stateParameter->midiCC = static_cast<int16_t>(cc);
  344. }
  345. xmlSubData = xmlSubData.nextSibling();
  346. }
  347. saveState.parameters.append(stateParameter);
  348. }
  349. // ----------------------------------------------
  350. // Custom Data
  351. else if (tag.compare("CustomData", Qt::CaseInsensitive) == 0)
  352. {
  353. StateCustomData* const stateCustomData(new StateCustomData());
  354. QDomNode xmlSubData(xmlData.toElement().firstChild());
  355. while (! xmlSubData.isNull())
  356. {
  357. const QString cTag(xmlSubData.toElement().tagName());
  358. const QString cText(xmlSubData.toElement().text().trimmed());
  359. if (cTag.compare("Type", Qt::CaseInsensitive) == 0)
  360. stateCustomData->type = xmlSafeStringChar(cText, false);
  361. else if (cTag.compare("Key", Qt::CaseInsensitive) == 0)
  362. stateCustomData->key = xmlSafeStringChar(cText, false);
  363. else if (cTag.compare("Value", Qt::CaseInsensitive) == 0)
  364. stateCustomData->value = xmlSafeStringChar(cText, false);
  365. xmlSubData = xmlSubData.nextSibling();
  366. }
  367. saveState.customData.append(stateCustomData);
  368. }
  369. // ----------------------------------------------
  370. // Chunk
  371. else if (tag.compare("Chunk", Qt::CaseInsensitive) == 0)
  372. {
  373. saveState.chunk = xmlSafeStringChar(text, false);
  374. }
  375. // ----------------------------------------------
  376. xmlData = xmlData.nextSibling();
  377. }
  378. }
  379. // ------------------------------------------------------
  380. node = node.nextSibling();
  381. }
  382. return saveState;
  383. }
  384. // -------------------------------------------------
  385. static inline
  386. const QString& getXMLFromSaveState(const SaveState& saveState)
  387. {
  388. static QString content;
  389. content.clear();
  390. {
  391. QString info(" <Info>\n");
  392. info += QString(" <Type>%1</Type>\n").arg(saveState.type);
  393. info += QString(" <Name>%1</Name>\n").arg(xmlSafeString(saveState.name, true));
  394. switch (getPluginTypeFromString(saveState.type))
  395. {
  396. case PLUGIN_NONE:
  397. break;
  398. case PLUGIN_INTERNAL:
  399. info += QString(" <Label>%1</Label>\n").arg(xmlSafeString(saveState.label, true));
  400. break;
  401. case PLUGIN_LADSPA:
  402. info += QString(" <Binary>%1</Binary>\n").arg(xmlSafeString(saveState.binary, true));
  403. info += QString(" <Label>%1</Label>\n").arg(xmlSafeString(saveState.label, true));
  404. info += QString(" <UniqueID>%1</UniqueID>\n").arg(saveState.uniqueID);
  405. break;
  406. case PLUGIN_DSSI:
  407. info += QString(" <Binary>%1</Binary>\n").arg(xmlSafeString(saveState.binary, true));
  408. info += QString(" <Label>%1</Label>\n").arg(xmlSafeString(saveState.label, true));
  409. break;
  410. case PLUGIN_LV2:
  411. info += QString(" <URI>%1</URI>\n").arg(xmlSafeString(saveState.label, true));
  412. break;
  413. case PLUGIN_VST:
  414. info += QString(" <Binary>%1</Binary>\n").arg(xmlSafeString(saveState.binary, true));
  415. info += QString(" <UniqueID>%1</UniqueID>\n").arg(saveState.uniqueID);
  416. break;
  417. case PLUGIN_VST3:
  418. // TODO?
  419. info += QString(" <Binary>%1</Binary>\n").arg(xmlSafeString(saveState.binary, true));
  420. info += QString(" <UniqueID>%1</UniqueID>\n").arg(saveState.uniqueID);
  421. break;
  422. case PLUGIN_GIG:
  423. case PLUGIN_SF2:
  424. case PLUGIN_SFZ:
  425. info += QString(" <Binary>%1</Binary>\n").arg(xmlSafeString(saveState.binary, true));
  426. info += QString(" <Label>%1</Label>\n").arg(xmlSafeString(saveState.label, true));
  427. break;
  428. }
  429. info += " </Info>\n\n";
  430. content += info;
  431. }
  432. {
  433. QString data(" <Data>\n");
  434. data += QString(" <Active>%1</Active>\n").arg(saveState.active ? "Yes" : "No");
  435. if (saveState.dryWet != 1.0f)
  436. data += QString(" <DryWet>%1</DryWet>\n").arg(saveState.dryWet);
  437. if (saveState.volume != 1.0f)
  438. data += QString(" <Volume>%1</Volume>\n").arg(saveState.volume);
  439. if (saveState.balanceLeft != -1.0f)
  440. data += QString(" <Balance-Left>%1</Balance-Left>\n").arg(saveState.balanceLeft);
  441. if (saveState.balanceRight != 1.0f)
  442. data += QString(" <Balance-Right>%1</Balance-Right>\n").arg(saveState.balanceRight);
  443. if (saveState.panning != 0.0f)
  444. data += QString(" <Panning>%1</Panning>\n").arg(saveState.panning);
  445. data += QString(" <ControlChannel>%1</ControlChannel>\n").arg(saveState.ctrlChannel+1);
  446. content += data;
  447. }
  448. for (auto it = saveState.parameters.begin(); it != saveState.parameters.end(); ++it)
  449. {
  450. StateParameter* const stateParameter(*it);
  451. QString parameter("\n"" <Parameter>\n");
  452. parameter += QString(" <Index>%1</Index>\n").arg(stateParameter->index);
  453. parameter += QString(" <Name>%1</Name>\n").arg(xmlSafeString(stateParameter->name, true));
  454. if (stateParameter->symbol != nullptr && *stateParameter->symbol != '\0')
  455. parameter += QString(" <Symbol>%1</Symbol>\n").arg(xmlSafeString(stateParameter->symbol, true));
  456. parameter += QString(" <Value>%1</Value>\n").arg(stateParameter->value);
  457. if (stateParameter->midiCC > 0)
  458. {
  459. parameter += QString(" <MidiCC>%1</MidiCC>\n").arg(stateParameter->midiCC);
  460. parameter += QString(" <MidiChannel>%1</MidiChannel>\n").arg(stateParameter->midiChannel+1);
  461. }
  462. parameter += " </Parameter>\n";
  463. content += parameter;
  464. }
  465. if (saveState.currentProgramIndex >= 0 && saveState.currentProgramName != nullptr)
  466. {
  467. // ignore 'default' program
  468. #ifdef __USE_GNU
  469. if ((saveState.currentProgramIndex > 0 || strcasecmp(saveState.currentProgramName, "Default") != 0))
  470. #else
  471. if ((saveState.currentProgramIndex > 0 || std::strcmp(saveState.currentProgramName, "Default") != 0))
  472. #endif
  473. {
  474. QString program("\n");
  475. program += QString(" <CurrentProgramIndex>%1</CurrentProgramIndex>\n").arg(saveState.currentProgramIndex+1);
  476. program += QString(" <CurrentProgramName>%1</CurrentProgramName>\n").arg(xmlSafeString(saveState.currentProgramName, true));
  477. content += program;
  478. }
  479. }
  480. if (saveState.currentMidiBank >= 0 && saveState.currentMidiProgram >= 0)
  481. {
  482. QString midiProgram("\n");
  483. midiProgram += QString(" <CurrentMidiBank>%1</CurrentMidiBank>\n").arg(saveState.currentMidiBank+1);
  484. midiProgram += QString(" <CurrentMidiProgram>%1</CurrentMidiProgram>\n").arg(saveState.currentMidiProgram+1);
  485. content += midiProgram;
  486. }
  487. for (auto it = saveState.customData.begin(); it != saveState.customData.end(); ++it)
  488. {
  489. StateCustomData* const stateCustomData(*it);
  490. QString customData("\n"" <CustomData>\n");
  491. customData += QString(" <Type>%1</Type>\n").arg(xmlSafeString(stateCustomData->type, true));
  492. customData += QString(" <Key>%1</Key>\n").arg(xmlSafeString(stateCustomData->key, true));
  493. if (std::strcmp(stateCustomData->type, CUSTOM_DATA_CHUNK) == 0)
  494. {
  495. customData += " <Value>\n";
  496. customData += QString("%1\n").arg(xmlSafeString(stateCustomData->value, true));
  497. customData += " </Value>\n";
  498. }
  499. else
  500. customData += QString(" <Value>%1</Value>\n").arg(xmlSafeString(stateCustomData->value, true));
  501. customData += " </CustomData>\n";
  502. content += customData;
  503. }
  504. if (saveState.chunk != nullptr && *saveState.chunk != '\0')
  505. {
  506. QString chunk("\n"" <Chunk>\n");
  507. chunk += QString("%1\n").arg(saveState.chunk);
  508. chunk += " </Chunk>\n";
  509. content += chunk;
  510. }
  511. content += " </Data>\n";
  512. return content;
  513. }
  514. // -------------------------------------------------
  515. CARLA_BACKEND_END_NAMESPACE
  516. #endif // __CARLA_STATE_UTILS_HPP__