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; 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/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; 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..f73fc67d --- /dev/null +++ b/src/sfizz/import/sfizz_import.cpp @@ -0,0 +1,32 @@ +// 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_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..7ae1184b --- /dev/null +++ b/src/sfizz/import/sfizz_import.h @@ -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 + +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @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); + +#ifdef __cplusplus +} // extern "C" +#endif