ttml_processor 0.3.1

A Rust library for parsing and generating TTML lyrics files, specifically designed for Apple Music and AMLL formats.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use quick_xml::{
    Reader,
    events::{
        BytesStart,
        Event,
    },
};

use crate::{
    constants::{
        attrs,
        tags,
        vals,
    },
    error::{
        ParseErrorKind,
        Result,
        ResultExt as _,
        TTMLProcessorError,
    },
    model::{
        LyricLine,
        RubyTag,
        SubLyricContent,
        Syllable,
    },
    parser::{
        ext::{
            BytesStartExt as _,
            QNameExt as _,
            ReaderExt as _,
        },
        state::ParserContext,
        utils::{
            is_spacing_text,
            mark_last_syllable_space,
            parse_basic_syllable,
            read_text_content,
            resolve_xml_entity,
        },
    },
};

/// 解析主歌词中的一个 `<span>` 标签
///
/// ## 示例
/// 普通音节样式:
/// ```xml
/// <span begin="1:06.534" end="1:06.929">prophecies</span>
/// ```
///
/// 背景人声样式:
///
/// *添加了整个背景人声区域以便理解*
/// ```xml
/// <span ttm:role="x-bg" begin="21.890" end="24.080">
///     <span begin="21.890" end="22.080">(And </span>
///     <span begin="22.080" end="22.310">there's </span>
///     <span begin="22.310" end="22.410">a </span>
///     <span begin="22.410" end="22.530">lotta </span>
///     <span begin="22.530" end="22.820">cool </span>
///     <span begin="22.820" end="23.160">chicks </span>
///     <span begin="23.160" end="23.530">out </span>
///     <span begin="23.530" end="24.080">there)</span>
/// </span>
/// ```
///
/// 内嵌翻译/音译样式:
/// ```xml
/// <span ttm:role="x-translation" xml:lang="zh-CN">即便不计其数的女孩们对你趋之若鹜</span>
/// ```
pub fn process_span(
    reader: &mut Reader<&[u8]>,
    context: &mut ParserContext,
    span_event: &BytesStart,
    line: &mut LyricLine,
    is_bg_context: bool,
) -> Result<()> {
    let role = span_event.get_attr_value(attrs::TTM_ROLE, reader, context)?;
    let role_deref = role.as_deref();

    // 内嵌翻译 / 音译
    if role_deref == Some(vals::ROLE_TRANS) || role_deref == Some(vals::ROLE_ROM) {
        let lang = span_event.get_attr_value(attrs::XML_LANG, reader, context)?;
        let text = read_text_content(reader, context, tags::SPAN)?;
        let trimmed_text = text.trim().to_string();

        if !trimmed_text.is_empty() {
            let sub_lyric = SubLyricContent {
                language: lang,
                text: trimmed_text,
                words: None, // 内嵌翻译/音译始终只有逐行的
            };

            if is_bg_context {
                let bg = line.bg_vocal_mut();

                if role_deref == Some(vals::ROLE_TRANS) {
                    bg.push_translation(sub_lyric);
                } else {
                    bg.push_romanization(sub_lyric);
                }
            } else if role_deref == Some(vals::ROLE_TRANS) {
                line.push_translation(sub_lyric);
            } else {
                line.push_romanization(sub_lyric);
            }
        }
        return Ok(());
    }

    // 背景人声
    let is_bg = is_bg_context || role_deref == Some(vals::ROLE_BG);
    let is_ruby_container = span_event
        .get_attr_value(attrs::TTS_RUBY, reader, context)?
        .as_deref()
        == Some(vals::RUBY_CONTAINER);

    if is_bg && !is_bg_context {
        let explicit_bg_start = span_event.get_timestamp_attr(attrs::BEGIN, reader, context)?;
        let explicit_bg_end = span_event.get_timestamp_attr(attrs::END, reader, context)?;

        let mut raw_bg_text = String::new();

        let mut buf = Vec::new();
        loop {
            match reader.read_event_with_context(&mut buf, context)? {
                Event::Start(ref e) => {
                    let qname = e.name();
                    let tag_name = std::str::from_utf8(qname.as_ref())
                        .map_err(TTMLProcessorError::Utf8Error)?;
                    context.tag_stack.push(tag_name.to_string());

                    if qname.is(tags::SPAN) {
                        process_span(reader, context, e, line, true)?;
                        context.tag_stack.pop();
                    }
                }
                Event::Text(e) => {
                    let text =
                        std::str::from_utf8(e.as_ref()).map_err(TTMLProcessorError::Utf8Error)?;
                    if is_spacing_text(text) {
                        mark_last_syllable_space(line, is_bg);
                    }
                    raw_bg_text.push_str(text);
                }
                Event::GeneralRef(reference) => {
                    raw_bg_text
                        .push_str(&resolve_xml_entity(&reference).with_context(reader, context)?);
                }
                Event::End(ref e) => {
                    if e.name().is(tags::SPAN) {
                        break;
                    }
                    context.tag_stack.pop();
                }
                Event::Eof => {
                    return Err(ParseErrorKind::UnexpectedEof).with_context(reader, context);
                }
                _ => (),
            }
            buf.clear();
        }

        let bg = line.bg_vocal_mut();

        bg.start_time = explicit_bg_start.unwrap_or_else(|| {
            bg.words
                .as_ref()
                .and_then(|w| w.first())
                .map_or(0, |w| w.start_time)
        });

        bg.end_time = explicit_bg_end.unwrap_or_else(|| {
            bg.words
                .as_ref()
                .and_then(|w| w.last())
                .map_or(0, |w| w.end_time)
        });

        let is_words_empty = bg.words.as_ref().is_none_or(Vec::is_empty);
        if is_words_empty {
            bg.words = None;
            bg.text = raw_bg_text;
        }

        return Ok(());
    }

    // ruby 容器
    if is_ruby_container {
        return process_ruby_container(reader, context, span_event, line, is_bg);
    }

    // 普通音节
    let syllable = parse_basic_syllable(reader, context, span_event)?;

    if is_bg {
        line.bg_vocal_mut().push_word(syllable);
    } else {
        line.push_word(syllable);
    }

    Ok(())
}

