diff --git a/interface/src/main.rs b/interface/src/main.rs index 3db6939..312152d 100644 --- a/interface/src/main.rs +++ b/interface/src/main.rs @@ -3,9 +3,10 @@ use std::{sync::mpsc, thread::sleep, time::Duration}; use anyhow::{Context, Ok, Result}; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; -use pitch_detection::detector::{mcleod::McLeodDetector, PitchDetector}; +use pitch_detection::{detector::{mcleod::McLeodDetector, PitchDetector}, utils}; fn main() -> Result<()> { + // assumes pulseaudio system with f32 samples and 2204 sample packets dbg!(cpal::available_hosts()); let host = cpal::default_host(); let device = host.devices().unwrap().find(|d|d.name().unwrap() == "pulse").context("no pulse")?; @@ -19,11 +20,17 @@ fn main() -> Result<()> { const POWER_THRESHOLD: f32 = 5.0; const CLARITY_THRESHOLD: f32 = 0.7; + const PACKET_LEN: usize = 2204; let stream = device.build_input_stream(&config.into(), move | data: &[f32], _: &_| { - let mut detctor = McLeodDetector::new(2204, 2204/2); - sender.send(detctor.get_pitch(&data[..2204], rate.0 as usize, POWER_THRESHOLD, CLARITY_THRESHOLD)).unwrap(); + assert!(data.len() >= PACKET_LEN); + let data = &data[..PACKET_LEN]; + + // reinitialized every packet as it is not thread safe + let mut detctor = McLeodDetector::new(PACKET_LEN, PACKET_LEN/2); + let vol = utils::buffer::square_sum(data); + sender.send((detctor.get_pitch(data, rate.0 as usize, POWER_THRESHOLD, CLARITY_THRESHOLD),vol)).unwrap(); }, move |err| {eprintln!("{err}")} , @@ -31,14 +38,14 @@ fn main() -> Result<()> { stream.play()?; - for note in notes { + for (note,vol) in notes { if let Some(note) = note { dbg!(note.frequency); dbg!(note.clarity); + dbg!(vol); } } - drop(stream); Ok(())