Skip to content

Commit

Permalink
fix(delay): compute the delay value as the number of samples (refs #218)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrene committed Oct 2, 2021
1 parent 1056276 commit f0752d5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion minidsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Client {
/// Writes data to the dsp memory area
pub async fn write_dsp<T: Into<Value>>(&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?
Expand Down
11 changes: 6 additions & 5 deletions minidsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)));
}
Expand Down
52 changes: 40 additions & 12 deletions minidsp/tests/m2x4hd.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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},
utils::Combine,
Channel, DeviceInfo, MiniDSP, MiniDSPError,
};
use tokio::sync::Mutex;
use hex_literal::hex;

pub struct TestDevice {
commands_rx: UnboundedReceiver<Bytes>,
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f0752d5

Please # to comment.