Skip to content

Commit 0b30133

Browse files
committed
Extract progress_callback as a function
1 parent a6833f2 commit 0b30133

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

bindings/ruby/ext/ruby_whisper.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,24 @@ static void new_segment_callback(struct whisper_context *ctx, struct whisper_sta
213213
}
214214
}
215215

216+
static void progress_callback(struct whisper_context *ctx, struct whisper_state *state, int progress_cur, void *user_data) {
217+
const ruby_whisper_callback_container *container = (ruby_whisper_callback_container *)user_data;
218+
const VALUE progress = INT2NUM(progress_cur);
219+
// Currently, doesn't support state because
220+
// those require to resolve GC-related problems.
221+
if (!NIL_P(container->callback)) {
222+
rb_funcall(container->callback, id_call, 4, *container->context, Qnil, progress, container->user_data);
223+
}
224+
const long callbacks_len = RARRAY_LEN(container->callbacks);
225+
if (0 == callbacks_len) {
226+
return;
227+
}
228+
for (int j = 0; j < callbacks_len; j++) {
229+
VALUE cb = rb_ary_entry(container->callbacks, j);
230+
rb_funcall(cb, id_call, 1, progress);
231+
}
232+
}
233+
216234
static VALUE ruby_whisper_params_allocate(VALUE klass) {
217235
ruby_whisper_params *rwp;
218236
rwp = ALLOC(ruby_whisper_params);
@@ -383,24 +401,8 @@ static VALUE ruby_whisper_transcribe(int argc, VALUE *argv, VALUE self) {
383401
}
384402

385403
if (!NIL_P(rwp->progress_callback_container->callback) || 0 != RARRAY_LEN(rwp->progress_callback_container->callbacks)) {
386-
rwp->params.progress_callback = [](struct whisper_context *ctx, struct whisper_state * /*state*/, int progress_cur, void *user_data) {
387-
const ruby_whisper_callback_container *container = (ruby_whisper_callback_container *)user_data;
388-
const VALUE progress = INT2NUM(progress_cur);
389-
// Currently, doesn't support state because
390-
// those require to resolve GC-related problems.
391-
if (!NIL_P(container->callback)) {
392-
rb_funcall(container->callback, id_call, 4, *container->context, Qnil, progress, container->user_data);
393-
}
394-
const long callbacks_len = RARRAY_LEN(container->callbacks);
395-
if (0 == callbacks_len) {
396-
return;
397-
}
398-
for (int j = 0; j < callbacks_len; j++) {
399-
VALUE cb = rb_ary_entry(container->callbacks, j);
400-
rb_funcall(cb, id_call, 1, progress);
401-
}
402-
};
403404
rwp->progress_callback_container->context = &self;
405+
rwp->params.progress_callback = progress_callback;
404406
rwp->params.progress_callback_user_data = rwp->progress_callback_container;
405407
}
406408

0 commit comments

Comments
 (0)