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.

237 lines
6.9KB

  1. /*
  2. * Carla JSFX utils
  3. * Copyright (C) 2021 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_JSFX_UTILS_HPP_INCLUDED
  18. #define CARLA_JSFX_UTILS_HPP_INCLUDED
  19. #include "CarlaDefines.h"
  20. #include "CarlaUtils.hpp"
  21. #include "CarlaString.hpp"
  22. #include "CarlaBase64Utils.hpp"
  23. #include "water/files/File.h"
  24. #include "water/xml/XmlElement.h"
  25. #include "water/xml/XmlDocument.h"
  26. #include "water/streams/MemoryInputStream.h"
  27. #include "water/streams/MemoryOutputStream.h"
  28. #pragma GCC diagnostic push
  29. #pragma GCC diagnostic ignored "-Wunused-parameter"
  30. #pragma GCC diagnostic ignored "-Wignored-attributes"
  31. #include "jsusfx.h"
  32. #include "jsusfx_file.h"
  33. #include "jsusfx_gfx.h"
  34. #include "jsusfx_serialize.h"
  35. #pragma GCC diagnostic pop
  36. #include <memory>
  37. class CarlaJsusFx : public JsusFx
  38. {
  39. public:
  40. explicit CarlaJsusFx(JsusFxPathLibrary &pathLibrary)
  41. : JsusFx(pathLibrary)
  42. {
  43. }
  44. void setQuiet(bool quiet)
  45. {
  46. fQuiet = quiet;
  47. }
  48. void displayMsg(const char* fmt, ...) override
  49. {
  50. if (!fQuiet)
  51. {
  52. char msgBuf[256];
  53. ::va_list args;
  54. ::va_start(args, fmt);
  55. std::vsnprintf(msgBuf, sizeof(msgBuf), fmt, args);
  56. msgBuf[255] = 0;
  57. ::va_end(args);
  58. carla_stdout("%s", msgBuf);
  59. }
  60. }
  61. void displayError(const char* fmt, ...) override
  62. {
  63. if (!fQuiet)
  64. {
  65. char msgBuf[256];
  66. ::va_list args;
  67. ::va_start(args, fmt);
  68. std::vsnprintf(msgBuf, sizeof(msgBuf), fmt, args);
  69. msgBuf[255] = 0;
  70. ::va_end(args);
  71. carla_stderr("%s", msgBuf);
  72. }
  73. }
  74. private:
  75. bool fQuiet = false;
  76. };
  77. // -------------------------------------------------------------------------------------------------------------------
  78. class CarlaJsfxUnit
  79. {
  80. public:
  81. CarlaJsfxUnit() = default;
  82. CarlaJsfxUnit(const water::File& rootPath, const water::File& filePath)
  83. : fRootPath(rootPath), fFileId(filePath.getRelativePathFrom(rootPath))
  84. {
  85. #ifdef CARLA_OS_WIN
  86. fFileId.replaceCharacter('\\', '/');
  87. #endif
  88. }
  89. explicit operator bool() const
  90. {
  91. return fFileId.isNotEmpty();
  92. }
  93. const water::File& getRootPath() const
  94. {
  95. return fRootPath;
  96. }
  97. const water::String& getFileId() const
  98. {
  99. return fFileId;
  100. }
  101. water::File getFilePath() const
  102. {
  103. return fRootPath.getChildFile(fFileId);
  104. }
  105. private:
  106. water::File fRootPath;
  107. water::String fFileId;
  108. };
  109. // -------------------------------------------------------------------------------------------------------------------
  110. class CarlaJsusFxPathLibrary : public JsusFxPathLibrary_Basic
  111. {
  112. public:
  113. explicit CarlaJsusFxPathLibrary(const CarlaJsfxUnit &unit)
  114. : JsusFxPathLibrary_Basic(unit.getRootPath().getFullPathName().toRawUTF8())
  115. {
  116. }
  117. };
  118. // -------------------------------------------------------------------------------------------------------------------
  119. class CarlaJsusFxFileAPI : public JsusFxFileAPI_Basic
  120. {
  121. public:
  122. };
  123. // -------------------------------------------------------------------------------------------------------------------
  124. class CarlaJsusFxSerializer : public JsusFxSerializer_Basic
  125. {
  126. public:
  127. explicit CarlaJsusFxSerializer(JsusFxSerializationData& data)
  128. : JsusFxSerializer_Basic(data)
  129. {
  130. }
  131. static water::String convertDataToString(const JsusFxSerializationData& data)
  132. {
  133. water::XmlElement root("JSFXState");
  134. std::size_t numSliders = data.sliders.size();
  135. for (std::size_t i = 0; i < numSliders; ++i)
  136. {
  137. water::XmlElement *slider = new water::XmlElement("Slider");
  138. slider->setAttribute("index", data.sliders[i].index);
  139. slider->setAttribute("value", data.sliders[i].value);
  140. root.addChildElement(slider);
  141. }
  142. std::size_t numVars = data.vars.size();
  143. if (numVars > 0)
  144. {
  145. water::MemoryOutputStream blob;
  146. for (std::size_t i = 0; i < numVars; ++i)
  147. {
  148. blob.writeFloat(data.vars[i]);
  149. }
  150. const CarlaString base64 = CarlaString::asBase64(blob.getData(), blob.getDataSize());
  151. water::XmlElement *var = new water::XmlElement("Serialization");
  152. var->addTextElement(base64.buffer());
  153. root.addChildElement(var);
  154. }
  155. water::MemoryOutputStream stream;
  156. root.writeToStream(stream, water::StringRef(), true);
  157. return stream.toUTF8();
  158. }
  159. static bool convertStringToData(const water::String& string, JsusFxSerializationData& data)
  160. {
  161. std::unique_ptr<water::XmlElement> root(water::XmlDocument::parse(string));
  162. CARLA_SAFE_ASSERT_RETURN(root != nullptr, false);
  163. CARLA_SAFE_ASSERT_RETURN(root->getTagName() == "JSFXState", false);
  164. data = JsusFxSerializationData();
  165. int numChildren = root->getNumChildElements();
  166. for (int i = 0; i < numChildren; ++i)
  167. {
  168. water::XmlElement* child = root->getChildElement(i);
  169. CARLA_SAFE_ASSERT_CONTINUE(child != nullptr);
  170. if (child->getTagName() == "Slider")
  171. {
  172. CARLA_SAFE_ASSERT_CONTINUE(child->hasAttribute("index"));
  173. CARLA_SAFE_ASSERT_CONTINUE(child->hasAttribute("value"));
  174. JsusFxSerializationData::Slider slider;
  175. slider.index = child->getIntAttribute("index");
  176. slider.value = child->getDoubleAttribute("value");
  177. data.sliders.push_back(slider);
  178. }
  179. else if (child->getTagName() == "Serialization")
  180. {
  181. std::vector<uint8_t> chunk = carla_getChunkFromBase64String(child->getAllSubText().toRawUTF8());
  182. water::MemoryInputStream blob(chunk.data(), chunk.size(), false);
  183. size_t numVars = chunk.size() / sizeof(float);
  184. data.vars.resize(numVars);
  185. for (std::size_t i = 0; i < numVars; ++i)
  186. {
  187. data.vars[i] = blob.readFloat();
  188. }
  189. }
  190. else
  191. {
  192. CARLA_SAFE_ASSERT_CONTINUE(false);
  193. }
  194. }
  195. return true;
  196. }
  197. };
  198. // -------------------------------------------------------------------------------------------------------------------
  199. #endif // CARLA_JSFX_UTILS_HPP_INCLUDED