Added full english JS tts support (needs performance optimization)

This commit is contained in:
PaulStets 2018-08-02 16:36:48 +03:00
parent 55232223a3
commit 82078bd942

View file

@ -20,9 +20,6 @@ import net.osmand.util.Algorithms;
import org.mozilla.javascript.ScriptableObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -95,7 +92,6 @@ public class JSTTSCommandPlayerImpl extends AbstractJSCommandPlayer {
initializeEngine(app, ctx);
params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, app.getSettings().AUDIO_STREAM_GUIDANCE
.getModeValue(getApplicationMode()).toString());
setJSContext(app.getAppPath(IndexConstants.VOICE_INDEX_DIR).getAbsolutePath() + "/" + voiceProvider + "/" + language + "_tts.js");
}
private void initializeEngine(final Context ctx, final Activity act) {
@ -224,36 +220,11 @@ public class JSTTSCommandPlayerImpl extends AbstractJSCommandPlayer {
@Override
public JSCommandBuilder newCommandBuilder() {
JSCommandBuilder commandBuilder = new JSCommandBuilder(this);
commandBuilder.setJSContext(jsContext, jsScope);
commandBuilder.setJSContext(app.getAppPath(IndexConstants.VOICE_INDEX_DIR).getAbsolutePath() + "/" + voiceProvider + "/" + language + "_tts.js");
commandBuilder.setParameters(app.getSettings().METRIC_SYSTEM.get().toTTSString(), true);
return commandBuilder;
}
private String readFileContents(String path) {
StringBuilder fileContent = new StringBuilder("");
try {
FileInputStream fis = new FileInputStream(new File(path));
byte[] buffer = new byte[1024];
int n;
while ((n = fis.read(buffer)) != -1)
{
fileContent.append(new String(buffer, 0, n));
}
fis.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return fileContent.toString();
}
private void setJSContext(String path) {
String script = readFileContents(path);
jsContext = org.mozilla.javascript.Context.enter();
jsContext.setOptimizationLevel(-1);
jsScope = jsContext.initStandardObjects();
jsContext.evaluateString(jsScope, script, "JS", 1, null);
}
@Override
public void playCommands(CommandBuilder builder) {
final List<String> execute = builder.execute(); //list of strings, the speech text, play it