]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
bindings.java : enable copyLibs task [no ci] (#2949)
authorDaniel Bevenius <redacted>
Wed, 26 Mar 2025 14:01:28 +0000 (15:01 +0100)
committerGitHub <redacted>
Wed, 26 Mar 2025 14:01:28 +0000 (15:01 +0100)
* bindings.java : enable copyLibs task [no ci]

This commit adds a dependency on the copyLibs task to the sourcesJar and
jar tasks. This ensures that the libwhisper.so file is copied to the
correct location before the jar is built.

It also sets the executable bit on the gradlew file.

* bindings.java : add copyLibs dep for processResources [no ci]

This will otherwise cause builds to fail after doing an initial build.

* bindings.java : pass structs by value to native code

This commit refactors the code to pass the structs by value to the
native code. This is done by creating a ByValue class for each struct
and using it in the Java code.

The motivation for this change is that without this application crashes
due to what I believe was memory mis-alignement. When the structs were
passed to the native code they would be att different memory locations.
Passing by value overcomes this issue and considering that the structs
hold parementers (context and full params) it might be alright do to
this. These changes allow all the tests to pass.

* bindings.java : fix javadoc warnings [no ci]

* bindings.java : fix libwhisper.dylib path in build.gradle [no ci]

This commit fixes the copyLibwhisperDynlib task in the build.gradle file
to copy the correct libwhisper.dylib file from build/src.

12 files changed:
bindings/java/build.gradle
bindings/java/gradlew [changed mode: 0644->0755]
bindings/java/src/main/java/io/github/ggerganov/whispercpp/WhisperConstants.java [new file with mode: 0644]
bindings/java/src/main/java/io/github/ggerganov/whispercpp/WhisperContext.java
bindings/java/src/main/java/io/github/ggerganov/whispercpp/WhisperCpp.java
bindings/java/src/main/java/io/github/ggerganov/whispercpp/WhisperCppJnaLibrary.java
bindings/java/src/main/java/io/github/ggerganov/whispercpp/callbacks/GgmlAbortCallback.java [new file with mode: 0644]
bindings/java/src/main/java/io/github/ggerganov/whispercpp/params/WhisperAhead.java [new file with mode: 0644]
bindings/java/src/main/java/io/github/ggerganov/whispercpp/params/WhisperAheads.java [new file with mode: 0644]
bindings/java/src/main/java/io/github/ggerganov/whispercpp/params/WhisperContextParams.java
bindings/java/src/main/java/io/github/ggerganov/whispercpp/params/WhisperFullParams.java
bindings/java/src/test/java/io/github/ggerganov/whispercpp/WhisperCppTest.java

index 75f3a9cd9018d684966bdf7b68cb6905935e3731..eb1a5c0759ea297a5625e69c413c304341121f07 100644 (file)
@@ -25,13 +25,13 @@ sourceSets {
 }\r
 \r
 tasks.register('copyLibwhisperDynlib', Copy) {\r
-    from '../../build'\r
-    include 'libwhisper.dynlib'\r
+    from '../../build/src'\r
+    include 'libwhisper.dylib'\r
     into 'build/generated/resources/main/darwin'\r
 }\r
 \r
 tasks.register('copyLibwhisperSo', Copy) {\r
-    from '../../build'\r
+    from '../../build/src'\r
     include 'libwhisper.so'\r
     into 'build/generated/resources/main/linux-x86-64'\r
 }\r
@@ -55,7 +55,12 @@ java {
     withJavadocJar()\r
 }\r
 \r
+sourcesJar() {\r
+    dependsOn copyLibs\r
+}\r
+\r
 jar {\r
+    dependsOn copyLibs\r
     exclude '**/whisper_java.exp', '**/whisper_java.lib'\r
 }\r
 \r
@@ -67,6 +72,9 @@ tasks.withType(Test) {
     useJUnitPlatform()\r
 }\r
 \r
+test.dependsOn copyLibs\r
+processResources.dependsOn copyLibs\r
+\r
 dependencies {\r
     implementation "net.java.dev.jna:jna:5.13.0"\r
     testImplementation "org.junit.jupiter:junit-jupiter:5.9.2"\r
old mode 100644 (file)
new mode 100755 (executable)
diff --git a/bindings/java/src/main/java/io/github/ggerganov/whispercpp/WhisperConstants.java b/bindings/java/src/main/java/io/github/ggerganov/whispercpp/WhisperConstants.java
new file mode 100644 (file)
index 0000000..0c828f1
--- /dev/null
@@ -0,0 +1,24 @@
+package io.github.ggerganov.whispercpp;
+
+/**
+ * Presets for alignment heads in DTW token timestamps
+ */
+public class WhisperConstants {
+    // Alignment heads presets
+    public static final int WHISPER_AHEADS_NONE = 0;
+    public static final int WHISPER_AHEADS_TINY_EN = 1;
+    public static final int WHISPER_AHEADS_TINY = 2;
+    public static final int WHISPER_AHEADS_BASE_EN = 3;
+    public static final int WHISPER_AHEADS_BASE = 4;
+    public static final int WHISPER_AHEADS_SMALL_EN = 5;
+    public static final int WHISPER_AHEADS_SMALL = 6;
+    public static final int WHISPER_AHEADS_MEDIUM_EN = 7;
+    public static final int WHISPER_AHEADS_MEDIUM = 8;
+    public static final int WHISPER_AHEADS_LARGE_V1 = 9;
+    public static final int WHISPER_AHEADS_LARGE_V2 = 10;
+    public static final int WHISPER_AHEADS_LARGE_V3 = 11;
+    public static final int WHISPER_AHEADS_LARGE_V3_TURBO = 12;
+    public static final int WHISPER_AHEADS_CUSTOM = 13;
+    public static final int WHISPER_AHEADS_N_TOP_MOST = 14;
+    public static final int WHISPER_AHEADS_COUNT = 15;
+}
index 0498eb4df817f96a06c1180b476a0c2637d500fa..7ac124edbe6573cc683b4a9375cd8d9ea84295a0 100644 (file)
@@ -1,7 +1,9 @@
 package io.github.ggerganov.whispercpp;\r
 \r
+import com.sun.jna.NativeLong;\r
 import com.sun.jna.Structure;\r
 import com.sun.jna.ptr.PointerByReference;\r
+import com.sun.jna.Pointer;\r
 import io.github.ggerganov.whispercpp.ggml.GgmlType;\r
 import io.github.ggerganov.whispercpp.WhisperModel;\r
 import io.github.ggerganov.whispercpp.params.WhisperContextParams;\r
@@ -9,33 +11,26 @@ import io.github.ggerganov.whispercpp.params.WhisperContextParams;
 import java.util.List;\r
 \r
 public class WhisperContext extends Structure {\r
-    int t_load_us = 0;\r
-    int t_start_us = 0;\r
+    public NativeLong t_load_us;\r
+    public NativeLong t_start_us;\r
 \r
     /** weight type (FP32 / FP16 / QX) */\r
-    GgmlType wtype = GgmlType.GGML_TYPE_F16;\r
+    public GgmlType wtype = GgmlType.GGML_TYPE_F16;\r
     /** intermediate type (FP32 or FP16) */\r
-    GgmlType itype = GgmlType.GGML_TYPE_F16;\r
+    public GgmlType itype = GgmlType.GGML_TYPE_F16;\r
 \r
-//    WhisperModel model;\r
-    public PointerByReference model;\r
-//    whisper_vocab vocab;\r
-//    whisper_state * state = nullptr;\r
-    public PointerByReference vocab;\r
-    public PointerByReference state;\r
+    public WhisperContextParams.ByValue params;\r
+\r
+    public Pointer model;\r
+    public Pointer vocab;\r
+    public Pointer state;\r
 \r
     /** populated by whisper_init_from_file_with_params() */\r
-    String path_model;\r
-    WhisperContextParams params;\r
-\r
-//    public static class ByReference extends WhisperContext implements Structure.ByReference {\r
-//    }\r
-//\r
-//    public static class ByValue extends WhisperContext implements Structure.ByValue {\r
-//    }\r
-//\r
-//    @Override\r
-//    protected List<String> getFieldOrder() {\r
-//        return List.of("t_load_us", "t_start_us", "wtype", "itype", "model", "vocab", "state", "path_model");\r
-//    }\r
+    public Pointer path_model;\r
+\r
+    @Override\r
+    protected List<String> getFieldOrder() {\r
+        return List.of("t_load_us", "t_start_us", "wtype", "itype",\r
+                "params", "model", "vocab", "state", "path_model");\r
+    }\r
 }\r
index 4c1594d5d5533cc1496e84ec7233a2bdc3387a80..621d8c636ca886b426cbe8bbf1f9f5da99245b52 100644 (file)
@@ -43,11 +43,11 @@ public class WhisperCpp implements AutoCloseable {
      * @param modelPath - absolute path, or just the name (eg: "base", "base-en" or "base.en")\r
      * @param params - params to use when initialising the context\r
      */\r
-    public void initContext(String modelPath, WhisperContextParams params) throws FileNotFoundException {\r
+    public void initContext(String modelPath, WhisperContextParams.ByValue params) throws FileNotFoundException {\r
         initContextImpl(modelPath, params);\r
     }\r
 \r
-    private void initContextImpl(String modelPath, WhisperContextParams params) throws FileNotFoundException {\r
+    private void initContextImpl(String modelPath, WhisperContextParams.ByValue params) throws FileNotFoundException {\r
         if (ctx != null) {\r
             lib.whisper_free(ctx);\r
         }\r
@@ -69,15 +69,13 @@ public class WhisperCpp implements AutoCloseable {
 \r
     /**\r
      * Provides default params which can be used with `whisper_init_from_file_with_params()` etc.\r
-     * Because this function allocates memory for the params, the caller must call either:\r
-     * - call `whisper_free_context_params()`\r
-     * - `Native.free(Pointer.nativeValue(pointer));`\r
+     * Returns a ByValue instance to ensure proper parameter passing to native code.\r
      */\r
-    public WhisperContextParams getContextDefaultParams() {\r
-        paramsPointer = lib.whisper_context_default_params_by_ref();\r
-        WhisperContextParams params = new WhisperContextParams(paramsPointer);\r
-        params.read();\r
-        return params;\r
+    public WhisperContextParams.ByValue getContextDefaultParams() {\r
+        WhisperContextParams.ByValue valueParams = new WhisperContextParams.ByValue(\r
+            lib.whisper_context_default_params_by_ref());\r
+        valueParams.read();\r
+        return valueParams;\r
     }\r
     \r
     /**\r
@@ -88,7 +86,7 @@ public class WhisperCpp implements AutoCloseable {
      *\r
      * @param strategy - GREEDY\r
      */\r
-    public WhisperFullParams getFullDefaultParams(WhisperSamplingStrategy strategy) {\r
+    public WhisperFullParams.ByValue getFullDefaultParams(WhisperSamplingStrategy strategy) {\r
         Pointer pointer;\r
 \r
         // whisper_full_default_params_by_ref allocates memory which we need to delete, so only create max 1 pointer for each strategy.\r
@@ -104,7 +102,7 @@ public class WhisperCpp implements AutoCloseable {
             pointer = beamParamsPointer;\r
         }\r
 \r
-        WhisperFullParams params = new WhisperFullParams(pointer);\r
+        WhisperFullParams.ByValue params = new WhisperFullParams.ByValue(pointer);\r
         params.read();\r
         return params;\r
     }\r
@@ -138,15 +136,21 @@ public class WhisperCpp implements AutoCloseable {
     }\r
 \r
     /**\r
-     * Run the entire model: PCM -> log mel spectrogram -> encoder -> decoder -> text.\r
+     * Run the entire model: PCM -&gt; log mel spectrogram -&gt; encoder -&gt; decoder -&gt; text.\r
      * Not thread safe for same context\r
      * Uses the specified decoding strategy to obtain the text.\r
      */\r
-    public String fullTranscribe(WhisperFullParams whisperParams, float[] audioData) throws IOException {\r
+    public String fullTranscribe(WhisperFullParams.ByValue whisperParams, float[] audioData) throws IOException {\r
         if (ctx == null) {\r
             throw new IllegalStateException("Model not initialised");\r
         }\r
 \r
+        /*\r
+        WhisperFullParams.ByValue valueParams = new WhisperFullParams.ByValue(\r
+            lib.whisper_full_default_params_by_ref(WhisperSamplingStrategy.WHISPER_SAMPLING_BEAM_SEARCH.ordinal()));\r
+        valueParams.read();\r
+        */\r
+\r
         if (lib.whisper_full(ctx, whisperParams, audioData, audioData.length) != 0) {\r
             throw new IOException("Failed to process audio");\r
         }\r
@@ -163,12 +167,17 @@ public class WhisperCpp implements AutoCloseable {
 \r
         return str.toString().trim();\r
     }\r
+\r
     public List<WhisperSegment> fullTranscribeWithTime(WhisperFullParams whisperParams, float[] audioData) throws IOException {\r
         if (ctx == null) {\r
             throw new IllegalStateException("Model not initialised");\r
         }\r
 \r
-        if (lib.whisper_full(ctx, whisperParams, audioData, audioData.length) != 0) {\r
+        WhisperFullParams.ByValue valueParams = new WhisperFullParams.ByValue(\r
+            lib.whisper_full_default_params_by_ref(WhisperSamplingStrategy.WHISPER_SAMPLING_BEAM_SEARCH.ordinal()));\r
+        valueParams.read();\r
+\r
+        if (lib.whisper_full(ctx, valueParams, audioData, audioData.length) != 0) {\r
             throw new IOException("Failed to process audio");\r
         }\r
 \r
index 1a73cee1181c40c289b2edfd8076cc1931293f8b..1cd2449f5341f2fa045d126776f4c10b65938f18 100644 (file)
@@ -38,7 +38,7 @@ public interface WhisperCppJnaLibrary extends Library {
      * @param params     Pointer to whisper_context_params\r
      * @return Whisper context on success, null on failure\r
      */\r
-    Pointer whisper_init_from_file_with_params(String path_model, WhisperContextParams params);\r
+    Pointer whisper_init_from_file_with_params(String path_model, WhisperContextParams.ByValue params);\r
 \r
     /**\r
      * Allocate (almost) all memory needed for the model by loading from a buffer.\r
@@ -180,12 +180,12 @@ public interface WhisperCppJnaLibrary extends Library {
     /**\r
      * @return the id of the specified language, returns -1 if not found.\r
      * Examples:\r
-     *   "de" -> 2\r
-     *   "german" -> 2\r
+     *   "de" -&gt; 2\r
+     *   "german" -&gt; 2\r
      */\r
     int whisper_lang_id(String lang);\r
 \r
-    /** @return the short string of the specified language id (e.g. 2 -> "de"), returns nullptr if not found */\r
+    /** @return the short string of the specified language id (e.g. 2 -&gt; "de"), returns nullptr if not found */\r
     String whisper_lang_str(int id);\r
 \r
     /**\r
@@ -268,20 +268,21 @@ public interface WhisperCppJnaLibrary extends Library {
     void whisper_free_params(Pointer params);\r
 \r
     /**\r
-     * Run the entire model: PCM -> log mel spectrogram -> encoder -> decoder -> text\r
+     * Run the entire model: PCM -&gt; log mel spectrogram -&gt; encoder -&gt; decoder -&gt; text\r
      * Not thread safe for same context\r
      * Uses the specified decoding strategy to obtain the text.\r
      */\r
-    int whisper_full(Pointer ctx, WhisperFullParams params, final float[] samples, int n_samples);\r
+    int whisper_full(Pointer ctx, WhisperFullParams.ByValue params, final float[] samples, int n_samples);\r
 \r
-    int whisper_full_with_state(Pointer ctx, Pointer state, WhisperFullParams params, final float[] samples, int n_samples);\r
+    public int whisper_full_with_state(Pointer ctx, Pointer state, WhisperFullParams.ByValue params, float[] samples, int n_samples);\r
+    //int whisper_full_with_state(Pointer ctx, Pointer state, WhisperFullParams params, final float[] samples, int n_samples);\r
 \r
     // Split the input audio in chunks and process each chunk separately using whisper_full_with_state()\r
     // Result is stored in the default state of the context\r
     // Not thread safe if executed in parallel on the same context.\r
     // It seems this approach can offer some speedup in some cases.\r
     // However, the transcription accuracy can be worse at the beginning and end of each chunk.\r
-    int whisper_full_parallel(Pointer ctx, WhisperFullParams params, final float[] samples, int n_samples, int n_processors);\r
+    int whisper_full_parallel(Pointer ctx, WhisperFullParams.ByValue params, final float[] samples, int n_samples, int n_processors);\r
 \r
     /**\r
      * Number of generated text segments.\r
diff --git a/bindings/java/src/main/java/io/github/ggerganov/whispercpp/callbacks/GgmlAbortCallback.java b/bindings/java/src/main/java/io/github/ggerganov/whispercpp/callbacks/GgmlAbortCallback.java
new file mode 100644 (file)
index 0000000..244e419
--- /dev/null
@@ -0,0 +1,17 @@
+package io.github.ggerganov.whispercpp.callbacks;
+
+import com.sun.jna.Callback;
+
+/**
+ * Callback for aborting GGML computation
+ * Maps to the C typedef: bool (*ggml_abort_callback)(void * data)
+ */
+public interface GgmlAbortCallback extends Callback {
+    /**
+     * Return true to abort the computation, false to continue
+     *
+     * @param data User data passed to the callback
+     * @return true to abort, false to continue
+     */
+    boolean invoke(com.sun.jna.Pointer data);
+}
diff --git a/bindings/java/src/main/java/io/github/ggerganov/whispercpp/params/WhisperAhead.java b/bindings/java/src/main/java/io/github/ggerganov/whispercpp/params/WhisperAhead.java
new file mode 100644 (file)
index 0000000..39691dc
--- /dev/null
@@ -0,0 +1,30 @@
+package io.github.ggerganov.whispercpp.params;
+import com.sun.jna.*;
+import java.util.Arrays;
+import java.util.List;
+
+public class WhisperAhead extends Structure {
+
+    public int n_text_layer;
+
+    public int n_head;
+
+    public WhisperAhead() {
+        super();
+    }
+
+    public WhisperAhead(int textLayer, int head) {
+        super();
+        this.n_text_layer = textLayer;
+        this.n_head = head;
+    }
+
+    @Override
+    protected List<String> getFieldOrder() {
+        return Arrays.asList("n_text_layer", "n_head");
+    }
+
+    public static class ByReference extends WhisperAhead implements Structure.ByReference {}
+
+    public static class ByValue extends WhisperAhead implements Structure.ByValue {}
+}
diff --git a/bindings/java/src/main/java/io/github/ggerganov/whispercpp/params/WhisperAheads.java b/bindings/java/src/main/java/io/github/ggerganov/whispercpp/params/WhisperAheads.java
new file mode 100644 (file)
index 0000000..bca5eb0
--- /dev/null
@@ -0,0 +1,41 @@
+package io.github.ggerganov.whispercpp.params;
+import com.sun.jna.*;
+import java.util.Arrays;
+import java.util.List;
+
+public class WhisperAheads extends Structure {
+    public NativeLong n_heads;
+
+    public Pointer heads;
+
+    public WhisperAheads() {
+        super();
+    }
+
+    /**
+     * Create alignment heads from an array of WhisperAhead objects
+     */
+    public void setHeads(WhisperAhead[] aheadsArray) {
+        this.n_heads = new NativeLong(aheadsArray.length);
+
+        int structSize = aheadsArray[0].size();
+        Memory mem = new Memory(structSize * aheadsArray.length);
+
+        for (int i = 0; i < aheadsArray.length; i++) {
+            aheadsArray[i].write();
+            byte[] buffer = aheadsArray[i].getPointer().getByteArray(0, structSize);
+            mem.write(i * structSize, buffer, 0, buffer.length);
+        }
+
+        this.heads = mem;
+    }
+
+    @Override
+    protected List<String> getFieldOrder() {
+        return Arrays.asList("n_heads", "heads");
+    }
+
+    public static class ByReference extends WhisperAheads implements Structure.ByReference {}
+
+    public static class ByValue extends WhisperAheads implements Structure.ByValue {}
+}
index cf98d2c3757eb6fff1712ac239f118269c1148bf..4bcdb6b0401864ac0c914d79d0f67542118515f1 100644 (file)
@@ -1,7 +1,5 @@
 package io.github.ggerganov.whispercpp.params;
-
 import com.sun.jna.*;
-
 import java.util.Arrays;
 import java.util.List;
 
@@ -11,21 +9,73 @@ import java.util.List;
  * whisper_context_default_params()
  */
 public class WhisperContextParams extends Structure {
-
     public WhisperContextParams(Pointer p) {
         super(p);
     }
 
-    /** Use GPU for inference Number (default = true) */
+    public WhisperContextParams() {
+        super();
+    }
+
+    /** Use GPU for inference (default = true) */
     public CBool use_gpu;
 
-    /** Use GPU for inference Number (default = true) */
+    /** Use flash attention (default = false) */
+    public CBool flash_attn;
+
+    /** CUDA device to use (default = 0) */
+    public int gpu_device;
+
+    /** [EXPERIMENTAL] Enable token-level timestamps with DTW (default = false) */
+    public CBool dtw_token_timestamps;
+
+    /** [EXPERIMENTAL] Alignment heads preset for DTW */
+    public int dtw_aheads_preset;
+
+    /** Number of top layers to use for DTW when using WHISPER_AHEADS_N_TOP_MOST preset */
+    public int dtw_n_top;
+
+    public WhisperAheads.ByValue dtw_aheads;
+
+    /** DTW memory size (internal use) */
+    public NativeLong dtw_mem_size;
+
+    /** Use GPU for inference */
     public void useGpu(boolean enable) {
         use_gpu = enable ? CBool.TRUE : CBool.FALSE;
     }
 
+    /** Use flash attention */
+    public void useFlashAttn(boolean enable) {
+        flash_attn = enable ? CBool.TRUE : CBool.FALSE;
+    }
+
+    /** Enable DTW token-level timestamps */
+    public void enableDtwTokenTimestamps(boolean enable) {
+        dtw_token_timestamps = enable ? CBool.TRUE : CBool.FALSE;
+    }
+
+    /** Set DTW alignment heads preset */
+    public void setDtwAheadsPreset(int preset) {
+        dtw_aheads_preset = preset;
+    }
+
     @Override
     protected List<String> getFieldOrder() {
-        return Arrays.asList("use_gpu");
+        return Arrays.asList(
+            "use_gpu",
+            "flash_attn",
+            "gpu_device",
+            "dtw_token_timestamps",
+            "dtw_aheads_preset",
+            "dtw_n_top",
+            "dtw_aheads",
+            "dtw_mem_size"
+        );
+    }
+
+    public static class ByValue extends WhisperContextParams implements Structure.ByValue {
+        public ByValue() { super(); }
+        public ByValue(Pointer p) { super(p); }
     }
 }
index 18c209fc83cf8d37c9831ccf2a314908ad99040e..498ff126037946d4c43cad33a71b997bbffa3a87 100644 (file)
@@ -5,6 +5,7 @@ import io.github.ggerganov.whispercpp.callbacks.WhisperEncoderBeginCallback;
 import io.github.ggerganov.whispercpp.callbacks.WhisperLogitsFilterCallback;\r
 import io.github.ggerganov.whispercpp.callbacks.WhisperNewSegmentCallback;\r
 import io.github.ggerganov.whispercpp.callbacks.WhisperProgressCallback;\r
+import io.github.ggerganov.whispercpp.callbacks.GgmlAbortCallback;\r
 \r
 import java.util.Arrays;\r
 import java.util.List;\r
@@ -16,10 +17,12 @@ import java.util.List;
  */\r
 public class WhisperFullParams extends Structure {\r
 \r
+    public WhisperFullParams() {\r
+        super();\r
+    }\r
+\r
     public WhisperFullParams(Pointer p) {\r
         super(p);\r
-//        super(p, ALIGN_MSVC);\r
-//        super(p, ALIGN_GNUC);\r
     }\r
 \r
     /** Sampling strategy for whisper_full() function. */\r
@@ -69,10 +72,10 @@ public class WhisperFullParams extends Structure {
         single_segment = single ? CBool.TRUE : CBool.FALSE;\r
     }\r
 \r
-    /** Flag to print special tokens (e.g., &lt;SOT>, &lt;EOT>, &lt;BEG>, etc.). (default = false) */\r
+    /** Flag to print special tokens (e.g., &lt;SOT&gt;, &lt;EOT&gt;, &lt;BEG&gt;, etc.). (default = false) */\r
     public CBool print_special;\r
 \r
-    /** Flag to print special tokens (e.g., &lt;SOT>, &lt;EOT>, &lt;BEG>, etc.). (default = false) */\r
+    /** Flag to print special tokens (e.g., &lt;SOT&gt;, &lt;EOT&gt;, &lt;BEG&gt;, etc.). (default = false) */\r
     public void printSpecial(boolean enable) {\r
         print_special = enable ? CBool.TRUE : CBool.FALSE;\r
     }\r
@@ -129,6 +132,14 @@ public class WhisperFullParams extends Structure {
     /** Maximum tokens per segment (0, default = no limit) */\r
     public int max_tokens;\r
 \r
+    /** [EXPERIMENTAL] Enable debug mode for extra info */\r
+    public CBool debug_mode;\r
+\r
+    /** Enable debug mode */\r
+    public void enableDebugMode(boolean enable) {\r
+        debug_mode = enable ? CBool.TRUE : CBool.FALSE;\r
+    }\r
+\r
     /** Overwrite the audio context size (0 = use default). */\r
     public int audio_ctx;\r
 \r
@@ -274,6 +285,16 @@ public class WhisperFullParams extends Structure {
      */\r
     public Pointer encoder_begin_callback_user_data;\r
 \r
+    /** Callback used to abort GGML computation */\r
+    public Pointer abort_callback;\r
+\r
+    /** User data for the abort_callback */\r
+    public Pointer abort_callback_user_data;\r
+\r
+    public void setAbortCallback(GgmlAbortCallback callback) {\r
+        abort_callback = CallbackReference.getFunctionPointer(callback);\r
+    }\r
+\r
     /**\r
      * Callback by each decoder to filter obtained logits.\r
      * WhisperLogitsFilterCallback\r
@@ -310,17 +331,28 @@ public class WhisperFullParams extends Structure {
 \r
     @Override\r
     protected List<String> getFieldOrder() {\r
-        return Arrays.asList("strategy", "n_threads", "n_max_text_ctx", "offset_ms", "duration_ms", "translate",\r
-                "no_context", "single_segment", "no_timestamps",\r
-                "print_special", "print_progress", "print_realtime", "print_timestamps",  "token_timestamps",\r
-                "thold_pt", "thold_ptsum", "max_len", "split_on_word", "max_tokens", "audio_ctx",\r
-                "tdrz_enable", "suppress_regex", "initial_prompt", "prompt_tokens", "prompt_n_tokens", "language", "detect_language",\r
-                "suppress_blank", "suppress_nst", "temperature", "max_initial_ts", "length_penalty",\r
-                "temperature_inc", "entropy_thold", "logprob_thold", "no_speech_thold", "greedy", "beam_search",\r
-                "new_segment_callback", "new_segment_callback_user_data",\r
+        return Arrays.asList("strategy", "n_threads", "n_max_text_ctx",\r
+                "offset_ms", "duration_ms", "translate", "no_context",\r
+                "no_timestamps", "single_segment", "print_special",\r
+                "print_progress", "print_realtime", "print_timestamps",\r
+                "token_timestamps", "thold_pt", "thold_ptsum", "max_len",\r
+                "split_on_word", "max_tokens", "debug_mode", "audio_ctx", \r
+                "tdrz_enable", "suppress_regex", "initial_prompt",\r
+                "prompt_tokens", "prompt_n_tokens", "language", "detect_language",\r
+                "suppress_blank", "suppress_nst", "temperature",\r
+                "max_initial_ts", "length_penalty", "temperature_inc",\r
+                "entropy_thold", "logprob_thold", "no_speech_thold", "greedy",\r
+                "beam_search", "new_segment_callback", "new_segment_callback_user_data",\r
                 "progress_callback", "progress_callback_user_data",\r
                 "encoder_begin_callback", "encoder_begin_callback_user_data",\r
+                "abort_callback", "abort_callback_user_data",\r
                 "logits_filter_callback", "logits_filter_callback_user_data",\r
                 "grammar_rules", "n_grammar_rules", "i_start_rule", "grammar_penalty");\r
     }\r
+\r
+    public static class ByValue extends WhisperFullParams implements Structure.ByValue {\r
+        public ByValue() { super(); }\r
+        public ByValue(Pointer p) { super(p); }\r
+    }\r
+\r
 }\r
index 034726ad29c972d907f3fe6f7a5c9ece42cf1a6f..9d63fff34e6ae5865167b85e3d87753879e4a1d5 100644 (file)
@@ -76,7 +76,7 @@ class WhisperCppTest {
         float[] floats = new float[b.length / 2];\r
 \r
         //WhisperFullParams params = whisper.getFullDefaultParams(WhisperSamplingStrategy.WHISPER_SAMPLING_GREEDY);\r
-        WhisperFullParams params = whisper.getFullDefaultParams(WhisperSamplingStrategy.WHISPER_SAMPLING_BEAM_SEARCH);\r
+        WhisperFullParams.ByValue params = whisper.getFullDefaultParams(WhisperSamplingStrategy.WHISPER_SAMPLING_BEAM_SEARCH);\r
         params.setProgressCallback((ctx, state, progress, user_data) -> System.out.println("progress: " + progress));\r
         params.print_progress = CBool.FALSE;\r
         //params.initial_prompt = "and so my fellow Americans um, like";\r