Jwhisper is a lightweight Java API for interacting with the whisper.cpp project, providing easy access to Whisper's speech recognition capabilities in Java applications.
The Java API is currently in development and may undergo changes. For production use, consider the following stable alternatives:
The project uses the following Maven profiles:
- JNI: Generates JNI header files
- CMAKE: Builds the
whisper.cpp
library andlibjwhisper.so
- LINUX_AMD64: Copies
libjwhisper.so
to the resource folder
To build the project, run the following commands:
mvn clean
mvn compile -P jni
mvn compile -P cmake,linux_amd64
Here's a simple example of how to use Jwhisper:
import java.nio.file.Paths;
import javax.sound.sampled.AudioSystem;
import java.io.File;
public class JwhisperExample {
public static void main(String[] args) {
try (Jwhisper jwhisper = Jwhisper.newInstance(Paths.get(System.getenv("GGML_MODELS"), "ggml-tiny.bin"))) {
// Load audio file
String wavFile = ResourceLoaderUtils.getResource(JwhisperTest.class, "samples/jfk.wav");
var wavInputStream = AudioSystem.getAudioInputStream(new File(wavFile));
float[] samples = AudioUtils.convertPcmS16MonoToPcmF32Mono(wavInputStream);
// Perform speech recognition
int segmentsSize = jwhisper.whisperFull(samples);
// Get and print the recognized text
String recognizedText = jwhisper.whisperFullGetSegmentText(0).trim();
System.out.println("Recognized text: " + recognizedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- Java Development Kit (JDK) 11 or later
- Maven
- CMake (for building native libraries)
- A C++ compiler compatible with C++11
The following environment variables need to be set:
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
export GGML_MODELS="path/to/the/ggml/model.bin"
# For SonarQube validation (optional):
export SONAR_PROJECT_KEY="project_key"
export SONAR_PROJECT_NAME="project_name"
export SONAR_HOST_URL="host_url"
export SONAR_TOKEN="token"
Distributed under the MIT License. See LICENSE file for more information.
Special thanks to the whisper.cpp contributors for their excellent work.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (git checkout -b feature/AmazingFeature)
- Commit your changes (git commit -m 'Add some AmazingFeature')
- Push to the branch (git push origin feature/AmazingFeature)
- Open a Pull Request
If you have any questions or suggestions, please open an issue in this repository.