Feat: fix scope / spectrum / vumeter
Some checks failed
Deploy Website / deploy (push) Failing after 6s
Some checks failed
Deploy Website / deploy (push) Failing after 6s
This commit is contained in:
@@ -11,7 +11,7 @@ use std::thread::{self, JoinHandle};
|
||||
use super::AudioCommand;
|
||||
|
||||
pub struct ScopeBuffer {
|
||||
pub samples: [AtomicU32; 64],
|
||||
pub samples: [AtomicU32; 256],
|
||||
peak_left: AtomicU32,
|
||||
peak_right: AtomicU32,
|
||||
}
|
||||
@@ -29,12 +29,19 @@ impl ScopeBuffer {
|
||||
let mut peak_l: f32 = 0.0;
|
||||
let mut peak_r: f32 = 0.0;
|
||||
|
||||
// Calculate peaks from ALL input frames for accurate VU metering
|
||||
for chunk in data.chunks(2) {
|
||||
if let [left, right] = chunk {
|
||||
peak_l = peak_l.max(left.abs());
|
||||
peak_r = peak_r.max(right.abs());
|
||||
}
|
||||
}
|
||||
|
||||
// Downsample for scope display
|
||||
let frames = data.len() / 2;
|
||||
for (i, atom) in self.samples.iter().enumerate() {
|
||||
let idx = i * 2;
|
||||
let left = data.get(idx).copied().unwrap_or(0.0);
|
||||
let right = data.get(idx + 1).copied().unwrap_or(0.0);
|
||||
peak_l = peak_l.max(left.abs());
|
||||
peak_r = peak_r.max(right.abs());
|
||||
let frame_idx = (i * frames) / self.samples.len();
|
||||
let left = data.get(frame_idx * 2).copied().unwrap_or(0.0);
|
||||
atom.store(left.to_bits(), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
@@ -42,7 +49,7 @@ impl ScopeBuffer {
|
||||
self.peak_right.store(peak_r.to_bits(), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn read(&self) -> [f32; 64] {
|
||||
pub fn read(&self) -> [f32; 256] {
|
||||
std::array::from_fn(|i| f32::from_bits(self.samples[i].load(Ordering::Relaxed)))
|
||||
}
|
||||
|
||||
@@ -146,7 +153,7 @@ impl SpectrumAnalyzer {
|
||||
let hi = self.band_edges[band + 1].max(lo + 1);
|
||||
let sum: f32 = self.fft_buf[lo..hi].iter().map(|c| c.norm()).sum();
|
||||
let avg = sum / (hi - lo) as f32;
|
||||
let amplitude = avg / (FFT_SIZE as f32 / 2.0);
|
||||
let amplitude = avg / (FFT_SIZE as f32 / 4.0);
|
||||
let db = 20.0 * amplitude.max(1e-10).log10();
|
||||
*mag = ((db + 60.0) / 60.0).clamp(0.0, 1.0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user