#pragma once #include "StereoBuffer.h" #include "Defaults.h" #include #include #include #include #include #include namespace sfz { class FilePool { public: FilePool() = default; void setRootDirectory(const std::filesystem::path& directory) { rootDirectory = directory; } struct FileInformation { uint32_t end { Default::sampleEndRange.getEnd() }; uint32_t loopBegin { Default::loopRange.getStart() }; uint32_t loopEnd { Default::loopRange.getEnd() }; std::shared_ptr> preloadedData; }; std::optional getFileInformation(std::string_view filename) { std::filesystem::path file { rootDirectory / filename }; if (!std::filesystem::exists(file)) return {}; SndfileHandle sndFile ( reinterpret_cast(file.c_str()) ); FileInformation returnedValue; returnedValue.end = static_cast(sndFile.frames()); SF_INSTRUMENT instrumentInfo; sndFile.command(SFC_GET_INSTRUMENT, &instrumentInfo, sizeof(instrumentInfo)); if (instrumentInfo.loop_count == 1) { returnedValue.loopBegin = instrumentInfo.loops[0].start; returnedValue.loopEnd = instrumentInfo.loops[0].end; } auto preloadedSize = std::min(returnedValue.end, static_cast(config::preloadSize)); returnedValue.preloadedData = std::make_shared>(preloadedSize); sndFile.readf(tempReadBuffer.data(), preloadedSize); returnedValue.preloadedData->readInterleaved(tempReadBuffer.data(), preloadedSize); preloadedData[filename] = returnedValue.preloadedData; // char buffer [2048] ; // sndFile.command(SFC_GET_LOG_INFO, buffer, sizeof(buffer)) ; // DBG(buffer); return returnedValue; } size_t getNumPreloadedSamples() { return preloadedData.size(); } private: std::filesystem::path rootDirectory; Buffer tempReadBuffer { config::preloadSize * 2 }; // std::map>> preloadedData; absl::flat_hash_map>> preloadedData; }; static inline FilePool filePool; }