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.

437 lines
13KB

  1. /*
  2. * Carla Backend utils
  3. * Copyright (C) 2011-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 "CarlaBackend.hpp"
  20. #include "CarlaUtils.hpp"
  21. #include <QtXml/QDomNode>
  22. #include <vector>
  23. CARLA_BACKEND_START_NAMESPACE
  24. // -------------------------------------------------
  25. // Carla GUI stuff
  26. struct StateParameter {
  27. uint32_t index;
  28. const char* name;
  29. const char* symbol;
  30. double value;
  31. uint8_t midiChannel;
  32. int16_t midiCC;
  33. StateParameter()
  34. : index(0),
  35. name(nullptr),
  36. symbol(nullptr),
  37. value(0.0),
  38. midiChannel(1),
  39. midiCC(-1) {}
  40. ~StateParameter()
  41. {
  42. if (name != nullptr)
  43. delete[] name;
  44. if (symbol != nullptr)
  45. delete[] symbol;
  46. }
  47. };
  48. struct StateCustomData {
  49. const char* type;
  50. const char* key;
  51. const char* value;
  52. StateCustomData()
  53. : type(nullptr),
  54. key(nullptr),
  55. value(nullptr) {}
  56. ~StateCustomData()
  57. {
  58. if (type != nullptr)
  59. delete[] type;
  60. if (key != nullptr)
  61. delete[] key;
  62. if (value != nullptr)
  63. delete[] value;
  64. }
  65. };
  66. typedef std::vector<StateParameter> StateParameterVector;
  67. typedef std::vector<StateCustomData> StateCustomDataVector;
  68. struct SaveState {
  69. const char* type;
  70. const char* name;
  71. const char* label;
  72. const char* binary;
  73. long uniqueID;
  74. bool active;
  75. double dryWet;
  76. double volume;
  77. double balanceLeft;
  78. double balanceRight;
  79. double panning;
  80. int32_t currentProgramIndex;
  81. const char* currentProgramName;
  82. int32_t currentMidiBank;
  83. int32_t currentMidiProgram;
  84. const char* chunk;
  85. StateParameterVector parameters;
  86. StateCustomDataVector customData;
  87. SaveState()
  88. : type(nullptr),
  89. name(nullptr),
  90. label(nullptr),
  91. binary(nullptr),
  92. uniqueID(0),
  93. active(false),
  94. dryWet(1.0),
  95. volume(1.0),
  96. balanceLeft(-1.0),
  97. balanceRight(1.0),
  98. panning(0.0),
  99. currentProgramIndex(-1),
  100. currentProgramName(nullptr),
  101. currentMidiBank(-1),
  102. currentMidiProgram(-1),
  103. chunk(nullptr) {}
  104. ~SaveState()
  105. {
  106. reset();
  107. }
  108. void reset()
  109. {
  110. if (type != nullptr)
  111. {
  112. delete[] type;
  113. type = nullptr;
  114. }
  115. if (name != nullptr)
  116. {
  117. delete[] name;
  118. name = nullptr;
  119. }
  120. if (label != nullptr)
  121. {
  122. delete[] label;
  123. label = nullptr;
  124. }
  125. if (binary != nullptr)
  126. {
  127. delete[] binary;
  128. binary = nullptr;
  129. }
  130. if (currentProgramName != nullptr)
  131. {
  132. delete[] currentProgramName;
  133. currentProgramName = nullptr;
  134. }
  135. if (chunk != nullptr)
  136. {
  137. delete[] chunk;
  138. chunk = nullptr;
  139. }
  140. uniqueID = 0;
  141. active = false;
  142. dryWet = 1.0;
  143. volume = 1.0;
  144. balanceLeft = -1.0;
  145. balanceRight = 1.0;
  146. panning = 0.0;
  147. currentProgramIndex = -1;
  148. currentMidiBank = -1;
  149. currentMidiProgram = -1;
  150. parameters.clear();
  151. customData.clear();
  152. }
  153. };
  154. // -------------------------------------------------
  155. // Carla XML helpers (xml to state)
  156. static inline
  157. QString xmlSafeString(const QString& string, const bool toXml)
  158. {
  159. QString newString(string);
  160. if (toXml)
  161. return newString.replace("&", "&amp;").replace("<","&lt;").replace(">","&gt;").replace("'","&apos;").replace("\"","&quot;");
  162. else
  163. return newString.replace("&amp;", "&").replace("&lt;","<").replace("&gt;",">").replace("&apos;","'").replace("&quot;","\"");
  164. }
  165. static inline
  166. const char* xmlSafeStringChar(const QString& string, const bool toXml)
  167. {
  168. return carla_strdup(xmlSafeString(string, toXml).toUtf8().constData());
  169. }
  170. static inline
  171. const SaveState& getSaveStateDictFromXML(const QDomNode& xmlNode)
  172. {
  173. static SaveState saveState;
  174. saveState.reset();
  175. QDomNode node(xmlNode.firstChild());
  176. while (! node.isNull())
  177. {
  178. // ------------------------------------------------------
  179. // Info
  180. if (node.toElement().tagName() == "Info")
  181. {
  182. QDomNode xmlInfo(node.toElement().firstChild());
  183. while (! xmlInfo.isNull())
  184. {
  185. const QString tag = xmlInfo.toElement().tagName();
  186. const QString text = xmlInfo.toElement().text(); //.strip();
  187. if (tag == "Type")
  188. saveState.type = xmlSafeStringChar(text, false);
  189. else if (tag == "Name")
  190. saveState.name = xmlSafeStringChar(text, false);
  191. else if (tag == "Label" || tag == "URI")
  192. saveState.label = xmlSafeStringChar(text, false);
  193. else if (tag == "Binary")
  194. saveState.binary = xmlSafeStringChar(text, false);
  195. else if (tag == "UniqueID")
  196. {
  197. bool ok;
  198. long uniqueID = text.toLong(&ok);
  199. if (ok) saveState.uniqueID = uniqueID;
  200. }
  201. xmlInfo = xmlInfo.nextSibling();
  202. }
  203. }
  204. // ------------------------------------------------------
  205. // Data
  206. else if (node.toElement().tagName() == "Data")
  207. {
  208. QDomNode xmlData(node.toElement().firstChild());
  209. while (! xmlData.isNull())
  210. {
  211. const QString tag = xmlData.toElement().tagName();
  212. const QString text = xmlData.toElement().text(); //.strip();
  213. // ----------------------------------------------
  214. // Internal Data
  215. if (tag == "Active")
  216. {
  217. saveState.active = bool(text == "Yes");
  218. }
  219. else if (tag == "DryWet")
  220. {
  221. bool ok;
  222. double value = text.toDouble(&ok);
  223. if (ok) saveState.dryWet = value;
  224. }
  225. else if (tag == "Volume")
  226. {
  227. bool ok;
  228. double value = text.toDouble(&ok);
  229. if (ok) saveState.volume = value;
  230. }
  231. else if (tag == "Balance-Left")
  232. {
  233. bool ok;
  234. double value = text.toDouble(&ok);
  235. if (ok) saveState.balanceLeft = value;
  236. }
  237. else if (tag == "Balance-Right")
  238. {
  239. bool ok;
  240. double value = text.toDouble(&ok);
  241. if (ok) saveState.balanceRight = value;
  242. }
  243. // ----------------------------------------------
  244. // Program (current)
  245. else if (tag == "CurrentProgramIndex")
  246. {
  247. bool ok;
  248. int value = text.toInt(&ok);
  249. if (ok) saveState.currentProgramIndex = value;
  250. }
  251. else if (tag == "CurrentProgramName")
  252. {
  253. saveState.currentProgramName = xmlSafeStringChar(text, false);
  254. }
  255. // ----------------------------------------------
  256. // Midi Program (current)
  257. else if (tag == "CurrentMidiBank")
  258. {
  259. bool ok;
  260. int value = text.toInt(&ok);
  261. if (ok) saveState.currentMidiBank = value;
  262. }
  263. else if (tag == "CurrentMidiProgram")
  264. {
  265. bool ok;
  266. int value = text.toInt(&ok);
  267. if (ok) saveState.currentMidiProgram = value;
  268. }
  269. // ----------------------------------------------
  270. // Parameters
  271. else if (tag == "Parameter")
  272. {
  273. StateParameter stateParameter;
  274. QDomNode xmlSubData(xmlData.toElement().firstChild());
  275. while (! xmlSubData.isNull())
  276. {
  277. const QString pTag = xmlSubData.toElement().tagName();
  278. const QString pText = xmlSubData.toElement().text(); //.strip();
  279. if (pTag == "index")
  280. {
  281. bool ok;
  282. uint index = pText.toUInt(&ok);
  283. if (ok) stateParameter.index = index;
  284. }
  285. else if (pTag == "name")
  286. {
  287. stateParameter.name = xmlSafeStringChar(pText, false);
  288. }
  289. else if (pTag == "symbol")
  290. {
  291. stateParameter.symbol = xmlSafeStringChar(pText, false);
  292. }
  293. else if (pTag == "value")
  294. {
  295. bool ok;
  296. double value = pText.toDouble(&ok);
  297. if (ok) stateParameter.value = value;
  298. }
  299. else if (pTag == "midiChannel")
  300. {
  301. bool ok;
  302. uint channel = pText.toUInt(&ok);
  303. if (ok && channel < 16)
  304. stateParameter.midiChannel = static_cast<uint8_t>(channel);
  305. }
  306. else if (pTag == "midiCC")
  307. {
  308. bool ok;
  309. int cc = pText.toInt(&ok);
  310. if (ok && cc < INT16_MAX)
  311. stateParameter.midiCC = static_cast<int16_t>(cc);
  312. }
  313. xmlSubData = xmlSubData.nextSibling();
  314. }
  315. saveState.parameters.push_back(stateParameter);
  316. }
  317. // ----------------------------------------------
  318. // Custom Data
  319. else if (tag == "CustomData")
  320. {
  321. StateCustomData stateCustomData;
  322. QDomNode xmlSubData(xmlData.toElement().firstChild());
  323. while (! xmlSubData.isNull())
  324. {
  325. const QString cTag = xmlSubData.toElement().tagName();
  326. const QString cText = xmlSubData.toElement().text(); //.strip();
  327. if (cTag == "type")
  328. stateCustomData.type = xmlSafeStringChar(cText, false);
  329. else if (cTag == "key")
  330. stateCustomData.key = xmlSafeStringChar(cText, false);
  331. else if (cTag == "value")
  332. stateCustomData.value = xmlSafeStringChar(cText, false);
  333. xmlSubData = xmlSubData.nextSibling();
  334. }
  335. saveState.customData.push_back(stateCustomData);
  336. }
  337. // ----------------------------------------------
  338. // Chunk
  339. else if (tag == "Chunk")
  340. {
  341. saveState.chunk = xmlSafeStringChar(text, false);
  342. }
  343. // ----------------------------------------------
  344. xmlData = xmlData.nextSibling();
  345. }
  346. }
  347. // ------------------------------------------------------
  348. node = node.nextSibling();
  349. }
  350. return saveState;
  351. }
  352. static inline
  353. QString getXMLFromSaveState(const SaveState& saveState)
  354. {
  355. return "";
  356. // TODO
  357. (void)saveState;
  358. }
  359. // -------------------------------------------------
  360. // Carla XML helpers (state to xml)
  361. // -------------------------------------------------
  362. CARLA_BACKEND_END_NAMESPACE
  363. #endif // __CARLA_STATE_UTILS_HPP__