config.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. 语音唤醒配置类
  3. """
  4. from pathlib import Path
  5. from typing import Optional
  6. class WakeWordConfig:
  7. """语音唤醒配置"""
  8. def __init__(
  9. self,
  10. model_path: str = "models",
  11. sample_rate: int = 16000,
  12. num_threads: int = 4,
  13. provider: str = "cpu",
  14. max_active_paths: int = 2,
  15. keywords_score: float = 1.8,
  16. keywords_threshold: float = 0.2,
  17. num_trailing_blanks: int = 1,
  18. detection_cooldown: float = 1.5,
  19. ):
  20. """
  21. 初始化语音唤醒配置
  22. Args:
  23. model_path: 模型文件目录路径
  24. sample_rate: 音频采样率
  25. num_threads: 线程数
  26. provider: 计算提供者 (cpu/cuda)
  27. max_active_paths: 最大激活路径数
  28. keywords_score: 关键词分数
  29. keywords_threshold: 关键词阈值
  30. num_trailing_blanks: 尾部空白数
  31. detection_cooldown: 检测冷却时间(秒)
  32. """
  33. self.model_path = Path(model_path)
  34. self.sample_rate = sample_rate
  35. self.num_threads = num_threads
  36. self.provider = provider
  37. self.max_active_paths = max_active_paths
  38. self.keywords_score = keywords_score
  39. self.keywords_threshold = keywords_threshold
  40. self.num_trailing_blanks = num_trailing_blanks
  41. self.detection_cooldown = detection_cooldown
  42. def validate(self) -> bool:
  43. """验证配置参数"""
  44. if not self.model_path.exists():
  45. raise FileNotFoundError(f"模型目录不存在: {self.model_path}")
  46. required_files = [
  47. "encoder.onnx",
  48. "decoder.onnx",
  49. "joiner.onnx",
  50. "tokens.txt",
  51. "keywords.txt",
  52. ]
  53. for file_name in required_files:
  54. if not (self.model_path / file_name).exists():
  55. raise FileNotFoundError(f"模型文件不存在: {self.model_path / file_name}")
  56. if not 0.1 <= self.keywords_threshold <= 1.0:
  57. raise ValueError(f"关键词阈值 {self.keywords_threshold} 超出范围 [0.1, 1.0]")
  58. if not 0.1 <= self.keywords_score <= 10.0:
  59. raise ValueError(f"关键词分数 {self.keywords_score} 超出范围 [0.1, 10.0]")
  60. return True