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.

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