Loop forever, handle errors more gracefully

This commit is contained in:
Joey Hines 2025-08-16 13:09:41 -06:00
parent 22ac4dbc2b
commit 10c9753262
Signed by: joeyahines
GPG Key ID: 38BA6F25C94C9382

View File

@ -8,7 +8,7 @@ use audiopus::{Application, Channels, SampleRate};
use clap::Parser;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{FromSample, Sample, StreamConfig};
use log::{LevelFilter, info};
use log::{LevelFilter, info, error};
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
@ -68,11 +68,8 @@ fn main() -> Result<(), anyhow::Error> {
udp_connection,
)));
// A flag to indicate that recording is in progress.
println!("Begin recording...");
let err_fn = move |err| {
eprintln!("an error occurred on stream: {err}");
error!("an error occurred on stream: {err}");
};
let sound_buffer2 = sound_buffer.clone();
@ -108,13 +105,12 @@ fn main() -> Result<(), anyhow::Error> {
}
};
info!("Running tap...");
stream.play()?;
// Let recording go for roughly three seconds.
std::thread::sleep(std::time::Duration::from_secs(3));
drop(stream);
drop(sound_buffer);
std::thread::park();
info!("Exiting...");
Ok(())
}
@ -128,6 +124,8 @@ fn write_input_data<const N: usize, T>(
if let Ok(mut ctx) = ctx.try_lock() {
let sample: Vec<f32> = input.iter().map(|s| f32::from_sample(*s)).collect();
ctx.write_data(&sample).unwrap();
if let Err(err) = ctx.write_data(&sample) {
error!("Got error when writing data to sound buffer: {err}")
}
}
}