Switched to Kanal buffers

This commit is contained in:
2025-06-05 13:47:52 +02:00
parent 457e634626
commit 292087be3e
6 changed files with 29 additions and 10 deletions

View File

@@ -1,24 +1,24 @@
use crate::*;
pub struct Allocator {
channel: tokio::sync::mpsc::Receiver<Arc<AudioChunk>>,
channel: kanal::Receiver<Arc<AudioChunk>>,
}
impl Allocator {
pub fn spawn(buffer_size: usize, pool_size: usize) -> (Self, tokio::task::JoinHandle<()>) {
let (allocated_buffer_sender, allocated_buffer_receiver) =
tokio::sync::mpsc::channel(pool_size);
let (allocated_buffer_sender, allocated_buffer_receiver) = kanal::bounded(pool_size);
// Pre-fill the channel with initial buffers
while allocated_buffer_sender
while let Ok(true) = allocated_buffer_sender
.try_send(AudioChunk::allocate(buffer_size))
.is_ok()
{}
let allocator = Allocator {
channel: allocated_buffer_receiver,
};
let allocated_buffer_sender = allocated_buffer_sender.to_async();
// Spawn the background task that continuously allocates buffers
let join_handle = tokio::runtime::Handle::current().spawn(async move {
loop {
@@ -35,9 +35,10 @@ impl Allocator {
impl ChunkFactory for Allocator {
fn create_chunk(&mut self) -> Result<std::sync::Arc<AudioChunk>> {
//match self.channel.try_recv_realtime() {
match self.channel.try_recv() {
Ok(chunk) => Ok(chunk),
Err(_) => Err(LooperError::ChunkAllocation(std::panic::Location::caller())),
Ok(Some(chunk)) => Ok(chunk),
_ => Err(LooperError::ChunkAllocation(std::panic::Location::caller())),
}
}
}

View File

@@ -24,7 +24,7 @@ async fn main() {
let (jack_client, audio_in, audio_out) = setup_jack();
let (allocator, factory_handle) = Allocator::spawn(jack_client.buffer_size() as usize * 10, 100);
let (allocator, factory_handle) = Allocator::spawn(jack_client.sample_rate(), 3);
let process_handler = ProcessHandler::new(audio_in, audio_out, allocator)
.expect("Could not create process handler");

View File

@@ -30,7 +30,7 @@ impl<F: ChunkFactory> jack::ProcessHandler for ProcessHandler<F> {
let output_buffer = self.output_port.as_mut_slice(ps);
let jack_buffer_size = client.buffer_size() as usize;
let recording_samples = client.sample_rate() * 5; // 5 seconds
let recording_samples = (client.sample_rate() as f64 * 0.6) as usize; // 0.6 seconds
let mut index = 0;