whisper-apr 0.3.3

WASM-first automatic speech recognition engine implementing OpenAI Whisper
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Output formatters for whisper-apr CLI
#![allow(clippy::expect_used, clippy::single_char_pattern)]
//!
//! Supports multiple output formats: txt, srt, vtt, json, csv, md

use std::fmt::Write;

use crate::TranscriptionResult;

/// Output format enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputFormat {
    /// Plain text
    Txt,
    /// SRT subtitles
    Srt,
    /// WebVTT subtitles
    Vtt,
    /// JSON format
    Json,
    /// Extended JSON with token-level details
    JsonFull,
    /// CSV format
    Csv,
    /// LRC lyrics format (whisper.cpp: -olrc)
    Lrc,
    /// Karaoke script with word timestamps (whisper.cpp: -owts)
    Wts,
    /// Markdown format
    Md,
}

impl OutputFormat {
    /// Get file extension for this format
    #[must_use]
    pub const fn extension(&self) -> &'static str {
        match self {
            Self::Txt => "txt",
            Self::Srt => "srt",
            Self::Vtt => "vtt",
            Self::Json | Self::JsonFull => "json",
            Self::Csv => "csv",
            Self::Lrc => "lrc",
            Self::Wts => "wts",
            Self::Md => "md",
        }
    }
}

/// Format transcription result according to output format
///
/// # Arguments
///
/// * `result` - The transcription result to format
/// * `format` - The desired output format
///
/// # Returns
///
/// Formatted string representation
pub fn format_output(result: &TranscriptionResult, format: OutputFormat) -> String {
    match format {
        OutputFormat::Txt => format_txt(result),
        OutputFormat::Srt => format_srt(result),
        OutputFormat::Vtt => format_vtt(result),
        OutputFormat::Json => format_json(result),
        OutputFormat::JsonFull => format_json_full(result),
        OutputFormat::Csv => format_csv(result),
        OutputFormat::Lrc => format_lrc(result),
        OutputFormat::Wts => format_wts(result),
        OutputFormat::Md => format_md(result),
    }
}

/// Format as plain text
#[must_use]
pub fn format_txt(result: &TranscriptionResult) -> String {
    result.text.clone()
}

/// Format as SRT subtitles
///
/// SRT format:
/// ```text
/// 1
/// 00:00:00,000 --> 00:00:01,500
/// Hello world
///
/// 2
/// 00:00:01,500 --> 00:00:03,000
/// This is a test
/// ```
#[must_use]
pub fn format_srt(result: &TranscriptionResult) -> String {
    let mut output = String::new();

    for (i, segment) in result.segments.iter().enumerate() {
        // Sequence number
        writeln!(output, "{}", i + 1).ok();

        // Timestamps
        writeln!(
            output,
            "{} --> {}",
            format_timestamp_srt(segment.start),
            format_timestamp_srt(segment.end)
        )
        .ok();

        // Text
        writeln!(output, "{}", segment.text.trim()).ok();

        // Blank line between entries
        writeln!(output).ok();
    }

    output
}

/// Format timestamp for SRT (HH:MM:SS,mmm)
#[must_use]
pub fn format_timestamp_srt(seconds: f32) -> String {
    let total_ms = (seconds * 1000.0) as u64;
    let ms = total_ms % 1000;
    let total_secs = total_ms / 1000;
    let secs = total_secs % 60;
    let total_mins = total_secs / 60;
    let mins = total_mins % 60;
    let hours = total_mins / 60;

    format!("{hours:02}:{mins:02}:{secs:02},{ms:03}")
}

/// Format as WebVTT subtitles
///
/// VTT format:
/// ```text
/// WEBVTT
///
/// 00:00:00.000 --> 00:00:01.500
/// Hello world
///
/// 00:00:01.500 --> 00:00:03.000
/// This is a test
/// ```
#[must_use]
pub fn format_vtt(result: &TranscriptionResult) -> String {
    let mut output = String::from("WEBVTT\n\n");

    for segment in &result.segments {
        // Timestamps (VTT uses . instead of ,)
        writeln!(
            output,
            "{} --> {}",
            format_timestamp_vtt(segment.start),
            format_timestamp_vtt(segment.end)
        )
        .ok();

        // Text
        writeln!(output, "{}", segment.text.trim()).ok();

        // Blank line between entries
        writeln!(output).ok();
    }

    output
}

