-
-
Save debamitro/36edbe3344049b401754f757eedd9044 to your computer and use it in GitHub Desktop.
some test code using Rust's cpal library which reads audio data from the microphone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn catch_error(err: StreamError) { | |
println!("found error {:?}",err); | |
} | |
fn get_audio() { | |
let mut num_samples = Arc::new(AtomicUsize::new(0)); | |
let num_samples2 = num_samples.clone(); | |
let catch_data = move |data: &[f32], _info: &InputCallbackInfo| { | |
num_samples.fetch_add(data.len(), Ordering::Relaxed); | |
}; | |
let host = cpal::default_host(); | |
if let Some(device) = host.default_input_device() { | |
if let Ok(config) = device.default_input_config() { | |
let custom_config = cpal::StreamConfig { | |
channels : 2, | |
sample_rate: cpal::SampleRate(44100), | |
buffer_size: cpal::BufferSize::Default | |
}; | |
if let Ok(stream) = device.build_input_stream (&custom_config, | |
catch_data, | |
catch_error) { | |
stream.play(); | |
std::thread::sleep(std::time::Duration::from_secs(20)); | |
stream.pause(); | |
println!("number of samples {:?}",num_samples2); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment