use cpal::Sample;
use crossbeam::queue::ArrayQueue;
use std::sync::Arc;
pub struct BufferPool {
pool: Arc<ArrayQueue<Vec<i32>>>,
capacity: usize,
}
impl BufferPool {
pub fn new(pool_size: usize, buffer_capacity: usize) -> Self {
let pool = Arc::new(ArrayQueue::new(pool_size));
for _ in 0..pool_size {
let mut buf = Vec::with_capacity(buffer_capacity);
buf.resize(buffer_capacity, i32::EQUILIBRIUM);
buf.clear(); let _ = pool.push(buf);
}
Self {
pool,
capacity: buffer_capacity,
}
}
pub fn get(&self) -> Vec<i32> {
self.pool.pop().unwrap_or_else(|| {
log::warn!(
"Buffer pool exhausted (capacity={} samples); falling back to heap allocation",
self.capacity
);
Vec::with_capacity(self.capacity)
})
}
pub fn put(&self, mut buf: Vec<i32>) {
buf.clear();
let _ = self.pool.push(buf); }
pub fn capacity(&self) -> usize {
self.capacity
}
}