From 0cb1b67bfab45963440d3cde36e875bfc1591019 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Wed, 9 Dec 2020 15:40:42 +0100 Subject: [PATCH] Use a custom SFZ path set in user configuration --- vst/SfizzFileScan.cpp | 64 ++++++++++++++++++++++++--------------- vst/SfizzFileScan.h | 7 +++-- vst/SfizzVstProcessor.cpp | 13 +++++++- 3 files changed, 57 insertions(+), 27 deletions(-) diff --git a/vst/SfizzFileScan.cpp b/vst/SfizzFileScan.cpp index 9971d6ab..a56f2e4a 100644 --- a/vst/SfizzFileScan.cpp +++ b/vst/SfizzFileScan.cpp @@ -6,10 +6,10 @@ #include "SfizzFileScan.h" #include "SfizzForeignPaths.h" +#include "SfizzSettings.h" #include "NativeHelpers.h" #include #include -#include #include // wait at least this much before refreshing the file rescan @@ -63,7 +63,7 @@ void SfzFileScan::refreshScan(bool force) FileTrieBuilder builder; - for (const fs::path& dirPath : SfizzPaths::sfzDefaultPaths()) { + for (const fs::path& dirPath : SfizzPaths::getSfzSearchPaths()) { std::error_code ec; const fs::directory_options dirOpts = fs::directory_options::skip_permission_denied; @@ -183,36 +183,52 @@ const fs::path& SfzFileScan::electBestMatch(const fs::path& path, absl::Span sfzDefaultPaths() +std::vector getSfzSearchPaths() { - static const auto paths = []() -> std::vector { - std::vector paths; - paths.reserve(8); - auto addPath = [&paths](const fs::path& newPath) { - if (absl::c_find(paths, newPath) == paths.end()) - paths.push_back(newPath); - }; + std::vector paths; + paths.reserve(8); + auto addPath = [&paths](const fs::path& newPath) { + if (absl::c_find(paths, newPath) == paths.end()) + paths.push_back(newPath); + }; - addPath(getUserDocumentsDirectory() / "SFZ instruments"); + absl::optional configDefaultPath = getSfzConfigDefaultPath(); + fs::path fallbackDefaultPath = getSfzFallbackDefaultPath(); - for (const fs::path& foreign : { - getAriaPathSetting("user_files_dir"), - getAriaPathSetting("Converted_path") }) - if (!foreign.empty() && foreign.is_absolute()) - addPath(foreign); + if (configDefaultPath) + addPath(*configDefaultPath); + addPath(fallbackDefaultPath); - paths.shrink_to_fit(); - return paths; - }(); + for (const fs::path& foreign : { + getAriaPathSetting("user_files_dir"), + getAriaPathSetting("Converted_path") }) + if (!foreign.empty() && foreign.is_absolute()) + addPath(foreign); + + paths.shrink_to_fit(); return paths; } -void createSfzDefaultPaths() +absl::optional getSfzConfigDefaultPath() { - for (const fs::path& path : sfzDefaultPaths()) { - std::error_code ec; - fs::create_directory(path, ec); - } + SfizzSettings settings; + fs::path path = fs::u8path(settings.load_or("user_files_dir", {})); + if (path.empty() || !path.is_absolute()) + return {}; + return std::move(path); +} + +void setSfzConfigDefaultPath(const fs::path& path) +{ + if (path.empty() || !path.is_absolute()) + return; + SfizzSettings settings; + settings.store("user_files_dir", path.u8string()); +} + +fs::path getSfzFallbackDefaultPath() +{ + return getUserDocumentsDirectory() / "SFZ instruments"; } } // namespace SfizzPaths diff --git a/vst/SfizzFileScan.h b/vst/SfizzFileScan.h index 2626ef15..19dabf56 100644 --- a/vst/SfizzFileScan.h +++ b/vst/SfizzFileScan.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -35,6 +36,8 @@ private: }; namespace SfizzPaths { -absl::Span sfzDefaultPaths(); -void createSfzDefaultPaths(); +std::vector getSfzSearchPaths(); +absl::optional getSfzConfigDefaultPath(); +void setSfzConfigDefaultPath(const fs::path& path); +fs::path getSfzFallbackDefaultPath(); } // namespace SfizzPaths diff --git a/vst/SfizzVstProcessor.cpp b/vst/SfizzVstProcessor.cpp index 77de7c43..708979eb 100644 --- a/vst/SfizzVstProcessor.cpp +++ b/vst/SfizzVstProcessor.cpp @@ -38,7 +38,18 @@ SfizzVstProcessor::SfizzVstProcessor() { setControllerClass(SfizzVstController::cid); - SfizzPaths::createSfzDefaultPaths(); + // ensure the SFZ path exists: + // the one specified in the configuration, otherwise the fallback + absl::optional configDefaultPath = SfizzPaths::getSfzConfigDefaultPath(); + if (configDefaultPath) { + std::error_code ec; + fs::create_directory(*configDefaultPath, ec); + } + else { + fs::path fallbackDefaultPath = SfizzPaths::getSfzFallbackDefaultPath(); + std::error_code ec; + fs::create_directory(fallbackDefaultPath, ec); + } } SfizzVstProcessor::~SfizzVstProcessor()