chrono literals and changes file handles
to enable aggregate initialization
This commit is contained in:
parent
2daf53ab6c
commit
bc8d2b61b8
2 changed files with 17 additions and 17 deletions
|
|
@ -32,10 +32,10 @@
|
||||||
#include "AtomicGuard.h"
|
#include "AtomicGuard.h"
|
||||||
#include "absl/types/span.h"
|
#include "absl/types/span.h"
|
||||||
#include "absl/strings/match.h"
|
#include "absl/strings/match.h"
|
||||||
|
#include "absl/memory/memory.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sndfile.hh>
|
#include <sndfile.hh>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
using namespace std::chrono_literals;
|
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
void readBaseFile(SndfileHandle& sndFile, sfz::AudioBuffer<T>& output, uint32_t numFrames)
|
void readBaseFile(SndfileHandle& sndFile, sfz::AudioBuffer<T>& output, uint32_t numFrames)
|
||||||
|
|
@ -57,13 +57,13 @@ void readBaseFile(SndfileHandle& sndFile, sfz::AudioBuffer<T>& output, uint32_t
|
||||||
template <class T>
|
template <class T>
|
||||||
std::unique_ptr<sfz::AudioBuffer<T>> readFromFile(SndfileHandle& sndFile, uint32_t numFrames, sfz::Oversampling factor)
|
std::unique_ptr<sfz::AudioBuffer<T>> readFromFile(SndfileHandle& sndFile, uint32_t numFrames, sfz::Oversampling factor)
|
||||||
{
|
{
|
||||||
auto baseBuffer = std::make_unique<sfz::AudioBuffer<T>>();
|
auto baseBuffer = absl::make_unique<sfz::AudioBuffer<T>>();
|
||||||
readBaseFile(sndFile, *baseBuffer, numFrames);
|
readBaseFile(sndFile, *baseBuffer, numFrames);
|
||||||
|
|
||||||
if (factor == sfz::Oversampling::x1)
|
if (factor == sfz::Oversampling::x1)
|
||||||
return baseBuffer;
|
return baseBuffer;
|
||||||
|
|
||||||
auto outputBuffer = std::make_unique<sfz::AudioBuffer<T>>(sndFile.channels(), numFrames * static_cast<int>(factor));
|
auto outputBuffer = absl::make_unique<sfz::AudioBuffer<T>>(sndFile.channels(), numFrames * static_cast<int>(factor));
|
||||||
sfz::Oversampler oversampler { factor };
|
sfz::Oversampler oversampler { factor };
|
||||||
oversampler.stream(*baseBuffer, *outputBuffer);
|
oversampler.stream(*baseBuffer, *outputBuffer);
|
||||||
return outputBuffer;
|
return outputBuffer;
|
||||||
|
|
@ -216,10 +216,9 @@ bool sfz::FilePool::preloadFile(const std::string& filename, uint32_t maxOffset)
|
||||||
preloadedFiles[filename].preloadedData = readFromFile<float>(sndFile, framesToLoad, oversamplingFactor);
|
preloadedFiles[filename].preloadedData = readFromFile<float>(sndFile, framesToLoad, oversamplingFactor);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
preloadedFiles.insert_or_assign(filename, {
|
const float sourceSampleRate { static_cast<float>(oversamplingFactor) * static_cast<float>(sndFile.samplerate()) };
|
||||||
readFromFile<float>(sndFile, framesToLoad, oversamplingFactor),
|
PreloadedFileHandle handle { readFromFile<float>(sndFile, framesToLoad, oversamplingFactor), sourceSampleRate };
|
||||||
static_cast<float>(oversamplingFactor) * static_cast<float>(sndFile.samplerate())
|
preloadedFiles.insert_or_assign(filename, handle);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -272,7 +271,7 @@ void sfz::FilePool::tryToClearPromises()
|
||||||
AtomicDisabler disabler { canAddPromisesToClear };
|
AtomicDisabler disabler { canAddPromisesToClear };
|
||||||
|
|
||||||
while (addingPromisesToClear)
|
while (addingPromisesToClear)
|
||||||
std::this_thread::sleep_for(1ms);
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
|
|
||||||
for (auto& promise: promisesToClear) {
|
for (auto& promise: promisesToClear) {
|
||||||
if (promise->dataReady)
|
if (promise->dataReady)
|
||||||
|
|
@ -284,7 +283,7 @@ void sfz::FilePool::clearingThread()
|
||||||
{
|
{
|
||||||
while (!quitThread) {
|
while (!quitThread) {
|
||||||
tryToClearPromises();
|
tryToClearPromises();
|
||||||
std::this_thread::sleep_for(50ms);
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -302,7 +301,7 @@ void sfz::FilePool::loadingThread() noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!promiseQueue.try_pop(promise)) {
|
if (!promiseQueue.try_pop(promise)) {
|
||||||
std::this_thread::sleep_for(1ms);
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -326,7 +325,7 @@ void sfz::FilePool::loadingThread() noexcept
|
||||||
|
|
||||||
while (!filledPromiseQueue.try_push(promise)) {
|
while (!filledPromiseQueue.try_push(promise)) {
|
||||||
DBG("[sfizz] Error enqueuing the promise for " << promise->filename << " in the filledPromiseQueue");
|
DBG("[sfizz] Error enqueuing the promise for " << promise->filename << " in the filledPromiseQueue");
|
||||||
std::this_thread::sleep_for(1ms);
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
promise.reset();
|
promise.reset();
|
||||||
|
|
@ -412,7 +411,7 @@ void sfz::FilePool::emptyFileLoadingQueues() noexcept
|
||||||
{
|
{
|
||||||
emptyQueue = true;
|
emptyQueue = true;
|
||||||
while (emptyQueue)
|
while (emptyQueue)
|
||||||
std::this_thread::sleep_for(1ms);
|
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
void sfz::FilePool::waitForBackgroundLoading() noexcept
|
void sfz::FilePool::waitForBackgroundLoading() noexcept
|
||||||
|
|
@ -421,11 +420,11 @@ void sfz::FilePool::waitForBackgroundLoading() noexcept
|
||||||
// of the files we need to load still.
|
// of the files we need to load still.
|
||||||
// Spinlocking on the size of the background queue
|
// Spinlocking on the size of the background queue
|
||||||
while (!promiseQueue.was_empty()){
|
while (!promiseQueue.was_empty()){
|
||||||
std::this_thread::sleep_for(0.1ms);
|
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spinlocking on the threads possibly logging in the background
|
// Spinlocking on the threads possibly logging in the background
|
||||||
while (threadsLoading > 0) {
|
while (threadsLoading > 0) {
|
||||||
std::this_thread::sleep_for(0.1ms);
|
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,10 +44,11 @@ namespace sfz {
|
||||||
using AudioBufferPtr = std::shared_ptr<AudioBuffer<float>>;
|
using AudioBufferPtr = std::shared_ptr<AudioBuffer<float>>;
|
||||||
|
|
||||||
|
|
||||||
|
// Strict C++11 disallows member initialization if aggregate initialization is to be used...
|
||||||
struct PreloadedFileHandle
|
struct PreloadedFileHandle
|
||||||
{
|
{
|
||||||
std::shared_ptr<AudioBuffer<float>> preloadedData {};
|
std::shared_ptr<AudioBuffer<float>> preloadedData;
|
||||||
float sampleRate { config::defaultSampleRate };
|
float sampleRate;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct FilePromise
|
struct FilePromise
|
||||||
|
|
@ -78,7 +79,7 @@ struct FilePromise
|
||||||
AudioBuffer<float> fileData {};
|
AudioBuffer<float> fileData {};
|
||||||
float sampleRate { config::defaultSampleRate };
|
float sampleRate { config::defaultSampleRate };
|
||||||
Oversampling oversamplingFactor { config::defaultOversamplingFactor };
|
Oversampling oversamplingFactor { config::defaultOversamplingFactor };
|
||||||
std::atomic_size_t availableFrames { 0 };
|
std::atomic<size_t> availableFrames { 0 };
|
||||||
std::atomic<bool> dataReady { false };
|
std::atomic<bool> dataReady { false };
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> creationTime;
|
std::chrono::time_point<std::chrono::high_resolution_clock> creationTime;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue