sfizz/sources/FilePool.cpp

107 lines
4.5 KiB
C++
Raw Normal View History

2019-08-05 00:38:24 +02:00
#include "FilePool.h"
#include "Globals.h"
#include "absl/types/span.h"
2019-08-07 23:39:35 +02:00
#include <chrono>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
2019-08-07 23:39:35 +02:00
using namespace std::chrono_literals;
2019-08-05 00:38:24 +02:00
std::optional<sfz::FilePool::FileInformation> sfz::FilePool::getFileInformation(std::string_view filename)
{
std::filesystem::path file { rootDirectory / filename };
if (!std::filesystem::exists(file))
return {};
2019-08-23 20:48:44 +02:00
SndfileHandle sndFile(reinterpret_cast<const char*>(file.c_str()));
if (sndFile.channels() != 1 && sndFile.channels() != 2) {
DBG("Missing logic for " << sndFile.channels() <<", discarding sample " << filename);
return {};
}
2019-08-05 00:38:24 +02:00
FileInformation returnedValue;
returnedValue.end = static_cast<uint32_t>(sndFile.frames());
returnedValue.sampleRate = static_cast<double>(sndFile.samplerate());
2019-08-05 00:38:24 +02:00
SF_INSTRUMENT instrumentInfo;
sndFile.command(SFC_GET_INSTRUMENT, &instrumentInfo, sizeof(instrumentInfo));
2019-08-23 20:48:44 +02:00
if (instrumentInfo.loop_count == 1) {
2019-08-05 00:38:24 +02:00
returnedValue.loopBegin = instrumentInfo.loops[0].start;
returnedValue.loopEnd = instrumentInfo.loops[0].end;
}
const auto preloadedSize = [&]() {
if (config::preloadSize == 0)
return returnedValue.end;
else
return std::min(returnedValue.end, static_cast<uint32_t>(config::preloadSize));
}();
2019-08-05 00:38:24 +02:00
returnedValue.preloadedData = std::make_shared<StereoBuffer<float>>(preloadedSize);
preloadedData[filename] = returnedValue.preloadedData;
// auto tempReadBuffer = std::make_unique<Buffer<float>>(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();
2019-08-05 00:38:24 +02:00
// char buffer [2048] ;
// sndFile.command(SFC_GET_LOG_INFO, buffer, sizeof(buffer)) ;
// DBG(buffer);
return returnedValue;
}
void sfz::FilePool::enqueueLoading(Voice* voice, std::string_view sample, int numFrames)
{
2019-08-23 20:48:44 +02:00
if (!loadingQueue.try_enqueue({ voice, sample, numFrames })) {
DBG("Problem enqueuing a file read for file " << sample);
2019-08-11 11:10:04 +02:00
}
}
void sfz::FilePool::loadingThread()
{
2019-08-11 11:10:04 +02:00
FileLoadingInformation fileToLoad {};
2019-08-23 20:48:44 +02:00
while (!quitThread) {
2019-08-23 01:27:39 +02:00
if (!loadingQueue.wait_dequeue_timed(fileToLoad, 1ms))
2019-08-07 23:39:35 +02:00
continue;
2019-08-23 20:48:44 +02:00
if (fileToLoad.voice == nullptr) {
DBG("Background thread error: voice is null.");
continue;
}
DBG("Background loading of: " << fileToLoad.sample);
std::filesystem::path file { rootDirectory / fileToLoad.sample };
2019-08-23 20:48:44 +02:00
if (!std::filesystem::exists(file)) {
DBG("Background thread: no file " << fileToLoad.sample << " exists.");
continue;
}
2019-08-23 20:48:44 +02:00
SndfileHandle sndFile(reinterpret_cast<const char*>(file.c_str()));
auto fileLoaded = std::make_unique<StereoBuffer<float>>(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<StereoBuffer<float>, std::function<void(StereoBuffer<float>*)>> fileLoaded(new StereoBuffer<float>(framesRead), deleteAndTrackBuffers);
// fileBuffers++;
fileToLoad.voice->setFileData(std::move(fileLoaded));
}
2019-08-05 00:38:24 +02:00
}