From 2d3b62b69c6826b9bef8c45346e2eafe978deb23 Mon Sep 17 00:00:00 2001 From: Paul Fd Date: Thu, 15 Apr 2021 22:25:17 +0200 Subject: [PATCH 1/4] Add Midnam bindings in C++ And a couple tests... --- src/sfizz.hpp | 10 ++++++++++ src/sfizz/sfizz.cpp | 5 +++++ tests/BindingsT.cpp | 38 ++++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 3 ++- 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/BindingsT.cpp diff --git a/src/sfizz.hpp b/src/sfizz.hpp index 2feb9fe1..f06e1867 100644 --- a/src/sfizz.hpp +++ b/src/sfizz.hpp @@ -242,6 +242,16 @@ public: */ int getNumCurves() const noexcept; + /** + * @brief Export a MIDI Name document describing the currently loaded SFZ file. + * @since 0.3.1 + * + * @param model The model name used if a non-empty string, otherwise generated. + * + * @return A newly allocated XML string, which must be freed after use. + */ + std::string exportMidnam(const std::string& model) const noexcept; + /** * @brief Return a list of unsupported opcodes, if any. * @since 0.2.0 diff --git a/src/sfizz/sfizz.cpp b/src/sfizz/sfizz.cpp index 57cb8e24..d4e301ae 100644 --- a/src/sfizz/sfizz.cpp +++ b/src/sfizz/sfizz.cpp @@ -110,6 +110,11 @@ int sfz::Sfizz::getNumCurves() const noexcept return synth->synth.getNumCurves(); } +std::string sfz::Sfizz::exportMidnam(const std::string& model) const noexcept +{ + return synth->synth.exportMidnam(model); +} + const std::vector& sfz::Sfizz::getUnknownOpcodes() const noexcept { return synth->synth.getUnknownOpcodes(); diff --git a/tests/BindingsT.cpp b/tests/BindingsT.cpp new file mode 100644 index 00000000..095c57ac --- /dev/null +++ b/tests/BindingsT.cpp @@ -0,0 +1,38 @@ +// 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 "sfizz.h" +#include "sfizz.hpp" +#include "catch2/catch.hpp" +#include "ghc/fs_std.hpp" + +TEST_CASE("[Bindings] Midnam C++") +{ + sfz::Sfizz synth; + const auto path = fs::current_path() / "tests/TestFiles/labels.sfz"; + synth.loadSfzFile(path.string()); + const std::string xmlMidnam = synth.exportMidnam(""); + REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); + REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); + REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); + REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); +} + +TEST_CASE("[Bindings] Midnam C") +{ + sfizz_synth_t* synth = sfizz_create_synth(); + const auto path = fs::current_path() / "tests/TestFiles/labels.sfz"; + const auto strPath = path.string(); + sfizz_load_file(synth, strPath.c_str()); + char* midnamChar = sfizz_export_midnam(synth, ""); + const std::string xmlMidnam = std::string(midnamChar); + REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); + REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); + REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); + REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); + free(midnamChar); + sfizz_free(synth); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 013115aa..2e5f70a9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -13,6 +13,7 @@ set(SFIZZ_TEST_SOURCES TestHelpers.h TestHelpers.cpp ParsingT.cpp + BindingsT.cpp HelpersT.cpp HelpersT.cpp AudioBufferT.cpp @@ -53,7 +54,7 @@ set(SFIZZ_TEST_SOURCES ) add_executable(sfizz_tests ${SFIZZ_TEST_SOURCES}) -target_link_libraries(sfizz_tests PRIVATE sfizz::internal sfizz::spin_mutex sfizz::jsl sfizz::filesystem) +target_link_libraries(sfizz_tests PRIVATE sfizz::internal sfizz::static sfizz::spin_mutex sfizz::jsl sfizz::filesystem) sfizz_enable_lto_if_needed(sfizz_tests) sfizz_enable_fast_math(sfizz_tests) catch_discover_tests(sfizz_tests) From bc4cf8a5ada2fc96936afee3a89795ade2d3fc32 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Fri, 16 Apr 2021 00:27:56 +0200 Subject: [PATCH 2/4] Update Midnam C++ API --- src/sfizz.hpp | 12 +----------- src/sfizz/sfizz.cpp | 7 +------ 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/sfizz.hpp b/src/sfizz.hpp index f06e1867..7b5a250a 100644 --- a/src/sfizz.hpp +++ b/src/sfizz.hpp @@ -242,16 +242,6 @@ public: */ int getNumCurves() const noexcept; - /** - * @brief Export a MIDI Name document describing the currently loaded SFZ file. - * @since 0.3.1 - * - * @param model The model name used if a non-empty string, otherwise generated. - * - * @return A newly allocated XML string, which must be freed after use. - */ - std::string exportMidnam(const std::string& model) const noexcept; - /** * @brief Return a list of unsupported opcodes, if any. * @since 0.2.0 @@ -990,7 +980,7 @@ public: * * @return A XML string. */ - std::string exportMidnam(const char* model); + std::string exportMidnam(const std::string& model) const; /** * @addtogroup Messaging diff --git a/src/sfizz/sfizz.cpp b/src/sfizz/sfizz.cpp index d4e301ae..5260caf7 100644 --- a/src/sfizz/sfizz.cpp +++ b/src/sfizz/sfizz.cpp @@ -110,11 +110,6 @@ int sfz::Sfizz::getNumCurves() const noexcept return synth->synth.getNumCurves(); } -std::string sfz::Sfizz::exportMidnam(const std::string& model) const noexcept -{ - return synth->synth.exportMidnam(model); -} - const std::vector& sfz::Sfizz::getUnknownOpcodes() const noexcept { return synth->synth.getUnknownOpcodes(); @@ -366,7 +361,7 @@ void sfz::Sfizz::clearExternalDefinitions() synth->synth.getParser().clearExternalDefinitions(); } -std::string sfz::Sfizz::exportMidnam(const char* model) +std::string sfz::Sfizz::exportMidnam(const std::string& model) const { return synth->synth.exportMidnam(model); } From bd68a4fbb0bef39604f5d966516f024b1feb1319 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Fri, 16 Apr 2021 01:19:05 +0200 Subject: [PATCH 3/4] Expose a function to free memory allocated by the library --- plugins/lv2/sfizz.cpp | 2 +- src/sfizz.h | 10 +++++++++- src/sfizz/sfizz_wrapper.cpp | 5 +++++ tests/BindingsT.cpp | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/plugins/lv2/sfizz.cpp b/plugins/lv2/sfizz.cpp index 912c04b1..ec8e1518 100644 --- a/plugins/lv2/sfizz.cpp +++ b/plugins/lv2/sfizz.cpp @@ -1707,7 +1707,7 @@ midnam_export(LV2_Handle instance) static void midnam_free(char *string) { - free(string); + sfizz_free_memory(string); } static const void * diff --git a/src/sfizz.h b/src/sfizz.h index 6dee8b7b..3c776e1b 100644 --- a/src/sfizz.h +++ b/src/sfizz.h @@ -274,7 +274,7 @@ SFIZZ_EXPORTED_API int sfizz_get_num_curves(sfizz_synth_t* synth); * @param synth The synth. * @param model The model name used if a non-empty string, otherwise generated. * - * @return A newly allocated XML string, which must be freed after use. + * @return A newly allocated XML string, which must be freed after use using sfizz_free_memory(). */ SFIZZ_EXPORTED_API char* sfizz_export_midnam(sfizz_synth_t* synth, const char* model); @@ -1097,6 +1097,14 @@ SFIZZ_EXPORTED_API int sfizz_get_cc_label_number(sfizz_synth_t* synth, int label */ SFIZZ_EXPORTED_API const char * sfizz_get_cc_label_text(sfizz_synth_t* synth, int label_index); +/** + * @brief Free a block of memory allocated by the library. + * @brief 1.0.0 + * + * @param ptr The address of the memory to free. + */ +SFIZZ_EXPORTED_API void sfizz_free_memory(void* ptr); + /** * @addtogroup Messaging * @{ diff --git a/src/sfizz/sfizz_wrapper.cpp b/src/sfizz/sfizz_wrapper.cpp index 4c5c4ca1..685c9ec7 100644 --- a/src/sfizz/sfizz_wrapper.cpp +++ b/src/sfizz/sfizz_wrapper.cpp @@ -410,6 +410,11 @@ const char * sfizz_get_cc_label_text(sfizz_synth_t* synth, int label_index) return ccLabels[label_index].second.c_str(); } +void sfizz_free_memory(void* ptr) +{ + free(ptr); +} + sfizz_client_t* sfizz_create_client(void* data) { return reinterpret_cast(new sfz::Client(data)); diff --git a/tests/BindingsT.cpp b/tests/BindingsT.cpp index 095c57ac..5ce32011 100644 --- a/tests/BindingsT.cpp +++ b/tests/BindingsT.cpp @@ -33,6 +33,6 @@ TEST_CASE("[Bindings] Midnam C") REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); REQUIRE(xmlMidnam.find("") != xmlMidnam.npos); - free(midnamChar); + sfizz_free_memory(midnamChar); sfizz_free(synth); } From f1573ecb03791b5456a1e5a6a4618b50f6b0ef9d Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Fri, 16 Apr 2021 02:25:27 +0200 Subject: [PATCH 4/4] Silence some unused parameter warnings --- src/sfizz/sfizz.cpp | 2 +- src/sfizz/sfizz_wrapper.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sfizz/sfizz.cpp b/src/sfizz/sfizz.cpp index 5260caf7..2a1d2d9d 100644 --- a/src/sfizz/sfizz.cpp +++ b/src/sfizz/sfizz.cpp @@ -338,7 +338,7 @@ void sfz::Sfizz::enableLogging(const std::string& prefix) noexcept void sfz::Sfizz::setLoggingPrefix(const std::string& prefix) noexcept { - + (void)prefix; } void sfz::Sfizz::disableLogging() noexcept diff --git a/src/sfizz/sfizz_wrapper.cpp b/src/sfizz/sfizz_wrapper.cpp index 685c9ec7..5166081d 100644 --- a/src/sfizz/sfizz_wrapper.cpp +++ b/src/sfizz/sfizz_wrapper.cpp @@ -319,7 +319,8 @@ void sfizz_enable_logging(sfizz_synth_t* synth, const char* prefix) void sfizz_set_logging_prefix(sfizz_synth_t* synth, const char* prefix) { - + (void)synth; + (void)prefix; } void sfizz_disable_logging(sfizz_synth_t* synth)