@@ -20,12 +20,16 @@ pub struct TranscriptionTask<S, T> {
20
20
pub trait AudioChunk : Send + ' static {
21
21
fn samples ( & self ) -> & [ f32 ] ;
22
22
fn meta ( & self ) -> Option < serde_json:: Value > ;
23
+ fn start_timestamp_ms ( & self ) -> Option < usize > ;
24
+ fn end_timestamp_ms ( & self ) -> Option < usize > ;
23
25
}
24
26
25
27
#[ derive( Default ) ]
26
28
pub struct SimpleAudioChunk {
27
29
pub samples : Vec < f32 > ,
28
30
pub meta : Option < serde_json:: Value > ,
31
+ pub start_timestamp_ms : Option < usize > ,
32
+ pub end_timestamp_ms : Option < usize > ,
29
33
}
30
34
31
35
impl AudioChunk for SimpleAudioChunk {
@@ -36,6 +40,14 @@ impl AudioChunk for SimpleAudioChunk {
36
40
fn meta ( & self ) -> Option < serde_json:: Value > {
37
41
self . meta . clone ( )
38
42
}
43
+
44
+ fn start_timestamp_ms ( & self ) -> Option < usize > {
45
+ self . start_timestamp_ms
46
+ }
47
+
48
+ fn end_timestamp_ms ( & self ) -> Option < usize > {
49
+ self . end_timestamp_ms
50
+ }
39
51
}
40
52
41
53
pub struct AudioChunkStream < S > ( pub S ) ;
@@ -116,6 +128,7 @@ where
116
128
& samples,
117
129
& mut this. current_segment_task ,
118
130
None ,
131
+ ( None , None ) ,
119
132
) {
120
133
Poll :: Ready ( result) => return Poll :: Ready ( result) ,
121
134
Poll :: Pending => continue ,
@@ -156,11 +169,14 @@ where
156
169
let meta = chunk. meta ( ) ;
157
170
let samples = chunk. samples ( ) ;
158
171
172
+ let timestamps = ( chunk. start_timestamp_ms ( ) , chunk. end_timestamp_ms ( ) ) ;
173
+
159
174
match process_transcription (
160
175
& mut this. whisper ,
161
176
samples,
162
177
& mut this. current_segment_task ,
163
178
meta,
179
+ timestamps,
164
180
) {
165
181
Poll :: Ready ( result) => return Poll :: Ready ( result) ,
166
182
Poll :: Pending => continue ,
@@ -178,6 +194,7 @@ fn process_transcription<'a>(
178
194
samples : & ' a [ f32 ] ,
179
195
current_segment_task : & ' a mut Option < Pin < Box < dyn Stream < Item = Segment > + Send > > > ,
180
196
meta : Option < serde_json:: Value > ,
197
+ timestamps : ( Option < usize > , Option < usize > ) ,
181
198
) -> Poll < Option < Segment > > {
182
199
if !samples. is_empty ( ) {
183
200
match whisper. transcribe ( samples) {
@@ -190,6 +207,11 @@ fn process_transcription<'a>(
190
207
Ok ( mut segments) => {
191
208
for segment in & mut segments {
192
209
segment. meta = meta. clone ( ) ;
210
+
211
+ if let ( Some ( start_ms) , Some ( end_ms) ) = timestamps {
212
+ segment. start = start_ms as f64 / 1000.0 ;
213
+ segment. end = end_ms as f64 / 1000.0 ;
214
+ }
193
215
}
194
216
195
217
* current_segment_task = Some ( Box :: pin ( futures_util:: stream:: iter ( segments) ) ) ;
0 commit comments