1
Fork 0

volume calculation

This commit is contained in:
Andy Killorin 2025-01-16 11:16:45 -05:00
parent fed2a43481
commit 945b4710e7
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

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