Skip to content
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

Remove chunking #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 4 additions & 40 deletions src/dsp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,53 +42,17 @@ impl PluginDsp {
}
}

// To take advantage of SIMD auto-vectorization, and for consistent parameter smoothing,
// audio is processed in "chunks" of 16 samples at a time. The number of samples requested
// by a host will generally be a multiple of 16, although the buffer may be truncated early
// in some cases.
//
// This approach is overly complex for such a simple use-case, but can be particularly
// useful for reducing unnecessary re-computation with many parameters.
let num_samples = buffer.samples();
let num_chunks = num_samples / 16;
let extra_samples = num_samples % 16;
let num_channels = buffer.input_count();

let (inputs, mut outputs) = buffer.split();
for chunk_start in (0..num_chunks).map(|i| i * 16) {
for sample_idx in 0..num_samples {
self.amplitude_range.process();

// Prepare the chunk's base amplitude value by placing it into a 16-element array, then
// linearly interpolate them towards the next value if the amplitude has recently been
// changed.
let mut chunk_amplitudes = [self.amplitude; 16];
if let Some(amplitude_range) = self.amplitude_range.get_new_value() {
let new_amplitude = amplitude_range * 2.;
let per_sample_difference = (new_amplitude - self.amplitude) / 16.;
chunk_amplitudes
.iter_mut()
.zip((0..16).map(|i| i as f32 * per_sample_difference))
.for_each(|(amplitude, difference)| *amplitude += difference);
if let Some(new_amplitude) = self.amplitude_range.get_new_value() {
self.amplitude = new_amplitude;
}

// Then, calculate each output sample by multiplying each input sample by its
// corresponding amplitude value.
for channel in 0..num_channels {
for (i, amplitude) in chunk_amplitudes.iter().enumerate() {
outputs[channel][chunk_start + i] =
inputs[channel][chunk_start + i] * amplitude;
}
}
}

// Finally, process the final <16 samples, if any.
for i in 0..extra_samples {
for channel in 0..num_channels {
// We could precompute extra interpolated amplitude values into a rollover buffer,
// but it's simpler to approximate by just reusing the last known amplitude value.
outputs[channel][num_chunks * 16 + i] =
inputs[channel][num_chunks * 16 + i] * self.amplitude;
for channel_idx in 0..num_channels {
outputs[channel_idx][sample_idx] = inputs[channel_idx][sample_idx] * self.amplitude;
}
}
}
Expand Down