sfizz/sources/FilePool.cpp

95 lines
3.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>
2019-08-29 10:59:17 +02:00
#include <sndfile.hh>
#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
2019-08-29 10:59:17 +02:00
template<class T>
void readFromFile(SndfileHandle& sndFile, int numFrames, StereoBuffer<T>& output)
{
if (sndFile.channels() == 1) {
auto tempReadBuffer = std::make_unique<Buffer<float>>(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<Buffer<float>>(2 * numFrames);
sndFile.readf(tempReadBuffer->data(), numFrames);
output.readInterleaved(*tempReadBuffer);
}
}
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;
2019-08-29 10:59:17 +02:00
readFromFile(sndFile, preloadedSize, *returnedValue.preloadedData);
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);
2019-08-29 10:59:17 +02:00
readFromFile(sndFile, fileToLoad.numFrames, *fileLoaded);
fileToLoad.voice->setFileData(std::move(fileLoaded));
}
2019-08-05 00:38:24 +02:00
}