/// 解析一个 ruby 容器
///
/// ## 示例
/// ```xml
/// <span tts:ruby="container">
///     <span tts:ruby="base">私</span>
///     <span tts:ruby="textContainer">
///         <span tts:ruby="text" begin="5:08.760" end="5:09.040">わ</span>
///         <span tts:ruby="text" begin="5:09.120" end="5:09.480">た</span>
///         <span tts:ruby="text" begin="5:09.480" end="5:09.950">し</span>
///     </span>
/// </span>
/// ```
fn process_ruby_container(
    reader: &mut Reader<&[u8]>,
    context: &mut ParserContext,
    container_event: &BytesStart,
    line: &mut LyricLine,
    is_bg: bool,
) -> Result<()> {
    let mut base_text = String::new();
    let mut ruby_tags = Vec::new();

    // 目前不雅用于和空拍属性暂未明确是添加在何处
    // 暂时直接从容器上提取
    let obscene = container_event
        .get_attr_value(attrs::AMLL_OBSCENE, reader, context)?
        .map(|v| v == vals::TRUE_STR);
    let empty_beat = container_event
        .get_attr_value(attrs::AMLL_EMPTY_BEAT, reader, context)?
        .and_then(|v| v.parse::<u32>().ok());

    let mut buf = Vec::new();
    loop {
        match reader.read_event_with_context(&mut buf, context)? {
            Event::Start(ref e) => {
                let qname = e.name();
                let tag_name =
                    std::str::from_utf8(qname.as_ref()).map_err(TTMLProcessorError::Utf8Error)?;
                context.tag_stack.push(tag_name.to_string());

                if qname.is(tags::SPAN) {
                    let ruby_attr = e.get_attr_value(attrs::TTS_RUBY, reader, context)?;
                    match ruby_attr.as_deref() {
                        Some(vals::RUBY_BASE) => {
                            base_text = read_text_content(reader, context, tags::SPAN)?;
                            context.tag_stack.pop();
                        }
                        Some(vals::RUBY_TEXT_CONTAINER) => {
                            let mut inner_buf = Vec::new();
                            loop {
                                match reader.read_event_with_context(&mut inner_buf, context)? {
                                    Event::Start(ref inner_e) => {
                                        let inner_qname = inner_e.name();
                                        let inner_tag = std::str::from_utf8(inner_qname.as_ref())
                                            .map_err(TTMLProcessorError::Utf8Error)?;
                                        context.tag_stack.push(inner_tag.to_string());

                                        if inner_qname.is(tags::SPAN)
                                            && inner_e
                                                .get_attr_value(attrs::TTS_RUBY, reader, context)?
                                                .as_deref()
                                                == Some(vals::RUBY_TEXT)
                                        {
                                            let r_start = inner_e.get_required_timestamp_attr(
                                                attrs::BEGIN,
                                                reader,
                                                context,
                                            )?;
                                            let r_end = inner_e.get_required_timestamp_attr(
                                                attrs::END,
                                                reader,
                                                context,
                                            )?;
                                            let r_text =
                                                read_text_content(reader, context, tags::SPAN)?;

                                            ruby_tags.push(RubyTag {
                                                text: r_text,
                                                start_time: r_start,
                                                end_time: r_end,
                                            });
                                            context.tag_stack.pop();
                                        }
                                    }
                                    Event::End(ref inner_e) => {
                                        if inner_e.name().is(tags::SPAN) {
                                            break;
                                        }
                                        context.tag_stack.pop();
                                    }
                                    Event::Eof => {
                                        return Err(ParseErrorKind::UnexpectedEof)
                                            .with_context(reader, context);
                                    }
                                    _ => (),
                                }
                                inner_buf.clear();
                            }
                            context.tag_stack.pop(); // RUBY_TEXT_CONTAINER
                        }
                        _ => {
                            let _ = read_text_content(reader, context, tags::SPAN)?;
                            context.tag_stack.pop();
                        }
                    }
                }
            }
            Event::End(ref e) => {
                if e.name().is(tags::SPAN) {
                    break;
                }
                context.tag_stack.pop();
            }
            Event::Eof => return Err(ParseErrorKind::UnexpectedEof).with_context(reader, context),
            _ => (),
        }
        buf.clear();
    }

    let start_time = ruby_tags.first().map_or(0, |r| r.start_time);
    let end_time = ruby_tags.last().map_or(0, |r| r.end_time);
    let syllable = Syllable {
        text: base_text,
        start_time,
        end_time,
        ruby: Some(ruby_tags),
        obscene,
        empty_beat,
        ..Default::default()
    };

    if is_bg {
        line.bg_vocal_mut().push_word(syllable);
    } else {
        line.push_word(syllable);
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use quick_xml::{
        Reader,
        events::Event,
    };

    use super::*;

    fn get_start_event(reader: &mut Reader<&[u8]>) -> BytesStart<'static> {
        let mut buf = Vec::new();
        loop {
            match reader
                .read_event_into(&mut buf)
                .expect("Failed to read XML")
            {
                Event::Start(e) => return e.into_owned(),
                Event::Eof => panic!("Reached EOF before finding start tag"),
                _ => (),
            }
        }
    }

    #[test]
    fn test_process_span_basic_syllable() {
        let xml = r#"<span begin="1:06.534" end="1:06.929">prophecies</span>"#;
        let mut reader = Reader::from_str(xml);
        let mut context = ParserContext::default();

        let start_event = get_start_event(&mut reader);
        let mut line = LyricLine::default();

        process_span(&mut reader, &mut context, &start_event, &mut line, false)
            .expect("Failed to process basic span");

        let words = line.words.as_ref().unwrap();
        assert_eq!(words.len(), 1);
        assert_eq!(words[0].text, "prophecies");

        assert_eq!(words[0].start_time, 66534);
        assert_eq!(words[0].end_time, 66929);
    }

    #[test]
    fn test_process_span_inline_translation() {
        let xml = r#"<span ttm:role="x-translation" xml:lang="zh-CN">即便不计其数的女孩们对你趋之若鹜</span>"#;
        let mut reader = Reader::from_str(xml);
        let mut context = ParserContext::default();

        let start_event = get_start_event(&mut reader);
        let mut line = LyricLine::default();

        process_span(&mut reader, &mut context, &start_event, &mut line, false)
            .expect("Failed to process translation span");

        assert!(line.words.is_none());

        let translations = line.translations.as_ref().unwrap();
        assert_eq!(translations.len(), 1);
        assert_eq!(translations[0].language.as_deref(), Some("zh-CN"));
        assert_eq!(translations[0].text, "即便不计其数的女孩们对你趋之若鹜");
    }

    #[test]
    fn test_process_span_background_vocal() {
        let xml = r#"
        <span ttm:role="x-bg" begin="21.890" end="24.080">
            <span begin="21.890" end="22.080">(And</span> <span begin="22.080" end="24.080">there's)</span>
        </span>"#;

        let mut reader = Reader::from_str(xml);
        let mut context = ParserContext::default();

        let start_event = get_start_event(&mut reader);
        let mut line = LyricLine::default();

        process_span(&mut reader, &mut context, &start_event, &mut line, false)
            .expect("Failed to process background vocal span");

        assert!(line.words.is_none());

        let bg = line.background_vocal.as_ref().unwrap();
        assert_eq!(bg.start_time, 21890);
        assert_eq!(bg.end_time, 24080);

        let bg_words = bg.words.as_ref().unwrap();
        assert_eq!(bg_words.len(), 2);
        assert_eq!(bg_words[0].text, "(And");
        assert_eq!(bg_words[0].ends_with_space, Some(true));
        assert_eq!(bg_words[1].text, "there's)");
    }

    #[test]
    fn test_process_ruby_container() {
        let xml = r#"
        <span tts:ruby="container" amll:obscene="false">
            <span tts:ruby="base">私</span>
            <span tts:ruby="textContainer">
                <span tts:ruby="text" begin="5:08.760" end="5:09.040">わ</span>
                <span tts:ruby="text" begin="5:09.120" end="5:09.480">た</span>
                <span tts:ruby="text" begin="5:09.480" end="5:09.950">し</span>
            </span>
        </span>"#;

        let mut reader = Reader::from_str(xml);
        let mut context = ParserContext::default();

        let start_event = get_start_event(&mut reader);
        let mut line = LyricLine::default();

        process_span(&mut reader, &mut context, &start_event, &mut line, false)
            .expect("Failed to process ruby container span");

        let words = line.words.as_ref().unwrap();
        assert_eq!(words.len(), 1);

        let word = &words[0];
        assert_eq!(word.text, "私");
        assert_eq!(word.obscene, Some(false));

        assert_eq!(word.start_time, 308_760);
        assert_eq!(word.end_time, 309_950);

        let rubies = word.ruby.as_ref().unwrap();
        assert_eq!(rubies.len(), 3);
        assert_eq!(rubies[0].text, "わ");
        assert_eq!(rubies[0].start_time, 308_760);
        assert_eq!(rubies[2].text, "し");
        assert_eq!(rubies[2].end_time, 309_950);
    }

    #[test]
    fn test_process_span_amll_attributes() {
        let xml = r#"<span begin="1:00" end="1:02" amll:obscene="true" amll:empty-beat="2">badword</span>"#;
        let mut reader = Reader::from_str(xml);
        let mut context = ParserContext::default();

        let start_event = get_start_event(&mut reader);
        let mut line = LyricLine::default();

        process_span(&mut reader, &mut context, &start_event, &mut line, false)
            .expect("Failed to process amll attributes span");

        let words = line.words.as_ref().unwrap();
        assert_eq!(words.len(), 1);

        let word = &words[0];
        assert_eq!(word.text, "badword");

        assert_eq!(word.obscene, Some(true));
        assert_eq!(word.empty_beat, Some(2));
    }

    #[test]
    fn test_process_ruby_container_amll_attributes() {
        let xml = r#"
        <span tts:ruby="container" amll:obscene="true" amll:empty-beat="2">
            <span tts:ruby="base">XX</span>
            <span tts:ruby="textContainer">
                <span tts:ruby="text" begin="0.000" end="1.000">x</span>
                <span tts:ruby="text" begin="1.000" end="2.000">x</span>
            </span>
        </span>"#;

        let mut reader = Reader::from_str(xml);
        let mut context = ParserContext::default();

        let start_event = get_start_event(&mut reader);
        let mut line = LyricLine::default();

        process_span(&mut reader, &mut context, &start_event, &mut line, false)
            .expect("Failed to process ruby container with amll attributes");

        let words = line.words.as_ref().unwrap();
        assert_eq!(words.len(), 1);

        let word = &words[0];
        assert_eq!(word.text, "XX");

        assert_eq!(word.obscene, Some(true));
        assert_eq!(word.empty_beat, Some(2));
    }
}