Skip to content
Merged
Show file tree
Hide file tree
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
40 changes: 18 additions & 22 deletions src/common/fft.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
use rustfft::{FftPlanner, num_complex::Complex};
use std::sync::Arc;

use rustfft::{Fft, FftPlanner, num_complex::Complex};

pub struct SpectrumAnalyzer {
planner: FftPlanner<f32>,
fft: Arc<dyn Fft<f32>>,
complex_buf: Vec<Complex<f32>>,
scratch: Vec<Complex<f32>>,
}

impl SpectrumAnalyzer {
pub fn new(max_size: usize) -> Self {
let mut planner = FftPlanner::new();
let fft = planner.plan_fft_forward(max_size);
let scratch = vec![Complex { re: 0.0, im: 0.0 }; fft.get_inplace_scratch_len()];
Self {
planner: FftPlanner::new(),
fft,
complex_buf: vec![Complex { re: 0.0, im: 0.0 }; max_size],
scratch: vec![Complex { re: 0.0, im: 0.0 }; max_size],
scratch,
}
}

pub fn process(&mut self, input: &[f32], output: &mut [f32]) {
let n = input.len();
if n == 0 {
if input.is_empty() {
output.fill(0.0);
return;
}

let fft = self.planner.plan_fft_forward(n);

for (i, &s) in input.iter().enumerate() {
self.complex_buf[i].re = s;
self.complex_buf[i].im = 0.0;
}
for complex in self.complex_buf.iter_mut().take(n).skip(input.len()) {
complex.re = 0.0;
complex.im = 0.0;
}

let scratch_len = fft.get_inplace_scratch_len();
if self.scratch.len() < scratch_len {
self.scratch
.resize(scratch_len, Complex { re: 0.0, im: 0.0 });
for (complex, &sample) in self.complex_buf.iter_mut().zip(input) {
*complex = Complex {
re: sample,
im: 0.0,
};
}
fft.process_with_scratch(&mut self.complex_buf[..n], &mut self.scratch[..scratch_len]);
self.complex_buf[input.len()..].fill(Complex { re: 0.0, im: 0.0 });
self.fft
.process_with_scratch(&mut self.complex_buf, &mut self.scratch);

let out_len = output.len().min(self.complex_buf.len());
for (c, o) in self.complex_buf[..out_len]
Expand Down
50 changes: 37 additions & 13 deletions src/common/lufs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const INTEGRATED_HOP_MS: f64 = 100.0;
const LOUDNESS_OFFSET: f64 = -0.691;
const ABSOLUTE_GATE_LUFS: f64 = -70.0;
const RELATIVE_GATE_LU: f64 = -10.0;
const MAX_INTEGRATED_BLOCKS: usize = 24 * 60 * 60 * 1000 / INTEGRATED_HOP_MS as usize;

#[derive(Clone, Copy)]
struct Biquad {
Expand Down Expand Up @@ -193,8 +194,11 @@ pub struct LufsMeter {
hop_len: usize,
hop_remaining: usize,
/// Channel-averaged 400 ms energies, one per 100 ms hop, for the
/// integrated measurement's absolute and relative gating.
/// integrated measurement's absolute and relative gating. The most recent
/// 24 hours are retained in fixed storage.
blocks: Vec<f64>,
blocks_len: usize,
block_pos: usize,
}

impl LufsMeter {
Expand All @@ -207,7 +211,9 @@ impl LufsMeter {
],
hop_len,
hop_remaining: hop_len,
blocks: Vec::new(),
blocks: vec![0.0; MAX_INTEGRATED_BLOCKS],
blocks_len: 0,
block_pos: 0,
}
}

Expand All @@ -216,7 +222,8 @@ impl LufsMeter {
channel.reset();
}
self.hop_remaining = self.hop_len;
self.blocks.clear();
self.blocks_len = 0;
self.block_pos = 0;
}

/// Feed one block of interleaved-by-slice stereo samples.
Expand All @@ -234,7 +241,16 @@ impl LufsMeter {
.map(|c| c.momentary.mean_square())
.sum::<f64>()
/ self.channels.len() as f64;
self.blocks.push(energy);
let write_pos = if self.blocks_len < self.blocks.len() {
let position = self.blocks_len;
self.blocks_len += 1;
position
} else {
let position = self.block_pos;
self.block_pos = (self.block_pos + 1) % self.blocks.len();
position
};
self.blocks[write_pos] = energy;
}
}
}
Expand All @@ -257,26 +273,34 @@ impl LufsMeter {
}

pub fn integrated_lufs(&self) -> f64 {
if self.blocks.is_empty() {
let blocks = &self.blocks[..self.blocks_len];
if blocks.is_empty() {
return f64::NEG_INFINITY;
}
let absolute_gate = 10.0_f64.powf((ABSOLUTE_GATE_LUFS - LOUDNESS_OFFSET) / 10.0);
let gated: Vec<f64> = self
.blocks
let (gated_sum, gated_count) = blocks
.iter()
.copied()
.filter(|&e| e >= absolute_gate)
.collect();
if gated.is_empty() {
.fold((0.0, 0_usize), |(sum, count), energy| {
(sum + energy, count + 1)
});
if gated_count == 0 {
return f64::NEG_INFINITY;
}
let gated_mean = gated.iter().sum::<f64>() / gated.len() as f64;
let gated_mean = gated_sum / gated_count as f64;
let relative_gate = gated_mean * 10.0_f64.powf(RELATIVE_GATE_LU / 10.0);
let final_blocks: Vec<f64> = gated.into_iter().filter(|&e| e >= relative_gate).collect();
if final_blocks.is_empty() {
let (final_sum, final_count) = blocks
.iter()
.copied()
.filter(|&e| e >= absolute_gate && e >= relative_gate)
.fold((0.0, 0_usize), |(sum, count), energy| {
(sum + energy, count + 1)
});
if final_count == 0 {
return f64::NEG_INFINITY;
}
let final_mean = final_blocks.iter().sum::<f64>() / final_blocks.len() as f64;
let final_mean = final_sum / final_count as f64;
LOUDNESS_OFFSET + 10.0 * final_mean.log10()
}
}
Expand Down
27 changes: 22 additions & 5 deletions src/common/true_peak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn phase_taps(factor: usize, phase: usize) -> [f32; TRUE_PEAK_TAPS] {
/// Polyphase interpolator producing `factor` sub-samples per input sample.
pub struct TruePeakDetector {
factor: usize,
taps: Vec<[f32; TRUE_PEAK_TAPS]>,
taps: [[f32; TRUE_PEAK_TAPS]; 8],
state: [f32; TRUE_PEAK_TAPS],
position: usize,
}
Expand All @@ -73,13 +73,30 @@ impl TruePeakDetector {
} else {
1
};
let taps = (0..factor).map(|phase| phase_taps(factor, phase)).collect();
Self {
let mut detector = Self {
factor,
taps,
taps: [[0.0; TRUE_PEAK_TAPS]; 8],
state: [0.0; TRUE_PEAK_TAPS],
position: 0,
};
detector.set_factor(factor);
detector
}

pub fn set_factor(&mut self, factor: usize) {
self.factor = if factor >= 8 {
8
} else if factor >= 4 {
4
} else if factor >= 2 {
2
} else {
1
};
for (phase, taps) in self.taps.iter_mut().enumerate().take(self.factor) {
*taps = phase_taps(self.factor, phase);
}
self.reset();
}

pub fn factor(&self) -> usize {
Expand Down Expand Up @@ -109,7 +126,7 @@ impl TruePeakDetector {
self.state[self.position] = x;
let mask = TRUE_PEAK_TAPS - 1;
let mut peak = 0.0_f32;
for phase_taps in &self.taps {
for phase_taps in self.taps.iter().take(self.factor) {
let mut acc = 0.0_f32;
for (k, &tap) in phase_taps.iter().enumerate() {
let index = (self.position + TRUE_PEAK_TAPS - k) & mask;
Expand Down
23 changes: 11 additions & 12 deletions src/delay/dsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ impl Default for Delay {
current_delay: 0.0,
chase: 0.0,
sample_rate: 48_000.0,
temp_dry_l: vec![0.0; 1024],
temp_dry_r: vec![0.0; 1024],
temp_wet_l: vec![0.0; 1024],
temp_wet_r: vec![0.0; 1024],
temp_dry_l: vec![0.0; 16_384],
temp_dry_r: vec![0.0; 16_384],
temp_wet_l: vec![0.0; 16_384],
temp_wet_r: vec![0.0; 16_384],
}
}
}
Expand All @@ -50,6 +50,13 @@ impl Delay {
self.sample_rate = sample_rate;
}

pub fn prepare(&mut self, max_frames: usize) {
self.temp_dry_l = vec![0.0; max_frames];
self.temp_dry_r = vec![0.0; max_frames];
self.temp_wet_l = vec![0.0; max_frames];
self.temp_wet_r = vec![0.0; max_frames];
}

pub fn reset(&mut self) {
self.buf_l.fill(0.0);
self.buf_r.fill(0.0);
Expand Down Expand Up @@ -113,14 +120,6 @@ impl Delay {
let max_len = self.buf_l.len();
let frames = left.len().min(right.len());

if self.temp_dry_l.len() < frames {
let new_len = frames.next_power_of_two();
self.temp_dry_l.resize(new_len, 0.0);
self.temp_dry_r.resize(new_len, 0.0);
self.temp_wet_l.resize(new_len, 0.0);
self.temp_wet_r.resize(new_len, 0.0);
}

for i in 0..frames {
let wp = self.write_pos;
let delayed_l = Self::read_interp(&self.buf_l, wp, self.current_delay);
Expand Down
1 change: 1 addition & 0 deletions src/delay/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl AudioProcessor {
fn new(sample_rate: f64, max_frames: u32, bus_data: Option<bus::PluginSharedData>) -> Self {
let mut dsp = Delay::default();
dsp.set_sample_rate(sample_rate);
dsp.prepare(max_frames as usize);
Self {
dsp,
temp_left: vec![0.0; max_frames as usize],
Expand Down
16 changes: 7 additions & 9 deletions src/drums/engine/limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct Limiter {
release_coeff: f32,
gain_reduction: f32,
enabled: bool,
gains: Vec<f32>,
}

impl Default for Limiter {
Expand All @@ -22,9 +23,14 @@ impl Limiter {
release_coeff: 1.0 - (-1000.0 / (release_ms * sr)).exp(),
gain_reduction: 1.0,
enabled: true,
gains: Vec::new(),
}
}

pub fn prepare(&mut self, max_frames: usize) {
self.gains = vec![1.0; max_frames];
}

pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
Expand All @@ -44,20 +50,12 @@ impl Limiter {
self.release_coeff = 1.0 - (-1000.0 / (release_ms * sr)).exp();
}

pub fn process(&mut self, buffers: &mut [Vec<f32>], frames: usize) {
if !self.enabled || buffers.is_empty() {
return;
}
let mut slices: Vec<&mut [f32]> = buffers.iter_mut().map(|b| b.as_mut_slice()).collect();
self.process_slices(&mut slices, frames);
}

pub fn process_slices(&mut self, slices: &mut [&mut [f32]], frames: usize) {
if !self.enabled || slices.is_empty() {
return;
}

let mut gains = vec![1.0f32; frames];
let gains = &mut self.gains[..frames];

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
unsafe {
Expand Down
Loading
Loading