95 lines
3.2 KiB
Rust
95 lines
3.2 KiB
Rust
//! Records a WAV file (roughly 3 seconds long) using the default input device and format.
|
|
//!
|
|
//! The input data is recorded to "$CARGO_MANIFEST_DIR/recorded.wav".
|
|
|
|
extern crate anyhow;
|
|
extern crate cpal;
|
|
extern crate hound;
|
|
|
|
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
|
use std::fs::File;
|
|
use std::io::BufWriter;
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
fn main() -> Result<(), anyhow::Error> {
|
|
// Use the default host for working with audio devices.
|
|
let host = cpal::default_host();
|
|
|
|
// Setup the default input device and stream with the default input format.
|
|
let device = host
|
|
.default_input_device()
|
|
.expect("Failed to get default input device");
|
|
println!("Default input device: {}", device.name()?);
|
|
let format = device
|
|
.default_input_format()
|
|
.expect("Failed to get default input format");
|
|
println!("Default input format: {:?}", format);
|
|
// The WAV file we're recording to.
|
|
const PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/recorded.wav");
|
|
let spec = wav_spec_from_format(&format);
|
|
let writer = hound::WavWriter::create(PATH, spec)?;
|
|
let writer = Arc::new(Mutex::new(Some(writer)));
|
|
|
|
// A flag to indicate that recording is in progress.
|
|
println!("Begin recording...");
|
|
|
|
// Run the input stream on a separate thread.
|
|
let writer_2 = writer.clone();
|
|
|
|
let err_fn = move |err| {
|
|
eprintln!("an error occurred on stream: {}", err);
|
|
};
|
|
|
|
let data_fn = move |data: &cpal::Data| match data.sample_format() {
|
|
cpal::SampleFormat::F32 => write_input_data::<f32, f32>(data, &writer_2),
|
|
cpal::SampleFormat::I16 => write_input_data::<i16, i16>(data, &writer_2),
|
|
cpal::SampleFormat::U16 => write_input_data::<u16, i16>(data, &writer_2),
|
|
};
|
|
|
|
let stream = device.build_input_stream_raw(&format, data_fn, err_fn)?;
|
|
|
|
stream.play()?;
|
|
|
|
// Let recording go for roughly three seconds.
|
|
std::thread::sleep(std::time::Duration::from_secs(3));
|
|
drop(stream);
|
|
writer.lock().unwrap().take().unwrap().finalize()?;
|
|
println!("Recording {} complete!", PATH);
|
|
Ok(())
|
|
}
|
|
|
|
fn sample_format(format: cpal::SampleFormat) -> hound::SampleFormat {
|
|
match format {
|
|
cpal::SampleFormat::U16 => hound::SampleFormat::Int,
|
|
cpal::SampleFormat::I16 => hound::SampleFormat::Int,
|
|
cpal::SampleFormat::F32 => hound::SampleFormat::Float,
|
|
}
|
|
}
|
|
|
|
fn wav_spec_from_format(format: &cpal::Format) -> hound::WavSpec {
|
|
hound::WavSpec {
|
|
channels: format.channels as _,
|
|
sample_rate: format.sample_rate.0 as _,
|
|
bits_per_sample: (format.data_type.sample_size() * 8) as _,
|
|
sample_format: sample_format(format.data_type),
|
|
}
|
|
}
|
|
|
|
type WavWriterHandle = Arc<Mutex<Option<hound::WavWriter<BufWriter<File>>>>>;
|
|
|
|
fn write_input_data<T, U>(input: &cpal::Data, writer: &WavWriterHandle)
|
|
where
|
|
T: cpal::Sample,
|
|
U: cpal::Sample + hound::Sample,
|
|
{
|
|
let input = input.as_slice::<T>().expect("unexpected sample format");
|
|
if let Ok(mut guard) = writer.try_lock() {
|
|
if let Some(writer) = guard.as_mut() {
|
|
for &sample in input.iter() {
|
|
let sample: U = cpal::Sample::from(&sample);
|
|
writer.write_sample(sample).ok();
|
|
}
|
|
}
|
|
}
|
|
}
|