From 6132fbd444bf7eae89e8b3fcf7e376a6a1bd3890 Mon Sep 17 00:00:00 2001 From: paulfd Date: Thu, 29 Aug 2019 10:59:17 +0200 Subject: [PATCH] Cleaned up the file pool --- sources/FilePool.cpp | 50 +++++++++++++++++--------------------------- sources/FilePool.h | 1 - 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/sources/FilePool.cpp b/sources/FilePool.cpp index f46013f4..0f0d9eff 100644 --- a/sources/FilePool.cpp +++ b/sources/FilePool.cpp @@ -4,10 +4,27 @@ #include #include #include +#include #include #include using namespace std::chrono_literals; + +template +void readFromFile(SndfileHandle& sndFile, int numFrames, StereoBuffer& output) +{ + if (sndFile.channels() == 1) { + auto tempReadBuffer = std::make_unique>(numFrames); + sndFile.readf(tempReadBuffer->data(), numFrames); + std::copy(tempReadBuffer->begin(), tempReadBuffer->end(), output.begin(Channel::left)); + std::copy(tempReadBuffer->begin(), tempReadBuffer->end(), output.begin(Channel::right)); + } else if (sndFile.channels() == 2) { + auto tempReadBuffer = std::make_unique>(2 * numFrames); + sndFile.readf(tempReadBuffer->data(), numFrames); + output.readInterleaved(*tempReadBuffer); + } +} + std::optional sfz::FilePool::getFileInformation(std::string_view filename) { std::filesystem::path file { rootDirectory / filename }; @@ -37,25 +54,7 @@ std::optional sfz::FilePool::getFileInformation( }(); returnedValue.preloadedData = std::make_shared>(preloadedSize); preloadedData[filename] = returnedValue.preloadedData; - // auto tempReadBuffer = std::make_unique>(2 * preloadedSize); - if (sndFile.channels() == 1) { - tempReadBuffer.resize(preloadedSize); - sndFile.readf(tempReadBuffer.data(), preloadedSize); - std::copy(tempReadBuffer.begin(), tempReadBuffer.end(), returnedValue.preloadedData->begin(Channel::left)); - std::copy(tempReadBuffer.begin(), tempReadBuffer.end(), returnedValue.preloadedData->begin(Channel::right)); - } else if (sndFile.channels() == 2) { - tempReadBuffer.resize(preloadedSize * 2); - sndFile.readf(tempReadBuffer.data(), preloadedSize); - returnedValue.preloadedData->readInterleaved(tempReadBuffer); - } - // std::stringstream dumpName { }; - // dumpName << filename << ".preloaded" << preloadedFilesWritten << ".bin"; - // dumpName << filename << ".preloaded" << ".bin"; - // std::ofstream ofile(dumpName.str(), std::ios::binary); - // preloadedFilesWritten++; - // for (auto& floatValue: tempReadBuffer) - // ofile.write((char*) &floatValue, sizeof(float)); - // ofile.close(); + readFromFile(sndFile, preloadedSize, *returnedValue.preloadedData); // char buffer [2048] ; // sndFile.command(SFC_GET_LOG_INFO, buffer, sizeof(buffer)) ; // DBG(buffer); @@ -90,18 +89,7 @@ void sfz::FilePool::loadingThread() SndfileHandle sndFile(reinterpret_cast(file.c_str())); auto fileLoaded = std::make_unique>(fileToLoad.numFrames); - if (sndFile.channels() == 1) { - tempReadBuffer.resize(fileToLoad.numFrames); - sndFile.readf(tempReadBuffer.data(), fileToLoad.numFrames); - std::copy(tempReadBuffer.begin(), tempReadBuffer.end(), fileLoaded->begin(Channel::left)); - std::copy(tempReadBuffer.begin(), tempReadBuffer.end(), fileLoaded->begin(Channel::right)); - } else if (sndFile.channels() == 2) { - tempReadBuffer.resize(fileToLoad.numFrames * 2); - sndFile.readf(tempReadBuffer.data(), fileToLoad.numFrames); - fileLoaded->readInterleaved(tempReadBuffer); - } - // std::unique_ptr, std::function*)>> fileLoaded(new StereoBuffer(framesRead), deleteAndTrackBuffers); - // fileBuffers++; + readFromFile(sndFile, fileToLoad.numFrames, *fileLoaded); fileToLoad.voice->setFileData(std::move(fileLoaded)); } } \ No newline at end of file diff --git a/sources/FilePool.h b/sources/FilePool.h index d106874b..b2448e0d 100644 --- a/sources/FilePool.h +++ b/sources/FilePool.h @@ -60,7 +60,6 @@ private: void loadingThread(); std::thread fileLoadingThread; bool quitThread { false }; - Buffer tempReadBuffer; absl::flat_hash_map>> preloadedData; LEAK_DETECTOR(FilePool); };