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.

CarlaStateUtils.cpp 21KB

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