Daniel Bevenius 0b43a02be8
bindings.java : enable copyLibs task [no ci] (#2949)
* 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.
2025-03-26 15:01:28 +01:00
..
2023-05-20 18:25:02 +03:00
2024-12-17 19:05:35 +02:00

Java JNI bindings for Whisper

This package provides Java JNI bindings for whisper.cpp. They have been tested on:

  • Darwin (OS X) 12.6 on x64_64
  • Ubuntu on x86_64
  • Windows on x86_64

The "low level" bindings are in WhisperCppJnaLibrary. The most simple usage is as follows:

JNA will attempt to load the whispercpp shared library from:

  • jna.library.path
  • jna.platform.library
  • ~/Library/Frameworks
  • /Library/Frameworks
  • /System/Library/Frameworks
  • classpath
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());
            }
        } finally {
             whisper.freeContext(context);
        }
     }
}

Building & Testing

In order to build, you need to have the JDK 8 or higher installed. Run the tests with:

git clone https://github.com/ggerganov/whisper.cpp.git
cd whisper.cpp/bindings/java

./gradlew build

You need to have the whisper library in your JNA library path. On Windows the dll is included in the jar and you can update it:

copy /y ..\..\build\bin\Release\whisper.dll build\generated\resources\main\win32-x86-64\whisper.dll

License

The license for the Java bindings is the same as the license for the rest of the whisper.cpp project, which is the MIT License. See the LICENSE file for more details.