Skip to content

Commit 4245c77

Browse files
authored
ruby : Add ruby binding for max_len (#3365)
* add ruby binding for max_len * add test, update param numbers
1 parent 0becabc commit 4245c77

File tree

3 files changed

+70
-25
lines changed

3 files changed

+70
-25
lines changed

bindings/ruby/ext/ruby_whisper_params.c

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
rb_define_method(cParams, #param_name, ruby_whisper_params_get_ ## param_name, 0); \
2727
rb_define_method(cParams, #param_name "=", ruby_whisper_params_set_ ## param_name, 1);
2828

29-
#define RUBY_WHISPER_PARAMS_PARAM_NAMES_COUNT 35
29+
#define RUBY_WHISPER_PARAMS_PARAM_NAMES_COUNT 36
3030

3131
extern VALUE cParams;
3232
extern VALUE cVADParams;
@@ -49,6 +49,7 @@ static ID id_print_timestamps;
4949
static ID id_suppress_blank;
5050
static ID id_suppress_nst;
5151
static ID id_token_timestamps;
52+
static ID id_max_len;
5253
static ID id_split_on_word;
5354
static ID id_initial_prompt;
5455
static ID id_diarize;
@@ -514,6 +515,33 @@ ruby_whisper_params_set_token_timestamps(VALUE self, VALUE value)
514515
{
515516
BOOL_PARAMS_SETTER(self, token_timestamps, value)
516517
}
518+
519+
/*
520+
* max segment length in characters.
521+
*
522+
* call-seq:
523+
* max_len -> Integer
524+
*/
525+
static VALUE
526+
ruby_whisper_params_get_max_len(VALUE self)
527+
{
528+
ruby_whisper_params *rwp;
529+
TypedData_Get_Struct(self, ruby_whisper_params, &ruby_whisper_params_type, rwp);
530+
return INT2NUM(rwp->params.max_len);
531+
}
532+
/*
533+
* call-seq:
534+
* max_len = length -> length
535+
*/
536+
static VALUE
537+
ruby_whisper_params_set_max_len(VALUE self, VALUE value)
538+
{
539+
ruby_whisper_params *rwp;
540+
TypedData_Get_Struct(self, ruby_whisper_params, &ruby_whisper_params_type, rwp);
541+
rwp->params.max_len = NUM2INT(value);
542+
return value;
543+
}
544+
517545
/*
518546
* If true, split on word rather than on token (when used with max_len).
519547
*
@@ -1137,6 +1165,7 @@ ruby_whisper_params_initialize(int argc, VALUE *argv, VALUE self)
11371165
SET_PARAM_IF_SAME(suppress_blank)
11381166
SET_PARAM_IF_SAME(suppress_nst)
11391167
SET_PARAM_IF_SAME(token_timestamps)
1168+
SET_PARAM_IF_SAME(max_len)
11401169
SET_PARAM_IF_SAME(split_on_word)
11411170
SET_PARAM_IF_SAME(initial_prompt)
11421171
SET_PARAM_IF_SAME(offset)
@@ -1271,30 +1300,31 @@ init_ruby_whisper_params(VALUE *mWhisper)
12711300
DEFINE_PARAM(suppress_blank, 8)
12721301
DEFINE_PARAM(suppress_nst, 9)
12731302
DEFINE_PARAM(token_timestamps, 10)
1274-
DEFINE_PARAM(split_on_word, 11)
1275-
DEFINE_PARAM(initial_prompt, 12)
1276-
DEFINE_PARAM(diarize, 13)
1277-
DEFINE_PARAM(offset, 14)
1278-
DEFINE_PARAM(duration, 15)
1279-
DEFINE_PARAM(max_text_tokens, 16)
1280-
DEFINE_PARAM(temperature, 17)
1281-
DEFINE_PARAM(max_initial_ts, 18)
1282-
DEFINE_PARAM(length_penalty, 19)
1283-
DEFINE_PARAM(temperature_inc, 20)
1284-
DEFINE_PARAM(entropy_thold, 21)
1285-
DEFINE_PARAM(logprob_thold, 22)
1286-
DEFINE_PARAM(no_speech_thold, 23)
1287-
DEFINE_PARAM(new_segment_callback, 24)
1288-
DEFINE_PARAM(new_segment_callback_user_data, 25)
1289-
DEFINE_PARAM(progress_callback, 26)
1290-
DEFINE_PARAM(progress_callback_user_data, 27)
1291-
DEFINE_PARAM(encoder_begin_callback, 28)
1292-
DEFINE_PARAM(encoder_begin_callback_user_data, 29)
1293-
DEFINE_PARAM(abort_callback, 30)
1294-
DEFINE_PARAM(abort_callback_user_data, 31)
1295-
DEFINE_PARAM(vad, 32)
1296-
DEFINE_PARAM(vad_model_path, 33)
1297-
DEFINE_PARAM(vad_params, 34)
1303+
DEFINE_PARAM(max_len, 11)
1304+
DEFINE_PARAM(split_on_word, 12)
1305+
DEFINE_PARAM(initial_prompt, 13)
1306+
DEFINE_PARAM(diarize, 14)
1307+
DEFINE_PARAM(offset, 15)
1308+
DEFINE_PARAM(duration, 16)
1309+
DEFINE_PARAM(max_text_tokens, 17)
1310+
DEFINE_PARAM(temperature, 18)
1311+
DEFINE_PARAM(max_initial_ts, 19)
1312+
DEFINE_PARAM(length_penalty, 20)
1313+
DEFINE_PARAM(temperature_inc, 21)
1314+
DEFINE_PARAM(entropy_thold, 22)
1315+
DEFINE_PARAM(logprob_thold, 23)
1316+
DEFINE_PARAM(no_speech_thold, 24)
1317+
DEFINE_PARAM(new_segment_callback, 25)
1318+
DEFINE_PARAM(new_segment_callback_user_data, 26)
1319+
DEFINE_PARAM(progress_callback, 27)
1320+
DEFINE_PARAM(progress_callback_user_data, 28)
1321+
DEFINE_PARAM(encoder_begin_callback, 29)
1322+
DEFINE_PARAM(encoder_begin_callback_user_data, 30)
1323+
DEFINE_PARAM(abort_callback, 31)
1324+
DEFINE_PARAM(abort_callback_user_data, 32)
1325+
DEFINE_PARAM(vad, 33)
1326+
DEFINE_PARAM(vad_model_path, 34)
1327+
DEFINE_PARAM(vad_params, 35)
12981328

12991329
rb_define_method(cParams, "on_new_segment", ruby_whisper_params_on_new_segment, 0);
13001330
rb_define_method(cParams, "on_progress", ruby_whisper_params_on_progress, 0);

bindings/ruby/sig/whisper.rbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ module Whisper
135135
?suppress_blank: boolish,
136136
?suppress_nst: boolish,
137137
?token_timestamps: boolish,
138+
?max_len: Integer,
138139
?split_on_word: boolish,
139140
?initial_prompt: string | nil,
140141
?diarize: boolish,
@@ -222,6 +223,12 @@ module Whisper
222223
#
223224
def token_timestamps: () -> (true | false)
224225

226+
def max_len=: (Integer) -> Integer
227+
228+
# max segment length in characters.
229+
#
230+
def max_len: () -> Integer
231+
225232
def split_on_word=: (boolish) -> boolish
226233

227234
# If true, split on word rather than on token (when used with max_len).

bindings/ruby/test/test_params.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class TestParams < TestBase
1313
:suppress_blank,
1414
:suppress_nst,
1515
:token_timestamps,
16+
:max_len,
1617
:split_on_word,
1718
:initial_prompt,
1819
:diarize,
@@ -139,6 +140,13 @@ def test_token_timestamps
139140
assert !@params.token_timestamps
140141
end
141142

143+
def test_max_len
144+
@params.max_len = 42
145+
assert_equal @params.max_len, 42
146+
@params.max_len = 0
147+
assert_equal @params.max_len, 0
148+
end
149+
142150
def test_split_on_word
143151
@params.split_on_word = true
144152
assert @params.split_on_word

0 commit comments

Comments
 (0)