'Methods how to save data from stream in cpal
Trying to save into file vector of data from cpal stream (Rust). Any ideas on how to make it would be much appreciated. I've tried to work via lazy_static::lazy_static crate and std::sync::Mutex. But stream integrity is violated.
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{BufferSize, SampleRate, StreamConfig};
use std::thread::sleep;
use std::time::Duration;
fn main() {
for host in cpal::available_hosts() {
println!("Host={:?}, input devices:", host);
for dev in cpal::host_from_id(host).unwrap().input_devices().unwrap() {
println!(
" {} - Supported input configs:",
dev.name()
.as_ref()
.map(|s| s.as_str())
.unwrap_or("<unknown>"),
);
for cfg in dev.supported_input_configs().unwrap() {
println!(" {:#?}", cfg);
}
}
}
let default_in = cpal::default_host().default_input_device().unwrap();
let stream = default_in
.build_input_stream::<f32, _, _>(
&StreamConfig {
channels: 2,
sample_rate: SampleRate(96000),
buffer_size: BufferSize::Default,
},
|data, _x| {
println!("got data: {:?} samples", data);
},
|_e| {},
)
.unwrap();
stream.play().unwrap();
sleep(Duration::from_secs(1));
stream.pause().unwrap();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
