From f0752d571674464ea1e280f086660c42e50fd471 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Sat, 2 Oct 2021 18:07:03 -0400 Subject: [PATCH] fix(delay): compute the delay value as the number of samples (refs #218) --- minidsp/src/client.rs | 2 +- minidsp/src/lib.rs | 11 +++++---- minidsp/tests/m2x4hd.rs | 52 +++++++++++++++++++++++++++++++---------- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/minidsp/src/client.rs b/minidsp/src/client.rs index 525812ef..49c5cbb8 100644 --- a/minidsp/src/client.rs +++ b/minidsp/src/client.rs @@ -116,7 +116,7 @@ impl Client { /// Writes data to the dsp memory area pub async fn write_dsp>(&self, addr: u16, value: T) -> Result<(), MiniDSPError> { self.roundtrip(Commands::Write { - addr: Addr::new(addr, 2), + addr: Addr::new(addr, 3), value: value.into(), }) .await? diff --git a/minidsp/src/lib.rs b/minidsp/src/lib.rs index 96550086..88ac9eec 100644 --- a/minidsp/src/lib.rs +++ b/minidsp/src/lib.rs @@ -354,12 +354,13 @@ impl<'a> Output<'a> { /// Sets the output delay setting pub async fn set_delay(&self, value: Duration) -> Result<()> { - // Each delay increment is 0.010 ms - // let value = value / Duration::from_micros(10); - let value = value.as_micros() / 10; - if value > 80 { + // Convert the duration to a number of samples + let value = (value.as_micros() as f64 * self.dsp.device.internal_sampling_rate as f64 + / 1_000_000_f64) + .round() as u64; + if value > 8000 { return Err(MiniDSPError::InternalError(anyhow!( - "Delay should be within [0, 80], was {:?}", + "Delay should be within [0, 80] ms was {:?}", value ))); } diff --git a/minidsp/tests/m2x4hd.rs b/minidsp/tests/m2x4hd.rs index b02aa78d..416d1e9a 100644 --- a/minidsp/tests/m2x4hd.rs +++ b/minidsp/tests/m2x4hd.rs @@ -1,10 +1,11 @@ -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; use bytes::Bytes; use futures::{ channel::mpsc::{self, UnboundedReceiver, UnboundedSender}, pin_mut, Future, FutureExt, SinkExt, StreamExt, }; +use hex_literal::hex; use minidsp::{ client::Client, transport::{Multiplexer, Transport}, @@ -12,7 +13,6 @@ use minidsp::{ Channel, DeviceInfo, MiniDSP, MiniDSPError, }; use tokio::sync::Mutex; -use hex_literal::hex; pub struct TestDevice { commands_rx: UnboundedReceiver, @@ -76,7 +76,7 @@ impl TestDevice { }, cmd = &mut cmd => { let cmd = cmd.unwrap(); - println!("{} vs {}", hex::encode(&cmd), hex::encode(expect_cmd.as_ref())); + println!("actual: {} vs expected: {}", hex::encode(&cmd), hex::encode(expect_cmd.as_ref())); assert_eq!(&cmd, expect_cmd.as_ref()); self.responses_tx.send(Ok(Bytes::from_static(response))).await.unwrap(); } @@ -106,30 +106,58 @@ async fn test_2x4hd() -> anyhow::Result<()> { ) .await .unwrap(); - } { // Input PEQs let peq = input.peq(0)?; + dev.run(peq.set_bypass(true), hex!("05 19 802085 43"), &[0x01]) + .await + .unwrap(); + dev.run(peq.set_bypass(false), hex!("05 19 002085 c3"), &[0x01]) + .await + .unwrap(); + dev.run( - peq.set_bypass(true), - hex!("05 19 802085 43"), + peq.set_coefficients(&[1.0, 0.2, 0.3, 0.4, 0.5]), + hex!("1b 30 802085 0000 0000803f cdcc4c3e 9a99993e cdcccc3e 0000003f 3e"), &[0x01], ) .await .unwrap(); + } + + let output = dsp.output(0)?; + { + // Gain & Mute dev.run( - peq.set_bypass(false), - hex!("05 19 002085 c3"), - &[0x01], + output.set_mute(true), + hex!("09 13 800002 01000000 9f"), + &[0x1], + ) + .await + .unwrap(); + dev.run( + output.set_mute(false), + hex!("09 13 800002 02000000 a0"), + &[0x1], + ) + .await + .unwrap(); + } + { + // Delays + dev.run( + output.set_delay(Duration::from_micros(10)), + hex!("09 13 800040 01000000 dd"), + &[0x1], ) .await .unwrap(); dev.run( - peq.set_coefficients(&[1.0, 0.2, 0.3, 0.4, 0.5]), - hex!("1b 30 802085 0000 0000803f cdcc4c3e 9a99993e cdcccc3e 0000003f 3e"), - &[0x01], + output.set_delay(Duration::from_millis(1)), + hex!("09 13 800040 60000000 3c"), + &[0x1], ) .await .unwrap();