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.

628 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 "CarlaMIDI.h"
  21. #include <QtCore/QVector>
  22. #include <QtXml/QDomNode>
  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()
  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. delete[] name;
  43. if (symbol != nullptr)
  44. delete[] symbol;
  45. }
  46. StateParameter(StateParameter&) = delete;
  47. StateParameter(const StateParameter&) = delete;
  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. StateCustomData(StateCustomData&) = delete;
  67. StateCustomData(const StateCustomData&) = delete;
  68. };
  69. typedef QVector<StateParameter*> StateParameterVector;
  70. typedef QVector<StateCustomData*> StateCustomDataVector;
  71. struct SaveState {
  72. const char* type;
  73. const char* name;
  74. const char* label;
  75. const char* binary;
  76. long uniqueID;
  77. bool active;
  78. float dryWet;
  79. float volume;
  80. float balanceLeft;
  81. float balanceRight;
  82. float panning;
  83. int8_t ctrlChannel;
  84. int32_t currentProgramIndex;
  85. const char* currentProgramName;
  86. int32_t currentMidiBank;
  87. int32_t currentMidiProgram;
  88. const char* chunk;
  89. StateParameterVector parameters;
  90. StateCustomDataVector customData;
  91. SaveState()
  92. : type(nullptr),
  93. name(nullptr),
  94. label(nullptr),
  95. binary(nullptr),
  96. uniqueID(0),
  97. active(false),
  98. dryWet(1.0f),
  99. volume(1.0f),
  100. balanceLeft(-1.0f),
  101. balanceRight(1.0f),
  102. panning(0.0f),
  103. ctrlChannel(0),
  104. currentProgramIndex(-1),
  105. currentProgramName(nullptr),
  106. currentMidiBank(-1),
  107. currentMidiProgram(-1),
  108. chunk(nullptr) {}
  109. ~SaveState()
  110. {
  111. reset();
  112. }
  113. void reset()
  114. {
  115. if (type != nullptr)
  116. {
  117. delete[] type;
  118. type = nullptr;
  119. }
  120. if (name != nullptr)
  121. {
  122. delete[] name;
  123. name = nullptr;
  124. }
  125. if (label != nullptr)
  126. {
  127. delete[] label;
  128. label = nullptr;
  129. }
  130. if (binary != nullptr)
  131. {
  132. delete[] binary;
  133. binary = nullptr;
  134. }
  135. if (currentProgramName != nullptr)
  136. {
  137. delete[] currentProgramName;
  138. currentProgramName = nullptr;
  139. }
  140. if (chunk != nullptr)
  141. {
  142. delete[] chunk;
  143. chunk = nullptr;
  144. }
  145. uniqueID = 0;
  146. active = false;
  147. dryWet = 1.0f;
  148. volume = 1.0f;
  149. balanceLeft = -1.0f;
  150. balanceRight = 1.0f;
  151. panning = 0.0f;
  152. ctrlChannel = 0;
  153. currentProgramIndex = -1;
  154. currentMidiBank = -1;
  155. currentMidiProgram = -1;
  156. for (auto it = parameters.begin(); it != parameters.end(); ++it)
  157. {
  158. StateParameter* const stateParameter(*it);
  159. delete stateParameter;
  160. }
  161. for (auto it = customData.begin(); it != customData.end(); ++it)
  162. {
  163. StateCustomData* const stateCustomData(*it);
  164. delete stateCustomData;
  165. }
  166. parameters.clear();
  167. customData.clear();
  168. }
  169. SaveState(SaveState&) = delete;
  170. SaveState(const SaveState&) = delete;
  171. };
  172. // -------------------------------------------------
  173. static inline
  174. QString xmlSafeString(const QString& string, const bool toXml)
  175. {
  176. QString newString(string);
  177. if (toXml)
  178. return newString.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;").replace("'","&apos;").replace("\"","&quot;");
  179. else
  180. return newString.replace("&amp;","&").replace("&lt;","<").replace("&gt;",">").replace("&apos;","'").replace("&quot;","\"");
  181. }
  182. static inline
  183. const char* xmlSafeStringChar(const QString& string, const bool toXml)
  184. {
  185. return carla_strdup(xmlSafeString(string, toXml).toUtf8().constData());
  186. }
  187. // -------------------------------------------------
  188. static inline
  189. const SaveState& getSaveStateDictFromXML(const QDomNode& xmlNode)
  190. {
  191. static SaveState saveState;
  192. saveState.reset();
  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)
  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__