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.

629 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. if (xmlNode.isNull())
  192. return saveState;
  193. QDomNode node(xmlNode.firstChild());
  194. while (! node.isNull())
  195. {
  196. // ------------------------------------------------------
  197. // Info
  198. if (node.toElement().tagName().compare("Info", Qt::CaseInsensitive) == 0)
  199. {
  200. QDomNode xmlInfo(node.toElement().firstChild());
  201. while (! xmlInfo.isNull())
  202. {
  203. const QString tag(xmlInfo.toElement().tagName());
  204. const QString text(xmlInfo.toElement().text().trimmed());
  205. if (tag.compare("Type", Qt::CaseInsensitive) == 0)
  206. saveState.type = xmlSafeStringChar(text, false);
  207. else if (tag.compare("Name", Qt::CaseInsensitive) == 0)
  208. saveState.name = xmlSafeStringChar(text, false);
  209. else if (tag.compare("Label", Qt::CaseInsensitive) == 0 || tag.compare("URI", Qt::CaseInsensitive) == 0)
  210. saveState.label = xmlSafeStringChar(text, false);
  211. else if (tag.compare("Binary", Qt::CaseInsensitive) == 0)
  212. saveState.binary = xmlSafeStringChar(text, false);
  213. else if (tag.compare("UniqueID", Qt::CaseInsensitive) == 0)
  214. {
  215. bool ok;
  216. long uniqueID = text.toLong(&ok);
  217. if (ok) saveState.uniqueID = uniqueID;
  218. }
  219. xmlInfo = xmlInfo.nextSibling();
  220. }
  221. }
  222. // ------------------------------------------------------
  223. // Data
  224. else if (node.toElement().tagName().compare("Data", Qt::CaseInsensitive) == 0)
  225. {
  226. QDomNode xmlData(node.toElement().firstChild());
  227. while (! xmlData.isNull())
  228. {
  229. const QString tag(xmlData.toElement().tagName());
  230. const QString text(xmlData.toElement().text().trimmed());
  231. // ----------------------------------------------
  232. // Internal Data
  233. if (tag.compare("Active", Qt::CaseInsensitive) == 0)
  234. {
  235. saveState.active = (text.compare("Yes", Qt::CaseInsensitive) == 0);
  236. }
  237. else if (tag.compare("DryWet", Qt::CaseInsensitive) == 0)
  238. {
  239. bool ok;
  240. float value = text.toFloat(&ok);
  241. if (ok) saveState.dryWet = value;
  242. }
  243. else if (tag.compare("Volume", Qt::CaseInsensitive) == 0)
  244. {
  245. bool ok;
  246. float value = text.toFloat(&ok);
  247. if (ok) saveState.volume = value;
  248. }
  249. else if (tag.compare("Balance-Left", Qt::CaseInsensitive) == 0)
  250. {
  251. bool ok;
  252. float value = text.toFloat(&ok);
  253. if (ok) saveState.balanceLeft = value;
  254. }
  255. else if (tag.compare("Balance-Right", Qt::CaseInsensitive) == 0)
  256. {
  257. bool ok;
  258. float value = text.toFloat(&ok);
  259. if (ok) saveState.balanceRight = value;
  260. }
  261. else if (tag.compare("Panning", Qt::CaseInsensitive) == 0)
  262. {
  263. bool ok;
  264. float value = text.toFloat(&ok);
  265. if (ok) saveState.panning = value;
  266. }
  267. else if (tag.compare("ControlChannel", Qt::CaseInsensitive) == 0)
  268. {
  269. bool ok;
  270. short value = text.toShort(&ok);
  271. if (ok && value >= 1 && value < INT8_MAX)
  272. saveState.ctrlChannel = static_cast<int8_t>(value-1);
  273. }
  274. // ----------------------------------------------
  275. // Program (current)
  276. else if (tag.compare("CurrentProgramIndex", Qt::CaseInsensitive) == 0)
  277. {
  278. bool ok;
  279. int value = text.toInt(&ok);
  280. if (ok && value >= 1)
  281. saveState.currentProgramIndex = value-1;
  282. }
  283. else if (tag.compare("CurrentProgramName", Qt::CaseInsensitive) == 0)
  284. {
  285. saveState.currentProgramName = xmlSafeStringChar(text, false);
  286. }
  287. // ----------------------------------------------
  288. // Midi Program (current)
  289. else if (tag.compare("CurrentMidiBank", Qt::CaseInsensitive) == 0)
  290. {
  291. bool ok;
  292. int value = text.toInt(&ok);
  293. if (ok && value >= 1)
  294. saveState.currentMidiBank = value-1;
  295. }
  296. else if (tag.compare("CurrentMidiProgram", Qt::CaseInsensitive) == 0)
  297. {
  298. bool ok;
  299. int value = text.toInt(&ok);
  300. if (ok && value >= 1)
  301. saveState.currentMidiProgram = value-1;
  302. }
  303. // ----------------------------------------------
  304. // Parameters
  305. else if (tag.compare("Parameter", Qt::CaseInsensitive) == 0)
  306. {
  307. StateParameter* const stateParameter(new StateParameter());
  308. QDomNode xmlSubData(xmlData.toElement().firstChild());
  309. while (! xmlSubData.isNull())
  310. {
  311. const QString pTag(xmlSubData.toElement().tagName());
  312. const QString pText(xmlSubData.toElement().text().trimmed());
  313. if (pTag.compare("Index", Qt::CaseInsensitive) == 0)
  314. {
  315. bool ok;
  316. uint index = pText.toUInt(&ok);
  317. if (ok) stateParameter->index = index;
  318. }
  319. else if (pTag.compare("Name", Qt::CaseInsensitive) == 0)
  320. {
  321. stateParameter->name = xmlSafeStringChar(pText, false);
  322. }
  323. else if (pTag.compare("Symbol", Qt::CaseInsensitive) == 0)
  324. {
  325. stateParameter->symbol = xmlSafeStringChar(pText, false);
  326. }
  327. else if (pTag.compare("Value", Qt::CaseInsensitive) == 0)
  328. {
  329. bool ok;
  330. float value = pText.toFloat(&ok);
  331. if (ok) stateParameter->value = value;
  332. }
  333. else if (pTag.compare("MidiChannel", Qt::CaseInsensitive) == 0)
  334. {
  335. bool ok;
  336. ushort channel = pText.toUShort(&ok);
  337. if (ok && channel >= 1 && channel < MAX_MIDI_CHANNELS)
  338. stateParameter->midiChannel = static_cast<uint8_t>(channel-1);
  339. }
  340. else if (pTag.compare("MidiCC", Qt::CaseInsensitive) == 0)
  341. {
  342. bool ok;
  343. int cc = pText.toInt(&ok);
  344. if (ok && cc >= 1 && cc < INT16_MAX)
  345. stateParameter->midiCC = static_cast<int16_t>(cc);
  346. }
  347. xmlSubData = xmlSubData.nextSibling();
  348. }
  349. saveState.parameters.append(stateParameter);
  350. }
  351. // ----------------------------------------------
  352. // Custom Data
  353. else if (tag.compare("CustomData", Qt::CaseInsensitive) == 0)
  354. {
  355. StateCustomData* const stateCustomData(new StateCustomData());
  356. QDomNode xmlSubData(xmlData.toElement().firstChild());
  357. while (! xmlSubData.isNull())
  358. {
  359. const QString cTag(xmlSubData.toElement().tagName());
  360. const QString cText(xmlSubData.toElement().text().trimmed());
  361. if (cTag.compare("Type", Qt::CaseInsensitive) == 0)
  362. stateCustomData->type = xmlSafeStringChar(cText, false);
  363. else if (cTag.compare("Key", Qt::CaseInsensitive) == 0)
  364. stateCustomData->key = xmlSafeStringChar(cText, false);
  365. else if (cTag.compare("Value", Qt::CaseInsensitive) == 0)
  366. stateCustomData->value = xmlSafeStringChar(cText, false);
  367. xmlSubData = xmlSubData.nextSibling();
  368. }
  369. saveState.customData.append(stateCustomData);
  370. }
  371. // ----------------------------------------------
  372. // Chunk
  373. else if (tag.compare("Chunk", Qt::CaseInsensitive) == 0)
  374. {
  375. saveState.chunk = xmlSafeStringChar(text, false);
  376. }
  377. // ----------------------------------------------
  378. xmlData = xmlData.nextSibling();
  379. }
  380. }
  381. // ------------------------------------------------------
  382. node = node.nextSibling();
  383. }
  384. return saveState;
  385. }
  386. // -------------------------------------------------
  387. static inline
  388. const QString& getXMLFromSaveState(const SaveState& saveState)
  389. {
  390. static QString content;
  391. content.clear();
  392. {
  393. QString info(" <Info>\n");
  394. info += QString(" <Type>%1</Type>\n").arg(saveState.type);
  395. info += QString(" <Name>%1</Name>\n").arg(xmlSafeString(saveState.name, true));
  396. switch (getPluginTypeFromString(saveState.type))
  397. {
  398. case PLUGIN_NONE:
  399. break;
  400. case PLUGIN_INTERNAL:
  401. info += QString(" <Label>%1</Label>\n").arg(xmlSafeString(saveState.label, true));
  402. break;
  403. case PLUGIN_LADSPA:
  404. info += QString(" <Binary>%1</Binary>\n").arg(xmlSafeString(saveState.binary, true));
  405. info += QString(" <Label>%1</Label>\n").arg(xmlSafeString(saveState.label, true));
  406. info += QString(" <UniqueID>%1</UniqueID>\n").arg(saveState.uniqueID);
  407. break;
  408. case PLUGIN_DSSI:
  409. info += QString(" <Binary>%1</Binary>\n").arg(xmlSafeString(saveState.binary, true));
  410. info += QString(" <Label>%1</Label>\n").arg(xmlSafeString(saveState.label, true));
  411. break;
  412. case PLUGIN_LV2:
  413. info += QString(" <URI>%1</URI>\n").arg(xmlSafeString(saveState.label, true));
  414. break;
  415. case PLUGIN_VST:
  416. info += QString(" <Binary>%1</Binary>\n").arg(xmlSafeString(saveState.binary, true));
  417. info += QString(" <UniqueID>%1</UniqueID>\n").arg(saveState.uniqueID);
  418. break;
  419. case PLUGIN_VST3:
  420. // TODO?
  421. info += QString(" <Binary>%1</Binary>\n").arg(xmlSafeString(saveState.binary, true));
  422. info += QString(" <UniqueID>%1</UniqueID>\n").arg(saveState.uniqueID);
  423. break;
  424. case PLUGIN_GIG:
  425. case PLUGIN_SF2:
  426. case PLUGIN_SFZ:
  427. info += QString(" <Binary>%1</Binary>\n").arg(xmlSafeString(saveState.binary, true));
  428. info += QString(" <Label>%1</Label>\n").arg(xmlSafeString(saveState.label, true));
  429. break;
  430. }
  431. info += " </Info>\n\n";
  432. content += info;
  433. }
  434. {
  435. QString data(" <Data>\n");
  436. data += QString(" <Active>%1</Active>\n").arg(saveState.active ? "Yes" : "No");
  437. if (saveState.dryWet != 1.0f)
  438. data += QString(" <DryWet>%1</DryWet>\n").arg(saveState.dryWet);
  439. if (saveState.volume != 1.0f)
  440. data += QString(" <Volume>%1</Volume>\n").arg(saveState.volume);
  441. if (saveState.balanceLeft != -1.0f)
  442. data += QString(" <Balance-Left>%1</Balance-Left>\n").arg(saveState.balanceLeft);
  443. if (saveState.balanceRight != 1.0f)
  444. data += QString(" <Balance-Right>%1</Balance-Right>\n").arg(saveState.balanceRight);
  445. if (saveState.panning != 0.0f)
  446. data += QString(" <Panning>%1</Panning>\n").arg(saveState.panning);
  447. data += QString(" <ControlChannel>%1</ControlChannel>\n").arg(saveState.ctrlChannel+1);
  448. content += data;
  449. }
  450. for (auto it = saveState.parameters.begin(); it != saveState.parameters.end(); ++it)
  451. {
  452. StateParameter* const stateParameter(*it);
  453. QString parameter("\n"" <Parameter>\n");
  454. parameter += QString(" <Index>%1</Index>\n").arg(stateParameter->index);
  455. parameter += QString(" <Name>%1</Name>\n").arg(xmlSafeString(stateParameter->name, true));
  456. if (stateParameter->symbol != nullptr && *stateParameter->symbol != '\0')
  457. parameter += QString(" <Symbol>%1</Symbol>\n").arg(xmlSafeString(stateParameter->symbol, true));
  458. parameter += QString(" <Value>%1</Value>\n").arg(stateParameter->value);
  459. if (stateParameter->midiCC > 0)
  460. {
  461. parameter += QString(" <MidiCC>%1</MidiCC>\n").arg(stateParameter->midiCC);
  462. parameter += QString(" <MidiChannel>%1</MidiChannel>\n").arg(stateParameter->midiChannel+1);
  463. }
  464. parameter += " </Parameter>\n";
  465. content += parameter;
  466. }
  467. if (saveState.currentProgramIndex >= 0 && saveState.currentProgramName != nullptr)
  468. {
  469. // ignore 'default' program
  470. #ifdef __USE_GNU
  471. if ((saveState.currentProgramIndex > 0 || strcasecmp(saveState.currentProgramName, "Default") != 0))
  472. #else
  473. if ((saveState.currentProgramIndex > 0 || std::strcmp(saveState.currentProgramName, "Default") != 0))
  474. #endif
  475. {
  476. QString program("\n");
  477. program += QString(" <CurrentProgramIndex>%1</CurrentProgramIndex>\n").arg(saveState.currentProgramIndex+1);
  478. program += QString(" <CurrentProgramName>%1</CurrentProgramName>\n").arg(xmlSafeString(saveState.currentProgramName, true));
  479. content += program;
  480. }
  481. }
  482. if (saveState.currentMidiBank >= 0 && saveState.currentMidiProgram >= 0)
  483. {
  484. QString midiProgram("\n");
  485. midiProgram += QString(" <CurrentMidiBank>%1</CurrentMidiBank>\n").arg(saveState.currentMidiBank+1);
  486. midiProgram += QString(" <CurrentMidiProgram>%1</CurrentMidiProgram>\n").arg(saveState.currentMidiProgram+1);
  487. content += midiProgram;
  488. }
  489. for (auto it = saveState.customData.begin(); it != saveState.customData.end(); ++it)
  490. {
  491. StateCustomData* const stateCustomData(*it);
  492. QString customData("\n"" <CustomData>\n");
  493. customData += QString(" <Type>%1</Type>\n").arg(xmlSafeString(stateCustomData->type, true));
  494. customData += QString(" <Key>%1</Key>\n").arg(xmlSafeString(stateCustomData->key, true));
  495. if (std::strcmp(stateCustomData->type, CUSTOM_DATA_CHUNK) == 0 || std::strlen(stateCustomData->value) >= 128)
  496. {
  497. customData += " <Value>\n";
  498. customData += QString("%1\n").arg(xmlSafeString(stateCustomData->value, true));
  499. customData += " </Value>\n";
  500. }
  501. else
  502. customData += QString(" <Value>%1</Value>\n").arg(xmlSafeString(stateCustomData->value, true));
  503. customData += " </CustomData>\n";
  504. content += customData;
  505. }
  506. if (saveState.chunk != nullptr && *saveState.chunk != '\0')
  507. {
  508. QString chunk("\n"" <Chunk>\n");
  509. chunk += QString("%1\n").arg(saveState.chunk);
  510. chunk += " </Chunk>\n";
  511. content += chunk;
  512. }
  513. content += " </Data>\n";
  514. return content;
  515. }
  516. // -------------------------------------------------
  517. CARLA_BACKEND_END_NAMESPACE
  518. #endif // __CARLA_STATE_UTILS_HPP__