Skip to content

Commit

Permalink
Store files to load and their offset and defer loading
Browse files Browse the repository at this point in the history
Better behavior when using a file multiple times with different offsets.
  • Loading branch information
paulfd committed Jun 28, 2021
1 parent 6b1a7cd commit 7d9e807
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/sfizz/Synth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ void Synth::Impl::finalizeSfzLoad()
size_t currentRegionIndex = 0;
size_t currentRegionCount = layers_.size();

filesToLoad_.clear();

auto removeCurrentRegion = [this, &currentRegionIndex, &currentRegionCount]() {
const Region& region = layers_[currentRegionIndex]->getRegion();
DBG("Removing the region with sample " << *region.sampleId);
Expand Down Expand Up @@ -679,10 +681,7 @@ void Synth::Impl::finalizeSfzLoad()
return Default::offsetMod.bounds.clamp(sumOffsetCC);
}();

if (!filePool.preloadFile(*region.sampleId, maxOffset)) {
removeCurrentRegion();
continue;
}
filesToLoad_[*region.sampleId] = maxOffset;
}
else if (!region.isGenerator()) {
if (!wavePool.createFileWave(filePool, std::string(region.sampleId->filename()))) {
Expand Down Expand Up @@ -763,6 +762,11 @@ void Synth::Impl::finalizeSfzLoad()

++currentRegionIndex;
}

for (const auto& toLoad: filesToLoad_) {
filePool.preloadFile(toLoad.first, toLoad.second);
}

if (currentRegionCount < layers_.size()) {
DBG("Removing " << (layers_.size() - currentRegionCount)
<< " out of " << layers_.size() << " regions");
Expand Down
3 changes: 3 additions & 0 deletions src/sfizz/SynthPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ struct Synth::Impl final: public Parser::Listener {
// Singletons passed as references to the voices
Resources resources_;

// Temporary structure to hold the files to load and their max offset
absl::flat_hash_map<sfz::FileId, int64_t> filesToLoad_;

// Root path
std::string rootPath_;

Expand Down

0 comments on commit 7d9e807

Please sign in to comment.