@@ -15,7 +15,10 @@ pub fn generate_lrc(
1515 let mut lrc_content = String :: new ( ) ;
1616
1717 // 歌词文件最上方添加 LRC 元数据标签
18- let file_name = output_path. file_stem ( ) . unwrap_or_default ( ) . to_string_lossy ( ) ;
18+ let file_name = output_path
19+ . file_stem ( )
20+ . unwrap_or_default ( )
21+ . to_string_lossy ( ) ;
1922 lrc_content. push_str ( "[ar:Generated by BaiTTS CLI-rs]\n " ) ;
2023 lrc_content. push_str ( "[al:Audio]\n " ) ;
2124 lrc_content. push_str ( & format ! ( "[ti:{}]\n \n " , file_name) ) ;
@@ -53,7 +56,7 @@ pub fn generate_lrc(
5356 // 为每个小行生成带时间戳的歌词
5457 for ( j, chunk) in chunks. iter ( ) . enumerate ( ) {
5558 let chunk_start_time = line_start_time + chunk_duration * j as u32 ;
56-
59+
5760 let minutes = chunk_start_time. as_secs ( ) / 60 ;
5861 let seconds = chunk_start_time. as_secs ( ) % 60 ;
5962 let millis = chunk_start_time. subsec_millis ( ) / 10 ;
@@ -64,16 +67,16 @@ pub fn generate_lrc(
6467 }
6568
6669 let lrc_path = PathBuf :: from ( output_path) . with_extension ( "lrc" ) ;
67- fs:: write ( & lrc_path, lrc_content)
68- . context ( format ! ( "无法写入 LRC 文件到 {:?}" , lrc_path) ) ?;
69-
70+ fs:: write ( & lrc_path, lrc_content) . context ( format ! ( "无法写入 LRC 文件到 {:?}" , lrc_path) ) ?;
71+
7072 println ! ( "成功生成 LRC 文件: {:?}" , lrc_path) ;
7173 Ok ( ( ) )
7274}
7375
7476// 查找理想的分割点(各类标点符号或空格)。
7577fn is_break_character ( c : char ) -> bool {
76- matches ! ( c,
78+ matches ! (
79+ c,
7780 // 中文常用标点 (全角)
7881 ',' | '。' | '?' | '!' | ';' | ':' | '、' |
7982 // 英文常用标点 (半角)
@@ -98,7 +101,9 @@ fn split_line_intelligently(line: &str, max_chars: usize) -> Vec<String> {
98101 let mut current_pos = 0 ;
99102
100103 // 循环前,先跳过所有行首的无效字符
101- while current_pos < chars. len ( ) && ( chars[ current_pos] . is_whitespace ( ) || is_break_character ( chars[ current_pos] ) ) {
104+ while current_pos < chars. len ( )
105+ && ( chars[ current_pos] . is_whitespace ( ) || is_break_character ( chars[ current_pos] ) )
106+ {
102107 current_pos += 1 ;
103108 }
104109
@@ -107,47 +112,66 @@ fn split_line_intelligently(line: &str, max_chars: usize) -> Vec<String> {
107112 let chunk = chars[ current_pos..] . iter ( ) . collect :: < String > ( ) ;
108113 let trimmed_chunk = chunk. trim ( ) ;
109114 if !trimmed_chunk. is_empty ( ) {
110- chunks. push ( trimmed_chunk. to_string ( ) ) ;
115+ chunks. push ( trimmed_chunk. to_string ( ) ) ;
111116 }
112117 break ;
113118 }
114119
115120 let search_end = current_pos + max_chars;
116121 let search_range = & chars[ current_pos..search_end] ;
117122
118- if let Some ( split_pos_relative) = search_range. iter ( ) . rposition ( |& c| is_break_character ( c) ) {
123+ if let Some ( split_pos_relative) = search_range. iter ( ) . rposition ( |& c| is_break_character ( c) )
124+ {
119125 // 找到了理想的分割点(在max_chars范围内)
120126 let split_pos_absolute = current_pos + split_pos_relative;
121- let chunk = chars[ current_pos..=split_pos_absolute] . iter ( ) . collect :: < String > ( ) . trim ( ) . to_string ( ) ;
127+ let chunk = chars[ current_pos..=split_pos_absolute]
128+ . iter ( )
129+ . collect :: < String > ( )
130+ . trim ( )
131+ . to_string ( ) ;
122132 if !chunk. is_empty ( ) {
123133 chunks. push ( chunk) ;
124134 }
125-
135+
126136 let mut next_pos = split_pos_absolute + 1 ;
127- while next_pos < chars. len ( ) && ( chars[ next_pos] . is_whitespace ( ) || is_break_character ( chars[ next_pos] ) ) {
137+ while next_pos < chars. len ( )
138+ && ( chars[ next_pos] . is_whitespace ( ) || is_break_character ( chars[ next_pos] ) )
139+ {
128140 next_pos += 1 ;
129141 }
130142 current_pos = next_pos;
131-
132143 } else {
133144 // 在max_chars范围内未找到分割点,因此向前查找下一个分割点
134145 // 从 search_end 开始向字符串末尾搜索
135- if let Some ( next_break_relative) = chars[ search_end..] . iter ( ) . position ( |& c| is_break_character ( c) ) {
146+ if let Some ( next_break_relative) = chars[ search_end..]
147+ . iter ( )
148+ . position ( |& c| is_break_character ( c) )
149+ {
136150 // 找到了下一个分割点
137151 let split_pos_absolute = search_end + next_break_relative;
138- let chunk = chars[ current_pos..=split_pos_absolute] . iter ( ) . collect :: < String > ( ) . trim ( ) . to_string ( ) ;
152+ let chunk = chars[ current_pos..=split_pos_absolute]
153+ . iter ( )
154+ . collect :: < String > ( )
155+ . trim ( )
156+ . to_string ( ) ;
139157 if !chunk. is_empty ( ) {
140158 chunks. push ( chunk) ;
141159 }
142160
143161 let mut next_pos = split_pos_absolute + 1 ;
144- while next_pos < chars. len ( ) && ( chars[ next_pos] . is_whitespace ( ) || is_break_character ( chars[ next_pos] ) ) {
162+ while next_pos < chars. len ( )
163+ && ( chars[ next_pos] . is_whitespace ( ) || is_break_character ( chars[ next_pos] ) )
164+ {
145165 next_pos += 1 ;
146166 }
147167 current_pos = next_pos;
148168 } else {
149169 // 从 search_end 到字符串末尾都没有任何分割点,将剩余部分全部作为一块
150- let chunk = chars[ current_pos..] . iter ( ) . collect :: < String > ( ) . trim ( ) . to_string ( ) ;
170+ let chunk = chars[ current_pos..]
171+ . iter ( )
172+ . collect :: < String > ( )
173+ . trim ( )
174+ . to_string ( ) ;
151175 if !chunk. is_empty ( ) {
152176 chunks. push ( chunk) ;
153177 }
0 commit comments