Added a buffer and voice tracker to check usage.

This commit is contained in:
paulfd 2019-08-25 02:13:20 +02:00
parent 2f203fe163
commit 7cc8edbd12
4 changed files with 26 additions and 6 deletions

View file

@ -1,6 +1,6 @@
#include "FilePool.h"
#include <chrono>
#include <memory>
using namespace std::chrono_literals;
std::optional<sfz::FilePool::FileInformation> 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<const char*>(file.c_str()));
auto fileLoaded = std::make_unique<StereoBuffer<float>>(fileToLoad.numFrames);
// auto deleteAndTrackBuffers = [this]
std::unique_ptr<StereoBuffer<float>, std::function<void(StereoBuffer<float>*)>> fileLoaded(new StereoBuffer<float>(fileToLoad.numFrames), deleteAndTrackBuffers);
auto readBuffer = std::make_unique<Buffer<float>>(fileToLoad.numFrames * 2);
sndFile.readf(readBuffer->data(), fileToLoad.numFrames);
fileLoaded->readInterleaved(*readBuffer);
ASSERT(fileLoaded != nullptr);
fileBuffers++;
fileToLoad.voice->setFileData(std::move(fileLoaded));
}
}

View file

@ -40,6 +40,14 @@ public:
};
std::optional<FileInformation> getFileInformation(std::string_view filename);
void enqueueLoading(Voice* voice, std::string_view sample, int numFrames);
static void deleteAndTrackBuffers(StereoBuffer<float>* 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<int> fileBuffers { 0 };
moodycamel::BlockingReaderWriterQueue<FileLoadingInformation> loadingQueue;
void loadingThread();
std::thread fileLoadingThread;
bool quitThread { false };
Buffer<float> tempReadBuffer { config::preloadSize * 2 };
// std::map<std::string_view, std::shared_ptr<StereoBuffer<float>>> preloadedData;
absl::flat_hash_map<std::string_view, std::shared_ptr<StereoBuffer<float>>> preloadedData;
LEAK_DETECTOR(FilePool);

View file

@ -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);
}
} };

View file

@ -63,7 +63,7 @@ public:
normalizePercents(region->amplitudeEG.getStart(ccState, velocity)));
}
void setFileData(std::unique_ptr<StereoBuffer<float>> file)
void setFileData(std::unique_ptr<StereoBuffer<float>, std::function<void(StereoBuffer<float>*)>> file)
{
fileData = std::move(file);
dataReady.store(true, std::memory_order_seq_cst);
@ -232,7 +232,7 @@ private:
uint32_t initialDelay;
std::atomic<bool> dataReady { false };
std::unique_ptr<StereoBuffer<float>> fileData;
std::unique_ptr<StereoBuffer<float>, std::function<void(StereoBuffer<float>*)>> fileData;
Buffer<float> tempBuffer1;
Buffer<float> tempBuffer2;