diff --git a/src/sfizz/BufferPool.h b/src/sfizz/BufferPool.h new file mode 100644 index 00000000..46c433e9 --- /dev/null +++ b/src/sfizz/BufferPool.h @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#pragma once +#include "Config.h" +#include "Buffer.h" +#include +#include + +namespace sfz +{ + +class BufferPool +{ +public: + BufferPool() + { + + } + void setBufferSize(unsigned bufferSize) + { + for (auto& buffer: buffers) { + // Trying to resize a buffer in use + ASSERT(buffer.use_count() != 1); + buffer->resize(bufferSize); + } + } + + std::shared_ptr> getBuffer() + { + auto bufferIt = buffers.begin(); + while (bufferIt < buffers.end()) { + if (bufferIt->use_count() == 1) + return *bufferIt; + } + + // No buffer found; debug message + DBG("[sfizz] No free buffer available!"); + } +private: + std::array>, config::bufferPoolSize> buffers; +}; +} diff --git a/src/sfizz/Config.h b/src/sfizz/Config.h index 0af2263f..2b81db1f 100644 --- a/src/sfizz/Config.h +++ b/src/sfizz/Config.h @@ -28,6 +28,7 @@ namespace config { constexpr float defaultSampleRate { 48000 }; constexpr int defaultSamplesPerBlock { 1024 }; constexpr int maxBlockSize { 8192 }; + constexpr int bufferPoolSize { 16 }; constexpr int preloadSize { 8192 }; constexpr int loggerQueueSize { 256 }; constexpr int voiceLoggerQueueSize { 256 }; diff --git a/src/sfizz/Resources.h b/src/sfizz/Resources.h index 99bc9dac..e129b355 100644 --- a/src/sfizz/Resources.h +++ b/src/sfizz/Resources.h @@ -6,6 +6,7 @@ #pragma once #include "FilePool.h" +#include "BufferPool.h" #include "FilterPool.h" #include "EQPool.h" #include "Logger.h" @@ -17,6 +18,7 @@ class WavetableMulti; struct Resources { + BufferPool bufferPool; MidiState midiState; Logger logger; FilePool filePool { logger };