Skip to content

Commit a6833f2

Browse files
committed
Extract new_segment_callback as a function
1 parent 4b49472 commit a6833f2

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

bindings/ruby/ext/ruby_whisper.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ static ID id_pre_converted_models;
5353

5454
static bool is_log_callback_finalized = false;
5555

56+
// High level API
57+
static VALUE rb_whisper_segment_initialize(VALUE context, int index);
58+
5659
/*
5760
* call-seq:
5861
* lang_max_id -> Integer
@@ -187,6 +190,29 @@ static ruby_whisper_callback_container * rb_whisper_callback_container_allocate(
187190
return container;
188191
}
189192

193+
static void new_segment_callback(struct whisper_context *ctx, struct whisper_state *state, int n_new, void *user_data) {
194+
const ruby_whisper_callback_container *container = (ruby_whisper_callback_container *)user_data;
195+
196+
// Currently, doesn't support state because
197+
// those require to resolve GC-related problems.
198+
if (!NIL_P(container->callback)) {
199+
rb_funcall(container->callback, id_call, 4, *container->context, Qnil, INT2NUM(n_new), container->user_data);
200+
}
201+
const long callbacks_len = RARRAY_LEN(container->callbacks);
202+
if (0 == callbacks_len) {
203+
return;
204+
}
205+
const int n_segments = whisper_full_n_segments_from_state(state);
206+
for (int i = n_new; i > 0; i--) {
207+
int i_segment = n_segments - i;
208+
VALUE segment = rb_whisper_segment_initialize(*container->context, i_segment);
209+
for (int j = 0; j < callbacks_len; j++) {
210+
VALUE cb = rb_ary_entry(container->callbacks, j);
211+
rb_funcall(cb, id_call, 1, segment);
212+
}
213+
}
214+
}
215+
190216
static VALUE ruby_whisper_params_allocate(VALUE klass) {
191217
ruby_whisper_params *rwp;
192218
rwp = ALLOC(ruby_whisper_params);
@@ -230,9 +256,6 @@ static VALUE ruby_whisper_initialize(int argc, VALUE *argv, VALUE self) {
230256
return self;
231257
}
232258

233-
// High level API
234-
static VALUE rb_whisper_segment_initialize(VALUE context, int index);
235-
236259
/*
237260
* transcribe a single file
238261
* can emit to a block results
@@ -354,29 +377,8 @@ static VALUE ruby_whisper_transcribe(int argc, VALUE *argv, VALUE self) {
354377
}
355378

356379
if (!NIL_P(rwp->new_segment_callback_container->callback) || 0 != RARRAY_LEN(rwp->new_segment_callback_container->callbacks)) {
357-
rwp->params.new_segment_callback = [](struct whisper_context * ctx, struct whisper_state * state, int n_new, void * user_data) {
358-
const ruby_whisper_callback_container *container = (ruby_whisper_callback_container *)user_data;
359-
360-
// Currently, doesn't support state because
361-
// those require to resolve GC-related problems.
362-
if (!NIL_P(container->callback)) {
363-
rb_funcall(container->callback, id_call, 4, *container->context, Qnil, INT2NUM(n_new), container->user_data);
364-
}
365-
const long callbacks_len = RARRAY_LEN(container->callbacks);
366-
if (0 == callbacks_len) {
367-
return;
368-
}
369-
const int n_segments = whisper_full_n_segments_from_state(state);
370-
for (int i = n_new; i > 0; i--) {
371-
int i_segment = n_segments - i;
372-
VALUE segment = rb_whisper_segment_initialize(*container->context, i_segment);
373-
for (int j = 0; j < callbacks_len; j++) {
374-
VALUE cb = rb_ary_entry(container->callbacks, j);
375-
rb_funcall(cb, id_call, 1, segment);
376-
}
377-
}
378-
};
379380
rwp->new_segment_callback_container->context = &self;
381+
rwp->params.new_segment_callback = new_segment_callback;
380382
rwp->params.new_segment_callback_user_data = rwp->new_segment_callback_container;
381383
}
382384

0 commit comments

Comments
 (0)