Feat: optimizations
This commit is contained in:
@@ -264,10 +264,6 @@ use cpal::Stream;
|
||||
use crossbeam_channel::{Receiver, Sender};
|
||||
#[cfg(feature = "cli")]
|
||||
use doux::{Engine, EngineMetrics};
|
||||
#[cfg(feature = "cli")]
|
||||
use std::collections::VecDeque;
|
||||
#[cfg(feature = "cli")]
|
||||
use std::sync::Mutex;
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
use super::AudioCommand;
|
||||
@@ -360,8 +356,7 @@ pub fn build_stream(
|
||||
let registry = Arc::clone(&engine.sample_registry);
|
||||
|
||||
const INPUT_BUFFER_SIZE: usize = 8192;
|
||||
let input_buffer: Arc<Mutex<VecDeque<f32>>> =
|
||||
Arc::new(Mutex::new(VecDeque::with_capacity(INPUT_BUFFER_SIZE)));
|
||||
let (input_producer, input_consumer) = HeapRb::<f32>::new(INPUT_BUFFER_SIZE).split();
|
||||
|
||||
let input_device = config
|
||||
.input_device
|
||||
@@ -399,17 +394,12 @@ pub fn build_stream(
|
||||
input_cfg.channels(),
|
||||
input_cfg.sample_rate()
|
||||
);
|
||||
let buf = Arc::clone(&input_buffer);
|
||||
let mut input_producer = input_producer;
|
||||
let stream = dev
|
||||
.build_input_stream(
|
||||
&input_cfg.into(),
|
||||
move |data: &[f32], _| {
|
||||
let mut b = buf.lock().unwrap();
|
||||
b.extend(data.iter().copied());
|
||||
let excess = b.len().saturating_sub(INPUT_BUFFER_SIZE);
|
||||
if excess > 0 {
|
||||
drop(b.drain(..excess));
|
||||
}
|
||||
input_producer.push_slice(data);
|
||||
},
|
||||
{
|
||||
let device_lost = Arc::clone(&device_lost);
|
||||
@@ -436,7 +426,7 @@ pub fn build_stream(
|
||||
let mut cmd_buffer = String::with_capacity(256);
|
||||
let mut rt_set = false;
|
||||
let mut live_scratch = vec![0.0f32; 4096];
|
||||
let input_buf_clone = Arc::clone(&input_buffer);
|
||||
let mut input_consumer = input_consumer;
|
||||
|
||||
let stream = device
|
||||
.build_output_stream(
|
||||
@@ -491,29 +481,28 @@ pub fn build_stream(
|
||||
if live_scratch.len() < stereo_len {
|
||||
live_scratch.resize(stereo_len, 0.0);
|
||||
}
|
||||
let mut buf = input_buf_clone.lock().unwrap();
|
||||
match input_channels {
|
||||
0 => {
|
||||
live_scratch[..stereo_len].fill(0.0);
|
||||
}
|
||||
1 => {
|
||||
for i in 0..buffer_samples {
|
||||
let s = buf.pop_front().unwrap_or(0.0);
|
||||
let s = input_consumer.try_pop().unwrap_or(0.0);
|
||||
live_scratch[i * 2] = s;
|
||||
live_scratch[i * 2 + 1] = s;
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
for sample in &mut live_scratch[..stereo_len] {
|
||||
*sample = buf.pop_front().unwrap_or(0.0);
|
||||
*sample = input_consumer.try_pop().unwrap_or(0.0);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
for i in 0..buffer_samples {
|
||||
let l = buf.pop_front().unwrap_or(0.0);
|
||||
let r = buf.pop_front().unwrap_or(0.0);
|
||||
let l = input_consumer.try_pop().unwrap_or(0.0);
|
||||
let r = input_consumer.try_pop().unwrap_or(0.0);
|
||||
for _ in 2..input_channels {
|
||||
buf.pop_front();
|
||||
input_consumer.try_pop();
|
||||
}
|
||||
live_scratch[i * 2] = l;
|
||||
live_scratch[i * 2 + 1] = r;
|
||||
@@ -521,11 +510,10 @@ pub fn build_stream(
|
||||
}
|
||||
}
|
||||
// Discard excess if input produced more than we consumed
|
||||
let excess = buf.len().saturating_sub(INPUT_BUFFER_SIZE / 2);
|
||||
if excess > 0 {
|
||||
drop(buf.drain(..excess));
|
||||
let excess = input_consumer.occupied_len().saturating_sub(INPUT_BUFFER_SIZE / 2);
|
||||
for _ in 0..excess {
|
||||
input_consumer.try_pop();
|
||||
}
|
||||
drop(buf);
|
||||
|
||||
engine.metrics.load.set_buffer_time(buffer_time_ns);
|
||||
engine.process_block(data, &[], &live_scratch[..stereo_len]);
|
||||
|
||||
Reference in New Issue
Block a user