use alsa by default on linux (#190)

* use alsa by default on linux

* only import trait when needed
This commit is contained in:
CJ Pais 2025-10-08 17:47:10 -07:00 committed by GitHub
parent f165b40a60
commit d602a3a8e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 19 additions and 4 deletions

View file

@ -67,7 +67,7 @@ fn play_audio_file(
OutputStreamBuilder::from_default_device()?
} else {
// Try to find the device by name
let host = cpal::default_host();
let host = crate::audio_toolkit::get_cpal_host();
let devices = host.output_devices()?;
let mut found_device = None;

View file

@ -8,7 +8,7 @@ pub struct CpalDeviceInfo {
}
pub fn list_input_devices() -> Result<Vec<CpalDeviceInfo>, Box<dyn std::error::Error>> {
let host = cpal::default_host();
let host = crate::audio_toolkit::get_cpal_host();
let default_name = host.default_input_device().and_then(|d| d.name().ok());
let mut out = Vec::<CpalDeviceInfo>::new();
@ -30,7 +30,7 @@ pub fn list_input_devices() -> Result<Vec<CpalDeviceInfo>, Box<dyn std::error::E
}
pub fn list_output_devices() -> Result<Vec<CpalDeviceInfo>, Box<dyn std::error::Error>> {
let host = cpal::default_host();
let host = crate::audio_toolkit::get_cpal_host();
let default_name = host.default_output_device().and_then(|d| d.name().ok());
let mut out = Vec::<CpalDeviceInfo>::new();

View file

@ -62,7 +62,7 @@ impl AudioRecorder {
let (sample_tx, sample_rx) = mpsc::channel::<Vec<f32>>();
let (cmd_tx, cmd_rx) = mpsc::channel::<Cmd>();
let host = cpal::default_host();
let host = crate::audio_toolkit::get_cpal_host();
let device = match device {
Some(dev) => dev,
None => host

View file

@ -1,10 +1,12 @@
pub mod audio;
pub mod constants;
pub mod text;
pub mod utils;
pub mod vad;
pub use audio::{
list_input_devices, list_output_devices, save_wav_file, AudioRecorder, CpalDeviceInfo,
};
pub use text::apply_custom_words;
pub use utils::get_cpal_host;
pub use vad::{SileroVad, VoiceActivityDetector};

View file

@ -0,0 +1,13 @@
/// Returns the appropriate CPAL host for the current platform.
/// On Linux, uses ALSA host. On other platforms, uses the default host.
pub fn get_cpal_host() -> cpal::Host {
#[cfg(target_os = "linux")]
{
cpal::host_from_id(cpal::HostId::Alsa).unwrap_or_else(|_| cpal::default_host())
}
#[cfg(not(target_os = "linux"))]
{
use cpal::traits::HostTrait;
cpal::default_host()
}
}