From f1d240b88b62344fab52d51e497fca5c5ced1ea4 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Mon, 8 Feb 2021 20:39:24 +0100 Subject: [PATCH] Make a common lib for plugins, move OSC there --- CMakeLists.txt | 17 +--------- plugins/CMakeLists.txt | 24 ++++++++++++++ plugins/common/plugin/MessageUtils.cpp | 39 ++++++++++++++++++++++ plugins/common/plugin/MessageUtils.h | 19 +++++++++++ plugins/editor/CMakeLists.txt | 3 +- plugins/editor/src/editor/Editor.cpp | 45 ++++---------------------- plugins/lv2/CMakeLists.txt | 4 +-- plugins/vst/CMakeLists.txt | 4 +-- 8 files changed, 95 insertions(+), 60 deletions(-) create mode 100644 plugins/CMakeLists.txt create mode 100644 plugins/common/plugin/MessageUtils.cpp create mode 100644 plugins/common/plugin/MessageUtils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 44d5ec51..f01465d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,22 +54,7 @@ add_subdirectory (src) # Optional targets add_subdirectory (clients) - -if ((SFIZZ_LV2 AND SFIZZ_LV2_UI) OR SFIZZ_VST) - add_subdirectory (plugins/editor) -endif() - -if (SFIZZ_LV2) - add_subdirectory (plugins/lv2) -endif() - -if (SFIZZ_VST) - add_subdirectory (plugins/vst) -else() - if (SFIZZ_AU) - message(WARNING "Audio Unit requires VST to be enabled") - endif() -endif() +add_subdirectory (plugins) if (SFIZZ_BENCHMARKS) add_subdirectory (benchmarks) diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt new file mode 100644 index 00000000..a2105364 --- /dev/null +++ b/plugins/CMakeLists.txt @@ -0,0 +1,24 @@ +add_library(plugins-common STATIC EXCLUDE_FROM_ALL + "common/plugin/MessageUtils.h" + "common/plugin/MessageUtils.cpp") +target_include_directories(plugins-common PUBLIC "common") +target_link_libraries(plugins-common + PUBLIC sfizz::spin_mutex + PUBLIC absl::strings) +add_library(sfizz::plugins-common ALIAS plugins-common) + +if((SFIZZ_LV2 AND SFIZZ_LV2_UI) OR SFIZZ_VST) + add_subdirectory(editor) +endif() + +if(SFIZZ_LV2) + add_subdirectory(lv2) +endif() + +if(SFIZZ_VST) + add_subdirectory(vst) +else() + if(SFIZZ_AU) + message(WARNING "Audio Unit requires VST to be enabled") + endif() +endif() diff --git a/plugins/common/plugin/MessageUtils.cpp b/plugins/common/plugin/MessageUtils.cpp new file mode 100644 index 00000000..84e1b5f9 --- /dev/null +++ b/plugins/common/plugin/MessageUtils.cpp @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#include "MessageUtils.h" +#include +#include +#include + +namespace Messages { + +bool matchOSC(const char* pattern, const char* path, unsigned* indices) +{ + unsigned nthIndex = 0; + + while (const char *endp = std::strchr(pattern, '&')) { + size_t length = endp - pattern; + if (std::strncmp(pattern, path, length)) + return false; + pattern += length; + path += length; + + length = 0; + while (absl::ascii_isdigit(path[length])) + ++length; + + if (!absl::SimpleAtoi(absl::string_view(path, length), &indices[nthIndex++])) + return false; + + pattern += 1; + path += length; + } + + return !std::strcmp(path, pattern); +} + +} // namespace Messages diff --git a/plugins/common/plugin/MessageUtils.h b/plugins/common/plugin/MessageUtils.h new file mode 100644 index 00000000..0faf54a5 --- /dev/null +++ b/plugins/common/plugin/MessageUtils.h @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#pragma once + +namespace Messages { + +/** + * Simple matcher for message handling in O(N) + * @param[in] pattern Pattern to match, where '&' characters match positive integer numbers + * @param[in] path Path to match against the pattern + * @param[out] indices Table which received the indices, with size >= the number of '&' in the pattern + */ +bool matchOSC(const char* pattern, const char* path, unsigned* indices); + +} // namespace Messages diff --git a/plugins/editor/CMakeLists.txt b/plugins/editor/CMakeLists.txt index 379e7899..ee8b259d 100644 --- a/plugins/editor/CMakeLists.txt +++ b/plugins/editor/CMakeLists.txt @@ -43,9 +43,8 @@ add_library(sfizz_editor STATIC EXCLUDE_FROM_ALL src/editor/utility/vstgui_before.h) add_library(sfizz::editor ALIAS sfizz_editor) target_include_directories(sfizz_editor PUBLIC "src") -target_link_libraries(sfizz_editor PUBLIC sfizz::messaging) +target_link_libraries(sfizz_editor PUBLIC sfizz::messaging sfizz::plugins-common) target_link_libraries(sfizz_editor PRIVATE sfizz::vstgui) -target_link_libraries(sfizz_editor PUBLIC absl::strings) if(APPLE) find_library(APPLE_APPKIT_LIBRARY "AppKit") find_library(APPLE_CORESERVICES_LIBRARY "CoreServices") diff --git a/plugins/editor/src/editor/Editor.cpp b/plugins/editor/src/editor/Editor.cpp index a8a90650..0b5c6e5a 100644 --- a/plugins/editor/src/editor/Editor.cpp +++ b/plugins/editor/src/editor/Editor.cpp @@ -10,6 +10,7 @@ #include "GUIComponents.h" #include "GUIPiano.h" #include "NativeHelpers.h" +#include "plugin/MessageUtils.h" #include #include #include @@ -372,43 +373,11 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v) } } -/// -static constexpr unsigned kMessageMaxIndices = 8; - -static bool matchMessage(const char* pattern, const char* path, unsigned* indices) -{ - unsigned nthIndex = 0; - - while (const char *endp = strchr(pattern, '&')) { - if (nthIndex == kMessageMaxIndices) - return false; - - size_t length = endp - pattern; - if (strncmp(pattern, path, length)) - return false; - pattern += length; - path += length; - - length = 0; - while (absl::ascii_isdigit(path[length])) - ++length; - - if (!absl::SimpleAtoi(absl::string_view(path, length), &indices[nthIndex++])) - return false; - - pattern += 1; - path += length; - } - - return !strcmp(path, pattern); -} - -/// void Editor::Impl::uiReceiveMessage(const char* path, const char* sig, const sfizz_arg_t* args) { - unsigned indices[kMessageMaxIndices]; + unsigned indices[8]; - if (!strcmp(path, "/cc/slots") && !strcmp(sig, "b")) { + if (Messages::matchOSC("/cc/slots", path, indices) && !strcmp(sig, "b")) { const uint8_t* bitChunks = args[0].b->data; uint32_t byteSize = args[0].b->size; @@ -426,7 +395,7 @@ void Editor::Impl::uiReceiveMessage(const char* path, const char* sig, const sfi } } } - else if (!strcmp(path, "/cc/changed") && !strcmp(sig, "b")) { + else if (Messages::matchOSC("/cc/changed", path, indices) && !strcmp(sig, "b")) { const uint8_t* bitChunks = args[0].b->data; uint32_t byteSize = args[0].b->size; for (unsigned cc = 0; cc < 8 * byteSize; ++cc) { @@ -438,13 +407,13 @@ void Editor::Impl::uiReceiveMessage(const char* path, const char* sig, const sfi } } } - else if (matchMessage("/cc&/value", path, indices) && !strcmp(sig, "f")) { + else if (Messages::matchOSC("/cc&/value", path, indices) && !strcmp(sig, "f")) { updateCCValue(indices[0], args[0].f); } - else if (matchMessage("/cc&/default", path, indices) && !strcmp(sig, "f")) { + else if (Messages::matchOSC("/cc&/default", path, indices) && !strcmp(sig, "f")) { updateCCDefaultValue(indices[0], args[0].f); } - else if (matchMessage("/cc&/label", path, indices) && !strcmp(sig, "s")) { + else if (Messages::matchOSC("/cc&/label", path, indices) && !strcmp(sig, "s")) { updateCCLabel(indices[0], args[0].s); } else { diff --git a/plugins/lv2/CMakeLists.txt b/plugins/lv2/CMakeLists.txt index e6623c1f..ee07b3da 100644 --- a/plugins/lv2/CMakeLists.txt +++ b/plugins/lv2/CMakeLists.txt @@ -21,14 +21,14 @@ source_group("Turtle Files" FILES add_library(${LV2PLUGIN_PRJ_NAME} MODULE ${PROJECT_NAME}.cpp ${LV2PLUGIN_TTL_SRC_FILES}) -target_link_libraries(${LV2PLUGIN_PRJ_NAME} PRIVATE sfizz::sfizz sfizz::spin_mutex) +target_link_libraries(${LV2PLUGIN_PRJ_NAME} PRIVATE sfizz::sfizz sfizz::plugins-common) if(SFIZZ_LV2_UI) add_library(${LV2PLUGIN_PRJ_NAME}_ui MODULE ${PROJECT_NAME}_ui.cpp vstgui_helpers.h vstgui_helpers.cpp) - target_link_libraries(${LV2PLUGIN_PRJ_NAME}_ui PRIVATE sfizz::editor sfizz::vstgui) + target_link_libraries(${LV2PLUGIN_PRJ_NAME}_ui PRIVATE sfizz::editor sfizz::vstgui sfizz::plugins-common) endif() # Explicitely strip all symbols on Linux but lv2_descriptor() diff --git a/plugins/vst/CMakeLists.txt b/plugins/vst/CMakeLists.txt index 8f328512..a3cba79b 100644 --- a/plugins/vst/CMakeLists.txt +++ b/plugins/vst/CMakeLists.txt @@ -63,7 +63,7 @@ endif() target_link_libraries(${VSTPLUGIN_PRJ_NAME} PRIVATE sfizz::sfizz PRIVATE sfizz::editor - PRIVATE sfizz::spin_mutex + PRIVATE sfizz::plugins-common PRIVATE sfizz::pugixml sfizz::filesystem) target_include_directories(${VSTPLUGIN_PRJ_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") @@ -202,7 +202,7 @@ elseif(SFIZZ_AU) target_link_libraries(${AUPLUGIN_PRJ_NAME} PRIVATE ${PROJECT_NAME}::${PROJECT_NAME} PRIVATE sfizz::editor - PRIVATE sfizz::spin_mutex + PRIVATE sfizz::plugins-common PRIVATE sfizz::pugixml sfizz::filesystem) target_include_directories(${AUPLUGIN_PRJ_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")