You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use cpal::traits::{DeviceTrait,HostTrait,StreamTrait};use cpal::{Stream,StreamConfig};fnmain() -> Result<(), anyhow::Error>{let host = cpal::default_host();let device = host.default_input_device().expect("no device");let config:StreamConfig = device.default_input_config()?.into();// 创建录音流let input_stream = device.build_input_stream(&config,move |data:&[f32], _:&cpal::InputCallbackInfo| {let rms = calculate_rms(data);println!("db:", 20.0 * rms.log10());},
|err| {eprintln!("error: {}", err);},Option::None,)?;
input_stream.play()?;
std::thread::sleep(std::time::Duration::from_secs(10));Ok(())}fncalculate_rms(data:&[f32]) -> f32{letmut sum = 0.0;for&sample in data {
sum += sample * sample;}(sum / data.len()asf32).sqrt()}
I wanted to get the DB value of the recording, but why is it that the data sampled is very close to 0, and the DB value calculated is basically kept at -50。Can someone help 🥲
The text was updated successfully, but these errors were encountered:
Are you sure that you're capturing the correct input device?
In Windows you can check this in the Windows Soundmixer and compare it with your selected device name.
Is this a device with 1 channel (mono) or 2 (stereo)?
I wanted to get the DB value of the recording, but why is it that the data sampled is very close to 0, and the DB value calculated is basically kept at -50。Can someone help 🥲
The text was updated successfully, but these errors were encountered: