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.

568 lines
21KB

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