/// Format timestamp for VTT (HH:MM:SS.mmm)
#[must_use]
pub fn format_timestamp_vtt(seconds: f32) -> String {
    let total_ms = (seconds * 1000.0) as u64;
    let ms = total_ms % 1000;
    let total_secs = total_ms / 1000;
    let secs = total_secs % 60;
    let total_mins = total_secs / 60;
    let mins = total_mins % 60;
    let hours = total_mins / 60;

    format!("{hours:02}:{mins:02}:{secs:02}.{ms:03}")
}

/// Format as JSON
#[must_use]
pub fn format_json(result: &TranscriptionResult) -> String {
    serde_json::to_string_pretty(result).unwrap_or_else(|_| "{}".to_string())
}

/// Format as CSV
///
/// CSV format:
/// ```text
/// start,end,text
/// 0.000,1.500,"Hello world"
/// 1.500,3.000,"This is a test"
/// ```
#[must_use]
pub fn format_csv(result: &TranscriptionResult) -> String {
    let mut output = String::from("start,end,text\n");

    for segment in &result.segments {
        writeln!(
            output,
            "{:.3},{:.3},\"{}\"",
            segment.start,
            segment.end,
            escape_csv(&segment.text)
        )
        .ok();
    }

    output
}

/// Escape string for CSV (double quotes become double-double quotes)
fn escape_csv(s: &str) -> String {
    s.replace('"', "\"\"")
}

/// Format as Markdown
///
/// Markdown format:
/// ```text
/// # Transcription
///
/// **Language:** en
/// **Duration:** 3.00s
///
/// ## Segments
///
/// | Start | End | Text |
/// |-------|-----|------|
/// | 0:00 | 0:01 | Hello world |
/// | 0:01 | 0:03 | This is a test |
/// ```
#[must_use]
pub fn format_md(result: &TranscriptionResult) -> String {
    let mut output = String::from("# Transcription\n\n");

    // Metadata
    writeln!(output, "**Language:** {}", result.language).ok();
    writeln!(output).ok();

    // Full text
    writeln!(output, "## Full Text\n").ok();
    writeln!(output, "{}\n", result.text).ok();

    // Segments table (if any)
    if !result.segments.is_empty() {
        writeln!(output, "## Segments\n").ok();
        writeln!(output, "| Start | End | Text |").ok();
        writeln!(output, "|-------|-----|------|").ok();

        for segment in &result.segments {
            writeln!(
                output,
                "| {} | {} | {} |",
                format_timestamp_short(segment.start),
                format_timestamp_short(segment.end),
                segment.text.trim()
            )
            .ok();
        }
    }

    output
}

/// Format as extended JSON with token-level details
///
/// JSON-full format includes:
/// - Token-level timing
/// - Word-level timestamps
/// - Per-token probabilities
#[must_use]
pub fn format_json_full(result: &TranscriptionResult) -> String {
    // For now, same as regular JSON - can be extended with token details
    serde_json::to_string_pretty(result).unwrap_or_else(|_| "{}".to_string())
}

/// Format as LRC lyrics (§7.7)
///
/// LRC format:
/// ```text
/// [00:00.00]Hello, world.
/// [00:05.12]This is a test.
/// ```
#[must_use]
pub fn format_lrc(result: &TranscriptionResult) -> String {
    let mut output = String::new();

    for segment in &result.segments {
        writeln!(
            output,
            "[{}]{}",
            format_timestamp_lrc(segment.start),
            segment.text.trim()
        )
        .ok();
    }

    output
}

/// Format timestamp for LRC (MM:SS.cc - centiseconds)
#[must_use]
pub fn format_timestamp_lrc(seconds: f32) -> String {
    let total_cs = (seconds * 100.0) as u64;
    let cs = total_cs % 100;
    let total_secs = total_cs / 100;
    let secs = total_secs % 60;
    let mins = total_secs / 60;

    format!("{mins:02}:{secs:02}.{cs:02}")
}

/// Format as karaoke word timestamps script (WTS)
///
/// WTS format shows word-level timing for karaoke display.
/// Note: Word-level timestamps require word_timestamps option during transcription.
/// Without word-level data, falls back to segment-level timing.
#[must_use]
pub fn format_wts(result: &TranscriptionResult) -> String {
    let mut output = String::new();

    for segment in &result.segments {
        // For now, use segment-level timing
        // Word-level timestamps would require extending the Segment struct
        writeln!(
            output,
            "{} --> {} | {}",
            format_timestamp_vtt(segment.start),
            format_timestamp_vtt(segment.end),
            segment.text.trim()
        )
        .ok();
    }

    output
}

