""" 语音唤醒配置类 """ from pathlib import Path from typing import Optional class WakeWordConfig: """语音唤醒配置""" def __init__( self, model_path: str = "models", sample_rate: int = 16000, num_threads: int = 4, provider: str = "cpu", max_active_paths: int = 2, keywords_score: float = 1.8, keywords_threshold: float = 0.2, num_trailing_blanks: int = 1, detection_cooldown: float = 1.5, ): """ 初始化语音唤醒配置 Args: model_path: 模型文件目录路径 sample_rate: 音频采样率 num_threads: 线程数 provider: 计算提供者 (cpu/cuda) max_active_paths: 最大激活路径数 keywords_score: 关键词分数 keywords_threshold: 关键词阈值 num_trailing_blanks: 尾部空白数 detection_cooldown: 检测冷却时间(秒) """ self.model_path = Path(model_path) self.sample_rate = sample_rate self.num_threads = num_threads self.provider = provider self.max_active_paths = max_active_paths self.keywords_score = keywords_score self.keywords_threshold = keywords_threshold self.num_trailing_blanks = num_trailing_blanks self.detection_cooldown = detection_cooldown def validate(self) -> bool: """验证配置参数""" if not self.model_path.exists(): raise FileNotFoundError(f"模型目录不存在: {self.model_path}") required_files = [ "encoder.onnx", "decoder.onnx", "joiner.onnx", "tokens.txt", "keywords.txt", ] for file_name in required_files: if not (self.model_path / file_name).exists(): raise FileNotFoundError(f"模型文件不存在: {self.model_path / file_name}") if not 0.1 <= self.keywords_threshold <= 1.0: raise ValueError(f"关键词阈值 {self.keywords_threshold} 超出范围 [0.1, 1.0]") if not 0.1 <= self.keywords_score <= 10.0: raise ValueError(f"关键词分数 {self.keywords_score} 超出范围 [0.1, 10.0]") return True