Package io.github.givimad.whisperjni
Class WhisperJNI
java.lang.Object
io.github.givimad.whisperjni.WhisperJNI
The
WhisperJNI class allows to use whisper.cpp thought the JNI.- Author:
- Miguel Álvarez Díez - Initial contribution
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe classWhisperJNI.LibraryLoggerallows to proxy the whisper.cpp logger.static classThe classWhisperJNI.LoadOptionsallows to customize the load of the required shared libraries.protected static classIn order to avoid sharing pointers between the c++ and java, we use this util base class which holds a random integer id generated in the whisper.cpp wrapper. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidfree(WhisperContext context) Release context memory in native implementation.voidfree(WhisperGrammar grammar) Release grammar memory in native implementation.voidfree(WhisperState state) Release state memory in native implementation.intfull(WhisperContext context, WhisperFullParams params, float[] samples, int numSamples) Run whisper.cpp full audio transcription.fullGetSegmentText(WhisperContext context, int index) Gets text segment by index.fullGetSegmentTextFromState(WhisperState state, int index) Gets text segment by index.longfullGetSegmentTimestamp0(WhisperContext context, int index) Gets start timestamp of text segment by index.longfullGetSegmentTimestamp0FromState(WhisperState state, int index) Gets start timestamp of text segment by index.longfullGetSegmentTimestamp1(WhisperContext context, int index) Gets end timestamp of text segment by index.longfullGetSegmentTimestamp1FromState(WhisperState state, int index) Gets end timestamp of text segment by index.intfullNSegments(WhisperContext context) Gets the available number of text segments.intGets the available number of text segments.intfullWithState(WhisperContext context, WhisperState state, WhisperFullParams params, float[] samples, int numSamples) Run whisper.cpp full audio transcription.Get whisper.cpp system info stream, to check enabled features in whisper.Creates a new whisper context.init(Path model, WhisperContextParams params) Creates a new whisper context.initNoState(Path model) Creates a new whisper context without state.initNoState(Path model, WhisperContextParams params) Creates a new whisper context without state.voidinitOpenVINO(WhisperContext context, String device) Initializes OpenVino encoder.initState(WhisperContext context) Creates a new whisper.cpp state for the provided context.booleanisMultilingual(WhisperContext context) Is multilingual.static voidRegister the native library, should be called at first.static voidloadLibrary(WhisperJNI.LoadOptions options) Register the native library, should be called at first.protected static voidCalled from the cpp side of the library to proxy the whisper.cpp logs.parseGrammar(String text) parseGrammar(Path grammarPath) static voidProxy whisper.cpp logger.
-
Constructor Details
-
WhisperJNI
public WhisperJNI()
-
-
Method Details
-
init
Creates a new whisper context.- Parameters:
model-Pathto the whisper ggml model file.- Returns:
- A new
WhisperContext. - Throws:
IOException- if model file is missing.
-
init
Creates a new whisper context.- Parameters:
model-Pathto the whisper ggml model file.params-WhisperContextParamsparams for context initialization.- Returns:
- A new
WhisperContext. - Throws:
IOException- if model file is missing.
-
initNoState
Creates a new whisper context without state.- Parameters:
model-Pathto the whisper ggml model file.- Returns:
- A new
WhisperContextwithout state. - Throws:
IOException- if model file is missing.
-
initNoState
Creates a new whisper context without state.- Parameters:
model-Pathto the whisper ggml model file.params-WhisperContextParamsparams for context initialization.- Returns:
- A new
WhisperContextwithout state. - Throws:
IOException- if model file is missing.
-
initState
Creates a new whisper.cpp state for the provided context.- Parameters:
context- theWhisperContextof this state.- Returns:
- A new
WhisperContext.
-
parseGrammar
- Throws:
IOException
-
parseGrammar
- Throws:
IOException
-
initOpenVINO
Initializes OpenVino encoder.- Parameters:
context- aWhisperContextinstance.device- the device name.
-
isMultilingual
Is multilingual.- Parameters:
context- theWhisperContextto check.- Returns:
- true if model support multiple languages
-
full
Run whisper.cpp full audio transcription.- Parameters:
context- theWhisperContextused to transcribe.params- aWhisperFullParamsinstance with the desired configuration.samples- the audio samples (f32 encoded samples with sample rate 16000).numSamples- the number of audio samples provided.- Returns:
- a result code, values other than 0 indicates problems.
-
fullWithState
public int fullWithState(WhisperContext context, WhisperState state, WhisperFullParams params, float[] samples, int numSamples) Run whisper.cpp full audio transcription.- Parameters:
context- theWhisperContextused to transcribe.state- theWhisperStateused to transcribe.params- aWhisperFullParamsinstance with the desired configuration.samples- the audio samples (f32 encoded samples with sample rate 16000).numSamples- the number of audio samples provided.- Returns:
- a result code, values other than 0 indicates problems.
-
fullNSegmentsFromState
Gets the available number of text segments.- Parameters:
state- theWhisperStateused to transcribe- Returns:
- available number of segments
-
fullNSegments
Gets the available number of text segments.- Parameters:
context- theWhisperContextused to transcribe- Returns:
- available number of segments
-
fullGetSegmentTimestamp0
Gets start timestamp of text segment by index.- Parameters:
context- aWhisperContextused to transcribeindex- the segment index- Returns:
- start timestamp of segment text, 800 -> 8s
-
fullGetSegmentTimestamp1
Gets end timestamp of text segment by index.- Parameters:
context- aWhisperContextused to transcribeindex- the segment index- Returns:
- end timestamp of segment text, 1050 -> 10.5s
-
fullGetSegmentText
Gets text segment by index.- Parameters:
context- aWhisperContextused to transcribeindex- the segment index- Returns:
- the segment text
-
fullGetSegmentTimestamp0FromState
Gets start timestamp of text segment by index.- Parameters:
state- aWhisperStateused to transcribeindex- the segment index- Returns:
- start timestamp of segment text, 1050 -> 10.5s
-
fullGetSegmentTimestamp1FromState
Gets end timestamp of text segment by index.- Parameters:
state- aWhisperStateused to transcribeindex- the segment index- Returns:
- end timestamp of segment text, 1050 -> 10.5s
-
fullGetSegmentTextFromState
Gets text segment by index.- Parameters:
state- aWhisperStateused to transcribeindex- the segment index- Returns:
- the segment text
-
free
Release context memory in native implementation.- Parameters:
context- theWhisperContextto release
-
free
Release state memory in native implementation.- Parameters:
state- theWhisperStateto release
-
free
Release grammar memory in native implementation.- Parameters:
grammar- theWhisperGrammarto release
-
getSystemInfo
Get whisper.cpp system info stream, to check enabled features in whisper.- Returns:
- the whisper.cpp system info stream.
-
loadLibrary
Register the native library, should be called at first.- Throws:
IOException- when unable to load the native library
-
loadLibrary
Register the native library, should be called at first.- Parameters:
options- instance ofWhisperJNI.LoadOptionsto customize library load.- Throws:
IOException- when unable to load the native library.
-
setLibraryLogger
Proxy whisper.cpp logger. Should be called afterloadLibrary().- Parameters:
logger- whisper.cpp log consumer, or null to disable the library default log to stderr.
-
log
Called from the cpp side of the library to proxy the whisper.cpp logs.- Parameters:
text- whisper.cpp log line.
-