/
Leopard.java
270 lines (240 loc) · 9.39 KB
/
Leopard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Copyright 2022-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing permissions and
limitations under the License.
*/
package ai.picovoice.leopard;
import android.content.Context;
import android.text.TextUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Android binding for Leopard Speech-to-Text engine.
*/
public class Leopard {
private static final String[] VALID_EXTENSIONS = {
"3gp",
"flac",
"m4a",
"mp3",
"mp4",
"ogg",
"opus",
"vorbis",
"wav",
"webm"
};
static {
System.loadLibrary("pv_leopard");
}
private long handle;
private static String _sdk = "android";
public static void setSdk(String sdk) {
Leopard._sdk = sdk;
}
/**
* Constructor.
*
* @param accessKey AccessKey obtained from Picovoice Console
* @param modelPath Absolute path to the file containing Leopard model parameters.
* @param enableAutomaticPunctuation Set to `true` to enable automatic punctuation insertion.
* @param enableDiarization Set to `true` to enable speaker diarization, which allows Leopard to
* differentiate speakers as part of the transcription process. Word
* metadata will include a `speaker_tag` to identify unique speakers.
* @throws LeopardException if there is an error while initializing Leopard.
*/
private Leopard(
String accessKey,
String modelPath,
boolean enableAutomaticPunctuation,
boolean enableDiarization) throws LeopardException {
LeopardNative.setSdk(Leopard._sdk);
handle = LeopardNative.init(
accessKey,
modelPath,
enableAutomaticPunctuation,
enableDiarization);
}
private static String extractResource(
Context context,
InputStream srcFileStream,
String dstFilename) throws IOException {
InputStream is = new BufferedInputStream(
srcFileStream,
256);
OutputStream os = new BufferedOutputStream(
context.openFileOutput(dstFilename, Context.MODE_PRIVATE),
256);
int r;
while ((r = is.read()) != -1) {
os.write(r);
}
os.flush();
is.close();
os.close();
return new File(context.getFilesDir(), dstFilename).getAbsolutePath();
}
/**
* Releases resources acquired by Leopard.
*/
public void delete() {
if (handle != 0) {
LeopardNative.delete(handle);
handle = 0;
}
}
/**
* Processes given audio data and returns its transcription.
*
* @param pcm A frame of audio samples. The incoming audio needs to have a sample rate
* equal to {@link #getSampleRate()} and be 16-bit linearly-encoded. Furthermore,
* Leopard operates on single channel audio. If you wish to process data in a different
* sample rate or format consider using `.process_file`.
* @return LeopardTranscript object which contains the transcription results of the engine.
* @throws LeopardException if there is an error while processing the audio frame.
*/
public LeopardTranscript process(short[] pcm) throws LeopardException {
if (handle == 0) {
throw new LeopardInvalidStateException("Attempted to call Leopard process after delete.");
}
if (pcm == null) {
throw new LeopardInvalidArgumentException("Passed null frame to Leopard process.");
}
return LeopardNative.process(handle, pcm, pcm.length);
}
/**
* Processes given audio data and returns its transcription.
*
* @param path Absolute path to the audio file. The supported formats are:
* `3gp (AMR)`, `FLAC`, `MP3`, `MP4/m4a (AAC)`, `Ogg`, `WAV` and `WebM`.
* @return LeopardTranscript object which contains the transcription results of the engine.
* @throws LeopardException if there is an error while processing the audio frame.
*/
public LeopardTranscript processFile(String path) throws LeopardException {
if (handle == 0) {
throw new LeopardInvalidStateException("Attempted to call Leopard processFile after delete.");
}
if (path == null || path.equals("")) {
throw new LeopardInvalidArgumentException("Passed null path to Leopard processFile.");
}
try {
return LeopardNative.processFile(handle, path);
} catch (LeopardInvalidArgumentException e) {
boolean endsWithValidExt = false;
for (String ext : VALID_EXTENSIONS) {
if (path.endsWith(ext)) {
endsWithValidExt = true;
break;
}
}
if (!endsWithValidExt) {
throw new LeopardInvalidArgumentException(
String.format(
"Specified file '%s' does not have an accepted file extension. " +
"Valid extensions are: %s",
path,
TextUtils.join(", ", VALID_EXTENSIONS)));
}
throw e;
}
}
/**
* Getter for required audio sample rate for PCM data.
*
* @return Required audio sample rate for PCM data.
*/
public int getSampleRate() {
return LeopardNative.getSampleRate();
}
/**
* Getter for Leopard version.
*
* @return Leopard version.
*/
public String getVersion() {
return LeopardNative.getVersion();
}
/**
* Builder for creating an instance of Leopard with a mixture of default arguments.
*/
public static class Builder {
private String accessKey = null;
private String modelPath = null;
private boolean enableAutomaticPunctuation = false;
private boolean enableDiarization = false;
/**
* Setter the AccessKey.
*
* @param accessKey AccessKey obtained from Picovoice Console
*/
public Builder setAccessKey(String accessKey) {
this.accessKey = accessKey;
return this;
}
/**
* Setter for the absolute path to the file containing Leopard model parameters.
*
* @param modelPath Absolute path to the file containing Leopard model parameters.
*/
public Builder setModelPath(String modelPath) {
this.modelPath = modelPath;
return this;
}
/**
* Setter for enabling automatic punctuation insertion.
*
* @param enableAutomaticPunctuation Set to `true` to enable automatic punctuation insertion.
*/
public Builder setEnableAutomaticPunctuation(boolean enableAutomaticPunctuation) {
this.enableAutomaticPunctuation = enableAutomaticPunctuation;
return this;
}
/**
* Setter for enabling speaker diarization.
*
* @param enableDiarization Set to `true` to enable speaker diarization, which allows Leopard to
* differentiate speakers as part of the transcription process. Word
* metadata will include a `speaker_tag` to identify unique speakers.
*/
public Builder setEnableDiarization(boolean enableDiarization) {
this.enableDiarization = enableDiarization;
return this;
}
/**
* Creates an instance of Leopard Speech-to-Text engine.
*/
public Leopard build(Context context) throws LeopardException {
if (accessKey == null || this.accessKey.equals("")) {
throw new LeopardInvalidArgumentException("No AccessKey was provided to Leopard");
}
if (modelPath == null) {
throw new LeopardInvalidArgumentException("ModelPath must not be null");
} else {
File modelFile = new File(modelPath);
String modelFilename = modelFile.getName();
if (!modelFile.exists() && !modelFilename.equals("")) {
try {
modelPath = extractResource(context,
context.getAssets().open(modelPath),
modelFilename);
} catch (IOException ex) {
throw new LeopardIOException(ex);
}
}
}
return new Leopard(
accessKey,
modelPath,
enableAutomaticPunctuation,
enableDiarization);
}
}
}