# whipser-cpp-java
**Repository Path**: ppnt/whipser-cpp-java
## Basic Information
- **Project Name**: whipser-cpp-java
- **Description**: Java JNI Bindings for Whisper
- **Primary Language**: Java
- **License**: MIT
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2023-11-01
- **Last Updated**: 2023-11-01
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Java JNI Bindings for Whisper
此包为 `whisper.cpp` 提供了 Java JNI 绑定。已在以下平台上成功测试:
- ~~Darwin (OS X) 12.6 on x64_64~~
- Ubuntu on x86_64
- Windows on x86_64
主要的“低级”绑定位于 `WhisperCppJnaLibrary`。其基本用法如下:
JNA 将尝试从以下路径加载 `whispercpp` 共享库:
- `jna.library.path`
- `jna.platform.library`
- `~/Library/Frameworks`
- `/Library/Frameworks`
- `/System/Library/Frameworks`
- classpath
## 导入
maven
```
com.litongjava
whisper-cpp-java
1.0.0
```
gradle
```
implementation "com.litongjava:whisper-cpp-java:1.0.0"
```
## 测试
### 加载模型
1. 下载 `whisper` 库或编译它以生成所需的库文件。来源:[Whisper GitHub 仓库](https://github.com/ggerganov/whisper.cpp)
2. 将 `whisper` 库放在适当的 [JNA 库路径](https://java-native-access.github.io/jna/4.2.1/com/sun/jna/NativeLibrary.html) 中。注意:对于 Windows 用户,`.dll` 文件已经包含在 `.jar` 中,但如果需要可以更新:
```bash
src\main\resources\win32-x86-64\whisper.dll
```
### 样本测试
```java
package com.litongjava.whipser.cpp.java;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class WhisperJnaLibraryTest {
@Test
void testWhisperPrint_system_info() {
String systemInfo = WhisperCppJnaLibrary.instance.whisper_print_system_info();
// eg: "AVX = 1 | AVX2 = 1 | AVX512 = 0 | FMA = 1 | NEON = 0 | ARM_FMA = 0 | F16C = 1 | FP16_VA = 0
// | WASM_SIMD = 0 | BLAS = 0 | SSE3 = 1 | VSX = 0 | COREML = 0 | "
System.out.println("System info: " + systemInfo);
assertTrue(systemInfo.length() > 10);
}
}
```
### 使用模型
1. 从以下位置下载模型:
- [Hugging Face 模型](https://huggingface.co/ggerganov/whisper.cpp)
- [GGML 模型](https://ggml.ggerganov.com)
2. 将模型存储在 `~/.cache/whisper/` 中。默认情况下,模型从此位置获取,通常命名为 "ggml-${name}.bin"。
3. 下载样本文件,如 [jfk.wav](https://github.com/ggerganov/whisper.cpp/blob/master/samples/jfk.wav),并将其放在 `samples` 文件夹中。
```java
package com.litongjava.whipser.cpp.java;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import com.litongjava.whipser.cpp.java.bean.WhisperSegment;
import com.litongjava.whipser.cpp.java.params.CBool;
import com.litongjava.whipser.cpp.java.params.WhisperFullParams;
import com.litongjava.whipser.cpp.java.params.WhisperSamplingStrategy;
public class WhisperCppDemo {
private static WhisperCpp whisper = new WhisperCpp();
private static boolean modelInitialised = false;
public static void main(String[] args) {
// 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.
String modelName = "base.en";
try {
whisper.initContext(modelName);
modelInitialised = true;
} catch (FileNotFoundException ex) {
System.out.println("Model " + modelName + " not found");
}
if (!modelInitialised) {
System.out.println("Model not initialised, skipping test");
return;
}
WhisperFullParams params = whisper.getFullDefaultParams(WhisperSamplingStrategy.WHISPER_SAMPLING_BEAM_SEARCH);
params.setProgressCallback((ctx, state, progress, user_data) -> System.out.println("progress: " + progress));
params.print_progress = CBool.FALSE;
// Given
File file = new File(System.getProperty("user.dir"), "/samples/jfk.wav");
try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);) {
byte[] b = new byte[audioInputStream.available()];
float[] floats = new float[b.length / 2];
audioInputStream.read(b);
for (int i = 0, j = 0; i < b.length; i += 2, j++) {
int intSample = (int) (b[i + 1]) << 8 | (int) (b[i]) & 0xFF;
floats[j] = intSample / 32767.0f;
}
List segments = whisper.fullTranscribeWithTime(params, floats);
System.out.println(segments.size());
for (WhisperSegment segment : segments) {
System.out.println(segment);
}
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
}
```
**输出:**
```
[0 --> 1100]: 所以,我亲爱的美国同胞们,不要问你的国家能为你做些什么,而要问你能为你的国家做些什么。
```
注意:这里的 1100 表示毫秒除以 10。
## 构建
要构建,请确保已安装 JDK 8 或更高版本。您可以使用以下命令运行测试:
```bash
git clone https://github.com/litongjava/whisper-cpp-java.git
cd whisper-cpp-java
mvn install -DskipTests -Dgpg.skip
```
## 许可证
这些 Java 绑定的许可证与整个 `whisper.cpp` 项目的许可证相匹配,该项目采用 MIT 许可证。请参考 [`LICENSE`](https://github.com/ggerganov/whisper.cpp/blob/master/LICENSE) 文件以获得详细信息。