Share parakeet full body function

This commit is contained in:
Kitaiti Makoto 2026-05-23 02:36:38 +09:00
parent 693fefde42
commit b765ac43bb
3 changed files with 22 additions and 64 deletions

View File

@ -110,6 +110,13 @@ typedef struct parsed_samples_t {
bool memview_exported;
} parsed_samples_t;
typedef struct {
VALUE *context;
VALUE *params;
float *samples;
int n_samples;
} ruby_whisper_parakeet_context_full_args;
typedef struct {
struct parakeet_full_params params;
ruby_whisper_callback_container *new_segment_callback_container;

View File

@ -201,17 +201,10 @@ parakeet_full_ubf(void *rb_args)
RUBY_ATOMIC_SET(args->abort_callback_user_data->is_interrupted, 1);
}
typedef struct {
VALUE *context;
VALUE *params;
float *samples;
int n_samples;
} parakeet_full_args;
static VALUE
parakeet_full_body(VALUE rb_args)
VALUE
ruby_whisper_parakeet_context_full_body(VALUE rb_args)
{
parakeet_full_args *args = (parakeet_full_args *)rb_args;
ruby_whisper_parakeet_context_full_args *args = (ruby_whisper_parakeet_context_full_args *)rb_args;
ruby_whisper_parakeet_context *rwpc;
GetParakeetContext(*args->context, rwpc);
ruby_whisper_parakeet_params *rwpp;
@ -248,18 +241,18 @@ ruby_whisper_parakeet_context_full(int argc, VALUE *argv, VALUE self)
VALUE n_samples = argc == 2 ? Qnil : argv[2];
struct parsed_samples_t parsed = parse_samples(&argv[1], &n_samples);
parakeet_full_args args = {
ruby_whisper_parakeet_context_full_args args = {
&self,
&argv[0],
parsed.samples,
parsed.n_samples,
};
VALUE rb_result = rb_ensure(parakeet_full_body, (VALUE)&args, release_samples, (VALUE)&parsed);
VALUE rb_result = rb_ensure(ruby_whisper_parakeet_context_full_body, (VALUE)&args, release_samples, (VALUE)&parsed);
const int result = NUM2INT(rb_result);
if (result == 0) {
return self;
} else {
rb_exc_raise(rb_funcall(eError, id_new, 1, result));
rb_exc_raise(rb_funcall(eError, id_new, 1, rb_result));
}
}
@ -287,4 +280,3 @@ init_ruby_whisper_parakeet_context(VALUE *mParakeet)
ITERATE_TOKEN_ATTRS(REGISTER_TOKEN_ATTR)
}

View File

@ -10,43 +10,13 @@ extern "C" {
extern const rb_data_type_t ruby_whisper_parakeet_context_type;
extern const rb_data_type_t ruby_whisper_parakeet_params_type;
extern void ruby_whisper_parakeet_prepare_transcription(ruby_whisper_parakeet_params *rwpp, VALUE *context, ruby_whisper_parakeet_abort_callback_user_data *abort_callback_user_data);
extern VALUE ruby_whisper_parakeet_context_full_body(VALUE rb_args);
extern ID id_to_s;
extern ID id_to_path;
extern ID id_new;
extern VALUE eError;
typedef struct transcribe_without_gvl_args {
struct parakeet_context *context;
struct parakeet_full_params params;
float *samples;
size_t n_samples;
int result;
} transcribe_without_gvl_args;
typedef struct {
ruby_whisper_parakeet_abort_callback_user_data *abort_callback_user_data;
} ruby_whisper_parakeet_transcribe_ubf_args;
static void
ruby_whisper_parakeet_transcribe_ubf(void *rb_args)
{
ruby_whisper_parakeet_transcribe_ubf_args *args = (ruby_whisper_parakeet_transcribe_ubf_args *)rb_args;
RUBY_ATOMIC_SET(args->abort_callback_user_data->is_interrupted, 1);
}
static void*
transcribe_without_gvl(void *rb_args)
{
struct transcribe_without_gvl_args *args = (struct transcribe_without_gvl_args *)rb_args;
args->result = parakeet_full(args->context, args->params, args->samples, args->n_samples);
return NULL;
}
VALUE
ruby_whisper_parakeet_transcribe(VALUE self, VALUE audio_path, VALUE params)
{
@ -68,29 +38,18 @@ ruby_whisper_parakeet_transcribe(VALUE self, VALUE audio_path, VALUE params)
GetParakeetContext(self, rwpc);
GetParakeetParams(params, rwpp);
ruby_whisper_parakeet_abort_callback_user_data abort_callback_user_data = {
0,
NULL,
};
ruby_whisper_parakeet_prepare_transcription(rwpp, &self, &abort_callback_user_data);
struct transcribe_without_gvl_args args = {
rwpc->context,
rwpp->params,
ruby_whisper_parakeet_context_full_args args = {
&self,
&params,
pcmf32.data(),
pcmf32.size(),
0,
(int)pcmf32.size(),
};
ruby_whisper_parakeet_transcribe_ubf_args ubf_args = {
&abort_callback_user_data,
};
rb_thread_call_without_gvl(transcribe_without_gvl, (void *)&args, ruby_whisper_parakeet_transcribe_ubf, (void *)&ubf_args);
if (args.result == 0) {
VALUE rb_result = ruby_whisper_parakeet_context_full_body((VALUE)&args);
const int result = NUM2INT(rb_result);
if (result == 0) {
return self;
} else {
rb_exc_raise(rb_funcall(eError, id_new, 1, INT2NUM(args.result)));
rb_exc_raise(rb_funcall(eError, id_new, 1, rb_result));
}
}