@@ -43,11 +43,11 @@ public void initContext(String modelPath) throws FileNotFoundException {
4343 * @param modelPath - absolute path, or just the name (eg: "base", "base-en" or "base.en")
4444 * @param params - params to use when initialising the context
4545 */
46- public void initContext (String modelPath , WhisperContextParams params ) throws FileNotFoundException {
46+ public void initContext (String modelPath , WhisperContextParams . ByValue params ) throws FileNotFoundException {
4747 initContextImpl (modelPath , params );
4848 }
4949
50- private void initContextImpl (String modelPath , WhisperContextParams params ) throws FileNotFoundException {
50+ private void initContextImpl (String modelPath , WhisperContextParams . ByValue params ) throws FileNotFoundException {
5151 if (ctx != null ) {
5252 lib .whisper_free (ctx );
5353 }
@@ -69,15 +69,13 @@ private void initContextImpl(String modelPath, WhisperContextParams params) thro
6969
7070 /**
7171 * Provides default params which can be used with `whisper_init_from_file_with_params()` etc.
72- * Because this function allocates memory for the params, the caller must call either:
73- * - call `whisper_free_context_params()`
74- * - `Native.free(Pointer.nativeValue(pointer));`
72+ * Returns a ByValue instance to ensure proper parameter passing to native code.
7573 */
76- public WhisperContextParams getContextDefaultParams () {
77- paramsPointer = lib . whisper_context_default_params_by_ref ();
78- WhisperContextParams params = new WhisperContextParams ( paramsPointer );
79- params .read ();
80- return params ;
74+ public WhisperContextParams . ByValue getContextDefaultParams () {
75+ WhisperContextParams . ByValue valueParams = new WhisperContextParams . ByValue (
76+ lib . whisper_context_default_params_by_ref () );
77+ valueParams .read ();
78+ return valueParams ;
8179 }
8280
8381 /**
@@ -88,7 +86,7 @@ public WhisperContextParams getContextDefaultParams() {
8886 *
8987 * @param strategy - GREEDY
9088 */
91- public WhisperFullParams getFullDefaultParams (WhisperSamplingStrategy strategy ) {
89+ public WhisperFullParams . ByValue getFullDefaultParams (WhisperSamplingStrategy strategy ) {
9290 Pointer pointer ;
9391
9492 // whisper_full_default_params_by_ref allocates memory which we need to delete, so only create max 1 pointer for each strategy.
@@ -104,7 +102,7 @@ public WhisperFullParams getFullDefaultParams(WhisperSamplingStrategy strategy)
104102 pointer = beamParamsPointer ;
105103 }
106104
107- WhisperFullParams params = new WhisperFullParams (pointer );
105+ WhisperFullParams . ByValue params = new WhisperFullParams . ByValue (pointer );
108106 params .read ();
109107 return params ;
110108 }
@@ -138,15 +136,21 @@ private void freeParams() {
138136 }
139137
140138 /**
141- * Run the entire model: PCM -> log mel spectrogram -> encoder -> decoder -> text.
139+ * Run the entire model: PCM -> log mel spectrogram -> encoder -> decoder -> text.
142140 * Not thread safe for same context
143141 * Uses the specified decoding strategy to obtain the text.
144142 */
145- public String fullTranscribe (WhisperFullParams whisperParams , float [] audioData ) throws IOException {
143+ public String fullTranscribe (WhisperFullParams . ByValue whisperParams , float [] audioData ) throws IOException {
146144 if (ctx == null ) {
147145 throw new IllegalStateException ("Model not initialised" );
148146 }
149147
148+ /*
149+ WhisperFullParams.ByValue valueParams = new WhisperFullParams.ByValue(
150+ lib.whisper_full_default_params_by_ref(WhisperSamplingStrategy.WHISPER_SAMPLING_BEAM_SEARCH.ordinal()));
151+ valueParams.read();
152+ */
153+
150154 if (lib .whisper_full (ctx , whisperParams , audioData , audioData .length ) != 0 ) {
151155 throw new IOException ("Failed to process audio" );
152156 }
@@ -163,12 +167,17 @@ public String fullTranscribe(WhisperFullParams whisperParams, float[] audioData)
163167
164168 return str .toString ().trim ();
165169 }
170+
166171 public List <WhisperSegment > fullTranscribeWithTime (WhisperFullParams whisperParams , float [] audioData ) throws IOException {
167172 if (ctx == null ) {
168173 throw new IllegalStateException ("Model not initialised" );
169174 }
170175
171- if (lib .whisper_full (ctx , whisperParams , audioData , audioData .length ) != 0 ) {
176+ WhisperFullParams .ByValue valueParams = new WhisperFullParams .ByValue (
177+ lib .whisper_full_default_params_by_ref (WhisperSamplingStrategy .WHISPER_SAMPLING_BEAM_SEARCH .ordinal ()));
178+ valueParams .read ();
179+
180+ if (lib .whisper_full (ctx , valueParams , audioData , audioData .length ) != 0 ) {
172181 throw new IOException ("Failed to process audio" );
173182 }
174183
0 commit comments