fix speed being off by 1% 😭

This commit is contained in:
Alex Janka 2023-03-19 18:48:49 +11:00
parent dd6df41ac2
commit d001e0d0e2

View file

@ -1,7 +1,5 @@
use crate::{connect::DownsampleType, constants::CLOCK_SPEED}; use crate::{connect::DownsampleType, constants::CLOCK_SPEED};
const TIME_PER_CYCLE: f32 = 1. / CLOCK_SPEED as f32;
struct Averager { struct Averager {
accum: [f32; 2], accum: [f32; 2],
num: usize, num: usize,
@ -48,7 +46,7 @@ pub(super) struct Downsampler {
impl Downsampler { impl Downsampler {
pub fn new(sample_rate: f32, algo: DownsampleType) -> Self { pub fn new(sample_rate: f32, algo: DownsampleType) -> Self {
Self { Self {
ratio: 1. / sample_rate, ratio: CLOCK_SPEED as f32 / sample_rate,
time_accum: 0., time_accum: 0.,
average: match algo { average: match algo {
DownsampleType::Linear => Some(Averager::default()), DownsampleType::Linear => Some(Averager::default()),
@ -60,12 +58,12 @@ impl Downsampler {
pub fn process(&mut self, signal: Vec<[f32; 2]>) -> Vec<[f32; 2]> { pub fn process(&mut self, signal: Vec<[f32; 2]>) -> Vec<[f32; 2]> {
let mut output = vec![]; let mut output = vec![];
for ref val in signal { for ref val in signal {
self.time_accum += TIME_PER_CYCLE; self.time_accum += 1.;
if let Some(ref mut averager) = self.average { if let Some(ref mut averager) = self.average {
averager.push(val); averager.push(val);
} }
if self.time_accum >= self.ratio { 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 { output.push(if let Some(ref mut averager) = self.average {
averager.finish() averager.finish()
} else { } else {