From cd6017f0e9b8c7019c2ab8f17972ade51fd04c1a Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Wed, 6 May 2020 19:10:35 +0200 Subject: [PATCH] Add the string loading API --- src/sfizz.h | 15 +++++++++++++++ src/sfizz.hpp | 16 ++++++++++++++++ src/sfizz/Synth.cpp | 26 ++++++++++++++++++++++++-- src/sfizz/Synth.h | 20 ++++++++++++++++++++ src/sfizz/sfizz.cpp | 5 +++++ src/sfizz/sfizz_wrapper.cpp | 6 ++++++ 6 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/sfizz.h b/src/sfizz.h index a941d59a..69be1a4a 100644 --- a/src/sfizz.h +++ b/src/sfizz.h @@ -70,6 +70,21 @@ SFIZZ_EXPORTED_API void sfizz_free(sfizz_synth_t* synth); */ SFIZZ_EXPORTED_API bool sfizz_load_file(sfizz_synth_t* synth, const char* path); +/** + * @brief Loads an SFZ file from textual data. This accepts a virtual + * path name for the imaginary sfz file, which is not required to + * exist on disk. The purpose of the virtual path is to locate + * samples with relative paths. + * + * @param synth The sfizz synth. + * @param path The virtual path of the SFZ file. + * @param text The contents of the virtual SFZ file. + * + * @return @true when file loading went OK, + * @false if some error occured while loading. + */ +SFIZZ_EXPORTED_API bool sfizz_load_string(sfizz_synth_t* synth, const char* path, const char* text); + /** * @brief Return the number of regions in the currently loaded SFZ file. * diff --git a/src/sfizz.hpp b/src/sfizz.hpp index 9c9d9208..8f0ad051 100644 --- a/src/sfizz.hpp +++ b/src/sfizz.hpp @@ -56,6 +56,22 @@ public: */ bool loadSfzFile(const std::string& path); + /** + * @brief Empties the current regions and load a new SFZ document from memory. + * + * This is similar to loadSfzFile() in functionality. + * This accepts a virtual path name for the imaginary sfz file, which is not + * required to exist on disk. The purpose of the virtual path is to locate + * samples with relative paths. + * + * @param path The virtual path of the SFZ file, as string. + * @param text The contents of the virtual SFZ file. + * + * @return @false if no regions were loaded, + * @true otherwise. + */ + bool loadSfzString(const std::string& path, const std::string& text); + /** * @brief Return the current number of regions loaded. */ diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index e9797e8a..ba747d54 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -336,6 +336,30 @@ bool sfz::Synth::loadSfzFile(const fs::path& file) if (regions.empty()) return false; + finalizeSfzLoad(); + + return true; +} + +bool sfz::Synth::loadSfzString(const fs::path& path, absl::string_view text) +{ + clear(); + + const std::lock_guard disableCallback { callbackGuard }; + parser.parseString(path, text); + if (parser.getErrorCount() > 0) + return false; + + if (regions.empty()) + return false; + + finalizeSfzLoad(); + + return true; +} + +void sfz::Synth::finalizeSfzLoad() +{ resources.filePool.setRootDirectory(parser.originalDirectory()); auto currentRegion = regions.begin(); @@ -469,8 +493,6 @@ bool sfz::Synth::loadSfzFile(const fs::path& file) voice->setMaxFiltersPerVoice(maxFilters); voice->setMaxEQsPerVoice(maxEQs); } - - return true; } sfz::Voice* sfz::Synth::findFreeVoice() noexcept diff --git a/src/sfizz/Synth.h b/src/sfizz/Synth.h index 0eddea0b..bfda4f42 100644 --- a/src/sfizz/Synth.h +++ b/src/sfizz/Synth.h @@ -90,6 +90,26 @@ public: * @return false if the file was not found or no regions were loaded. */ bool loadSfzFile(const fs::path& file); + /** + * @brief Empties the current regions and load a new SFZ document from memory. + * + * This is similar to loadSfzFile() in functionality. + * This accepts a virtual path name for the imaginary sfz file, which is not + * required to exist on disk. The purpose of the virtual path is to locate + * samples with relative paths. + * + * @param path The virtual path of the SFZ file, as string. + * @param text The contents of the virtual SFZ file. + * + * @return @false if no regions were loaded, + * @true otherwise. + */ + bool loadSfzString(const fs::path& path, absl::string_view text); + /** + * @brief Finalize SFZ loading, following a successful execution of the + * parsing step. + */ + void finalizeSfzLoad(); /** * @brief Get the current number of regions loaded * diff --git a/src/sfizz/sfizz.cpp b/src/sfizz/sfizz.cpp index 5ce7c133..6020dd11 100644 --- a/src/sfizz/sfizz.cpp +++ b/src/sfizz/sfizz.cpp @@ -23,6 +23,11 @@ bool sfz::Sfizz::loadSfzFile(const std::string& path) return synth->loadSfzFile(path); } +bool sfz::Sfizz::loadSfzString(const std::string& path, const std::string& text) +{ + return synth->loadSfzString(path, text); +} + int sfz::Sfizz::getNumRegions() const noexcept { return synth->getNumRegions(); diff --git a/src/sfizz/sfizz_wrapper.cpp b/src/sfizz/sfizz_wrapper.cpp index e27d0872..28eb3fdd 100644 --- a/src/sfizz/sfizz_wrapper.cpp +++ b/src/sfizz/sfizz_wrapper.cpp @@ -25,6 +25,12 @@ bool sfizz_load_file(sfizz_synth_t* synth, const char* path) return self->loadSfzFile(path); } +bool sfizz_load_string(sfizz_synth_t* synth, const char* path, const char* text) +{ + auto self = reinterpret_cast(synth); + return self->loadSfzString(path, text); +} + void sfizz_free(sfizz_synth_t* synth) { delete reinterpret_cast(synth);