Skip to content

Commit e0ebea2

Browse files
committed
bindings : use new API
1 parent 22ab809 commit e0ebea2

File tree

7 files changed

+51
-19
lines changed

7 files changed

+51
-19
lines changed

bindings/go/whisper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ var (
103103
func Whisper_init(path string) *Context {
104104
cPath := C.CString(path)
105105
defer C.free(unsafe.Pointer(cPath))
106-
if ctx := C.whisper_init_from_file(cPath); ctx != nil {
106+
if ctx := C.whisper_init_from_file_with_params(cPath, C.whisper_context_default_params()); ctx != nil {
107107
return (*Context)(ctx)
108108
} else {
109109
return nil

bindings/java/src/main/java/io/github/ggerganov/whispercpp/WhisperCpp.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
public class WhisperCpp implements AutoCloseable {
1616
private WhisperCppJnaLibrary lib = WhisperCppJnaLibrary.instance;
1717
private Pointer ctx = null;
18-
private Pointer greedyPointer = null;
19-
private Pointer beamPointer = null;
18+
private Pointer paramPointer = null;
19+
private Pointer greedyParamsPointer = null;
20+
private Pointer beamParamsPointer = null;
2021

2122
public File modelDir() {
2223
String modelDirPath = System.getenv("XDG_CACHE_HOME");
@@ -43,7 +44,8 @@ public void initContext(String modelPath) throws FileNotFoundException {
4344
modelPath = new File(modelDir(), modelPath).getAbsolutePath();
4445
}
4546

46-
ctx = lib.whisper_init_from_file(modelPath);
47+
paramPointer = lib.whisper_context_default_params_by_ref();
48+
ctx = lib.whisper_init_from_file_with_params(modelPath, paramPointer);
4749

4850
if (ctx == null) {
4951
throw new FileNotFoundException(modelPath);
@@ -63,15 +65,15 @@ public WhisperFullParams getFullDefaultParams(WhisperSamplingStrategy strategy)
6365

6466
// whisper_full_default_params_by_ref allocates memory which we need to delete, so only create max 1 pointer for each strategy.
6567
if (strategy == WhisperSamplingStrategy.WHISPER_SAMPLING_GREEDY) {
66-
if (greedyPointer == null) {
67-
greedyPointer = lib.whisper_full_default_params_by_ref(strategy.ordinal());
68+
if (greedyParamsPointer == null) {
69+
greedyParamsPointer = lib.whisper_full_default_params_by_ref(strategy.ordinal());
6870
}
69-
pointer = greedyPointer;
71+
pointer = greedyParamsPointer;
7072
} else {
71-
if (beamPointer == null) {
72-
beamPointer = lib.whisper_full_default_params_by_ref(strategy.ordinal());
73+
if (beamParamsPointer == null) {
74+
beamParamsPointer = lib.whisper_full_default_params_by_ref(strategy.ordinal());
7375
}
74-
pointer = beamPointer;
76+
pointer = beamParamsPointer;
7577
}
7678

7779
WhisperFullParams params = new WhisperFullParams(pointer);
@@ -93,13 +95,17 @@ private void freeContext() {
9395
}
9496

9597
private void freeParams() {
96-
if (greedyPointer != null) {
97-
Native.free(Pointer.nativeValue(greedyPointer));
98-
greedyPointer = null;
98+
if (paramPointer != null) {
99+
Native.free(Pointer.nativeValue(paramPointer));
100+
paramPointer = null;
99101
}
100-
if (beamPointer != null) {
101-
Native.free(Pointer.nativeValue(beamPointer));
102-
beamPointer = null;
102+
if (greedyParamsPointer != null) {
103+
Native.free(Pointer.nativeValue(greedyParamsPointer));
104+
greedyParamsPointer = null;
105+
}
106+
if (beamParamsPointer != null) {
107+
Native.free(Pointer.nativeValue(beamParamsPointer));
108+
beamParamsPointer = null;
103109
}
104110
}
105111

bindings/java/src/main/java/io/github/ggerganov/whispercpp/WhisperCppJnaLibrary.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,31 @@ public interface WhisperCppJnaLibrary extends Library {
1313
String whisper_print_system_info();
1414

1515
/**
16-
* Allocate (almost) all memory needed for the model by loading from a file.
16+
* DEPRECATED. Allocate (almost) all memory needed for the model by loading from a file.
1717
*
1818
* @param path_model Path to the model file
1919
* @return Whisper context on success, null on failure
2020
*/
2121
Pointer whisper_init_from_file(String path_model);
22+
23+
/**
24+
* Provides default params which can be used with `whisper_init_from_file_with_params()` etc.
25+
* Because this function allocates memory for the params, the caller must call either:
26+
* - call `whisper_free_context_params()`
27+
* - `Native.free(Pointer.nativeValue(pointer));`
28+
*/
29+
Pointer whisper_context_default_params_by_ref();
30+
31+
void whisper_free_context_params(Pointer params);
32+
33+
/**
34+
* Allocate (almost) all memory needed for the model by loading from a file.
35+
*
36+
* @param path_model Path to the model file
37+
* @param params Pointer to whisper_context_params
38+
* @return Whisper context on success, null on failure
39+
*/
40+
Pointer whisper_init_from_file_with_params(String path_model, Pointer params);
2241

2342
/**
2443
* Allocate (almost) all memory needed for the model by loading from a buffer.

bindings/javascript/emscripten.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct whisper_context * g_context;
2020
EMSCRIPTEN_BINDINGS(whisper) {
2121
emscripten::function("init", emscripten::optional_override([](const std::string & path_model) {
2222
if (g_context == nullptr) {
23-
g_context = whisper_init_from_file(path_model.c_str());
23+
g_context = whisper_init_from_file_with_params(path_model.c_str(), whisper_context_default_params());
2424
if (g_context != nullptr) {
2525
return true;
2626
} else {

bindings/ruby/ext/ruby_whisper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static VALUE ruby_whisper_initialize(int argc, VALUE *argv, VALUE self) {
8787
if (!rb_respond_to(whisper_model_file_path, rb_intern("to_s"))) {
8888
rb_raise(rb_eRuntimeError, "Expected file path to model to initialize Whisper::Context");
8989
}
90-
rw->context = whisper_init_from_file(StringValueCStr(whisper_model_file_path));
90+
rw->context = whisper_init_from_file_with_params(StringValueCStr(whisper_model_file_path), whisper_context_default_params());
9191
if (rw->context == nullptr) {
9292
rb_raise(rb_eRuntimeError, "error: failed to initialize whisper context");
9393
}

whisper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,6 +3267,12 @@ void whisper_free(struct whisper_context * ctx) {
32673267
}
32683268
}
32693269

3270+
void whisper_free_context_params(struct whisper_context_params * params) {
3271+
if (params) {
3272+
delete params;
3273+
}
3274+
}
3275+
32703276
void whisper_free_params(struct whisper_full_params * params) {
32713277
if (params) {
32723278
delete params;

whisper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ extern "C" {
170170
WHISPER_API void whisper_free (struct whisper_context * ctx);
171171
WHISPER_API void whisper_free_state(struct whisper_state * state);
172172
WHISPER_API void whisper_free_params(struct whisper_full_params * params);
173+
WHISPER_API void whisper_free_context_params(struct whisper_context_params * params);
173174

174175
// Convert RAW PCM audio to log mel spectrogram.
175176
// The resulting spectrogram is stored inside the default state of the provided whisper context.

0 commit comments

Comments
 (0)