Skip to content

Commit 647c5f6

Browse files
committed
Add Params#new_segment_callback= method
1 parent a5abfe6 commit 647c5f6

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

bindings/ruby/ext/ruby_whisper.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static VALUE ruby_whisper_params_allocate(VALUE klass) {
7373
ruby_whisper_params *rwp;
7474
rwp = ALLOC(ruby_whisper_params);
7575
rwp->params = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
76+
rwp->new_segment_callback = Qnil;
7677
return Data_Wrap_Struct(klass, rb_whisper_params_mark, rb_whisper_params_free, rwp);
7778
}
7879

@@ -205,6 +206,28 @@ static VALUE ruby_whisper_transcribe(int argc, VALUE *argv, VALUE self) {
205206
};
206207
rwp->params.encoder_begin_callback_user_data = &is_aborted;
207208
}
209+
{
210+
// This cannot be used later because it is not incremented when new_segment_callback is not given.
211+
static int n_segments = 0;
212+
213+
rwp->params.new_segment_callback = [](struct whisper_context * ctx, struct whisper_state * state, int n_new, void * user_data) {
214+
VALUE callback = *(VALUE *)user_data;
215+
if (NIL_P(callback)){
216+
return;
217+
}
218+
219+
for (int i = 0; i < n_new; i++) {
220+
const int i_segment = n_segments + i;
221+
const char * text = whisper_full_get_segment_text_from_state(state, i_segment);
222+
// Multiplying 10 shouldn't cause overflow because to_timestamp() in whisper.cpp does it
223+
const int64_t t0 = whisper_full_get_segment_t0_from_state(state, i_segment) * 10;
224+
const int64_t t1 = whisper_full_get_segment_t1_from_state(state, i_segment) * 10;
225+
rb_funcall(callback, rb_intern("call"), 4, rb_str_new2(text), INT2NUM(t0), INT2NUM(t1), INT2FIX(i_segment));
226+
}
227+
n_segments += n_new;
228+
};
229+
rwp->params.new_segment_callback_user_data = &rwp->new_segment_callback;
230+
}
208231

209232
if (whisper_full_parallel(rw->context, rwp->params, pcmf32.data(), pcmf32.size(), 1) != 0) {
210233
fprintf(stderr, "failed to process audio\n");
@@ -365,6 +388,12 @@ static VALUE ruby_whisper_params_set_max_text_tokens(VALUE self, VALUE value) {
365388
rwp->params.n_max_text_ctx = NUM2INT(value);
366389
return value;
367390
}
391+
static VALUE ruby_whisper_params_set_new_segment_callback(VALUE self, VALUE value) {
392+
ruby_whisper_params *rwp;
393+
Data_Get_Struct(self, ruby_whisper_params, rwp);
394+
rwp->new_segment_callback = value;
395+
return value;
396+
}
368397

369398
void Init_whisper() {
370399
mWhisper = rb_define_module("Whisper");
@@ -412,6 +441,8 @@ void Init_whisper() {
412441

413442
rb_define_method(cParams, "max_text_tokens", ruby_whisper_params_get_max_text_tokens, 0);
414443
rb_define_method(cParams, "max_text_tokens=", ruby_whisper_params_set_max_text_tokens, 1);
444+
445+
rb_define_method(cParams, "new_segment_callback=", ruby_whisper_params_set_new_segment_callback, 1);
415446
}
416447
#ifdef __cplusplus
417448
}

bindings/ruby/ext/ruby_whisper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct {
1010
typedef struct {
1111
struct whisper_full_params params;
1212
bool diarize;
13+
VALUE new_segment_callback;
1314
} ruby_whisper_params;
1415

1516
#endif

0 commit comments

Comments
 (0)