diff --git a/sfizz/FilePool.cpp b/sfizz/FilePool.cpp index f08c5dfc..c5613f0c 100644 --- a/sfizz/FilePool.cpp +++ b/sfizz/FilePool.cpp @@ -45,7 +45,7 @@ std::unique_ptr> readFromFile(SndfileHandle& sndFile, int numFram return returnedBuffer; } -std::optional sfz::FilePool::getFileInformation(std::string_view filename) noexcept +std::optional sfz::FilePool::getFileInformation(std::string_view filename, uint32_t offset) noexcept { std::filesystem::path file { rootDirectory / filename }; if (!std::filesystem::exists(file)) @@ -72,11 +72,15 @@ std::optional sfz::FilePool::getFileInformation( if (config::preloadSize == 0) return returnedValue.end; else - return std::min(returnedValue.end, static_cast(config::preloadSize)); + return std::min(returnedValue.end, offset + static_cast(config::preloadSize)); }(); if (preloadedData.contains(filename)) { - returnedValue.preloadedData = preloadedData[filename]; + auto alreadyPreloaded = preloadedData[filename]; + if (preloadedSize > alreadyPreloaded->getNumFrames()) { + alreadyPreloaded.reset(readFromFile(sndFile, preloadedSize).get()); + } + returnedValue.preloadedData = alreadyPreloaded; } else { returnedValue.preloadedData = std::shared_ptr>(readFromFile(sndFile, preloadedSize)); preloadedData[filename] = returnedValue.preloadedData; diff --git a/sfizz/FilePool.h b/sfizz/FilePool.h index a6295fd2..31e31fb8 100644 --- a/sfizz/FilePool.h +++ b/sfizz/FilePool.h @@ -57,7 +57,7 @@ public: double sampleRate { config::defaultSampleRate }; std::shared_ptr> preloadedData; }; - std::optional getFileInformation(std::string_view filename) noexcept; + std::optional getFileInformation(std::string_view filename, uint32_t offset) noexcept; void enqueueLoading(Voice* voice, std::string_view sample, int numFrames, unsigned ticket) noexcept; private: std::filesystem::path rootDirectory; diff --git a/sfizz/Synth.cpp b/sfizz/Synth.cpp index e59d66ea..86166454 100644 --- a/sfizz/Synth.cpp +++ b/sfizz/Synth.cpp @@ -194,7 +194,7 @@ bool sfz::Synth::loadSfzFile(const std::filesystem::path& filename) auto region = currentRegion->get(); if (!region->isGenerator()) { - auto fileInformation = filePool.getFileInformation(region->sample); + auto fileInformation = filePool.getFileInformation(region->sample, region->offset + region->offsetRandom); if (!fileInformation) { DBG("Removing the region with sample " << region->sample); std::iter_swap(currentRegion, lastRegion);