Added ogg support for en_tts.js

This commit is contained in:
PaulStets 2018-08-08 09:12:02 +03:00
parent 3abf742456
commit 4fe5a2c2d9
4 changed files with 103 additions and 7 deletions

View file

@ -46,6 +46,7 @@ import net.osmand.plus.search.QuickSearchHelper;
import net.osmand.plus.views.corenative.NativeCoreContext; import net.osmand.plus.views.corenative.NativeCoreContext;
import net.osmand.plus.voice.CommandPlayer; import net.osmand.plus.voice.CommandPlayer;
import net.osmand.plus.voice.CommandPlayerException; import net.osmand.plus.voice.CommandPlayerException;
import net.osmand.plus.voice.JSMediaCommandPlayerImpl;
import net.osmand.plus.voice.JSTTSCommandPlayerImpl; import net.osmand.plus.voice.JSTTSCommandPlayerImpl;
import net.osmand.plus.voice.MediaCommandPlayerImpl; import net.osmand.plus.voice.MediaCommandPlayerImpl;
import net.osmand.plus.voice.TTSCommandPlayerImpl; import net.osmand.plus.voice.TTSCommandPlayerImpl;
@ -578,10 +579,12 @@ public class AppInitializer implements IProgress {
if (!voiceDir.exists()) { if (!voiceDir.exists()) {
throw new CommandPlayerException(ctx.getString(R.string.voice_data_unavailable)); throw new CommandPlayerException(ctx.getString(R.string.voice_data_unavailable));
} }
if (app.getSettings().USE_JS_VOICE_GUIDANCE.get() && JSTTSCommandPlayerImpl.isMyData(voiceDir)) { boolean useJs = app.getSettings().USE_JS_VOICE_GUIDANCE.get();
if (useJs && JSTTSCommandPlayerImpl.isMyData(voiceDir)) {
return new JSTTSCommandPlayerImpl(ctx, applicationMode, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider); return new JSTTSCommandPlayerImpl(ctx, applicationMode, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider);
} else if (MediaCommandPlayerImpl.isMyData(voiceDir)) { } else if (MediaCommandPlayerImpl.isMyData(voiceDir)) {
return new MediaCommandPlayerImpl(osmandApplication, applicationMode, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider); return useJs ? new JSMediaCommandPlayerImpl(osmandApplication, applicationMode, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider)
: new MediaCommandPlayerImpl(osmandApplication, applicationMode, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider);
} else if (TTSCommandPlayerImpl.isMyData(voiceDir)) { } else if (TTSCommandPlayerImpl.isMyData(voiceDir)) {
return new TTSCommandPlayerImpl(ctx, applicationMode, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider); return new TTSCommandPlayerImpl(ctx, applicationMode, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider);
} }

View file

@ -97,7 +97,8 @@ public class RoutingHelper {
public RoutingHelper(OsmandApplication context){ public RoutingHelper(OsmandApplication context){
this.app = context; this.app = context;
settings = context.getSettings(); settings = context.getSettings();
boolean useJS = settings.VOICE_PROVIDER.get().contains("-js"); boolean useJS = settings.VOICE_PROVIDER.get().contains("-js") ||
(!settings.VOICE_PROVIDER.get().contains("-tts") && settings.USE_JS_VOICE_GUIDANCE.get());
voiceRouter = useJS ? new JSVoiceRouter(this, settings) voiceRouter = useJS ? new JSVoiceRouter(this, settings)
: new VoiceRouter(this, settings); : new VoiceRouter(this, settings);
provider = new RouteProvider(); provider = new RouteProvider();

View file

@ -0,0 +1,92 @@
package net.osmand.plus.voice;
import android.media.MediaPlayer;
import android.system.Os;
import net.osmand.IndexConstants;
import net.osmand.PlatformUtil;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.routing.VoiceRouter;
import org.apache.commons.logging.Log;
import org.mozilla.javascript.ScriptableObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static net.osmand.plus.voice.AbstractPrologCommandPlayer.DELAY_CONST;
public class JSMediaCommandPlayerImpl extends MediaCommandPlayerImpl {
private static final org.apache.commons.logging.Log log = PlatformUtil.getLog(JSMediaCommandPlayerImpl.class);
private ScriptableObject jsScope;
private OsmandApplication app;
public JSMediaCommandPlayerImpl(OsmandApplication ctx, ApplicationMode applicationMode, VoiceRouter vrt, String voiceProvider) throws CommandPlayerException {
super(ctx, applicationMode, vrt, voiceProvider);
app = ctx;
org.mozilla.javascript.Context context = org.mozilla.javascript.Context.enter();
context.setOptimizationLevel(-1);
jsScope = context.initSafeStandardObjects();
try {
BufferedReader br = new BufferedReader(new FileReader(new File(
app.getAppPath(IndexConstants.VOICE_INDEX_DIR).getAbsolutePath() +
"/" + voiceProvider + "/" + language + "_tts.js")));
context.evaluateReader(jsScope, br, "JS", 1, null);
br.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
org.mozilla.javascript.Context.exit();
}
}
@Override
public synchronized void playCommands(CommandBuilder builder) {
if(vrt.isMute()) {
return;
}
filesToPlay.addAll(splitAnnouncements(builder.execute()));
// If we have not already started to play audio, start.
if (mediaPlayer == null) {
requestAudioFocus();
// Delay first prompt of each batch to allow BT SCO connection being established
if (ctx != null && ctx.getSettings().AUDIO_STREAM_GUIDANCE.getModeValue(getApplicationMode()) == 0) {
try {
log.debug("Delaying MediaCommandPlayer for BT SCO");
Thread.sleep(ctx.getSettings().BT_SCO_DELAY.get());
} catch (InterruptedException e) {
}
}
}
playQueue();
}
private List<String> splitAnnouncements(List<String> execute) {
List<String> result = new ArrayList<>();
for (String files : execute) {
result.addAll(Arrays.asList(files.split(" ")));
}
return result;
}
@Override
public JSCommandBuilder newCommandBuilder() {
JSCommandBuilder commandBuilder = new JSCommandBuilder(this);
commandBuilder.setJSContext(jsScope);
commandBuilder.setParameters(app.getSettings().METRIC_SYSTEM.get().toTTSString(), false);
return commandBuilder;
}
}

View file

@ -29,10 +29,10 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen
private static final Log log = PlatformUtil.getLog(MediaCommandPlayerImpl.class); private static final Log log = PlatformUtil.getLog(MediaCommandPlayerImpl.class);
// playing media // playing media
private MediaPlayer mediaPlayer; MediaPlayer mediaPlayer;
// indicates that player is ready to play first file // indicates that player is ready to play first file
private List<String> filesToPlay = Collections.synchronizedList(new ArrayList<String>()); List<String> filesToPlay = Collections.synchronizedList(new ArrayList<String>());
private VoiceRouter vrt; VoiceRouter vrt;
public MediaCommandPlayerImpl(OsmandApplication ctx, ApplicationMode applicationMode, VoiceRouter vrt, String voiceProvider) public MediaCommandPlayerImpl(OsmandApplication ctx, ApplicationMode applicationMode, VoiceRouter vrt, String voiceProvider)
@ -95,7 +95,7 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen
playQueue(); playQueue();
} }
private synchronized void playQueue() { synchronized void playQueue() {
if (mediaPlayer == null) { if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer(); mediaPlayer = new MediaPlayer();
} }