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
4 changes: 2 additions & 2 deletions crates/firewheel-core/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl InstantSeconds {
/// Returns the amount of time elapsed from another instant to this one, or
/// `None`` if that instant is later than this one.
pub fn checked_duration_since(&self, earlier: Self) -> Option<DurationSeconds> {
(self.0 >= earlier.0).then(|| DurationSeconds(self.0 - earlier.0))
(self.0 >= earlier.0).then_some(DurationSeconds(self.0 - earlier.0))
}

/// Returns the amount of time elapsed from another instant to this one, or
Expand Down Expand Up @@ -465,7 +465,7 @@ fn whole_seconds_and_fract(samples: i64, sample_rate: NonZeroU32) -> (i64, u32)
if fract_samples < 0 {
(
whole_seconds - 1,
sample_rate.get() - (fract_samples.abs() as u32),
sample_rate.get() - (fract_samples.unsigned_abs() as u32),
)
} else {
(whole_seconds, fract_samples as u32)
Expand Down
12 changes: 3 additions & 9 deletions crates/firewheel-core/src/dsp/declick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Declicker {
),
};

let frames_processed = crossfade_buffers(frames_left, values_a, &values_b);
let frames_processed = crossfade_buffers(frames_left, values_a, values_b);

if frames_processed < frames {
for (ch_a, ch_b) in buffers_a.iter().zip(buffers_b.iter_mut()) {
Expand Down Expand Up @@ -298,17 +298,11 @@ impl Declicker {
}

pub fn trending_towards_zero(&self) -> bool {
match self {
Declicker::SettledAt0 | Declicker::FadingTo0 { .. } => true,
_ => false,
}
matches!(self, Declicker::SettledAt0 | Declicker::FadingTo0 { .. })
Comment thread
atlv24 marked this conversation as resolved.
}

pub fn trending_towards_one(&self) -> bool {
match self {
Declicker::SettledAt1 | Declicker::FadingTo1 { .. } => true,
_ => false,
}
matches!(self, Declicker::SettledAt1 | Declicker::FadingTo1 { .. })
}

pub fn frames_left(&self) -> usize {
Expand Down
8 changes: 4 additions & 4 deletions crates/firewheel-core/src/dsp/distance_attenuation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ impl DistanceAttenuatorStereoDsp {
return true;
} else if self.damping_disabled {
for i in 0..frames {
out1[i] = out1[i] * self.gain.target_value();
out2[i] = out2[i] * self.gain.target_value();
out1[i] *= self.gain.target_value();
out2[i] *= self.gain.target_value();
}
} else {
// The cutoff parameter is not currently smoothing, so we can optimize by
Expand All @@ -347,8 +347,8 @@ impl DistanceAttenuatorStereoDsp {
for i in 0..frames {
let gain = self.gain.next_smoothed();

out1[i] = out1[i] * gain;
out2[i] = out2[i] * gain;
out1[i] *= gain;
out2[i] *= gain;
}
} else {
let mut coeff = OnePoleIirLPFCoeffSimd::default();
Expand Down
3 changes: 3 additions & 0 deletions crates/firewheel-core/src/dsp/filter/butterworth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(clippy::excessive_precision)]
#![allow(clippy::approx_constant)]

pub const Q_BUTTERWORTH_ORD2: f32 = 0.70710678118654752440;
pub const Q_BUTTERWORTH_ORD4: [f32; 2] = [0.54119610014619698440, 1.3065629648763765279];
pub const Q_BUTTERWORTH_ORD6: [f32; 3] = [
Expand Down
26 changes: 13 additions & 13 deletions crates/firewheel-core/src/dsp/filter/svf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ impl SvfCoeff {

pub fn lowpass_ord4(cutoff_hz: f32, q: f32, sample_rate_recip: f32) -> [Self; 2] {
let g = g(cutoff_hz, sample_rate_recip);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD4_Q_SCALE as f32);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD4_Q_SCALE);

core::array::from_fn(|i| {
let q = q_norm * Q_BUTTERWORTH_ORD4[i] as f32;
let q = q_norm * Q_BUTTERWORTH_ORD4[i];
let k = 1.0 / q;

Self::from_g_and_k(g, k, 0.0, 0.0, 1.0)
Expand All @@ -54,10 +54,10 @@ impl SvfCoeff {

pub fn lowpass_ord6(cutoff_hz: f32, q: f32, sample_rate_recip: f32) -> [Self; 3] {
let g = g(cutoff_hz, sample_rate_recip);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD4_Q_SCALE as f32);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD4_Q_SCALE);

core::array::from_fn(|i| {
let q = q_norm * Q_BUTTERWORTH_ORD6[i] as f32;
let q = q_norm * Q_BUTTERWORTH_ORD6[i];
let k = 1.0 / q;

Self::from_g_and_k(g, k, 0.0, 0.0, 1.0)
Expand All @@ -66,10 +66,10 @@ impl SvfCoeff {

pub fn lowpass_ord8(cutoff_hz: f32, q: f32, sample_rate_recip: f32) -> [Self; 4] {
let g = g(cutoff_hz, sample_rate_recip);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD8_Q_SCALE as f32);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD8_Q_SCALE);

core::array::from_fn(|i| {
let q = q_norm * Q_BUTTERWORTH_ORD8[i] as f32;
let q = q_norm * Q_BUTTERWORTH_ORD8[i];
let k = 1.0 / q;

Self::from_g_and_k(g, k, 0.0, 0.0, 1.0)
Expand All @@ -85,10 +85,10 @@ impl SvfCoeff {

pub fn highpass_ord4(cutoff_hz: f32, q: f32, sample_rate_recip: f32) -> [Self; 2] {
let g = g(cutoff_hz, sample_rate_recip);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD4_Q_SCALE as f32);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD4_Q_SCALE);

core::array::from_fn(|i| {
let q = q_norm * Q_BUTTERWORTH_ORD4[i] as f32;
let q = q_norm * Q_BUTTERWORTH_ORD4[i];
let k = 1.0 / q;

Self::from_g_and_k(g, k, 1.0, -k, -1.0)
Expand All @@ -97,10 +97,10 @@ impl SvfCoeff {

pub fn highpass_ord6(cutoff_hz: f32, q: f32, sample_rate_recip: f32) -> [Self; 3] {
let g = g(cutoff_hz, sample_rate_recip);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD6_Q_SCALE as f32);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD6_Q_SCALE);

core::array::from_fn(|i| {
let q = q_norm * Q_BUTTERWORTH_ORD6[i] as f32;
let q = q_norm * Q_BUTTERWORTH_ORD6[i];
let k = 1.0 / q;

Self::from_g_and_k(g, k, 1.0, -k, -1.0)
Expand All @@ -109,10 +109,10 @@ impl SvfCoeff {

pub fn highpass_ord8(cutoff_hz: f32, q: f32, sample_rate_recip: f32) -> [Self; 4] {
let g = g(cutoff_hz, sample_rate_recip);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD8_Q_SCALE as f32);
let q_norm = scale_q_norm_for_order(q_norm(q), ORD8_Q_SCALE);

core::array::from_fn(|i| {
let q = q_norm * Q_BUTTERWORTH_ORD8[i] as f32;
let q = q_norm * Q_BUTTERWORTH_ORD8[i];
let k = 1.0 / q;

Self::from_g_and_k(g, k, 1.0, -k, -1.0)
Expand Down Expand Up @@ -373,7 +373,7 @@ fn g(cutoff_hz: f32, sample_rate_recip: f32) -> f32 {

#[inline]
fn q_norm(q: f32) -> f32 {
q * (1.0 / Q_BUTTERWORTH_ORD2 as f32)
q * (1.0 / Q_BUTTERWORTH_ORD2)
}

/*
Expand Down
32 changes: 10 additions & 22 deletions crates/firewheel-core/src/dsp/interleave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ pub fn deinterleave<V: AsMut<[f32]>>(

ch.copy_from_slice(interleaved);

if calculate_silence_mask {
if ch.iter().find(|&&s| s != 0.0).is_none() {
silence_mask.set_channel(0, true);
}
if calculate_silence_mask && ch.iter().all(|&s| s == 0.0) {
silence_mask.set_channel(0, true);
}

(1, samples)
Expand All @@ -57,11 +55,7 @@ pub fn deinterleave<V: AsMut<[f32]>>(

if calculate_silence_mask {
for (ch_i, ch) in channels.iter_mut().enumerate() {
if ch.as_mut()[0..samples]
.iter()
.find(|&&s| s != 0.0)
.is_none()
{
if ch.as_mut()[0..samples].iter().all(|&s| s == 0.0) {
silence_mask.set_channel(ch_i, true);
}
}
Expand All @@ -82,10 +76,8 @@ pub fn deinterleave<V: AsMut<[f32]>>(
*out_s = in_chunk[ch_i];
}

if calculate_silence_mask && ch_i < 64 {
if ch.iter().find(|&&s| s != 0.0).is_none() {
silence_mask.set_channel(ch_i, true);
}
if calculate_silence_mask && ch_i < 64 && ch.iter().all(|&s| s == 0.0) {
silence_mask.set_channel(ch_i, true);
}

num_filled_channels += 1;
Expand Down Expand Up @@ -121,11 +113,9 @@ pub fn interleave<V: AsRef<[f32]>>(
}

if let Some(silence_mask) = silence_mask {
if channels.len() <= 64 {
if silence_mask.all_channels_silent(channels.len()) {
interleaved.fill(0.0);
return;
}
if channels.len() <= 64 && silence_mask.all_channels_silent(channels.len()) {
interleaved.fill(0.0);
return;
}
}

Expand Down Expand Up @@ -172,10 +162,8 @@ pub fn interleave<V: AsRef<[f32]>>(

for (ch_i, ch) in (0..num_interleaved_channels).zip(channels.iter()) {
if let Some(silence_mask) = silence_mask {
if ch_i < 64 {
if silence_mask.is_channel_silent(ch_i) {
continue;
}
if ch_i < 64 && silence_mask.is_channel_silent(ch_i) {
continue;
}
}

Expand Down
78 changes: 33 additions & 45 deletions crates/firewheel-core/src/dsp/mix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,15 @@ impl MixDSP {

self.gain_0.settle();
self.gain_1.settle();
} else if self.gain_1.target_value() <= 0.00001 && self.gain_0.target_value() >= 0.99999 {
// Simply copy first signal to output.
second.copy_from_slice(first);
} else if self.gain_0.target_value() <= 0.00001 && self.gain_1.target_value() >= 0.99999 {
// Signal is already fully second
} else {
if self.gain_1.target_value() <= 0.00001 && self.gain_0.target_value() >= 0.99999 {
// Simply copy first signal to output.
second.copy_from_slice(first);
} else if self.gain_0.target_value() <= 0.00001 && self.gain_1.target_value() >= 0.99999
{
// Signal is already fully second
return;
} else {
for (first_s, second_s) in first.iter().zip(second.iter_mut()) {
*second_s = first_s * self.gain_0.target_value()
+ *second_s * self.gain_1.target_value();
}
for (first_s, second_s) in first.iter().zip(second.iter_mut()) {
*second_s =
first_s * self.gain_0.target_value() + *second_s * self.gain_1.target_value();
}
}
}
Expand Down Expand Up @@ -242,22 +238,18 @@ impl MixDSP {

self.gain_0.settle();
self.gain_1.settle();
} else if self.gain_1.target_value() <= 0.00001 && self.gain_0.target_value() >= 0.99999 {
// Simply copy first signal to output.
second_l.copy_from_slice(first_l);
second_r.copy_from_slice(first_r);
} else if self.gain_0.target_value() <= 0.00001 && self.gain_1.target_value() >= 0.99999 {
// Signal is already fully second
} else {
if self.gain_1.target_value() <= 0.00001 && self.gain_0.target_value() >= 0.99999 {
// Simply copy first signal to output.
second_l.copy_from_slice(first_l);
second_r.copy_from_slice(first_r);
} else if self.gain_0.target_value() <= 0.00001 && self.gain_1.target_value() >= 0.99999
{
// Signal is already fully second
return;
} else {
for i in 0..frames {
second_l[i] = first_l[i] * self.gain_0.target_value()
+ second_l[i] * self.gain_1.target_value();
second_r[i] = first_r[i] * self.gain_0.target_value()
+ second_r[i] * self.gain_1.target_value();
}
for i in 0..frames {
second_l[i] = first_l[i] * self.gain_0.target_value()
+ second_l[i] * self.gain_1.target_value();
second_r[i] = first_r[i] * self.gain_0.target_value()
+ second_r[i] * self.gain_1.target_value();
}
}
}
Expand Down Expand Up @@ -302,25 +294,21 @@ impl MixDSP {

self.gain_0.settle();
self.gain_1.settle();
} else if self.gain_1.target_value() <= 0.00001 && self.gain_0.target_value() >= 0.99999 {
// Simply copy input 0 to output.
for (first_ch, second_ch) in first[..second.len()].iter().zip(second.iter_mut()) {
second_ch.as_mut()[..frames].copy_from_slice(&first_ch.as_ref()[..frames]);
}
} else if self.gain_0.target_value() <= 0.00001 && self.gain_1.target_value() >= 0.99999 {
// Signal is already fully second
} else {
if self.gain_1.target_value() <= 0.00001 && self.gain_0.target_value() >= 0.99999 {
// Simply copy input 0 to output.
for (first_ch, second_ch) in first[..second.len()].iter().zip(second.iter_mut()) {
second_ch.as_mut()[..frames].copy_from_slice(&first_ch.as_ref()[..frames]);
}
} else if self.gain_0.target_value() <= 0.00001 && self.gain_1.target_value() >= 0.99999
{
// Signal is already fully second
return;
} else {
for (first_ch, second_ch) in first[..second.len()].iter().zip(second.iter_mut()) {
for (&first_s, second_s) in first_ch.as_ref()[..frames]
.iter()
.zip(second_ch.as_mut()[..frames].iter_mut())
{
*second_s = first_s * self.gain_0.target_value()
+ *second_s * self.gain_1.target_value();
}
for (first_ch, second_ch) in first[..second.len()].iter().zip(second.iter_mut()) {
for (&first_s, second_s) in first_ch.as_ref()[..frames]
.iter()
.zip(second_ch.as_mut()[..frames].iter_mut())
{
*second_s = first_s * self.gain_0.target_value()
+ *second_s * self.gain_1.target_value();
}
}
}
Expand Down
14 changes: 4 additions & 10 deletions crates/firewheel-core/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,9 @@ impl RealtimeLogger {
slot.push_str(message);

let _ = self.debug_prod.try_push(slot);

return Ok(());
}

#[cfg(not(debug_assertions))]
return Ok(());
Ok(())
}

/// Log a debug message into the given string.
Expand Down Expand Up @@ -188,12 +185,9 @@ impl RealtimeLogger {
(f)(&mut slot);

let _ = self.debug_prod.try_push(slot);

return Ok(());
}

#[cfg(not(debug_assertions))]
return Ok(());
Ok(())
}

/// Log the given error message.
Expand Down Expand Up @@ -280,12 +274,12 @@ impl RealtimeLoggerMainThread {
#[cfg(debug_assertions)]
for slot in self.debug_cons.pop_iter() {
(log_debug)(&slot);
let _ = self.debug_prod.try_push(slot).unwrap();
self.debug_prod.try_push(slot).unwrap();
}

for slot in self.error_cons.pop_iter() {
(log_error)(&slot);
let _ = self.error_prod.try_push(slot).unwrap();
self.error_prod.try_push(slot).unwrap();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/firewheel-core/src/mask.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{ops::Range, u64};
use core::ops::Range;

/// An optional optimization hint on which channels contain all
/// zeros (silence). The first bit (`0x1`) is the first channel,
Expand Down Expand Up @@ -69,7 +69,7 @@ impl SilenceMask {
if range.start >= 64 {
false
} else if range.end >= 64 {
let mask = u64::MAX & !((0b1 << range.start) - 1);
let mask = !((0b1 << range.start) - 1);
self.0 & mask == mask
} else {
let mask = ((0b1 << range.end) - 1) & !((0b1 << range.start) - 1);
Expand Down Expand Up @@ -173,7 +173,7 @@ impl ConstantMask {
if range.start >= 64 {
false
} else if range.end >= 64 {
let mask = u64::MAX & !((0b1 << range.start) - 1);
let mask = !((0b1 << range.start) - 1);
self.0 & mask == mask
} else {
let mask = ((0b1 << range.end) - 1) & !((0b1 << range.start) - 1);
Expand Down
Loading