From 23a1ab268d9ad305d4ec48ff4daf582a30291f24 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 22 Jun 2021 21:18:16 +0200 Subject: [PATCH 1/5] Deduplicate load-or-import code from plugins --- plugins/lv2/sfizz.cpp | 17 ++-------------- plugins/vst/SfizzVstProcessor.cpp | 13 ++---------- src/CMakeLists.txt | 2 ++ src/sfizz/import/sfizz_import.cpp | 33 +++++++++++++++++++++++++++++++ src/sfizz/import/sfizz_import.h | 31 +++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 src/sfizz/import/sfizz_import.cpp create mode 100644 src/sfizz/import/sfizz_import.h diff --git a/plugins/lv2/sfizz.cpp b/plugins/lv2/sfizz.cpp index c29d9ebf..6f22f8fc 100644 --- a/plugins/lv2/sfizz.cpp +++ b/plugins/lv2/sfizz.cpp @@ -35,7 +35,7 @@ #include "sfizz_lv2.h" #include "sfizz_lv2_plugin.h" -#include "sfizz/import/ForeignInstrument.h" +#include "sfizz/import/sfizz_import.h" #include "plugin/InstrumentDescription.h" #include @@ -1231,8 +1231,6 @@ sfizz_lv2_update_sfz_info(sfizz_plugin_t *self) static bool sfizz_lv2_load_file(sfizz_plugin_t *self, const char *file_path) { - bool status; - char buf[MAX_PATH_SIZE]; if (file_path[0] == '\0') { @@ -1241,18 +1239,7 @@ sfizz_lv2_load_file(sfizz_plugin_t *self, const char *file_path) } /// - const sfz::InstrumentFormatRegistry& formatRegistry = sfz::InstrumentFormatRegistry::getInstance(); - const sfz::InstrumentFormat* format = formatRegistry.getMatchingFormat(file_path); - - if (!format) - status = sfizz_load_file(self->synth, file_path); - else { - auto importer = format->createImporter(); - std::string virtual_path = std::string(file_path) + ".sfz"; - std::string sfz_text = importer->convertToSfz(file_path); - status = sfizz_load_string(self->synth, virtual_path.c_str(), sfz_text.c_str()); - } - + bool status = sfizz_load_or_import_file(self->synth, file_path, nullptr); sfizz_lv2_update_sfz_info(self); sfizz_lv2_update_file_info(self, file_path); return status; diff --git a/plugins/vst/SfizzVstProcessor.cpp b/plugins/vst/SfizzVstProcessor.cpp index bb1bcac5..cf01f04f 100644 --- a/plugins/vst/SfizzVstProcessor.cpp +++ b/plugins/vst/SfizzVstProcessor.cpp @@ -9,7 +9,7 @@ #include "SfizzVstState.h" #include "SfizzVstParameters.h" #include "SfizzVstIDs.h" -#include "sfizz/import/ForeignInstrument.h" +#include "sfizz/import/sfizz_import.h" #include "plugin/SfizzFileScan.h" #include "plugin/InstrumentDescription.h" #include "base/source/fstreamer.h" @@ -682,16 +682,7 @@ void SfizzVstProcessor::loadSfzFileOrDefault(const std::string& filePath, bool i sfz::Sfizz& synth = *_synth; if (!filePath.empty()) { - const sfz::InstrumentFormatRegistry& formatRegistry = sfz::InstrumentFormatRegistry::getInstance(); - const sfz::InstrumentFormat* format = formatRegistry.getMatchingFormat(filePath); - if (!format) - synth.loadSfzFile(filePath); - else { - auto importer = format->createImporter(); - std::string virtualPath = filePath + ".sfz"; - std::string sfzText = importer->convertToSfz(filePath); - synth.loadSfzString(virtualPath, sfzText); - } + sfizz_load_or_import_file(synth.handle(), filePath.c_str(), nullptr); } else { synth.loadSfzString("default.sfz", defaultSfzText); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 000c1eaa..a1a2533b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -251,11 +251,13 @@ endif() # Import library set(SFIZZ_IMPORT_HEADERS + sfizz/import/sfizz_import.h sfizz/import/ForeignInstrument.h sfizz/import/foreign_instruments/AudioFile.h sfizz/import/foreign_instruments/DecentSampler.h) set(SFIZZ_IMPORT_SOURCES + sfizz/import/sfizz_import.cpp sfizz/import/ForeignInstrument.cpp sfizz/import/foreign_instruments/AudioFile.cpp sfizz/import/foreign_instruments/DecentSampler.cpp) diff --git a/src/sfizz/import/sfizz_import.cpp b/src/sfizz/import/sfizz_import.cpp new file mode 100644 index 00000000..3048f7c3 --- /dev/null +++ b/src/sfizz/import/sfizz_import.cpp @@ -0,0 +1,33 @@ +// 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_import.h" +#include "ForeignInstrument.h" + +bool sfizz_load_or_import_file(sfizz_synth_t* synth, const char* path, const char** format) +{ + const sfz::InstrumentFormatRegistry& ireg = sfz::InstrumentFormatRegistry::getInstance(); + const sfz::InstrumentFormat* ifmt = ireg.getMatchingFormat(path); + + if (!ifmt) { + if (!sfizz_load_file(synth, path)) + return false; + if (format) + *format = nullptr; + } + else { + auto importer = ifmt->createImporter(); + std::string virtualPath = std::string(path) + ".sfz"; + std::string sfzText = importer->convertToSfz(path); + if (!sfizz_load_string(synth, virtualPath.c_str(), sfzText.c_str())) + return false; + if (format) + *format = ifmt->name(); + } + + return true; +} diff --git a/src/sfizz/import/sfizz_import.h b/src/sfizz/import/sfizz_import.h new file mode 100644 index 00000000..7ea7312d --- /dev/null +++ b/src/sfizz/import/sfizz_import.h @@ -0,0 +1,31 @@ +// 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 + +typedef struct sfizz_synth_t sfizz_synth_t; + +/** + * @brief Loads or imports an instrument file. + * + * The file path can be absolute or relative. + * @since 1.0.1 + * + * @param synth The synth. + * @param path A null-terminated string representing a path to an instrument + * in SFZ format, or another format which can be imported. + * @param format An optional pointer to a string pointer, which receives the + * null-terminated name of the format if the file was imported, + * or null if the file was loaded directly as SFZ. + * + * @return @true when file loading went OK, + * @false if some error occured while loading. + * + * @par Thread-safety constraints + * - @b CT: the function must be invoked from the Control thread + * - @b OFF: the function cannot be invoked while a thread is calling @b RT functions + */ +bool sfizz_load_or_import_file(sfizz_synth_t* synth, const char* path, const char** format); From f9cc84b5f03eeeef1b7cbedfc0c6bbdc4d27a161 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 22 Jun 2021 21:25:39 +0200 Subject: [PATCH 2/5] Use the importer in the JACK client --- clients/CMakeLists.txt | 2 +- clients/jack_client.cpp | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/clients/CMakeLists.txt b/clients/CMakeLists.txt index 37e6ed77..a22bbe29 100644 --- a/clients/CMakeLists.txt +++ b/clients/CMakeLists.txt @@ -1,6 +1,6 @@ if(SFIZZ_JACK) add_executable(sfizz_jack MidiHelpers.h jack_client.cpp) - target_link_libraries(sfizz_jack PRIVATE sfizz::sfizz sfizz::jack sfizz::spin_mutex absl::flags_parse) + target_link_libraries(sfizz_jack PRIVATE sfizz::import sfizz::sfizz sfizz::jack sfizz::spin_mutex absl::flags_parse) sfizz_enable_lto_if_needed(sfizz_jack) install(TARGETS sfizz_jack DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT "jack" OPTIONAL) diff --git a/clients/jack_client.cpp b/clients/jack_client.cpp index cd5f8fd7..806ddadb 100644 --- a/clients/jack_client.cpp +++ b/clients/jack_client.cpp @@ -22,6 +22,7 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "sfizz.hpp" +#include "sfizz/import/sfizz_import.h" #include "MidiHelpers.h" #include #include @@ -188,7 +189,13 @@ int main(int argc, char** argv) sfz::Sfizz synth; synth.setOversamplingFactor(factor); synth.setPreloadSize(preload_size); - synth.loadSfzFile(filesToParse[0]); + + const char *importFormat = nullptr; + if (!sfizz_load_or_import_file(synth.handle(), filesToParse[0], &importFormat)) { + std::cout << "Could not load the instrument file: " << filesToParse[0] << '\n'; + return 1; + } + std::cout << "==========" << '\n'; std::cout << "Total:" << '\n'; std::cout << "\tMasters: " << synth.getNumMasters() << '\n'; @@ -211,6 +218,10 @@ int main(int argc, char** argv) for (auto& opcode : synth.getUnknownOpcodes()) std::cout << opcode << ','; std::cout << '\n'; + if (importFormat) { + std::cout << "==========" << '\n'; + std::cout << "Import format: " << importFormat << '\n'; + } // std::cout << std::flush; jack_status_t status; From fdf25d640738369dd8e057d252c6151d59807f38 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 22 Jun 2021 21:28:09 +0200 Subject: [PATCH 3/5] Enable the importer in Puredata --- plugins/puredata/CMakeLists.txt | 2 +- plugins/puredata/sfizz_puredata.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/puredata/CMakeLists.txt b/plugins/puredata/CMakeLists.txt index e861cf92..c8a33a65 100644 --- a/plugins/puredata/CMakeLists.txt +++ b/plugins/puredata/CMakeLists.txt @@ -27,7 +27,7 @@ add_pd_external(sfizz_puredata "sfizz_puredata.c") target_compile_definitions(sfizz_puredata PRIVATE "SFIZZ_NUM_CCS=${SFIZZ_NUM_CCS}" "SFIZZ_VERSION=\"${CMAKE_PROJECT_VERSION}\"") -target_link_libraries(sfizz_puredata PRIVATE sfizz::sfizz) +target_link_libraries(sfizz_puredata PRIVATE sfizz::import sfizz::sfizz) set_target_properties(sfizz_puredata PROPERTIES OUTPUT_NAME "sfizz" diff --git a/plugins/puredata/sfizz_puredata.c b/plugins/puredata/sfizz_puredata.c index 94cfca22..4043b6ca 100644 --- a/plugins/puredata/sfizz_puredata.c +++ b/plugins/puredata/sfizz_puredata.c @@ -7,6 +7,7 @@ #include "GitBuildId.h" #include #include +#include #include #include #include @@ -62,7 +63,7 @@ static bool sfizz_tilde_do_load(t_sfizz_tilde* self) { bool loaded; if (self->filepath[0] != '\0') - loaded = sfizz_load_file(self->synth, self->filepath); + loaded = sfizz_load_or_import_file(self->synth, self->filepath, NULL); else loaded = sfizz_load_string(self->synth, "default.sfz", "sample=*sine"); return loaded; From 6c88b3f6636f2de07e376f6af4197462cae205ff Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 22 Jun 2021 21:30:16 +0200 Subject: [PATCH 4/5] Fix a warning with sfizz_import.h in C --- src/sfizz/import/sfizz_import.cpp | 1 - src/sfizz/import/sfizz_import.h | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/sfizz/import/sfizz_import.cpp b/src/sfizz/import/sfizz_import.cpp index 3048f7c3..f73fc67d 100644 --- a/src/sfizz/import/sfizz_import.cpp +++ b/src/sfizz/import/sfizz_import.cpp @@ -4,7 +4,6 @@ // 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_import.h" #include "ForeignInstrument.h" diff --git a/src/sfizz/import/sfizz_import.h b/src/sfizz/import/sfizz_import.h index 7ea7312d..1b4ddf36 100644 --- a/src/sfizz/import/sfizz_import.h +++ b/src/sfizz/import/sfizz_import.h @@ -5,8 +5,7 @@ // If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz #pragma once - -typedef struct sfizz_synth_t sfizz_synth_t; +#include /** * @brief Loads or imports an instrument file. From 7f49eab79f37958e3cb8aafe4fa5a5fa06fb212f Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 22 Jun 2021 21:38:54 +0200 Subject: [PATCH 5/5] Mark the importer API with extern C --- src/sfizz/import/sfizz_import.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/sfizz/import/sfizz_import.h b/src/sfizz/import/sfizz_import.h index 1b4ddf36..7ae1184b 100644 --- a/src/sfizz/import/sfizz_import.h +++ b/src/sfizz/import/sfizz_import.h @@ -7,6 +7,10 @@ #pragma once #include +#ifdef __cplusplus +extern "C" { +#endif + /** * @brief Loads or imports an instrument file. * @@ -28,3 +32,7 @@ * - @b OFF: the function cannot be invoked while a thread is calling @b RT functions */ bool sfizz_load_or_import_file(sfizz_synth_t* synth, const char* path, const char** format); + +#ifdef __cplusplus +} // extern "C" +#endif