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
use crate::audio::load_audio;
use crate::decoder::TranscriptionResult;
use crate::error::Result;
use crate::timestamps::TimestampMode;
use std::path::Path;
/// Trait for common transcription functionality
pub trait Transcriber {
/// Transcribe audio samples.
///
/// # Arguments
///
/// * `audio` - Audio samples as f32 values
/// * `sample_rate` - Sample rate in Hz
/// * `channels` - Number of audio channels
/// * `mode` - Optional timestamp output mode (Tokens, Words, or Sentences)
///
/// # Returns
///
/// A `TranscriptionResult` containing the transcribed text and timestamps at the requested level.
fn transcribe_samples(
&mut self,
audio: Vec<f32>,
sample_rate: u32,
channels: u16,
mode: Option<TimestampMode>,
) -> Result<TranscriptionResult>;
/// Transcribe an audio file with timestamps
///
/// # Arguments
///
/// * `audio_path` - A path to the audio file that needs to be transcribed.
/// * `mode` - Optional timestamp output mode (Tokens, Words, or Sentences)
///
/// # Returns
///
/// This function returns a `TranscriptionResult` which includes the transcribed text along with timestamps at the requested level.
fn transcribe_file<P: AsRef<Path>>(
&mut self,
audio_path: P,
mode: Option<TimestampMode>,
) -> Result<TranscriptionResult> {
let audio_path = audio_path.as_ref();
let (audio, spec) = load_audio(audio_path)?;
self.transcribe_samples(audio, spec.sample_rate, spec.channels, mode)
}
/// Transcribes multiple audio files in batch.
///
/// # Arguments
///
/// * `audio_paths`: A slice of paths to the audio files that need to be transcribed.
/// * `mode` - Optional timestamp output mode (Tokens, Words, or Sentences)
///
/// # Returns
///
/// This function returns a `TranscriptionResult` which includes the transcribed text along with timestamps at the requested level.
fn transcribe_file_batch<P: AsRef<Path>>(
&mut self,
audio_paths: &[P],
mode: Option<TimestampMode>,
) -> Result<Vec<TranscriptionResult>> {
let mut results = Vec::with_capacity(audio_paths.len());
for path in audio_paths {
let result = self.transcribe_file(path, mode)?;
results.push(result);
}
Ok(results)
}
}