From 5b89e876874278f59d3e0270d090e57492908992 Mon Sep 17 00:00:00 2001 From: sonora Date: Sun, 29 Mar 2020 14:13:21 +0200 Subject: [PATCH] Voice Prompt Delay for all modes (#8706) --- .../src/net/osmand/plus/OsmandSettings.java | 10 +++++- .../plus/development/TestVoiceActivity.java | 32 +++++++++---------- .../net/osmand/plus/routing/VoiceRouter.java | 15 +++++---- .../voice/AbstractPrologCommandPlayer.java | 3 -- .../plus/voice/JSMediaCommandPlayerImpl.java | 14 ++++---- .../plus/voice/MediaCommandPlayerImpl.java | 14 ++++---- .../plus/voice/TTSCommandPlayerImpl.java | 19 ++++++----- 7 files changed, 59 insertions(+), 48 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 85c93e31f4..39e781ed76 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -2090,7 +2090,15 @@ public class OsmandSettings { 12/*AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE*/).makeProfile(); // For now this can be changed only in TestVoiceActivity - public final OsmandPreference BT_SCO_DELAY = new IntPreference("bt_sco_delay", 1500).makeGlobal().cache(); + public final OsmandPreference[] VOICE_PROMPT_DELAY = new IntPreference[5]; + + { + // On most devices sound output works pomptly so usually no voice prompt delay needed + VOICE_PROMPT_DELAY[3] = new IntPreference("voice_prompt_delay_3", 0).makeGlobal().cache(); /*AudioManager.STREAM_MUSIC*/ + VOICE_PROMPT_DELAY[5] = new IntPreference("voice_prompt_delay_5", 0).makeGlobal().cache(); /*AudioManager.STREAM_NOTIFICATION*/ + // 1500 ms delay works for most configurations to establish a BT SCO link + VOICE_PROMPT_DELAY[0] = new IntPreference("voice_prompt_delay_0", 1500).makeGlobal().cache(); /*AudioManager.STREAM_VOICE_CALL*/ + } // this value string is synchronized with settings_pref.xml preference name public final CommonPreference MAP_ONLINE_DATA = new BooleanPreference("map_online_data", false).makeProfile(); diff --git a/OsmAnd/src/net/osmand/plus/development/TestVoiceActivity.java b/OsmAnd/src/net/osmand/plus/development/TestVoiceActivity.java index 90acf2e9fa..f6bce308a0 100644 --- a/OsmAnd/src/net/osmand/plus/development/TestVoiceActivity.java +++ b/OsmAnd/src/net/osmand/plus/development/TestVoiceActivity.java @@ -147,7 +147,8 @@ public class TestVoiceActivity extends OsmandActionBarActivity { v += "\n \u25CF BT SCO: The current app profile is not set to use 'Phone call audio'."; } - v += "\n \u25CF Phone call audio delay: " + ((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.get() + "\u00A0ms"; + v += "\n \u25CF Voice prompt delay for selected output: " + ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY + [((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get()].get() + "\u00A0ms"; return v; } @@ -230,7 +231,7 @@ public class TestVoiceActivity extends OsmandActionBarActivity { addButton(ll, "Voice system info:", builder(p)); addButton(ll, "\u25BA (11.1) (Tap to refresh)\n" + getVoiceSystemInfo(), builder(p).attention("")); - addButton(ll, "\u25BA (11.2) Tap to change Phone call audio delay (if car stereo cuts off prompts). Default is 1500\u00A0ms.", builder(p).attention("")); + addButton(ll, "\u25BA (11.2) Tap to change voice prompt delay (if car stereo cuts off prompts). Default is 1500\u00A0ms for Phone call audio, or else 0\u00A0ms.", builder(p).attention("")); ll.forceLayout(); } @@ -282,23 +283,20 @@ public class TestVoiceActivity extends OsmandActionBarActivity { Toast.makeText(TestVoiceActivity.this, "Info refreshed.", Toast.LENGTH_LONG).show(); } if (description.startsWith("\u25BA (11.2)")) { - if (((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get() == 0) { - if (((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.get() == 1000) { - ((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.set(1500); - } else if (((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.get() == 1500) { - ((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.set(2000); - } else if (((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.get() == 2000) { - ((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.set(2500); - } else if (((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.get() == 2500) { - ((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.set(3000); - } else { - ((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.set(1000); - } - infoButton.setText("\u25BA (11.1) (Tap to refresh)\n" + getVoiceSystemInfo()); - Toast.makeText(TestVoiceActivity.this, "BT SCO init delay changed to " + ((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.get() + "\u00A0ms.", Toast.LENGTH_LONG).show(); + int ams = ((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get(); + if (((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams].get() == 1000) { + ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams].set(1500); + } else if (((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams].get() == 1500) { + ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams].set(2000); + } else if (((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams].get() == 2000) { + ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams].set(2500); + } else if (((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams].get() == 2500) { + ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams].set(3000); } else { - Toast.makeText(TestVoiceActivity.this, "Setting only available when using 'Phone call audio'.", Toast.LENGTH_LONG).show(); + ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams].set(1000); } + infoButton.setText("\u25BA (11.1) (Tap to refresh)\n" + getVoiceSystemInfo()); + Toast.makeText(TestVoiceActivity.this, "Voice prompt delay changed to " + ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams].get() + "\u00A0ms.", Toast.LENGTH_LONG).show(); } } }); diff --git a/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java b/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java index 0b34ec6a1d..1f5d0e7ebe 100644 --- a/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java +++ b/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java @@ -192,7 +192,7 @@ public class VoiceRouter { } } - private double btScoDelayDistance = 0; + private double voicePromptDelayDistance = 0; public boolean isDistanceLess(float currentSpeed, double dist, double etalon, float defSpeed) { if (defSpeed <= 0) { @@ -202,12 +202,13 @@ public class VoiceRouter { currentSpeed = DEFAULT_SPEED; } - // Trigger close prompts earlier if delayed for BT SCO connection establishment - if ((settings.AUDIO_MANAGER_STREAM.getModeValue(router.getAppMode()) == 0) && !AbstractPrologCommandPlayer.btScoStatus) { - btScoDelayDistance = currentSpeed * (double) settings.BT_SCO_DELAY.get() / 1000; + // Trigger close prompts earlier to allow BT SCO link being established, or when VOICE_PROMPT_DELAY is set >0 for the other stream types + int ams = settings.AUDIO_MANAGER_STREAM.getModeValue(router.getAppMode()); + if (((ams == 0) && !AbstractPrologCommandPlayer.btScoStatus) || (ams > 0)) { + voicePromptDelayDistance = currentSpeed * (double) settings.VOICE_PROMPT_DELAY[ams].get() / 1000; } - if ((dist < etalon + btScoDelayDistance) || ((dist - btScoDelayDistance) / currentSpeed) < (etalon / defSpeed)) { + if ((dist < etalon + voicePromptDelayDistance) || ((dist - voicePromptDelayDistance) / currentSpeed) < (etalon / defSpeed)) { return true; } return false; @@ -520,9 +521,9 @@ public class VoiceRouter { if (repeat || dist >= TURN_IN_DISTANCE_END) { if ((isDistanceLess(speed, nextNextInfo.distanceTo, TURN_DISTANCE, 0f) || nextNextInfo.distanceTo < TURN_IN_DISTANCE_END) && nextNextInfo != null) { - playMakeTurnIn(currentSegment, next, dist - (int) btScoDelayDistance, nextNextInfo.directionInfo); + playMakeTurnIn(currentSegment, next, dist - (int) voicePromptDelayDistance, nextNextInfo.directionInfo); } else { - playMakeTurnIn(currentSegment, next, dist - (int) btScoDelayDistance, null); + playMakeTurnIn(currentSegment, next, dist - (int) voicePromptDelayDistance, null); } playGoAndArriveAtDestination(repeat, nextInfo, currentSegment); } diff --git a/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java b/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java index a645fc8c9d..04449bf62b 100644 --- a/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java +++ b/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java @@ -322,9 +322,6 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat public static boolean btScoStatus = false; - // BT_SCO_DELAY now in Settings. 1500 ms works for most configurations. - //public static final int BT_SCO_DELAY = 1500; - // This only needed for init debugging in TestVoiceActivity: public static String btScoInit = "-"; diff --git a/OsmAnd/src/net/osmand/plus/voice/JSMediaCommandPlayerImpl.java b/OsmAnd/src/net/osmand/plus/voice/JSMediaCommandPlayerImpl.java index c22973427a..cd4511724e 100644 --- a/OsmAnd/src/net/osmand/plus/voice/JSMediaCommandPlayerImpl.java +++ b/OsmAnd/src/net/osmand/plus/voice/JSMediaCommandPlayerImpl.java @@ -55,12 +55,14 @@ public class JSMediaCommandPlayerImpl extends MediaCommandPlayerImpl { // 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_MANAGER_STREAM.getModeValue(getApplicationMode()) == 0) { - try { - log.debug("Delaying MediaCommandPlayer for BT SCO"); - Thread.sleep(ctx.getSettings().BT_SCO_DELAY.get()); - } catch (InterruptedException e) { + // Delay first prompt of each batch to allow BT SCO link being established, or when VOICE_PROMPT_DELAY is set >0 for the other stream types + if (ctx != null) { + int vpd = ctx.getSettings().VOICE_PROMPT_DELAY[ctx.getSettings().AUDIO_MANAGER_STREAM.getModeValue(getApplicationMode())].get(); + if (vpd > 0) { + try { + Thread.sleep(vpd); + } catch (InterruptedException e) { + } } } } diff --git a/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java b/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java index 8c5b454ddd..273bfe7ccc 100644 --- a/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java +++ b/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java @@ -87,12 +87,14 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen // 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_MANAGER_STREAM.getModeValue(getApplicationMode()) == 0) { - try { - log.debug("Delaying MediaCommandPlayer for BT SCO"); - Thread.sleep(ctx.getSettings().BT_SCO_DELAY.get()); - } catch (InterruptedException e) { + // Delay first prompt of each batch to allow BT SCO link being established, or when VOICE_PROMPT_DELAY is set >0 for the other stream types + if (ctx != null) { + int vpd = ctx.getSettings().VOICE_PROMPT_DELAY[ctx.getSettings().AUDIO_MANAGER_STREAM.getModeValue(getApplicationMode())].get(); + if (vpd > 0) { + try { + Thread.sleep(vpd); + } catch (InterruptedException e) { + } } } } diff --git a/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java b/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java index 7bb19f361b..ad81dbef28 100644 --- a/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java +++ b/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java @@ -123,14 +123,17 @@ public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer { .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build()); } - // Delay first prompt of each batch to allow BT SCO connection being established - if (ctx.getSettings().AUDIO_MANAGER_STREAM.getModeValue(getApplicationMode()) == 0) { - ttsRequests++; - if (android.os.Build.VERSION.SDK_INT < 21) { - params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,""+System.currentTimeMillis()); - mTts.playSilence(ctx.getSettings().BT_SCO_DELAY.get(), TextToSpeech.QUEUE_ADD, params); - } else { - mTts.playSilentUtterance(ctx.getSettings().BT_SCO_DELAY.get(), TextToSpeech.QUEUE_ADD, ""+System.currentTimeMillis()); + // Delay first prompt of each batch to allow BT SCO link being established, or when VOICE_PROMPT_DELAY is set >0 for the other stream types + if (ctx != null) { + int vpd = ctx.getSettings().VOICE_PROMPT_DELAY[ctx.getSettings().AUDIO_MANAGER_STREAM.getModeValue(getApplicationMode())].get(); + if (vpd > 0) { + ttsRequests++; + if (android.os.Build.VERSION.SDK_INT < 21) { + params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,""+System.currentTimeMillis()); + mTts.playSilence(vpd, TextToSpeech.QUEUE_ADD, params); + } else { + mTts.playSilentUtterance(vpd, TextToSpeech.QUEUE_ADD, ""+System.currentTimeMillis()); + } } } }