Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit e059748

Browse files
authored
Merge pull request #205 from newfla/fix_segment_callback
fix: segment_callaback logic
2 parents a5d279c + 684ec8e commit e059748

File tree

1 file changed

+25
-37
lines changed

1 file changed

+25
-37
lines changed

src/whisper_params.rs

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -419,26 +419,20 @@ impl<'a, 'b> FullParams<'a, 'b> {
419419
{
420420
unsafe {
421421
let user_data = &mut *(user_data as *mut SegmentCallbackFn);
422-
let n_segments = whisper_rs_sys::whisper_full_n_segments_from_state(state);
423-
let s0 = n_segments - n_new;
424-
//let user_data = user_data as *mut Box<dyn FnMut(SegmentCallbackData)>;
425-
426-
for i in s0..n_segments {
427-
let text = whisper_rs_sys::whisper_full_get_segment_text_from_state(state, i);
428-
let text = CStr::from_ptr(text);
429-
430-
let t0 = whisper_rs_sys::whisper_full_get_segment_t0_from_state(state, i);
431-
let t1 = whisper_rs_sys::whisper_full_get_segment_t1_from_state(state, i);
432-
433-
match text.to_str() {
434-
Ok(n) => user_data(SegmentCallbackData {
435-
segment: i,
436-
start_timestamp: t0,
437-
end_timestamp: t1,
438-
text: n.to_string(),
439-
}),
440-
Err(_) => {}
441-
}
422+
let text = whisper_rs_sys::whisper_full_get_segment_text_from_state(state, n_new);
423+
let text = CStr::from_ptr(text);
424+
425+
let t0 = whisper_rs_sys::whisper_full_get_segment_t0_from_state(state, n_new);
426+
let t1 = whisper_rs_sys::whisper_full_get_segment_t1_from_state(state, n_new);
427+
428+
match text.to_str() {
429+
Ok(n) => user_data(SegmentCallbackData {
430+
segment: n_new + 1,
431+
start_timestamp: t0,
432+
end_timestamp: t1,
433+
text: n.to_string(),
434+
}),
435+
Err(_) => {}
442436
}
443437
}
444438
}
@@ -488,23 +482,17 @@ impl<'a, 'b> FullParams<'a, 'b> {
488482
{
489483
unsafe {
490484
let user_data = &mut *(user_data as *mut SegmentCallbackFn);
491-
let n_segments = whisper_rs_sys::whisper_full_n_segments_from_state(state);
492-
let s0 = n_segments - n_new;
493-
//let user_data = user_data as *mut Box<dyn FnMut(SegmentCallbackData)>;
494-
495-
for i in s0..n_segments {
496-
let text = whisper_rs_sys::whisper_full_get_segment_text_from_state(state, i);
497-
let text = CStr::from_ptr(text);
498-
499-
let t0 = whisper_rs_sys::whisper_full_get_segment_t0_from_state(state, i);
500-
let t1 = whisper_rs_sys::whisper_full_get_segment_t1_from_state(state, i);
501-
user_data(SegmentCallbackData {
502-
segment: i,
503-
start_timestamp: t0,
504-
end_timestamp: t1,
505-
text: text.to_string_lossy().to_string(),
506-
});
507-
}
485+
let text = whisper_rs_sys::whisper_full_get_segment_text_from_state(state, n_new);
486+
let text = CStr::from_ptr(text);
487+
488+
let t0 = whisper_rs_sys::whisper_full_get_segment_t0_from_state(state, n_new);
489+
let t1 = whisper_rs_sys::whisper_full_get_segment_t1_from_state(state, n_new);
490+
user_data(SegmentCallbackData {
491+
segment: n_new,
492+
start_timestamp: t0,
493+
end_timestamp: t1,
494+
text: text.to_string_lossy().to_string(),
495+
});
508496
}
509497
}
510498

0 commit comments

Comments
 (0)