fixed feedback delay buffer, it was dependent on the sample rate

This commit is contained in:
Weird Constructor 2021-06-07 05:09:58 +02:00
parent e9e84df708
commit 404c8d6509
2 changed files with 23 additions and 33 deletions

View file

@ -5,7 +5,7 @@
use super::{ use super::{
GraphMessage, QuickMessage, DropMsg, NodeProg, GraphMessage, QuickMessage, DropMsg, NodeProg,
UNUSED_MONITOR_IDX, MAX_ALLOCATED_NODES, MAX_SMOOTHERS, 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::dsp::{NodeId, Node, MAX_BLOCK_SIZE};
use crate::util::{Smoother, AtomicFloat}; use crate::util::{Smoother, AtomicFloat};
@ -96,20 +96,15 @@ pub struct FeedbackBuffer {
write_ptr: usize, write_ptr: usize,
/// Read pointer, is always behind write_ptr by an initial amount /// Read pointer, is always behind write_ptr by an initial amount
read_ptr: usize, 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 { impl FeedbackBuffer {
pub fn new() -> Self { pub fn new() -> Self {
let delay_sample_count = (44100.0 as usize * FB_DELAY_TIME_US) / 1000000;
Self { Self {
buffer: [0.0; MAX_FB_DELAY_SIZE], buffer: [0.0; MAX_FB_DELAY_SIZE],
write_ptr: 0, write_ptr: delay_sample_count % MAX_FB_DELAY_SIZE,
read_ptr: 0, read_ptr: 0,
sample_count: 0,
} }
} }
@ -117,7 +112,7 @@ impl FeedbackBuffer {
self.buffer = [0.0; MAX_FB_DELAY_SIZE]; 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]; self.buffer = [0.0; MAX_FB_DELAY_SIZE];
// The delay sample count maximum is defined by MAX_FB_DELAY_SRATE, // The delay sample count maximum is defined by MAX_FB_DELAY_SRATE,
// after that the feedback delays become shorter than they should be // 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 // For more elaborate and longer delays an extra delay node should
// be used before FbWr or after FbRd. // be used before FbWr or after FbRd.
// let delay_sample_count = (sr as usize * FB_DELAY_TIME_US) / 1000000; let delay_sample_count = (sr as usize * FB_DELAY_TIME_US) / 1000000;
self.write_ptr = 0; self.write_ptr = delay_sample_count % MAX_FB_DELAY_SIZE;
self.sample_count = 0;
self.read_ptr = 0; self.read_ptr = 0;
} }
#[inline] #[inline]
pub fn write(&mut self, s: f32) { pub fn write(&mut self, s: f32) {
self.write_ptr = (self.write_ptr + 1) % MAX_FB_DELAY_SIZE; self.write_ptr = (self.write_ptr + 1) % MAX_FB_DELAY_SIZE;
self.sample_count += 1;
self.buffer[self.write_ptr] = s; self.buffer[self.write_ptr] = s;
//d// println!("WRITE[{}] = {:8.3}", self.write_ptr, s);
} }
#[inline] #[inline]
pub fn read(&mut self) -> f32 { 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.read_ptr = (self.read_ptr + 1) % MAX_FB_DELAY_SIZE;
self.buffer[self.read_ptr] self.buffer[self.read_ptr]
} else { //d// let s = self.buffer[self.read_ptr];
0.0 //d// println!("READ[{}] = {:8.3}", self.read_ptr, s);
} //d// s
} }
} }

View file

@ -1010,26 +1010,24 @@ fn check_matrix_node_feedback() {
assert_decimated_feq!( assert_decimated_feq!(
out_l, 15, vec![ out_l, 15, vec![
// The initial zeros are the feedback delays: // 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.0, 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.68328893, 0.9925844, 0.48698083, -0.4184115, -0.9803018,
-0.90973216, -0.20093217, 0.6728242, 0.99420625, 0.49937242, -0.73738277, 0.110905044, 0.8681419, 0.9126584, 0.20790927,
-0.40543562, -0.9773846, -0.7469322, 0.096737176, 0.86098254, -0.6675302, -0.99494797, -0.50553185, 0.39891028, 0.97586703,
0.9183933, 0.22181934, -0.6568572, -0.9962849, -0.5177674, 0.7516482, -0.089641616, -0.8573498, -0.9211795, -0.22875604 ]);
0.3858096]);
assert_decimated_feq!( assert_decimated_feq!(
out_r, 15, vec![ out_r, 15, vec![
// The initial zeros are the feedback delays: // The initial zeros are the feedback delays:
// The frequency will be established a bit later because // The frequency will be established a bit later because
// the parameter setting of 880 Hz will be smoothed: // 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.0, 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.6889244, 0.9872897, 0.40561166, -0.5643036, -0.99751425,
-0.7120098, 0.28678903, 0.9854398, 0.56962675, -0.52827656, -0.42060927, 0.6112674, 0.979101, 0.21974884, -0.81355125,
-0.9845062, -0.17494306, 0.8781463, 0.7117191, -0.49352795, -0.83687085, 0.23462467, 0.9970811, 0.35165882, -0.8186541,
-0.95974547, 0.085026, 0.9966484, 0.21335565, -0.9510752, -0.7396998, 0.5222105, 0.9234876, -0.2683919, -0.983062 ]);
-0.3690226]);
// Let the frequency settle... // 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 (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); let fft_res_l = fft_thres_at_ms(&mut out_l[..], FFT::F1024, 100, 0.0);