From 7cc8edbd1260a16286bdeb64400bc3436b65a10d Mon Sep 17 00:00:00 2001 From: paulfd Date: Sun, 25 Aug 2019 02:13:20 +0200 Subject: [PATCH] Added a buffer and voice tracker to check usage. --- sources/FilePool.cpp | 10 ++++++---- sources/FilePool.h | 12 ++++++++++++ sources/Synth.h | 6 ++++++ sources/Voice.h | 4 ++-- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/sources/FilePool.cpp b/sources/FilePool.cpp index a8a62a83..2b4f36aa 100644 --- a/sources/FilePool.cpp +++ b/sources/FilePool.cpp @@ -1,6 +1,6 @@ #include "FilePool.h" - #include +#include using namespace std::chrono_literals; std::optional sfz::FilePool::getFileInformation(std::string_view filename) @@ -49,20 +49,22 @@ void sfz::FilePool::loadingThread() DBG("Background thread error: voice is null."); continue; } - + DBG("Background loading of: " << fileToLoad.sample); std::filesystem::path file { rootDirectory / fileToLoad.sample }; if (!std::filesystem::exists(file)) { DBG("Background thread: no file " << fileToLoad.sample << " exists."); continue; } - + SndfileHandle sndFile(reinterpret_cast(file.c_str())); - auto fileLoaded = std::make_unique>(fileToLoad.numFrames); + // auto deleteAndTrackBuffers = [this] + std::unique_ptr, std::function*)>> fileLoaded(new StereoBuffer(fileToLoad.numFrames), deleteAndTrackBuffers); auto readBuffer = std::make_unique>(fileToLoad.numFrames * 2); sndFile.readf(readBuffer->data(), fileToLoad.numFrames); fileLoaded->readInterleaved(*readBuffer); ASSERT(fileLoaded != nullptr); + fileBuffers++; fileToLoad.voice->setFileData(std::move(fileLoaded)); } } \ No newline at end of file diff --git a/sources/FilePool.h b/sources/FilePool.h index 5e0cf1ea..ccae535b 100644 --- a/sources/FilePool.h +++ b/sources/FilePool.h @@ -40,6 +40,14 @@ public: }; std::optional getFileInformation(std::string_view filename); void enqueueLoading(Voice* voice, std::string_view sample, int numFrames); + static void deleteAndTrackBuffers(StereoBuffer* buffer) { + fileBuffers--; + delete buffer; + }; + static int getFileBuffers() + { + return fileBuffers.load(); + } private: std::filesystem::path rootDirectory; struct FileLoadingInformation @@ -48,11 +56,15 @@ private: std::string_view sample; int numFrames; }; + + inline static std::atomic fileBuffers { 0 }; + moodycamel::BlockingReaderWriterQueue loadingQueue; void loadingThread(); std::thread fileLoadingThread; bool quitThread { false }; Buffer tempReadBuffer { config::preloadSize * 2 }; + // std::map>> preloadedData; absl::flat_hash_map>> preloadedData; LEAK_DETECTOR(FilePool); diff --git a/sources/Synth.h b/sources/Synth.h index b4d5cd68..f66100e5 100644 --- a/sources/Synth.h +++ b/sources/Synth.h @@ -176,8 +176,14 @@ private: bool threadsShouldQuit { false }; std::thread garbageCollectionThread { [&]() { while (!threadsShouldQuit) { + auto activeVoices { 0 }; for (auto& voice : voices) + { voice->garbageCollect(); + if (!voice->isFree()) + activeVoices++; + } + DBG("Active voices:" << activeVoices << " | Stray buffers: " << FilePool::getFileBuffers()); std::this_thread::sleep_for(1s); } } }; diff --git a/sources/Voice.h b/sources/Voice.h index d619294e..01bc2bfc 100644 --- a/sources/Voice.h +++ b/sources/Voice.h @@ -63,7 +63,7 @@ public: normalizePercents(region->amplitudeEG.getStart(ccState, velocity))); } - void setFileData(std::unique_ptr> file) + void setFileData(std::unique_ptr, std::function*)>> file) { fileData = std::move(file); dataReady.store(true, std::memory_order_seq_cst); @@ -232,7 +232,7 @@ private: uint32_t initialDelay; std::atomic dataReady { false }; - std::unique_ptr> fileData; + std::unique_ptr, std::function*)>> fileData; Buffer tempBuffer1; Buffer tempBuffer2;