From d001e0d0e267f8b568cd5a5a80713c946843712f Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Sun, 19 Mar 2023 18:48:49 +1100 Subject: [PATCH] =?UTF-8?q?fix=20speed=20being=20off=20by=201%=20?= =?UTF-8?q?=F0=9F=98=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/processor/memory/mmio/apu/downsampler.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/src/processor/memory/mmio/apu/downsampler.rs b/lib/src/processor/memory/mmio/apu/downsampler.rs index d7ba281..926483a 100644 --- a/lib/src/processor/memory/mmio/apu/downsampler.rs +++ b/lib/src/processor/memory/mmio/apu/downsampler.rs @@ -1,7 +1,5 @@ use crate::{connect::DownsampleType, constants::CLOCK_SPEED}; -const TIME_PER_CYCLE: f32 = 1. / CLOCK_SPEED as f32; - struct Averager { accum: [f32; 2], num: usize, @@ -48,7 +46,7 @@ pub(super) struct Downsampler { impl Downsampler { pub fn new(sample_rate: f32, algo: DownsampleType) -> Self { Self { - ratio: 1. / sample_rate, + ratio: CLOCK_SPEED as f32 / sample_rate, time_accum: 0., average: match algo { DownsampleType::Linear => Some(Averager::default()), @@ -60,12 +58,12 @@ impl Downsampler { pub fn process(&mut self, signal: Vec<[f32; 2]>) -> Vec<[f32; 2]> { let mut output = vec![]; for ref val in signal { - self.time_accum += TIME_PER_CYCLE; + self.time_accum += 1.; if let Some(ref mut averager) = self.average { averager.push(val); } if self.time_accum >= self.ratio { - self.time_accum = 0.; + self.time_accum -= self.ratio; output.push(if let Some(ref mut averager) = self.average { averager.finish() } else {