Skip to content

Commit 5b31f63

Browse files
committed
Run new segment callback
1 parent e9f4034 commit 5b31f63

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

bindings/ruby/ext/ruby_whisper.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,17 @@ static VALUE ruby_whisper_transcribe(int argc, VALUE *argv, VALUE self) {
241241

242242
if (!NIL_P(rwp->new_segment_callback)) {
243243
rwp->params.new_segment_callback = [](struct whisper_context * ctx, struct whisper_state * state, int n_new, void * user_data) {
244-
VALUE callback = *(VALUE *)user_data;
245-
int n_segments = whisper_full_n_segments_from_state(state);
246-
for (int i = n_new; i > 0; --i) {
247-
const int i_segment = n_segments - i;
248-
const char * text = whisper_full_get_segment_text_from_state(state, i_segment);
249-
// Multiplying 10 shouldn't cause overflow because to_timestamp() in whisper.cpp does it
250-
const int64_t t0 = whisper_full_get_segment_t0_from_state(state, i_segment) * 10;
251-
const int64_t t1 = whisper_full_get_segment_t1_from_state(state, i_segment) * 10;
252-
rb_funcall(callback, rb_intern("call"), 4, rb_str_new2(text), INT2NUM(t0), INT2NUM(t1), INT2FIX(i_segment));
253-
}
244+
ruby_whisper *rw;;
245+
VALUE context = *(VALUE *)user_data;
246+
Data_Get_Struct(context, ruby_whisper, rw);
247+
VALUE callback = rw->new_segment_callback;
248+
249+
// Currently, doesn't support state and user_data because
250+
// those require to resolve GC-related problems.
251+
rb_funcall(callback, rb_intern("call"), 4, context, Qnil, INT2NUM(n_new), Qnil);
254252
};
255-
rwp->params.new_segment_callback_user_data = &rwp->new_segment_callback;
253+
rw->new_segment_callback = rwp->new_segment_callback;
254+
rwp->params.new_segment_callback_user_data = &self;
256255
}
257256

258257
if (whisper_full_parallel(rw->context, rwp->params, pcmf32.data(), pcmf32.size(), 1) != 0) {

bindings/ruby/ext/ruby_whisper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
typedef struct {
77
struct whisper_context *context;
8+
VALUE new_segment_callback;
89
} ruby_whisper;
910

1011
typedef struct {

0 commit comments

Comments
 (0)