Merge pull request #8727 from osmandapp/VoicePromptDelay

Voice prompt delay for all audio manager streams
This commit is contained in:
vshcherb 2020-04-10 12:11:43 +02:00 committed by GitHub
commit 9e8b471b19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 52 deletions

View file

@ -2118,7 +2118,15 @@ public class OsmandSettings {
12/*AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE*/).makeProfile();
// For now this can be changed only in TestVoiceActivity
public final OsmandPreference<Integer> BT_SCO_DELAY = new IntPreference("bt_sco_delay", 1500).makeGlobal().cache();
public final OsmandPreference<Integer>[] VOICE_PROMPT_DELAY = new IntPreference[10];
{
// 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*/
// 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*/
}
// this value string is synchronized with settings_pref.xml preference name
public final CommonPreference<Boolean> MAP_ONLINE_DATA = new BooleanPreference("map_online_data", false).makeProfile();

View file

@ -19,6 +19,7 @@ import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.OsmandActionBarActivity;
import net.osmand.plus.routing.data.StreetName;
@ -125,14 +126,15 @@ public class TestVoiceActivity extends OsmandActionBarActivity {
String v ="";
v += " \u25CF App profile: " + ((OsmandApplication) getApplication()).getSettings().APPLICATION_MODE.get().getStringKey();
if (((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get() == 3) {
int stream = ((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get();
if (stream == 3) {
v += "\n \u25CF Voice guidance output: Media/music audio";
} else if (((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get() == 5) {
} else if (stream == 5) {
v += "\n \u25CF Voice guidance output: Notification audio";
} else if (((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get() == 0) {
} else if (stream == 0) {
v += "\n \u25CF Voice guidance output: Phone call audio";
} else {
v += "\n \u25CF Voice guidance output: " + ((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get();
v += "\n \u25CF Voice guidance output: " + stream;
}
v += "\n \u25CF OsmAnd voice: " + osmandVoice;
@ -141,13 +143,16 @@ public class TestVoiceActivity extends OsmandActionBarActivity {
v += "\n \u25CF TTS voice language availability: " + TTSCommandPlayerImpl.getTtsVoiceStatus();
v += "\n \u25CF TTS voice actually used: " + TTSCommandPlayerImpl.getTtsVoiceUsed();
if (((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get() == 0) {
if (stream == 0) {
v += "\n \u25CF BT SCO: " + AbstractPrologCommandPlayer.btScoInit;
} else {
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";
OsmandSettings.OsmandPreference<Integer> pref = ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[stream];
if(pref != null) {
v += "\n \u25CF Voice prompt delay for selected output: " + pref.get() + "\u00A0ms";
}
return v;
}
@ -230,7 +235,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 +287,18 @@ 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);
int ams = ((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get();
OsmandSettings.OsmandPreference<Integer> pref = ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams];
if (pref != null) {
if (pref.get() >= 3000) {
pref.set(0);
} else {
((OsmandApplication) getApplication()).getSettings().BT_SCO_DELAY.set(1000);
pref.set(pref.get() + 500);
}
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();
} else {
Toast.makeText(TestVoiceActivity.this, "Setting only available when using 'Phone call audio'.", Toast.LENGTH_LONG).show();
Toast.makeText(TestVoiceActivity.this, "Voice prompt delay changed to " + pref.get() + "\u00A0ms.", Toast.LENGTH_LONG).show();
}
infoButton.setText("\u25BA (11.1) (Tap to refresh)\n" + getVoiceSystemInfo());
}
}
});

View file

@ -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,15 @@ 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) {
if (settings.VOICE_PROMPT_DELAY[ams] != null) {
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 +523,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);
}

View file

@ -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 = "-";

View file

@ -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) {
}
}
}
}

View file

@ -8,6 +8,7 @@ import android.os.Build;
import net.osmand.PlatformUtil;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.routing.VoiceRouter;
import org.apache.commons.logging.Log;
@ -87,12 +88,15 @@ 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) {
Integer stream = ctx.getSettings().AUDIO_MANAGER_STREAM.getModeValue(getApplicationMode());
OsmandSettings.OsmandPreference<Integer> pref = ctx.getSettings().VOICE_PROMPT_DELAY[stream];
if (pref.getModeValue(getApplicationMode()) > 0) {
try {
Thread.sleep(pref.getModeValue(getApplicationMode()));
} catch (InterruptedException e) {
}
}
}
}

View file

@ -17,6 +17,7 @@ import androidx.appcompat.app.AlertDialog;
import net.osmand.PlatformUtil;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.SettingsActivity;
import net.osmand.plus.routing.VoiceRouter;
@ -123,14 +124,19 @@ 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) {
Integer streamModeValue = ctx.getSettings().AUDIO_MANAGER_STREAM.getModeValue(getApplicationMode());
OsmandSettings.OsmandPreference<Integer> pref = ctx.getSettings().VOICE_PROMPT_DELAY[streamModeValue];
int vpd = pref == null ? 0 : pref.getModeValue(getApplicationMode());
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());
}
}
}
}