use crate::audio::Sample;
use crossbeam::queue::ArrayQueue;
use std::sync::Arc;
pub struct BufferPool {
pool: Arc<ArrayQueue<Vec<Sample>>>,
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, Sample::ZERO);
buf.clear(); let _ = pool.push(buf);
}
Self {
pool,
capacity: buffer_capacity,
}
}
pub fn get(&self) -> Vec<Sample> {
self.pool
.pop()
.unwrap_or_else(|| Vec::with_capacity(self.capacity))
}
pub fn put(&self, mut buf: Vec<Sample>) {
buf.clear();
let _ = self.pool.push(buf); }
pub fn capacity(&self) -> usize {
self.capacity
}
}