-
Notifications
You must be signed in to change notification settings - Fork 40
/
SelfAwareCache.java
292 lines (250 loc) · 9.9 KB
/
SelfAwareCache.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Copyright (c) 2016. Saiy Ltd. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ai.saiy.android.service.helper;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Process;
import android.speech.tts.TextToSpeech;
import android.support.annotation.NonNull;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import ai.saiy.android.cache.speech.SpeechCachePrepare;
import ai.saiy.android.database.DBSpeech;
import ai.saiy.android.service.SelfAware;
import ai.saiy.android.tts.SaiyProgressListener;
import ai.saiy.android.tts.helper.SaiyVoice;
import ai.saiy.android.utils.MyLog;
import ai.saiy.android.utils.SPH;
import ai.saiy.android.utils.UtilsFile;
import ai.saiy.android.utils.UtilsString;
/**
* Utility class to manage the caching of data associated with the
* {@link SelfAware} Service.
* <p/>
* Created by benrandall76@gmail.com on 28/04/2016.
*/
public class SelfAwareCache extends SaiyProgressListener {
private final boolean DEBUG = MyLog.DEBUG;
private final String CLS_NAME = SelfAwareCache.class.getSimpleName();
public static final int MAX_UTTERANCE_CHARS = 150;
private static final long DBS_MAINTENANCE_INCREMENT = 40L;
private final Context mContext;
private volatile TextToSpeech ttsCache;
private volatile SpeechCachePrepare scp;
private volatile File tempFile;
/**
* Constructor
*
* @param mContext the application context
*/
public SelfAwareCache(@NonNull final Context mContext) {
this.mContext = mContext;
}
/**
* Check if the current synthesis should be cached.
*
* @param params the {@link SelfAwareParameters}
* @param ttsLocale the {@link TextToSpeech} {@link Locale}
* @param utterance the utterance
* @param initEngine the initialised {@link TextToSpeech} engine
* @param voice the {@link SaiyVoice}
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void shouldCache(@NonNull final SelfAwareParameters params, @NonNull final Locale ttsLocale,
@NonNull final String utterance, @NonNull final String initEngine,
@NonNull final SaiyVoice voice) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_LESS_FAVORABLE);
if (UtilsString.notNaked(initEngine)) {
if (voice.isNetworkConnectionRequired()) {
if (params.shouldNetwork()) {
final DBSpeech dbSpeech = new DBSpeech(mContext);
if (!dbSpeech.entryExists(initEngine, voice.getName(), utterance)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "shouldCache: proceeding");
}
final SpeechCachePrepare scp1 = new SpeechCachePrepare(mContext);
scp1.setVoice(voice);
scp1.setEngine(initEngine);
scp1.setUtterance(utterance);
scp1.setLocale(ttsLocale.toString());
SelfAwareCache.this.doAudioCache(scp1, params);
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "shouldCache: entry already exists");
}
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "shouldCache: network not requested");
}
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "shouldCache: not network voice");
}
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "shouldCache: initEngine naked");
}
}
}
});
}
/**
* Synthesise the audio to a temporary file in the application's internal storage cache, using
* a temporary and short-lived {@link TextToSpeech} object. The result of this will report
* to {@link SaiyProgressListener#onDone(String)} from where the raw pcm will be stored in {@link DBSpeech}
*
* @param scp the populated {@link SpeechCachePrepare}
* @param params the {@link SelfAwareParameters}
*/
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void doAudioCache(@NonNull final SpeechCachePrepare scp, @NonNull final SelfAwareParameters params) {
this.scp = scp;
AsyncTask.execute(new Runnable() {
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO);
ttsCache = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
@Override
public void onInit(final int status) {
switch (status) {
case TextToSpeech.SUCCESS:
ttsCache.setVoice(scp.getVoice());
ttsCache.setOnUtteranceProgressListener(SelfAwareCache.this);
tempFile = UtilsFile.getTempAudioFile(mContext);
if (DEBUG) {
MyLog.i(CLS_NAME, "doAudioCache: " + tempFile.getAbsolutePath());
}
ttsCache.synthesizeToFile(scp.getUtterance(), params.getBundle(), tempFile, params.getUtteranceId());
break;
case TextToSpeech.ERROR:
break;
}
}
}, scp.getEngine());
}
});
}
@Override
public void onAudioAvailable(final String utteranceId, final byte[] audio) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onAudioAvailable");
}
}
@Override
public void onBeginSynthesis(final String utteranceId, final int sampleRateInHz, final int audioFormat,
final int channelCount) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onBeginSynthesis");
}
}
@Override
public void onStart(final String utteranceId) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onStart");
}
}
@Override
public void onDone(final String utteranceId) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onDone");
}
try {
scp.setUncompressedAudio(FileUtils.readFileToByteArray(tempFile));
} catch (final IOException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onDone: IOException");
e.printStackTrace();
}
} finally {
if (tempFile != null) {
final boolean success = tempFile.delete();
if (DEBUG) {
MyLog.i(CLS_NAME, "onDone: tempFile deleted: " + success);
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "onDone: tempFile null");
}
}
}
shutdownTTS();
speechMaintenance();
}
@Override
public void onError(final String utteranceId) {
if (DEBUG) {
MyLog.w(CLS_NAME, "onError");
}
shutdownTTS();
}
/**
* Check the {@link DBSpeech} size to see if we need to reduce it.
*/
private void speechMaintenance() {
if (DEBUG) {
MyLog.i(CLS_NAME, "speechMaintenance");
}
if (SPH.getUsedIncrement(mContext) % DBS_MAINTENANCE_INCREMENT == 0) {
new Thread() {
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
final DBSpeech dbSpeech = new DBSpeech(mContext);
if (dbSpeech.shouldRunMaintenance(mContext)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "shouldRunMaintenance: true");
}
dbSpeech.runMaintenance(mContext);
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "shouldRunMaintenance: false");
}
}
}
}.start();
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "shouldRunMaintenance: false: " + SPH.getUsedIncrement(mContext));
}
}
}
/**
* Shutdown the temporary {@link TextToSpeech} object
*/
private void shutdownTTS() {
if (ttsCache != null) {
try {
ttsCache.shutdown();
} catch (final Exception e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "shutdownTTS: Exception");
e.printStackTrace();
}
}
}
}
}