1515public class WhisperCpp implements AutoCloseable {
1616 private WhisperCppJnaLibrary lib = WhisperCppJnaLibrary .instance ;
1717 private Pointer ctx = null ;
18- private Pointer greedyPointer = null ;
19- private Pointer beamPointer = null ;
18+ private Pointer paramPointer = null ;
19+ private Pointer greedyParamsPointer = null ;
20+ private Pointer beamParamsPointer = null ;
2021
2122 public File modelDir () {
2223 String modelDirPath = System .getenv ("XDG_CACHE_HOME" );
@@ -43,7 +44,8 @@ public void initContext(String modelPath) throws FileNotFoundException {
4344 modelPath = new File (modelDir (), modelPath ).getAbsolutePath ();
4445 }
4546
46- ctx = lib .whisper_init_from_file (modelPath );
47+ paramPointer = lib .whisper_context_default_params_by_ref ();
48+ ctx = lib .whisper_init_from_file_with_params (modelPath , paramPointer );
4749
4850 if (ctx == null ) {
4951 throw new FileNotFoundException (modelPath );
@@ -63,15 +65,15 @@ public WhisperFullParams getFullDefaultParams(WhisperSamplingStrategy strategy)
6365
6466 // whisper_full_default_params_by_ref allocates memory which we need to delete, so only create max 1 pointer for each strategy.
6567 if (strategy == WhisperSamplingStrategy .WHISPER_SAMPLING_GREEDY ) {
66- if (greedyPointer == null ) {
67- greedyPointer = lib .whisper_full_default_params_by_ref (strategy .ordinal ());
68+ if (greedyParamsPointer == null ) {
69+ greedyParamsPointer = lib .whisper_full_default_params_by_ref (strategy .ordinal ());
6870 }
69- pointer = greedyPointer ;
71+ pointer = greedyParamsPointer ;
7072 } else {
71- if (beamPointer == null ) {
72- beamPointer = lib .whisper_full_default_params_by_ref (strategy .ordinal ());
73+ if (beamParamsPointer == null ) {
74+ beamParamsPointer = lib .whisper_full_default_params_by_ref (strategy .ordinal ());
7375 }
74- pointer = beamPointer ;
76+ pointer = beamParamsPointer ;
7577 }
7678
7779 WhisperFullParams params = new WhisperFullParams (pointer );
@@ -93,13 +95,17 @@ private void freeContext() {
9395 }
9496
9597 private void freeParams () {
96- if (greedyPointer != null ) {
97- Native .free (Pointer .nativeValue (greedyPointer ));
98- greedyPointer = null ;
98+ if (paramPointer != null ) {
99+ Native .free (Pointer .nativeValue (paramPointer ));
100+ paramPointer = null ;
99101 }
100- if (beamPointer != null ) {
101- Native .free (Pointer .nativeValue (beamPointer ));
102- beamPointer = null ;
102+ if (greedyParamsPointer != null ) {
103+ Native .free (Pointer .nativeValue (greedyParamsPointer ));
104+ greedyParamsPointer = null ;
105+ }
106+ if (beamParamsPointer != null ) {
107+ Native .free (Pointer .nativeValue (beamParamsPointer ));
108+ beamParamsPointer = null ;
103109 }
104110 }
105111
0 commit comments