/// Format timestamp in short form (M:SS or H:MM:SS)
fn format_timestamp_short(seconds: f32) -> String {
    let total_secs = seconds as u64;
    let secs = total_secs % 60;
    let total_mins = total_secs / 60;
    let mins = total_mins % 60;
    let hours = total_mins / 60;

    if hours > 0 {
        format!("{hours}:{mins:02}:{secs:02}")
    } else {
        format!("{mins}:{secs:02}")
    }
}

// ============================================================================
// Unit Tests (EXTREME TDD - RED phase)
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Segment;

    // Helper to create test result
    fn test_result() -> TranscriptionResult {
        TranscriptionResult {
            text: "Hello world. This is a test.".to_string(),
            language: "en".to_string(),
            segments: vec![
                Segment {
                    start: 0.0,
                    end: 1.5,
                    text: "Hello world.".to_string(),
                    ..Default::default()
                },
                Segment {
                    start: 1.5,
                    end: 3.0,
                    text: "This is a test.".to_string(),
                    ..Default::default()
                },
            ],
            ..Default::default()
        }
    }

    fn empty_result() -> TranscriptionResult {
        TranscriptionResult::default()
    }

    // -------------------------------------------------------------------------
    // OutputFormat tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_output_format_extension() {
        assert_eq!(OutputFormat::Txt.extension(), "txt");
        assert_eq!(OutputFormat::Srt.extension(), "srt");
        assert_eq!(OutputFormat::Vtt.extension(), "vtt");
        assert_eq!(OutputFormat::Json.extension(), "json");
        assert_eq!(OutputFormat::Csv.extension(), "csv");
        assert_eq!(OutputFormat::Md.extension(), "md");
    }

    // -------------------------------------------------------------------------
    // Plain text tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_format_txt_returns_text() {
        let result = test_result();
        let output = format_txt(&result);
        assert_eq!(output, "Hello world. This is a test.");
    }

    #[test]
    fn test_format_txt_empty() {
        let result = empty_result();
        let output = format_txt(&result);
        assert_eq!(output, "");
    }

    // -------------------------------------------------------------------------
    // SRT format tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_format_srt_structure() {
        let result = test_result();
        let srt = format_srt(&result);

        // Should have sequence numbers
        assert!(srt.contains("1\n"));
        assert!(srt.contains("2\n"));

        // Should have timestamps
        assert!(srt.contains("00:00:00,000 --> 00:00:01,500"));
        assert!(srt.contains("00:00:01,500 --> 00:00:03,000"));

        // Should have text
        assert!(srt.contains("Hello world."));
        assert!(srt.contains("This is a test."));
    }

    #[test]
    fn test_format_srt_empty() {
        let result = empty_result();
        let srt = format_srt(&result);
        assert_eq!(srt, "");
    }

    #[test]
    fn test_format_timestamp_srt_zero() {
        assert_eq!(format_timestamp_srt(0.0), "00:00:00,000");
    }

    #[test]
    fn test_format_timestamp_srt_seconds() {
        assert_eq!(format_timestamp_srt(1.5), "00:00:01,500");
        assert_eq!(format_timestamp_srt(59.999), "00:00:59,999");
    }

    #[test]
    fn test_format_timestamp_srt_minutes() {
        assert_eq!(format_timestamp_srt(60.0), "00:01:00,000");
        assert_eq!(format_timestamp_srt(90.5), "00:01:30,500");
    }

    #[test]
    fn test_format_timestamp_srt_hours() {
        assert_eq!(format_timestamp_srt(3600.0), "01:00:00,000");
        assert_eq!(format_timestamp_srt(3661.5), "01:01:01,500");
    }

    // -------------------------------------------------------------------------
    // VTT format tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_format_vtt_header() {
        let result = test_result();
        let vtt = format_vtt(&result);
        assert!(vtt.starts_with("WEBVTT\n\n"));
    }

    #[test]
    fn test_format_vtt_timestamps() {
        let result = test_result();
        let vtt = format_vtt(&result);

        // VTT uses . instead of ,
        assert!(vtt.contains("00:00:00.000 --> 00:00:01.500"));
        assert!(vtt.contains("00:00:01.500 --> 00:00:03.000"));
    }

    #[test]
    fn test_format_vtt_empty() {
        let result = empty_result();
        let vtt = format_vtt(&result);
        assert_eq!(vtt, "WEBVTT\n\n");
    }

    #[test]
    fn test_format_timestamp_vtt_uses_dot() {
        assert_eq!(format_timestamp_vtt(1.5), "00:00:01.500");
    }

    // -------------------------------------------------------------------------
    // JSON format tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_format_json_valid() {
        let result = test_result();
        let json = format_json(&result);

        // Should be valid JSON
        let parsed: serde_json::Value = serde_json::from_str(&json).expect("Should be valid JSON");
        assert!(parsed.is_object());
    }

    #[test]
    fn test_format_json_contains_fields() {
        let result = test_result();
        let json = format_json(&result);

        assert!(json.contains("\"text\""));
        assert!(json.contains("\"language\""));
        assert!(json.contains("\"segments\""));
    }

    #[test]
    fn test_format_json_empty() {
        let result = empty_result();
        let json = format_json(&result);
        let parsed: serde_json::Value = serde_json::from_str(&json).expect("Should be valid JSON");
        assert!(parsed.is_object());
    }

    // -------------------------------------------------------------------------
    // CSV format tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_format_csv_header() {
        let result = test_result();
        let csv = format_csv(&result);
        assert!(csv.starts_with("start,end,text\n"));
    }

    #[test]
    fn test_format_csv_rows() {
        let result = test_result();
        let csv = format_csv(&result);

        assert!(csv.contains("0.000,1.500,\"Hello world.\""));
        assert!(csv.contains("1.500,3.000,\"This is a test.\""));
    }

    #[test]
    fn test_format_csv_escapes_quotes() {
        let result = TranscriptionResult {
            text: "He said \"hello\"".to_string(),
            segments: vec![Segment {
                start: 0.0,
                end: 1.0,
                text: "He said \"hello\"".to_string(),
                ..Default::default()
            }],
            ..Default::default()
        };
        let csv = format_csv(&result);

        // Quotes should be doubled
        assert!(csv.contains("\"\"hello\"\""));
    }

    #[test]
    fn test_format_csv_empty() {
        let result = empty_result();
        let csv = format_csv(&result);
        assert_eq!(csv, "start,end,text\n");
    }

    #[test]
    fn test_escape_csv() {
        assert_eq!(escape_csv("hello"), "hello");
        assert_eq!(escape_csv("say \"hi\""), "say \"\"hi\"\"");
    }

    // -------------------------------------------------------------------------
    // Markdown format tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_format_md_header() {
        let result = test_result();
        let md = format_md(&result);
        assert!(md.starts_with("# Transcription\n"));
    }

    #[test]
    fn test_format_md_metadata() {
        let result = test_result();
        let md = format_md(&result);

        assert!(md.contains("**Language:** en"));
    }

    #[test]
    fn test_format_md_full_text() {
        let result = test_result();
        let md = format_md(&result);

        assert!(md.contains("## Full Text"));
        assert!(md.contains("Hello world. This is a test."));
    }

    #[test]
    fn test_format_md_segments_table() {
        let result = test_result();
        let md = format_md(&result);

        assert!(md.contains("## Segments"));
        assert!(md.contains("| Start | End | Text |"));
        assert!(md.contains("|-------|-----|------|"));
    }

    #[test]
    fn test_format_md_empty_no_segments_table() {
        let result = empty_result();
        let md = format_md(&result);

        // Should not have segments table if no segments
        assert!(!md.contains("## Segments"));
    }

    #[test]
    fn test_format_timestamp_short_minutes() {
        assert_eq!(format_timestamp_short(0.0), "0:00");
        assert_eq!(format_timestamp_short(30.0), "0:30");
        assert_eq!(format_timestamp_short(90.0), "1:30");
    }

    #[test]
    fn test_format_timestamp_short_hours() {
        assert_eq!(format_timestamp_short(3600.0), "1:00:00");
        assert_eq!(format_timestamp_short(3661.0), "1:01:01");
    }

    // -------------------------------------------------------------------------
    // format_output dispatch tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_format_output_dispatches_correctly() {
        let result = test_result();

        // Each format should produce different output
        let txt = format_output(&result, OutputFormat::Txt);
        let srt = format_output(&result, OutputFormat::Srt);
        let vtt = format_output(&result, OutputFormat::Vtt);
        let json = format_output(&result, OutputFormat::Json);
        let csv = format_output(&result, OutputFormat::Csv);
        let md = format_output(&result, OutputFormat::Md);

        // TXT is just the text
        assert_eq!(txt, result.text);

        // SRT has sequence numbers
        assert!(srt.contains("1\n"));

        // VTT starts with WEBVTT
        assert!(vtt.starts_with("WEBVTT"));

        // JSON has braces
        assert!(json.contains("{"));

        // CSV has header
        assert!(csv.starts_with("start,end,text"));

        // MD has markdown header
        assert!(md.starts_with("# Transcription"));
    }
}