Merge branch 'bugfix/cancel_background_loads_on_voice_change' into develop

This commit is contained in:
Paul Ferrand 2019-11-30 16:33:00 +01:00
commit b8bee5429e
3 changed files with 16 additions and 1 deletions

View file

@ -112,6 +112,12 @@ void sfz::FilePool::enqueueLoading(Voice* voice, const std::string* sample, int
void sfz::FilePool::loadingThread() noexcept
{
while (!quitThread) {
if (emptyQueue) {
while(loadingQueue.pop()) {}
emptyQueue = false;
continue;
}
FileLoadingInformation fileToLoad {};
if (!loadingQueue.wait_dequeue_timed(fileToLoad, 200ms)) {
continue;
@ -135,7 +141,7 @@ void sfz::FilePool::loadingThread() noexcept
}
SndfileHandle sndFile(reinterpret_cast<const char*>(file.c_str()));
std::lock_guard<std::mutex> guard { fileHandleMutex };
fileHandles.emplace_back(readFromFile<float>(sndFile, fileToLoad.numFrames));
fileToLoad.voice->setFileData(fileHandles.back(), fileToLoad.ticket);

View file

@ -113,6 +113,8 @@ public:
*
*/
void clear();
void emptyFileLoadingQueue() noexcept { emptyQueue = true; }
bool shouldEmptyQueue() const noexcept { return emptyQueue; }
private:
fs::path rootDirectory;
struct FileLoadingInformation {
@ -126,6 +128,7 @@ private:
void loadingThread() noexcept;
void garbageThread() noexcept;
bool quitThread { false };
bool emptyQueue { false };
std::mutex fileHandleMutex;
std::vector<std::shared_ptr<AudioBuffer<float>>> fileHandles;
absl::flat_hash_map<absl::string_view, std::shared_ptr<AudioBuffer<float>>> preloadedData;

View file

@ -533,6 +533,12 @@ void sfz::Synth::resetVoices(int numVoices)
std::this_thread::sleep_for(1ms);
}
// Empty the file pool loading queue and spinlock on it
filePool.emptyFileLoadingQueue();
while (filePool.shouldEmptyQueue()) {
std::this_thread::sleep_for(1ms);
}
voices.clear();
for (int i = 0; i < numVoices; ++i)
voices.push_back(std::make_unique<Voice>(midiState));