From 404c8d6509864f93d3be45f98be3cdefd4d48329 Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Mon, 7 Jun 2021 05:09:58 +0200 Subject: [PATCH] fixed feedback delay buffer, it was dependent on the sample rate --- src/nodes/node_exec.rs | 32 ++++++++++++-------------------- tests/basics.rs | 24 +++++++++++------------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/nodes/node_exec.rs b/src/nodes/node_exec.rs index 76bb049..6df99ec 100644 --- a/src/nodes/node_exec.rs +++ b/src/nodes/node_exec.rs @@ -5,7 +5,7 @@ use super::{ GraphMessage, QuickMessage, DropMsg, NodeProg, UNUSED_MONITOR_IDX, MAX_ALLOCATED_NODES, MAX_SMOOTHERS, - MAX_FB_DELAY_SIZE + MAX_FB_DELAY_SIZE, FB_DELAY_TIME_US }; use crate::dsp::{NodeId, Node, MAX_BLOCK_SIZE}; use crate::util::{Smoother, AtomicFloat}; @@ -96,20 +96,15 @@ pub struct FeedbackBuffer { write_ptr: usize, /// Read pointer, is always behind write_ptr by an initial amount read_ptr: usize, - /// The number of samples written into the buffer (to prevent overreads) - /// We need to keep track of the number of samples actually written into - /// the delay because the first or previous periods may have produced - /// not enough samples. - sample_count: usize, } impl FeedbackBuffer { pub fn new() -> Self { + let delay_sample_count = (44100.0 as usize * FB_DELAY_TIME_US) / 1000000; Self { buffer: [0.0; MAX_FB_DELAY_SIZE], - write_ptr: 0, + write_ptr: delay_sample_count % MAX_FB_DELAY_SIZE, read_ptr: 0, - sample_count: 0, } } @@ -117,7 +112,7 @@ impl FeedbackBuffer { self.buffer = [0.0; MAX_FB_DELAY_SIZE]; } - pub fn set_sample_rate(&mut self, _sr: f32) { + pub fn set_sample_rate(&mut self, sr: f32) { self.buffer = [0.0; MAX_FB_DELAY_SIZE]; // The delay sample count maximum is defined by MAX_FB_DELAY_SRATE, // after that the feedback delays become shorter than they should be @@ -131,28 +126,25 @@ impl FeedbackBuffer { // For more elaborate and longer delays an extra delay node should // be used before FbWr or after FbRd. - // let delay_sample_count = (sr as usize * FB_DELAY_TIME_US) / 1000000; - self.write_ptr = 0; - self.sample_count = 0; + let delay_sample_count = (sr as usize * FB_DELAY_TIME_US) / 1000000; + self.write_ptr = delay_sample_count % MAX_FB_DELAY_SIZE; self.read_ptr = 0; } #[inline] pub fn write(&mut self, s: f32) { self.write_ptr = (self.write_ptr + 1) % MAX_FB_DELAY_SIZE; - self.sample_count += 1; self.buffer[self.write_ptr] = s; + //d// println!("WRITE[{}] = {:8.3}", self.write_ptr, s); } #[inline] pub fn read(&mut self) -> f32 { - if self.sample_count > 0 { - self.sample_count -= 1; - self.read_ptr = (self.read_ptr + 1) % MAX_FB_DELAY_SIZE; - self.buffer[self.read_ptr] - } else { - 0.0 - } + self.read_ptr = (self.read_ptr + 1) % MAX_FB_DELAY_SIZE; + self.buffer[self.read_ptr] + //d// let s = self.buffer[self.read_ptr]; + //d// println!("READ[{}] = {:8.3}", self.read_ptr, s); + //d// s } } diff --git a/tests/basics.rs b/tests/basics.rs index 4303321..79b6447 100644 --- a/tests/basics.rs +++ b/tests/basics.rs @@ -1010,26 +1010,24 @@ fn check_matrix_node_feedback() { assert_decimated_feq!( out_l, 15, vec![ // The initial zeros are the feedback delays: - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, - 0.4248709, 0.9816776, 0.7325672, -0.11797579, -0.8716552, - -0.90973216, -0.20093217, 0.6728242, 0.99420625, 0.49937242, - -0.40543562, -0.9773846, -0.7469322, 0.096737176, 0.86098254, - 0.9183933, 0.22181934, -0.6568572, -0.9962849, -0.5177674, - 0.3858096]); + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.68328893, 0.9925844, 0.48698083, -0.4184115, -0.9803018, + -0.73738277, 0.110905044, 0.8681419, 0.9126584, 0.20790927, + -0.6675302, -0.99494797, -0.50553185, 0.39891028, 0.97586703, + 0.7516482, -0.089641616, -0.8573498, -0.9211795, -0.22875604 ]); assert_decimated_feq!( out_r, 15, vec![ // The initial zeros are the feedback delays: // The frequency will be established a bit later because // the parameter setting of 880 Hz will be smoothed: - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, - 0.42738196, 0.9861684, 0.68267936, -0.2549982, -0.96115613, - -0.7120098, 0.28678903, 0.9854398, 0.56962675, -0.52827656, - -0.9845062, -0.17494306, 0.8781463, 0.7117191, -0.49352795, - -0.95974547, 0.085026, 0.9966484, 0.21335565, -0.9510752, - -0.3690226]); + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.6889244, 0.9872897, 0.40561166, -0.5643036, -0.99751425, + -0.42060927, 0.6112674, 0.979101, 0.21974884, -0.81355125, + -0.83687085, 0.23462467, 0.9970811, 0.35165882, -0.8186541, + -0.7396998, 0.5222105, 0.9234876, -0.2683919, -0.983062 ]); // Let the frequency settle... - run_for_ms(&mut node_exec, 50.0); + run_for_ms(&mut node_exec, 80.0); let (mut out_l, mut out_r) = run_for_ms(&mut node_exec, 50.0); let fft_res_l = fft_thres_at_ms(&mut out_l[..], FFT::F1024, 100, 0.0);