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.

181 lines
5.1KB

  1. /*
  2. * Carla JSFX utils
  3. * Copyright (C) 2021 Jean Pierre Cimalando
  4. * Copyright (C) 2021-2024 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  17. */
  18. #ifndef CARLA_JSFX_UTILS_HPP_INCLUDED
  19. #define CARLA_JSFX_UTILS_HPP_INCLUDED
  20. #include "CarlaBackend.h"
  21. #include "CarlaString.hpp"
  22. #include "water/files/File.h"
  23. #ifdef YSFX_API
  24. # error YSFX_API is not private
  25. #endif
  26. #include "ysfx/include/ysfx.h"
  27. CARLA_BACKEND_START_NAMESPACE
  28. // --------------------------------------------------------------------------------------------------------------------
  29. struct CarlaJsfxLogging
  30. {
  31. static void logAll(intptr_t, const ysfx_log_level level, const char* const message)
  32. {
  33. switch (level)
  34. {
  35. case ysfx_log_info:
  36. carla_stdout("%s: %s", ysfx_log_level_string(level), message);
  37. break;
  38. case ysfx_log_warning:
  39. carla_stderr("%s: %s", ysfx_log_level_string(level), message);
  40. break;
  41. case ysfx_log_error:
  42. carla_stderr2("%s: %s", ysfx_log_level_string(level), message);
  43. break;
  44. }
  45. };
  46. static void logErrorsOnly(intptr_t, const ysfx_log_level level, const char* const message)
  47. {
  48. switch (level)
  49. {
  50. case ysfx_log_info:
  51. break;
  52. case ysfx_log_warning:
  53. carla_stderr("%s: %s", ysfx_log_level_string(level), message);
  54. break;
  55. case ysfx_log_error:
  56. carla_stderr2("%s: %s", ysfx_log_level_string(level), message);
  57. break;
  58. }
  59. };
  60. };
  61. // --------------------------------------------------------------------------------------------------------------------
  62. struct CarlaJsfxCategories
  63. {
  64. static PluginCategory getFromEffect(ysfx_t* const effect)
  65. {
  66. PluginCategory category = PLUGIN_CATEGORY_OTHER;
  67. if (const uint32_t tagCount = ysfx_get_tags(effect, nullptr, 0))
  68. {
  69. std::vector<const char*> tags;
  70. tags.resize(tagCount);
  71. ysfx_get_tags(effect, tags.data(), tagCount);
  72. for (uint32_t i=0; i<tagCount && category == PLUGIN_CATEGORY_OTHER; ++i)
  73. {
  74. PluginCategory current = getFromTag(tags[i]);
  75. if (current != PLUGIN_CATEGORY_NONE)
  76. category = current;
  77. }
  78. }
  79. return category;
  80. }
  81. static PluginCategory getFromTag(const char* const tag)
  82. {
  83. if (carla_strcasecmp(tag, "synthesis") == 0)
  84. return PLUGIN_CATEGORY_SYNTH;
  85. if (carla_strcasecmp(tag, "delay") == 0)
  86. return PLUGIN_CATEGORY_DELAY;
  87. if (carla_strcasecmp(tag, "equalizer") == 0)
  88. return PLUGIN_CATEGORY_EQ;
  89. if (carla_strcasecmp(tag, "filter") == 0)
  90. return PLUGIN_CATEGORY_FILTER;
  91. if (carla_strcasecmp(tag, "distortion") == 0)
  92. return PLUGIN_CATEGORY_DISTORTION;
  93. if (carla_strcasecmp(tag, "dynamics") == 0)
  94. return PLUGIN_CATEGORY_DYNAMICS;
  95. if (carla_strcasecmp(tag, "modulation") == 0)
  96. return PLUGIN_CATEGORY_MODULATOR;
  97. if (carla_strcasecmp(tag, "utility") == 0)
  98. return PLUGIN_CATEGORY_UTILITY;
  99. return PLUGIN_CATEGORY_NONE;
  100. }
  101. };
  102. // --------------------------------------------------------------------------------------------------------------------
  103. class CarlaJsfxUnit
  104. {
  105. static CarlaString createFileId(const water::File& rootPath, const water::File& filePath)
  106. {
  107. CarlaString fileId(filePath.getRelativePathFrom(rootPath).toRawUTF8());
  108. #ifdef CARLA_OS_WIN
  109. fileId.replace('\\', '/');
  110. #endif
  111. return fileId;
  112. }
  113. public:
  114. CarlaJsfxUnit() noexcept
  115. : fFileId(),
  116. fFilePath(),
  117. fRootPath() {}
  118. CarlaJsfxUnit(const water::File& rootPath, const water::File& filePath)
  119. : fFileId(createFileId(rootPath, filePath)),
  120. fFilePath(rootPath.getChildFile(fFileId).getFullPathName().toRawUTF8()),
  121. fRootPath(rootPath.getFullPathName().toRawUTF8()) {}
  122. explicit operator bool() const noexcept
  123. {
  124. return fFileId.isNotEmpty();
  125. }
  126. const CarlaString& getFileId() const noexcept
  127. {
  128. return fFileId;
  129. }
  130. const CarlaString& getFilePath() const noexcept
  131. {
  132. return fFilePath;
  133. }
  134. const CarlaString& getRootPath() const noexcept
  135. {
  136. return fRootPath;
  137. }
  138. private:
  139. CarlaString fFileId;
  140. CarlaString fFilePath;
  141. CarlaString fRootPath;
  142. };
  143. // --------------------------------------------------------------------------------------------------------------------
  144. CARLA_BACKEND_END_NAMESPACE
  145. #endif // CARLA_JSFX_UTILS_HPP_INCLUDED