Skip to content

Commit f19f856

Browse files
committed
wip
1 parent abd9e62 commit f19f856

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

crates/transcribe-whisper-local/src/service/streaming.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ where
297297
Ok(chunk) => Some(hypr_whisper_local::SimpleAudioChunk {
298298
samples: chunk.samples,
299299
meta: Some(serde_json::json!({ "source": source_name })),
300+
start_timestamp_ms: Some(chunk.start_timestamp_ms),
301+
end_timestamp_ms: Some(chunk.end_timestamp_ms),
300302
}),
301303
})
302304
})

crates/whisper-local/src/stream.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ pub struct TranscriptionTask<S, T> {
2020
pub trait AudioChunk: Send + 'static {
2121
fn samples(&self) -> &[f32];
2222
fn meta(&self) -> Option<serde_json::Value>;
23+
fn start_timestamp_ms(&self) -> Option<usize>;
24+
fn end_timestamp_ms(&self) -> Option<usize>;
2325
}
2426

2527
#[derive(Default)]
2628
pub struct SimpleAudioChunk {
2729
pub samples: Vec<f32>,
2830
pub meta: Option<serde_json::Value>,
31+
pub start_timestamp_ms: Option<usize>,
32+
pub end_timestamp_ms: Option<usize>,
2933
}
3034

3135
impl AudioChunk for SimpleAudioChunk {
@@ -36,6 +40,14 @@ impl AudioChunk for SimpleAudioChunk {
3640
fn meta(&self) -> Option<serde_json::Value> {
3741
self.meta.clone()
3842
}
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+
}
3951
}
4052

4153
pub struct AudioChunkStream<S>(pub S);
@@ -116,6 +128,7 @@ where
116128
&samples,
117129
&mut this.current_segment_task,
118130
None,
131+
(None, None),
119132
) {
120133
Poll::Ready(result) => return Poll::Ready(result),
121134
Poll::Pending => continue,
@@ -156,11 +169,14 @@ where
156169
let meta = chunk.meta();
157170
let samples = chunk.samples();
158171

172+
let timestamps = (chunk.start_timestamp_ms(), chunk.end_timestamp_ms());
173+
159174
match process_transcription(
160175
&mut this.whisper,
161176
samples,
162177
&mut this.current_segment_task,
163178
meta,
179+
timestamps,
164180
) {
165181
Poll::Ready(result) => return Poll::Ready(result),
166182
Poll::Pending => continue,
@@ -178,6 +194,7 @@ fn process_transcription<'a>(
178194
samples: &'a [f32],
179195
current_segment_task: &'a mut Option<Pin<Box<dyn Stream<Item = Segment> + Send>>>,
180196
meta: Option<serde_json::Value>,
197+
timestamps: (Option<usize>, Option<usize>),
181198
) -> Poll<Option<Segment>> {
182199
if !samples.is_empty() {
183200
match whisper.transcribe(samples) {
@@ -190,6 +207,11 @@ fn process_transcription<'a>(
190207
Ok(mut segments) => {
191208
for segment in &mut segments {
192209
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+
}
193215
}
194216

195217
*current_segment_task = Some(Box::pin(futures_util::stream::iter(segments)));

0 commit comments

Comments
 (0)