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

Commit f5ee632

Browse files
authored
Merge pull request #210 from newfla/fix_callback_v2
2 parents cb8f79d + 747f71d commit f5ee632

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

src/whisper_params.rs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ impl<'a, 'b> FullParams<'a, 'b> {
376376
/// Do not use this function unless you know what you are doing.
377377
/// * Be careful not to mutate the state of the whisper_context pointer returned in the callback.
378378
/// This could cause undefined behavior, as this violates the thread-safety guarantees of the underlying C library.
379+
/// **Warning** Can't be used with DTW. DTW will produce inconsistent callback invocation
379380
///
380381
/// Defaults to None.
381382
pub unsafe fn set_new_segment_callback(
@@ -389,6 +390,7 @@ impl<'a, 'b> FullParams<'a, 'b> {
389390
///
390391
/// # Safety
391392
/// See the safety notes for `set_new_segment_callback`.
393+
/// **Warning** Can't be used with DTW. DTW will produce inconsistent callback invocation
392394
///
393395
/// Defaults to None.
394396
pub unsafe fn set_new_segment_callback_user_data(&mut self, user_data: *mut std::ffi::c_void) {
@@ -399,6 +401,7 @@ impl<'a, 'b> FullParams<'a, 'b> {
399401
///
400402
/// Provides a limited segment_callback to ensure safety.
401403
/// See `set_new_segment_callback` if you need to use `whisper_context` and `whisper_state`
404+
/// **Warning** Can't be used with DTW. DTW will produce inconsistent callback invocation
402405
///
403406
/// Defaults to None.
404407
pub fn set_segment_callback_safe<O, F>(&mut self, closure: O)
@@ -419,20 +422,26 @@ impl<'a, 'b> FullParams<'a, 'b> {
419422
{
420423
unsafe {
421424
let user_data = &mut *(user_data as *mut SegmentCallbackFn);
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(_) => {}
425+
let n_segments = whisper_rs_sys::whisper_full_n_segments_from_state(state);
426+
let s0 = n_segments - n_new;
427+
//let user_data = user_data as *mut Box<dyn FnMut(SegmentCallbackData)>;
428+
429+
for i in s0..n_segments {
430+
let text = whisper_rs_sys::whisper_full_get_segment_text_from_state(state, i);
431+
let text = CStr::from_ptr(text);
432+
433+
let t0 = whisper_rs_sys::whisper_full_get_segment_t0_from_state(state, i);
434+
let t1 = whisper_rs_sys::whisper_full_get_segment_t1_from_state(state, i);
435+
436+
match text.to_str() {
437+
Ok(n) => user_data(SegmentCallbackData {
438+
segment: i,
439+
start_timestamp: t0,
440+
end_timestamp: t1,
441+
text: n.to_string(),
442+
}),
443+
Err(_) => {}
444+
}
436445
}
437446
}
438447
}
@@ -462,6 +471,7 @@ impl<'a, 'b> FullParams<'a, 'b> {
462471
///
463472
/// Provides a limited segment_callback to ensure safety with lossy handling of bad UTF-8 characters.
464473
/// See `set_new_segment_callback` if you need to use `whisper_context` and `whisper_state`.
474+
/// **Warning** Can't be used with DTW. DTW will produce inconsistent callback invocation
465475
///
466476
/// Defaults to None.
467477
pub fn set_segment_callback_safe_lossy<O, F>(&mut self, closure: O)
@@ -482,17 +492,23 @@ impl<'a, 'b> FullParams<'a, 'b> {
482492
{
483493
unsafe {
484494
let user_data = &mut *(user_data as *mut SegmentCallbackFn);
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-
});
495+
let n_segments = whisper_rs_sys::whisper_full_n_segments_from_state(state);
496+
let s0 = n_segments - n_new;
497+
//let user_data = user_data as *mut Box<dyn FnMut(SegmentCallbackData)>;
498+
499+
for i in s0..n_segments {
500+
let text = whisper_rs_sys::whisper_full_get_segment_text_from_state(state, i);
501+
let text = CStr::from_ptr(text);
502+
503+
let t0 = whisper_rs_sys::whisper_full_get_segment_t0_from_state(state, i);
504+
let t1 = whisper_rs_sys::whisper_full_get_segment_t1_from_state(state, i);
505+
user_data(SegmentCallbackData {
506+
segment: i,
507+
start_timestamp: t0,
508+
end_timestamp: t1,
509+
text: text.to_string_lossy().to_string(),
510+
});
511+
}
496512
}
497513
}
498514

0 commit comments

Comments
 (0)