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.

CarlaJsfxUtils.hpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "CarlaUtils.hpp"
  20. #include "water/files/File.h"
  21. #include "water/xml/XmlElement.h"
  22. #include "water/xml/XmlDocument.h"
  23. #include "water/streams/MemoryOutputStream.h"
  24. #pragma GCC diagnostic push
  25. #pragma GCC diagnostic ignored "-Wunused-parameter"
  26. #pragma GCC diagnostic ignored "-Wignored-attributes"
  27. #include "jsusfx.h"
  28. #include "jsusfx_file.h"
  29. #include "jsusfx_gfx.h"
  30. #include "jsusfx_serialize.h"
  31. #pragma GCC diagnostic pop
  32. #include <memory>
  33. class CarlaJsusFx : public JsusFx
  34. {
  35. public:
  36. explicit CarlaJsusFx(JsusFxPathLibrary &pathLibrary)
  37. : JsusFx(pathLibrary)
  38. {
  39. }
  40. void setQuiet(bool quiet)
  41. {
  42. fQuiet = quiet;
  43. }
  44. void displayMsg(const char* fmt, ...) override
  45. {
  46. if (!fQuiet)
  47. {
  48. char msgBuf[256];
  49. ::va_list args;
  50. ::va_start(args, fmt);
  51. std::vsnprintf(msgBuf, sizeof(msgBuf), fmt, args);
  52. msgBuf[255] = 0;
  53. ::va_end(args);
  54. carla_stdout("%s", msgBuf);
  55. }
  56. }
  57. void displayError(const char* fmt, ...) override
  58. {
  59. if (!fQuiet)
  60. {
  61. char msgBuf[256];
  62. ::va_list args;
  63. ::va_start(args, fmt);
  64. std::vsnprintf(msgBuf, sizeof(msgBuf), fmt, args);
  65. msgBuf[255] = 0;
  66. ::va_end(args);
  67. carla_stderr("%s", msgBuf);
  68. }
  69. }
  70. private:
  71. bool fQuiet = false;
  72. };
  73. // -------------------------------------------------------------------------------------------------------------------
  74. class CarlaJsusFxPathLibrary : public JsusFxPathLibrary_Basic
  75. {
  76. public:
  77. explicit CarlaJsusFxPathLibrary(const water::File &file)
  78. : JsusFxPathLibrary_Basic(determineDataRoot(file).c_str())
  79. {
  80. }
  81. private:
  82. static std::string determineDataRoot(const water::File &file)
  83. {
  84. return file.getParentDirectory().getFullPathName().toRawUTF8();
  85. }
  86. };
  87. // -------------------------------------------------------------------------------------------------------------------
  88. class CarlaJsusFxFileAPI : public JsusFxFileAPI_Basic
  89. {
  90. public:
  91. };
  92. // -------------------------------------------------------------------------------------------------------------------
  93. class CarlaJsusFxSerializer : public JsusFxSerializer_Basic
  94. {
  95. public:
  96. explicit CarlaJsusFxSerializer(JsusFxSerializationData& data)
  97. : JsusFxSerializer_Basic(data)
  98. {
  99. }
  100. static water::String convertDataToString(const JsusFxSerializationData& data)
  101. {
  102. water::XmlElement root("JSFXState");
  103. std::size_t numSliders = data.sliders.size();
  104. for (std::size_t i = 0; i < numSliders; ++i)
  105. {
  106. water::XmlElement *slider = new water::XmlElement("Slider");
  107. slider->setAttribute("index", data.sliders[i].index);
  108. slider->setAttribute("value", data.sliders[i].value);
  109. root.addChildElement(slider);
  110. }
  111. std::size_t numVars = data.vars.size();
  112. for (std::size_t i = 0; i < numVars; ++i)
  113. {
  114. water::XmlElement *var = new water::XmlElement("Var");
  115. var->setAttribute("value", data.vars[i]);
  116. root.addChildElement(var);
  117. }
  118. water::MemoryOutputStream stream;
  119. root.writeToStream(stream, water::StringRef(), true);
  120. return stream.toUTF8();
  121. }
  122. static bool convertStringToData(const water::String& string, JsusFxSerializationData& data)
  123. {
  124. std::unique_ptr<water::XmlElement> root(water::XmlDocument::parse(string));
  125. CARLA_SAFE_ASSERT_RETURN(root != nullptr, false);
  126. CARLA_SAFE_ASSERT_RETURN(root->getTagName() == "JSFXState", false);
  127. data = JsusFxSerializationData();
  128. int numChildren = root->getNumChildElements();
  129. for (int i = 0; i < numChildren; ++i)
  130. {
  131. water::XmlElement* child = root->getChildElement(i);
  132. CARLA_SAFE_ASSERT_CONTINUE(child != nullptr);
  133. if (child->getTagName() == "Slider")
  134. {
  135. CARLA_SAFE_ASSERT_CONTINUE(child->hasAttribute("index"));
  136. CARLA_SAFE_ASSERT_CONTINUE(child->hasAttribute("value"));
  137. JsusFxSerializationData::Slider slider;
  138. slider.index = child->getIntAttribute("index");
  139. slider.value = child->getDoubleAttribute("value");
  140. data.sliders.push_back(slider);
  141. }
  142. else if (child->getTagName() == "Var")
  143. {
  144. CARLA_SAFE_ASSERT_CONTINUE(child->hasAttribute("value"));
  145. float value = child->getDoubleAttribute("value");
  146. data.vars.push_back(value);
  147. }
  148. else
  149. {
  150. CARLA_SAFE_ASSERT_CONTINUE(false);
  151. }
  152. }
  153. return true;
  154. }
  155. };
  156. // -------------------------------------------------------------------------------------------------------------------
  157. #endif // CARLA_JSFX_UTILS_HPP_INCLUDED