Files
EchoionApi/include/echoion/api/utils/MemoryPool.hpp

148 lines
3.5 KiB
C++

#pragma once
#include "Types.hpp"
#include <Void/Containers/BufferPool.hpp>
#include <cstdint>
#include <cstring>
#include <memory>
#include <span>
namespace Echoion::Api
{
// Reusable int16 audio sample buffer, recycled by the pool on drop.
using AudioSampleBuffer = Void::BufferPool::BufferPool<int16_t>::Buffer;
// Specialized audio buffer pool for int16 (direct TeamSpeak compatibility).
// Backed by Void::BufferPool, which pools by power-of-two size class and
// reuses storage across acquisitions (grow-on-demand, no fixed capacity).
class AudioBufferPool
{
public:
static constexpr size_t DEFAULT_BUFFER_SIZE =
1920; // 48000Hz * 2ch * 20ms
static constexpr size_t DEFAULT_POOL_SIZE =
256; // pre-warm hint (buffers)
static AudioBufferPool& instance();
void initialize(size_t bufferSize = DEFAULT_BUFFER_SIZE,
size_t poolSize = DEFAULT_POOL_SIZE);
void shutdown();
// Borrow an int16 buffer of the configured size. An empty Buffer is
// returned if the pool has not been initialized.
[[nodiscard]] AudioSampleBuffer acquireBuffer();
// Get buffer size
[[nodiscard]] size_t getBufferSize() const { return bufferSize_; }
// Get stats
[[nodiscard]] size_t getAvailableBuffers() const;
[[nodiscard]] size_t getTotalBuffers() const;
private:
AudioBufferPool() = default;
std::unique_ptr<Void::BufferPool::BufferPool<int16_t>> pool_;
size_t bufferSize_ = DEFAULT_BUFFER_SIZE;
};
// Zero-copy audio buffer view
template <typename T> class AudioBufferView
{
public:
AudioBufferView() = default;
AudioBufferView(T* data, size_t size, size_t channels = 2)
: data_(data), size_(size), channels_(channels)
{
}
[[nodiscard]] T* data() { return data_; }
[[nodiscard]] const T* data() const { return data_; }
[[nodiscard]] size_t size() const { return size_; }
[[nodiscard]] size_t channels() const { return channels_; }
[[nodiscard]] size_t samplesPerChannel() const
{
return size_ / channels_;
}
[[nodiscard]] T* channel(size_t ch) { return data_ + ch; }
[[nodiscard]] T& sample(size_t frame, size_t ch)
{
return data_[frame * channels_ + ch];
}
[[nodiscard]] const T& sample(size_t frame, size_t ch) const
{
return data_[frame * channels_ + ch];
}
// Span for single channel
[[nodiscard]] std::span<T> getChannelSpan(size_t ch)
{
return std::span<T>(data_ + ch, size_, channels_);
}
// Span for all data
[[nodiscard]] std::span<T> getSpan()
{
return std::span<T>(data_, size_);
}
[[nodiscard]] bool empty() const
{
return data_ == nullptr || size_ == 0;
}
void clear()
{
if (data_) {
std::memset(data_, 0, size_ * sizeof(T));
}
}
// Apply gain in-place
void applyGain(float gain)
{
if (!data_) {
return;
}
for (size_t i = 0; i < size_; ++i) {
data_[i] *= gain;
}
}
// Mix another buffer into this one
void mix(const AudioBufferView& other, float gain = 1.0f)
{
if (!data_ || !other.data_) {
return;
}
size_t mixSize = std::min(size_, other.size_);
for (size_t i = 0; i < mixSize; ++i) {
data_[i] += other.data_[i] * gain;
}
}
private:
T* data_ = nullptr;
size_t size_ = 0;
size_t channels_ = 2;
};
// Audio buffer view types - using int16 (short) for direct TeamSpeak
// compatibility
using Int16AudioView = AudioBufferView<int16_t>;
// Deprecated: Float audio view is no longer used, all audio is int16
using FloatAudioView = AudioBufferView<float>;
} // namespace Echoion::Api