amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
Documentation
/// 语音识别系统
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 {}