Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added status full loaded into FileData #1246

Merged
merged 10 commits into from
Mar 20, 2024
Next Next commit
Added FileData::Status::FullLoaded
KKQ-KKQ committed Feb 13, 2024
commit 1d004c223229dd979fbca2c9eb3e5c78e56a700b
41 changes: 28 additions & 13 deletions src/sfizz/FilePool.cpp
Original file line number Diff line number Diff line change
@@ -332,19 +332,22 @@ bool sfz::FilePool::preloadFile(const FileId& fileId, uint32_t maxOffset) noexce

const auto existingFile = preloadedFiles.find(fileId);
if (existingFile != preloadedFiles.end()) {
if (framesToLoad > existingFile->second.preloadedData.getNumFrames()) {
auto& fileData = existingFile->second;
if (framesToLoad > fileData.preloadedData.getNumFrames()) {
preloadedFiles[fileId].information.maxOffset = maxOffset;
preloadedFiles[fileId].preloadedData = readFromFile(*reader, framesToLoad);
if (fileData.status == FileData::Status::Preloaded)
fileData.status = frames == framesToLoad ? FileData::Status::FullLoaded : FileData::Status::Preloaded;
}
existingFile->second.preloadCallCount++;
fileData.preloadCallCount++;
} else {
fileInformation->sampleRate = static_cast<double>(reader->sampleRate());
auto insertedPair = preloadedFiles.insert_or_assign(fileId, {
readFromFile(*reader, framesToLoad),
*fileInformation
});

insertedPair.first->second.status = FileData::Status::Preloaded;
insertedPair.first->second.status = frames == framesToLoad ? FileData::Status::FullLoaded : FileData::Status::Preloaded;
insertedPair.first->second.preloadCallCount++;
}

@@ -399,7 +402,7 @@ sfz::FileDataHolder sfz::FilePool::loadFile(const FileId& fileId) noexcept
readFromFile(*reader, frames),
*fileInformation
});
insertedPair.first->second.status = FileData::Status::Preloaded;
insertedPair.first->second.status = FileData::Status::FullLoaded;
insertedPair.first->second.preloadCallCount++;
return { &insertedPair.first->second };
}
@@ -417,7 +420,7 @@ sfz::FileDataHolder sfz::FilePool::loadFromRam(const FileId& fileId, const std::
readFromFile(*reader, frames),
*fileInformation
});
insertedPair.first->second.status = FileData::Status::Preloaded;
insertedPair.first->second.status = FileData::Status::FullLoaded;
insertedPair.first->second.preloadCallCount++;
DBG("Added a file " << fileId.filename());
return { &insertedPair.first->second };
@@ -435,8 +438,9 @@ sfz::FileDataHolder sfz::FilePool::getFilePromise(const std::shared_ptr<FileId>&
return {};
}

if (!loadInRam) {
QueuedFileData queuedData { fileId, &preloaded->second };
auto& fileData = preloaded->second;
if (fileData.status != FileData::Status::FullLoaded) {
QueuedFileData queuedData { fileId, &fileData };
if (!filesToLoad->try_push(queuedData)) {
DBG("[sfizz] Could not enqueue the file to load for " << fileId << " (queue capacity " << filesToLoad->capacity() << ")");
return {};
@@ -458,10 +462,19 @@ void sfz::FilePool::setPreloadSize(uint32_t preloadSize) noexcept

// Update all the preloaded sizes
for (auto& preloadedFile : preloadedFiles) {
const auto maxOffset = preloadedFile.second.information.maxOffset;
fs::path file { rootDirectory / preloadedFile.first.filename() };
AudioReaderPtr reader = createAudioReader(file, preloadedFile.first.isReverse());
preloadedFile.second.preloadedData = readFromFile(*reader, preloadSize + maxOffset);
auto& fileId = preloadedFile.first;
auto& fileData = preloadedFile.second;
const uint32_t maxOffset = fileData.information.maxOffset;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to make the maxOffset in information a uint32_t. I don't recall why I put an int64_t, maybe for full generality and to better support integer arithmetics since you tend to add or remove offsets around. Was there a reason for you to force uint32_t here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I will fix this.

fs::path file { rootDirectory / fileId.filename() };
AudioReaderPtr reader = createAudioReader(file, fileId.isReverse());
const uint32_t frames = static_cast<uint32_t>(reader->frames());
const uint32_t framesToLoad = min(frames, maxOffset + preloadSize);
fileData.preloadedData = readFromFile(*reader, framesToLoad);

const FileData::Status status = fileData.status;
if (status == FileData::Status::FullLoaded || status == FileData::Status::Preloaded) {
fileData.status = frames == framesToLoad ? FileData::Status::FullLoaded : FileData::Status::Preloaded;
}
}
}

@@ -619,10 +632,12 @@ void sfz::FilePool::setRamLoading(bool loadInRam) noexcept
for (auto& preloadedFile : preloadedFiles) {
fs::path file { rootDirectory / preloadedFile.first.filename() };
AudioReaderPtr reader = createAudioReader(file, preloadedFile.first.isReverse());
preloadedFile.second.preloadedData = readFromFile(
auto& fileData = preloadedFile.second;
fileData.preloadedData = readFromFile(
*reader,
preloadedFile.second.information.end
fileData.information.end
);
fileData.status = FileData::Status::FullLoaded;
}
} else {
setPreloadSize(preloadSize);
2 changes: 1 addition & 1 deletion src/sfizz/FilePool.h
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ struct FileInformation {
// Strict C++11 disallows member initialization if aggregate initialization is to be used...
struct FileData
{
enum class Status { Invalid, Preloaded, Streaming, Done };
enum class Status { Invalid, Preloaded, Streaming, Done, FullLoaded };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this FullyLoaded.

Copy link
Contributor Author

@KKQ-KKQ KKQ-KKQ Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better that we have a boolean fullyLoaded variable on FileData instead of adding it's status enum.

FileData() = default;
FileData(FileAudioBuffer preloaded, FileInformation info)
: preloadedData(std::move(preloaded)), information(std::move(info))