Skip to main content

novel_tts/
model.rs

1//! TTS模型和语音数据管理模块
2//!
3//! 该模块提供了对TTS所需模型文件和语音数据的管理功能,
4//! 包括自动下载、路径管理和状态检查等。
5
6use crate::download::{Download, get_cache_dir};
7use std::{
8    ops::{Deref, DerefMut},
9    path::Path,
10};
11
12/// TTS模型文件的下载地址
13static CHECKPOINT_URL: &str =
14    "https://github.com/mzdk100/kokoro/releases/download/V1.1/kokoro-v1.1-zh.onnx";
15
16/// 语音数据文件的下载地址
17static VOICES_URL: &str =
18    "https://github.com/mzdk100/kokoro/releases/download/V1.1/voices-v1.1-zh.bin";
19
20/// TTS检查点模型管理器
21///
22/// 负责管理TTS引擎所需的检查点模型文件,包括下载、路径管理和状态检查。
23/// 该结构体是对Download的封装,提供了更具体的模型文件管理功能。
24#[derive(Debug, Clone)]
25pub struct CheckpointModel(Download);
26
27impl CheckpointModel {
28    /// 创建新的检查点模型管理器
29    ///
30    /// # 参数
31    /// * `path` - 模型文件的本地存储路径
32    ///
33    /// # 返回值
34    /// 返回一个新的CheckpointModel实例
35    pub fn new<P: AsRef<Path>>(path: P) -> Self {
36        Self(Download::new(path, CHECKPOINT_URL))
37    }
38}
39
40impl Deref for CheckpointModel {
41    type Target = Download;
42
43    fn deref(&self) -> &Self::Target {
44        &self.0
45    }
46}
47
48impl DerefMut for CheckpointModel {
49    fn deref_mut(&mut self) -> &mut Self::Target {
50        &mut self.0
51    }
52}
53
54impl Default for CheckpointModel {
55    /// 创建默认的检查点模型管理器
56    ///
57    /// 默认会将模型文件存储在用户主目录下的.novel-tts/kokoro目录中
58    ///
59    /// # 返回值
60    /// 返回一个配置了默认路径和URL的CheckpointModel实例
61    fn default() -> Self {
62        let cache_dir = get_cache_dir().unwrap().join("kokoro");
63        let path = cache_dir.join("kokoro-v1.1-zh.onnx");
64        Self(Download::new(path, CHECKPOINT_URL))
65    }
66}
67
68/// 语音数据管理器
69///
70/// 负责管理TTS引擎所需的语音数据文件,包括下载、路径管理和状态检查。
71/// 该结构体是对Download的封装,提供了更具体的语音数据文件管理功能。
72#[derive(Debug, Clone)]
73pub struct VoicesData(Download);
74
75impl VoicesData {
76    /// 创建新的语音数据管理器
77    ///
78    /// # 参数
79    /// * `path` - 语音数据文件的本地存储路径
80    ///
81    /// # 返回值
82    /// 返回一个新的VoicesData实例
83    pub fn new<P: AsRef<Path>>(path: P) -> Self {
84        Self(Download::new(path, VOICES_URL))
85    }
86}
87
88impl Default for VoicesData {
89    /// 创建默认的语音数据管理器
90    ///
91    /// 默认会将语音数据文件存储在用户主目录下的.novel-tts/kokoro目录中
92    ///
93    /// # 返回值
94    /// 返回一个配置了默认路径和URL的VoicesData实例
95    fn default() -> Self {
96        let cache_dir = get_cache_dir().unwrap().join("kokoro");
97        let path = cache_dir.join("voices-v1.1-zh.bin");
98        Self(Download::new(path, VOICES_URL))
99    }
100}
101
102impl Deref for VoicesData {
103    type Target = Download;
104
105    fn deref(&self) -> &Self::Target {
106        &self.0
107    }
108}
109
110impl DerefMut for VoicesData {
111    fn deref_mut(&mut self) -> &mut Self::Target {
112        &mut self.0
113    }
114}