use std::collections::HashMap;
use std::collections::{BTreeMap};
#[derive(Debug, Clone)]
pub struct VoiceConfig {
pub language: String,
pub enable_wake_word: bool,
pub wake_word: String,
pub enable_continuous: bool,
}
impl Default for VoiceConfig {
fn default() -> Self {
Self {
language: "en-US".to_string(),
enable_wake_word: false,
wake_word: "Hey Amber".to_string(),
enable_continuous: false,
}
}
}
#[derive(Debug, Clone)]
pub struct VoiceCommand {
pub phrase: String,
pub action: String,
}
#[derive(Debug, Clone)]
pub struct SpeechResult {
pub text: String,
pub confidence: f32,
pub matched_command: Option<String>,
}
pub struct VoiceRecognition {
config: VoiceConfig,
commands: HashMap<String, VoiceCommand>,
listening: bool,
}
impl VoiceRecognition {
pub fn new(config: VoiceConfig) -> Result<Self, VoiceError> {
Ok(Self {
config,
commands: HashMap::new(),
listening: false,
})
}
pub fn wake_word_enabled(&self) -> bool {
self.config.enable_wake_word
}
pub fn register_command(&mut self, command: VoiceCommand) {
self.commands.insert(command.phrase.clone(), command);
}
pub fn unregister_command(&mut self, phrase: &str) {
self.commands.remove(phrase);
}
pub fn process_audio(&mut self, audio: &[f32]) -> Result<Option<SpeechResult>, VoiceError> {
Ok(None)
}
pub fn start_listening(&mut self) -> Result<(), VoiceError> {
self.listening = true;
Ok(())
}
pub fn stop_listening(&mut self) {
self.listening = false;
}
pub fn is_listening(&self) -> bool {
self.listening
}
pub fn command_count(&self) -> usize {
self.commands.len()
}
}
#[derive(Debug, Clone)]
pub enum VoiceError {
InitializationFailed(String),
RecognitionFailed(String),
AudioProcessingFailed(String),
}
impl std::fmt::Display for VoiceError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InitializationFailed(msg) => write!(f, "初始化失败: {}", msg),
Self::RecognitionFailed(msg) => write!(f, "识别失败: {}", msg),
Self::AudioProcessingFailed(msg) => write!(f, "音频处理失败: {}", msg),
}
}
}
impl std::error::Error for VoiceError {}