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.

617 lines
22KB

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