/
Orca.java
255 lines (222 loc) · 8.56 KB
/
Orca.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
/*
Copyright 2024 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.orca;
import android.content.Context;
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 Orca Text-to-Speech engine. Orca converts text to spoken audio
* without network latency.
*/
public class Orca {
private static String _sdk = "android";
static {
System.loadLibrary("pv_orca");
}
private long handle;
/**
* Constructor.
*
* @param accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
* @param modelPath Absolute path to the file containing Orca model parameters.
* @throws OrcaException if there is an error while initializing Orca.
*/
private Orca(String accessKey, String modelPath) throws OrcaException {
OrcaNative.setSdk(Orca._sdk);
handle = OrcaNative.init(
accessKey,
modelPath);
}
public static void setSdk(String sdk) {
Orca._sdk = sdk;
}
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 Orca.
*/
public void delete() {
if (handle != 0) {
OrcaNative.delete(handle);
handle = 0;
}
}
/**
* Generates audio from text. The returned audio contains the speech representation of the text.
*
* @param text Text to be converted to audio. The maximum length can be attained by calling
* `getMaxCharacterLimit()`. Allowed characters can be retrieved by calling
* `getValidCharacters()`. Custom pronunciations can be embedded in the text via the
* syntax `{word|pronunciation}`. The pronunciation is expressed in ARPAbet format,
* e.g.: `I {liv|L IH V} in {Sevilla|S EH V IY Y AH}`.
* @param params Global parameters for synthesized text. See 'OrcaSynthesizeParams' for details.
* @return The output audio.
* @throws OrcaException if there is an error while synthesizing audio.
*/
public short[] synthesize(String text, OrcaSynthesizeParams params) throws OrcaException {
if (handle == 0) {
throw new OrcaInvalidStateException(
"Attempted to call Orca synthesize after delete."
);
}
return OrcaNative.synthesize(
handle,
text,
params.getSpeechRate());
}
/**
* Generates audio from text and saves it to a file. The file contains the speech
* representation of the text.
*
* @param text Text to be converted to audio. The maximum length can be attained by calling
* `getMaxCharacterLimit()`. Allowed characters can be retrieved by calling
* `getValidCharacters()`. Custom pronunciations can be embedded in the text via the
* syntax `{word|pronunciation}`. The pronunciation is expressed in ARPAbet format,
* e.g.: `I {liv|L IH V} in {Sevilla|S EH V IY Y AH}`.
* @param outputPath Absolute path to the output audio file. The output file is saved as
* `WAV (.wav)` and consists of a single mono channel.
* @param params Global parameters for synthesized text. See 'OrcaSynthesizeParams' for details.
* @throws OrcaException if there is an error while synthesizing audio to file.
*/
public void synthesizeToFile(
String text,
String outputPath,
OrcaSynthesizeParams params) throws OrcaException {
if (handle == 0) {
throw new OrcaInvalidStateException(
"Attempted to call Orca synthesize after delete."
);
}
OrcaNative.synthesizeToFile(
handle,
text,
outputPath,
params.getSpeechRate());
}
/**
* Getter for version.
*
* @return Version.
*/
public String getVersion() {
return OrcaNative.getVersion();
}
/**
* Getter for the maximum number of characters that can be synthesized at once.
*
* @return The maximum number of characters that can be synthesized at once.
*/
public int getMaxCharacterLimit() {
return OrcaNative.getMaxCharacterLimit();
}
/**
* Getter for the audio sampling rate of the audio produced by Orca.
*
* @return Audio sampling rate of the audio produced by Orca.
*/
public int getSampleRate() throws OrcaException {
if (handle == 0) {
throw new OrcaInvalidStateException(
"Attempted to call Orca getSampleRate after delete."
);
}
return OrcaNative.getSampleRate(handle);
}
/**
* Getter for the set of characters that are accepted as input to Orca synthesize functions.
*
* @return Array of characters that are accepted as input to Orca synthesize functions.
*/
public String[] getValidCharacters() throws OrcaException {
if (handle == 0) {
throw new OrcaInvalidStateException(
"Attempted to call Orca getValidCharacters after delete."
);
}
return OrcaNative.getValidCharacters(handle);
}
/**
* Builder for creating instance of Orca.
*/
public static class Builder {
private String accessKey = null;
private String modelPath = null;
/**
* Sets the AccessKey.
*
* @param accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
* @return Modified builder object.
*/
public Builder setAccessKey(String accessKey) {
this.accessKey = accessKey;
return this;
}
/**
* Sets the path to the model file (`.pv`).
*
* @param modelPath Absolute path to the file (`.pv`) containing Orca model parameters.
* @return Modified builder object.
*/
public Builder setModelPath(String modelPath) {
this.modelPath = modelPath;
return this;
}
/**
* Validates properties and creates an instance of the Orca Text-to-Speech engine.
*
* @return An instance Orca Text-to-Speech engine
* @throws OrcaException if there is an error while initializing Orca.
*/
public Orca build(Context context) throws OrcaException {
if (accessKey == null || this.accessKey.equals("")) {
throw new OrcaInvalidArgumentException("No AccessKey was provided to Orca");
}
if (modelPath == null) {
throw new OrcaInvalidArgumentException("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 OrcaIOException(ex);
}
}
}
return new Orca(
accessKey,
modelPath);
}
}
}