J.A.R.V.I.S-rust/crates/jarvis-core/src/audio.rs

89 lines
2.3 KiB
Rust
Raw Normal View History

mod kira;
2025-12-11 23:43:50 +05:00
mod rodio;
2025-12-11 23:43:50 +05:00
use once_cell::sync::OnceCell;
use std::cmp::Ordering;
use std::path::PathBuf;
use crate::config::structs::AudioType;
2025-12-11 23:43:50 +05:00
use crate::{config, DB, SOUND_DIR};
static AUDIO_TYPE: OnceCell<AudioType> = OnceCell::new();
pub fn init() -> Result<(), ()> {
if AUDIO_TYPE.get().is_some() {
2025-12-11 23:43:50 +05:00
return Ok(());
} // already initialized
// set default audio type
// @TODO. Make it configurable?
AUDIO_TYPE.set(config::DEFAULT_AUDIO_TYPE).unwrap();
// load given audio backend
match AUDIO_TYPE.get().unwrap() {
AudioType::Rodio => {
// Init Rodio
info!("Initializing Rodio audio backend.");
match rodio::init() {
Ok(_) => {
info!("Successfully initialized Rodio audio backend.");
2025-12-11 23:43:50 +05:00
}
Err(()) => {
error!("Failed to initialize Rodio audio backend.");
2025-12-11 23:43:50 +05:00
return Err(());
}
}
2025-12-11 23:43:50 +05:00
}
AudioType::Kira => {
// Init Kira
info!("Initializing Kira audio backend.");
match kira::init() {
Ok(_) => {
info!("Successfully initialized Kira audio backend.");
2025-12-11 23:43:50 +05:00
}
Err(msg) => {
error!("Failed to initialize Kira audio backend.");
2025-12-11 23:43:50 +05:00
return Err(());
}
}
}
}
Ok(())
}
pub fn play_sound(filename: &PathBuf) {
info!("Playing {}", filename.display());
match AUDIO_TYPE.get().unwrap() {
AudioType::Rodio => {
rodio::play_sound(filename, true);
}
2025-12-11 23:43:50 +05:00
AudioType::Kira => kira::play_sound(filename),
}
}
pub fn get_sound_directory() -> Option<PathBuf> {
let voice = DB.get().unwrap().voice.as_str();
let voice_path = SOUND_DIR.join(voice);
match voice_path.exists() && voice_path.cmp(&SOUND_DIR) != Ordering::Equal {
true => Some(voice_path),
_ => {
let default_voice_path = SOUND_DIR.join(config::DEFAULT_VOICE);
match default_voice_path.exists() {
true => Some(default_voice_path),
2025-12-11 23:43:50 +05:00
_ => {
error!("No sounds found. Search path - {:?}", voice_path);
None
}
}
}
}
2025-12-11 23:43:50 +05:00
}