Cleanup
[carla.git] / source / utils / CarlaJsfxUtils.hpp
blobc14b23bb6569a8626d040532d586438fdd1f3082
1 /*
2 * Carla JSFX utils
3 * Copyright (C) 2021 Jean Pierre Cimalando
4 * Copyright (C) 2021-2024 Filipe Coelho <falktx@falktx.com>
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.
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.
16 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
19 #ifndef CARLA_JSFX_UTILS_HPP_INCLUDED
20 #define CARLA_JSFX_UTILS_HPP_INCLUDED
22 #include "CarlaBackend.h"
23 #include "CarlaString.hpp"
25 #include "water/files/File.h"
27 #ifdef YSFX_API
28 # error YSFX_API is not private
29 #endif
30 #include "ysfx/include/ysfx.h"
32 CARLA_BACKEND_START_NAMESPACE
34 // --------------------------------------------------------------------------------------------------------------------
36 struct CarlaJsfxLogging
38 static void logAll(intptr_t, const ysfx_log_level level, const char* const message)
40 switch (level)
42 case ysfx_log_info:
43 carla_stdout("%s: %s", ysfx_log_level_string(level), message);
44 break;
45 case ysfx_log_warning:
46 carla_stderr("%s: %s", ysfx_log_level_string(level), message);
47 break;
48 case ysfx_log_error:
49 carla_stderr2("%s: %s", ysfx_log_level_string(level), message);
50 break;
54 static void logErrorsOnly(intptr_t, const ysfx_log_level level, const char* const message)
56 switch (level)
58 case ysfx_log_info:
59 break;
60 case ysfx_log_warning:
61 carla_stderr("%s: %s", ysfx_log_level_string(level), message);
62 break;
63 case ysfx_log_error:
64 carla_stderr2("%s: %s", ysfx_log_level_string(level), message);
65 break;
70 // --------------------------------------------------------------------------------------------------------------------
72 struct CarlaJsfxCategories
74 static PluginCategory getFromEffect(ysfx_t* const effect)
76 PluginCategory category = PLUGIN_CATEGORY_OTHER;
78 if (const uint32_t tagCount = ysfx_get_tags(effect, nullptr, 0))
80 std::vector<const char*> tags;
81 tags.resize(tagCount);
82 ysfx_get_tags(effect, tags.data(), tagCount);
84 for (uint32_t i=0; i<tagCount && category == PLUGIN_CATEGORY_OTHER; ++i)
86 PluginCategory current = getFromTag(tags[i]);
88 if (current != PLUGIN_CATEGORY_NONE)
89 category = current;
93 return category;
96 static PluginCategory getFromTag(const char* const tag)
98 if (carla_strcasecmp(tag, "synthesis") == 0)
99 return PLUGIN_CATEGORY_SYNTH;
101 if (carla_strcasecmp(tag, "delay") == 0)
102 return PLUGIN_CATEGORY_DELAY;
104 if (carla_strcasecmp(tag, "equalizer") == 0)
105 return PLUGIN_CATEGORY_EQ;
107 if (carla_strcasecmp(tag, "filter") == 0)
108 return PLUGIN_CATEGORY_FILTER;
110 if (carla_strcasecmp(tag, "distortion") == 0)
111 return PLUGIN_CATEGORY_DISTORTION;
113 if (carla_strcasecmp(tag, "dynamics") == 0)
114 return PLUGIN_CATEGORY_DYNAMICS;
116 if (carla_strcasecmp(tag, "modulation") == 0)
117 return PLUGIN_CATEGORY_MODULATOR;
119 if (carla_strcasecmp(tag, "utility") == 0)
120 return PLUGIN_CATEGORY_UTILITY;
122 return PLUGIN_CATEGORY_NONE;
126 // --------------------------------------------------------------------------------------------------------------------
128 class CarlaJsfxUnit
130 static CarlaString createFileId(const water::File& rootPath, const water::File& filePath)
132 CarlaString fileId(filePath.getRelativePathFrom(rootPath).toRawUTF8());
133 #ifdef CARLA_OS_WIN
134 fileId.replace('\\', '/');
135 #endif
136 return fileId;
139 public:
140 CarlaJsfxUnit() noexcept
141 : fFileId(),
142 fFilePath(),
143 fRootPath() {}
145 CarlaJsfxUnit(const water::File& rootPath, const water::File& filePath)
146 : fFileId(createFileId(rootPath, filePath)),
147 fFilePath(rootPath.getChildFile(fFileId).getFullPathName().toRawUTF8()),
148 fRootPath(rootPath.getFullPathName().toRawUTF8()) {}
150 explicit operator bool() const noexcept
152 return fFileId.isNotEmpty();
155 const CarlaString& getFileId() const noexcept
157 return fFileId;
160 const CarlaString& getFilePath() const noexcept
162 return fFilePath;
165 const CarlaString& getRootPath() const noexcept
167 return fRootPath;
170 private:
171 CarlaString fFileId;
172 CarlaString fFilePath;
173 CarlaString fRootPath;
176 // --------------------------------------------------------------------------------------------------------------------
178 CARLA_BACKEND_END_NAMESPACE
180 #endif // CARLA_JSFX_UTILS_HPP_INCLUDED