]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
bindings.java : update java example (#3281)
authorJoas Dev <redacted>
Wed, 25 Jun 2025 04:35:38 +0000 (23:35 -0500)
committerGitHub <redacted>
Wed, 25 Jun 2025 04:35:38 +0000 (06:35 +0200)
This commit updates the example in the README.md file as the current Java example code is not working.

Resolves: https://github.com/ggml-org/whisper.cpp/issues/2860

bindings/java/README.md

index 2be84890e0ebaf305d4b3bf48a2dd61abc1f1e1e..904269972377e0f313cabe84adfdb77c9392db6e 100644 (file)
@@ -23,26 +23,42 @@ import io.github.ggerganov.whispercpp.WhisperCpp;
 public class Example {
 
     public static void main(String[] args) {
+        
         WhisperCpp whisper = new WhisperCpp();
-        // By default, models are loaded from ~/.cache/whisper/ and are usually named "ggml-${name}.bin"
-        // or you can provide the absolute path to the model file.
-        long context = whisper.initContext("base.en");
         try {
-            var whisperParams = whisper.getFullDefaultParams(WhisperSamplingStrategy.WHISPER_SAMPLING_GREEDY);
-            // custom configuration if required
-            whisperParams.temperature_inc = 0f;
-
-            var samples = readAudio(); // divide each value by 32767.0f
-            whisper.fullTranscribe(whisperParams, samples);
-
-            int segmentCount = whisper.getTextSegmentCount(context);
-            for (int i = 0; i < segmentCount; i++) {
-                String text = whisper.getTextSegment(context, i);
-                System.out.println(segment.getText());
+            // By default, models are loaded from ~/.cache/whisper/ and are usually named "ggml-${name}.bin"
+            // or you can provide the absolute path to the model file.
+            whisper.initContext("../ggml-base.en.bin"); 
+            WhisperFullParams.ByValue whisperParams = whisper.getFullDefaultParams(WhisperSamplingStrategy.WHISPER_SAMPLING_BEAM_SEARCH); 
+            
+            // custom configuration if required      
+            //whisperParams.n_threads = 8;
+            whisperParams.temperature = 0.0f;
+            whisperParams.temperature_inc = 0.2f;
+            //whisperParams.language = "en";
+                            
+            float[] samples = readAudio(); // divide each value by 32767.0f
+            List<WhisperSegment> whisperSegmentList = whisper.fullTranscribeWithTime(whisperParams, samples);
+            
+            for (WhisperSegment whisperSegment : whisperSegmentList) {
+
+                long start = whisperSegment.getStart();
+                long end = whisperSegment.getEnd();
+
+                String text = whisperSegment.getSentence();
+                    
+                System.out.println("start: "+start);
+                System.out.println("end: "+end);
+                System.out.println("text: "+text);
+                
             }
+    
+        } catch (IOException e) {
+            e.printStackTrace();
         } finally {
-             whisper.freeContext(context);
+            whisper.close();
         }
+        
      }
 }
 ```