Skip to content

Improve ConvolverNode #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 10, 2024
32 changes: 14 additions & 18 deletions src/node/convolver.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::any::Any;
// use std::sync::Arc;

// use realfft::{num_complex::Complex, ComplexToReal, RealFftPlanner, RealToComplex};
use fft_convolver::FFTConvolver;

use crate::buffer::AudioBuffer;
Expand Down Expand Up @@ -283,16 +281,19 @@ impl ConvolverNode {
};

let mut convolvers = Vec::<FFTConvolver<f32>>::new();
// Size of the partition changes a lot the perf...
// - RENDER_QUANTUM_SIZE -> 20x (compared to real-time)
// - RENDER_QUANTUM_SIZE * 8 -> 134x
// @note - value defined by "rule of thumb", to be explored further
let partition_size = RENDER_QUANTUM_SIZE * 8;
// @todo - implement multichannel
// cf. https://webaudio.github.io/web-audio-api/#Convolution-channel-configurations
let num_convolvers = 1;

for index in 0..num_convolvers {
let channel = std::cmp::min(buffer.number_of_channels(), index);

[0..buffer.number_of_channels()].iter().for_each(|_| {
let mut scaled_channel = vec![0.; buffer.length()];
scaled_channel
.iter_mut()
.zip(buffer.get_channel_data(0))
.zip(buffer.get_channel_data(channel))
.for_each(|(o, i)| *o = *i * scale);

let mut convolver = FFTConvolver::<f32>::default();
Expand All @@ -301,7 +302,7 @@ impl ConvolverNode {
.expect("Unable to initialize convolution engine");

convolvers.push(convolver);
});
}

let msg = ConvolverInfosMessage {
convolvers: Some(convolvers),
Expand Down Expand Up @@ -345,7 +346,7 @@ impl AudioProcessor for ConvolverRenderer {
// single input/output node
let input = &inputs[0];
let output = &mut outputs[0];
output.force_mono();
output.make_silent();

let convolvers = match &mut self.convolvers {
None => {
Expand All @@ -360,19 +361,14 @@ impl AudioProcessor for ConvolverRenderer {
let mut mono = input.clone();
mono.mix(1, ChannelInterpretation::Speakers);

// let input = &mono.channel_data(0)[..];
// let output = &mut output.channel_data_mut(0)[..];
let _ = convolvers[0].process(&mono.channel_data(0), &mut output.channel_data_mut(0));
let i = &mono.channel_data(0)[..];
let o = &mut output.channel_data_mut(0)[..];
let _ = convolvers[0].process(i, o);

// handle tail time
if input.is_silent() {
self.tail_count += RENDER_QUANTUM_SIZE;

if self.tail_count >= self.impulse_length {
return false;
} else {
return true
}
return self.tail_count < self.impulse_length;
}

self.tail_count = 0;
Expand Down
Loading