novel_tts/lib.rs
1//! novel-tts 是一个专门为小说阅读设计的文本转语音(TTS)库。
2//! 它基于 [kokoro-tts](https://github.com/mzdk100/kokoro) 引擎,
3//! 提供了针对长文本(如小说章节)优化的流式处理功能。
4//!
5//! ## 功能特性
6//!
7//! - 📚 专为小说阅读优化的TTS解决方案
8//! - 🌊 流式音频处理,支持边生成边播放
9//! - 🎵 支持多种语音选择
10//! - 🔁 实时字符位置追踪,便于同步文本高亮
11//! - ⏹️ 支持播放控制(暂停、取消)
12//! - 📦 自动下载和管理TTS模型文件
13//! - 🧵 异步API设计,适用于现代Rust应用
14//!
15//! ## 快速开始
16//!
17//! ```rust
18//! use novel_tts::{NovelTTS, CheckpointModel, VoicesData, ChapterTTS};
19//! use kokoro_tts::Voice;
20//!
21//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
22//! // 初始化模型和语音数据
23//! let model = CheckpointModel::default();
24//! let voices = VoicesData::default();
25//!
26//! // 检查并下载必要的模型文件
27//! if !model.is_downloaded() {
28//! model.async_download(|downloaded, total| {
29//! println!("模型下载进度: {}/{}", downloaded, total);
30//! }).await?;
31//! }
32//!
33//! if !voices.is_downloaded() {
34//! voices.async_download(|downloaded, total| {
35//! println!("语音数据下载进度: {}/{}", downloaded, total);
36//! }).await?;
37//! }
38//!
39//! // 创建TTS实例
40//! let novel_tts = NovelTTS::new(&model, &voices).await?;
41//! let mut chapter_tts = novel_tts.chapter_tts();
42//!
43//! // 准备要转换的文本
44//! let text = "这是小说的第一段落。\n这是第二段落。".to_string();
45//!
46//! // 流式处理文本到音频
47//! let (audio_queue, mut position_rx) = chapter_tts.stream(
48//! text,
49//! Voice::Zf006(1),
50//! |error| eprintln!("TTS处理错误: {:?}", error)
51//! )?;
52//!
53//! // 监听字符位置更新
54//! tokio::spawn(async move {
55//! while let Some((start, end)) = position_rx.recv().await {
56//! println!("正在朗读字符位置: {} 到 {}", start, end);
57//! }
58//! });
59//! # Ok(())
60//! # }
61//! ```
62
63mod chapter;
64pub mod download;
65mod error;
66mod model;
67mod player;
68pub mod utils;
69
70// 重新导出公共类型
71pub use chapter::*;
72pub use error::*;
73pub use kokoro_tts;
74use kokoro_tts::KokoroTts;
75pub use model::*;
76pub use player::*;
77pub mod queue;
78use rodio::{OutputStream, Source};
79use std::sync::Arc;
80
81/// NovelTTS主结构体
82///
83/// 负责管理TTS引擎实例,是整个TTS功能的核心入口点。
84pub struct NovelTTS {
85 pub tts: Arc<KokoroTts>,
86 pub output_stream: OutputStream,
87}
88
89impl NovelTTS {
90 /// 创建新的NovelTTS实例
91 ///
92 /// # 参数
93 /// * `model` - TTS模型文件信息
94 /// * `voices` - 语音数据文件信息
95 ///
96 /// # 返回值
97 /// 返回Result包装的NovelTTS实例,如果模型或语音数据加载失败则返回错误
98 pub async fn new(model: &CheckpointModel, voices: &VoicesData) -> Result<Self> {
99 let tts = KokoroTts::new(&model.path, &voices.path).await?;
100 let output_stream = rodio::OutputStreamBuilder::open_default_stream()?;
101
102 Ok(Self {
103 tts: Arc::new(tts),
104 output_stream,
105 })
106 }
107
108 /// 创建章节TTS处理器
109 ///
110 /// # 返回值
111 /// 返回一个新的ChapterTTS实例,用于处理特定章节的文本转语音
112 pub fn chapter_tts(&self, text: &str) -> ChapterTTS {
113 ChapterTTS::new(self.tts.clone(), text)
114 }
115
116 /// 创建一个新的音频播放器实例
117 ///
118 /// 该函数接收一个音频源队列输出,并使用内部的音频输出流混音器创建一个播放器
119 ///
120 /// # 参数
121 /// * `output` - 音频源队列输出,包含待播放的音频数据
122 ///
123 /// # 返回值
124 /// 返回一个新的Player实例,可用于控制音频播放
125 pub fn player<T: Source + Send + 'static>(&self, output: T) -> Player {
126 Player::new(output, self.output_stream.mixer())
127 }
128}
129
130unsafe impl Send for NovelTTS {}
131unsafe impl Sync for NovelTTS {}