use alsa by default on linux (#190)
* use alsa by default on linux * only import trait when needed
This commit is contained in:
parent
f165b40a60
commit
d602a3a8e2
5 changed files with 19 additions and 4 deletions
|
|
@ -67,7 +67,7 @@ fn play_audio_file(
|
||||||
OutputStreamBuilder::from_default_device()?
|
OutputStreamBuilder::from_default_device()?
|
||||||
} else {
|
} else {
|
||||||
// Try to find the device by name
|
// 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 devices = host.output_devices()?;
|
||||||
|
|
||||||
let mut found_device = None;
|
let mut found_device = None;
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ pub struct CpalDeviceInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_input_devices() -> Result<Vec<CpalDeviceInfo>, Box<dyn std::error::Error>> {
|
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 default_name = host.default_input_device().and_then(|d| d.name().ok());
|
||||||
|
|
||||||
let mut out = Vec::<CpalDeviceInfo>::new();
|
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>> {
|
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 default_name = host.default_output_device().and_then(|d| d.name().ok());
|
||||||
|
|
||||||
let mut out = Vec::<CpalDeviceInfo>::new();
|
let mut out = Vec::<CpalDeviceInfo>::new();
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ impl AudioRecorder {
|
||||||
let (sample_tx, sample_rx) = mpsc::channel::<Vec<f32>>();
|
let (sample_tx, sample_rx) = mpsc::channel::<Vec<f32>>();
|
||||||
let (cmd_tx, cmd_rx) = mpsc::channel::<Cmd>();
|
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 {
|
let device = match device {
|
||||||
Some(dev) => dev,
|
Some(dev) => dev,
|
||||||
None => host
|
None => host
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
pub mod audio;
|
pub mod audio;
|
||||||
pub mod constants;
|
pub mod constants;
|
||||||
pub mod text;
|
pub mod text;
|
||||||
|
pub mod utils;
|
||||||
pub mod vad;
|
pub mod vad;
|
||||||
|
|
||||||
pub use audio::{
|
pub use audio::{
|
||||||
list_input_devices, list_output_devices, save_wav_file, AudioRecorder, CpalDeviceInfo,
|
list_input_devices, list_output_devices, save_wav_file, AudioRecorder, CpalDeviceInfo,
|
||||||
};
|
};
|
||||||
pub use text::apply_custom_words;
|
pub use text::apply_custom_words;
|
||||||
|
pub use utils::get_cpal_host;
|
||||||
pub use vad::{SileroVad, VoiceActivityDetector};
|
pub use vad::{SileroVad, VoiceActivityDetector};
|
||||||
|
|
|
||||||
13
src-tauri/src/audio_toolkit/utils.rs
Normal file
13
src-tauri/src/audio_toolkit/utils.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue