Use a custom SFZ path set in user configuration

This commit is contained in:
Jean Pierre Cimalando 2020-12-09 15:40:42 +01:00
parent 0acbce5274
commit 0cb1b67bfa
3 changed files with 57 additions and 27 deletions

View file

@ -6,10 +6,10 @@
#include "SfizzFileScan.h" #include "SfizzFileScan.h"
#include "SfizzForeignPaths.h" #include "SfizzForeignPaths.h"
#include "SfizzSettings.h"
#include "NativeHelpers.h" #include "NativeHelpers.h"
#include <absl/strings/ascii.h> #include <absl/strings/ascii.h>
#include <absl/algorithm/container.h> #include <absl/algorithm/container.h>
#include <vector>
#include <memory> #include <memory>
// wait at least this much before refreshing the file rescan // wait at least this much before refreshing the file rescan
@ -63,7 +63,7 @@ void SfzFileScan::refreshScan(bool force)
FileTrieBuilder builder; FileTrieBuilder builder;
for (const fs::path& dirPath : SfizzPaths::sfzDefaultPaths()) { for (const fs::path& dirPath : SfizzPaths::getSfzSearchPaths()) {
std::error_code ec; std::error_code ec;
const fs::directory_options dirOpts = const fs::directory_options dirOpts =
fs::directory_options::skip_permission_denied; fs::directory_options::skip_permission_denied;
@ -183,9 +183,8 @@ const fs::path& SfzFileScan::electBestMatch(const fs::path& path, absl::Span<con
namespace SfizzPaths { namespace SfizzPaths {
absl::Span<const fs::path> sfzDefaultPaths() std::vector<fs::path> getSfzSearchPaths()
{ {
static const auto paths = []() -> std::vector<fs::path> {
std::vector<fs::path> paths; std::vector<fs::path> paths;
paths.reserve(8); paths.reserve(8);
auto addPath = [&paths](const fs::path& newPath) { auto addPath = [&paths](const fs::path& newPath) {
@ -193,7 +192,12 @@ absl::Span<const fs::path> sfzDefaultPaths()
paths.push_back(newPath); paths.push_back(newPath);
}; };
addPath(getUserDocumentsDirectory() / "SFZ instruments"); absl::optional<fs::path> configDefaultPath = getSfzConfigDefaultPath();
fs::path fallbackDefaultPath = getSfzFallbackDefaultPath();
if (configDefaultPath)
addPath(*configDefaultPath);
addPath(fallbackDefaultPath);
for (const fs::path& foreign : { for (const fs::path& foreign : {
getAriaPathSetting("user_files_dir"), getAriaPathSetting("user_files_dir"),
@ -203,16 +207,28 @@ absl::Span<const fs::path> sfzDefaultPaths()
paths.shrink_to_fit(); paths.shrink_to_fit();
return paths; return paths;
}();
return paths;
} }
void createSfzDefaultPaths() absl::optional<fs::path> getSfzConfigDefaultPath()
{ {
for (const fs::path& path : sfzDefaultPaths()) { SfizzSettings settings;
std::error_code ec; fs::path path = fs::u8path(settings.load_or("user_files_dir", {}));
fs::create_directory(path, ec); 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 } // namespace SfizzPaths

View file

@ -10,6 +10,7 @@
#include <absl/types/optional.h> #include <absl/types/optional.h>
#include <ghc/fs_std.hpp> #include <ghc/fs_std.hpp>
#include <string> #include <string>
#include <vector>
#include <list> #include <list>
#include <unordered_map> #include <unordered_map>
#include <mutex> #include <mutex>
@ -35,6 +36,8 @@ private:
}; };
namespace SfizzPaths { namespace SfizzPaths {
absl::Span<const fs::path> sfzDefaultPaths(); std::vector<fs::path> getSfzSearchPaths();
void createSfzDefaultPaths(); absl::optional<fs::path> getSfzConfigDefaultPath();
void setSfzConfigDefaultPath(const fs::path& path);
fs::path getSfzFallbackDefaultPath();
} // namespace SfizzPaths } // namespace SfizzPaths

View file

@ -38,7 +38,18 @@ SfizzVstProcessor::SfizzVstProcessor()
{ {
setControllerClass(SfizzVstController::cid); setControllerClass(SfizzVstController::cid);
SfizzPaths::createSfzDefaultPaths(); // ensure the SFZ path exists:
// the one specified in the configuration, otherwise the fallback
absl::optional<fs::path> 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() SfizzVstProcessor::~SfizzVstProcessor()