Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let speak instructions without route progress. #32

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changes to the Mapbox Navigation SDK for iOS

## Unreleased
- The `speak` method in `RouteVoiceController` can be used without a given `RouteProgress` or the `RouteProgress` can explicitly ignored so that it will not be added to the voice instruction.
- `RouteProgress` is now optional in `willSpeak` method of `VoiceControllerDelegate` if the `RouteProgress` in the `speak` method of the `RouteVoiceController is `nil`.
- Uses the `Locale` given in `RouteOptions` to create the corresponding `AVSpeechSynthesisVoice`.
- Removed setCamera() from updateCourseTracking()
- Added setCamera() to progressDidChange()
Expand Down
2 changes: 1 addition & 1 deletion Examples/Swift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ extension ViewController: VoiceControllerDelegate {
print(interruptedInstruction.text, interruptingInstruction.text)
}

func voiceController(_ voiceController: RouteVoiceController, willSpeak instruction: SpokenInstruction, routeProgress: RouteProgress) -> SpokenInstruction? {
func voiceController(_ voiceController: RouteVoiceController, willSpeak instruction: SpokenInstruction, routeProgress: RouteProgress?) -> SpokenInstruction? {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a CHANGELOG entry for the changed APIs? Both here and for the RouteVoiceController?

return SpokenInstruction(distanceAlongStep: instruction.distanceAlongStep, text: "New Instruction!", ssmlText: "<speak>New Instruction!</speak>")
}

Expand Down
45 changes: 19 additions & 26 deletions MapboxNavigation/RouteVoiceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,9 @@ open class RouteVoiceController: NSObject, AVSpeechSynthesizerDelegate {

- parameter instruction: The instruction to read aloud.
- parameter locale: The `Locale` used to create the voice read aloud the given instruction. If `nil` the `Locale.preferredLocalLanguageCountryCode` is used for creating the voice.
- parameter ignoreProgress: A `Bool` that indicates if the routeProgress is added to the instruction.
*/
open func speak(_ instruction: SpokenInstruction, with locale: Locale?) {
guard let routeProgress else {
assertionFailure("routeProgress should not be nil.")
return
}

open func speak(_ instruction: SpokenInstruction, with locale: Locale?, ignoreProgress: Bool = false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I am not sure if we really need the ignoreProgress switch here?

We would need to make route progress optional as well for modifiedInstruction.attributedText(for: routeProgress.currentLegProgress).

if speechSynth.isSpeaking, let lastSpokenInstruction = lastSpokenInstruction {
voiceControllerDelegate?.voiceController?(self, didInterrupt: lastSpokenInstruction, with: instruction)
}
Expand All @@ -209,29 +205,26 @@ open class RouteVoiceController: NSObject, AVSpeechSynthesizerDelegate {
voiceControllerDelegate?.voiceController?(self, spokenInstructionsDidFailWith: error)
}

var utterance: AVSpeechUtterance?
let modifiedInstruction = voiceControllerDelegate?.voiceController?(self, willSpeak: instruction, routeProgress: routeProgress) ?? instruction

let utterance: AVSpeechUtterance

if locale?.identifier == "en-US" {
// Alex can’t handle attributed text.
utterance = AVSpeechUtterance(string: instruction.text)
utterance!.voice = AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex)
}

let modifiedInstruction = voiceControllerDelegate?.voiceController?(self, willSpeak: instruction, routeProgress: routeProgress) ?? instruction

if #available(iOS 10.0, *), utterance?.voice == nil {
utterance = AVSpeechUtterance(attributedString: modifiedInstruction.attributedText(for: routeProgress.currentLegProgress))
} else {
utterance = AVSpeechUtterance(string: modifiedInstruction.text)
utterance.voice = AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex)
} else {
if #available(iOS 10.0, *), !ignoreProgress, let routeProgress {
utterance = AVSpeechUtterance(attributedString: modifiedInstruction.attributedText(for: routeProgress.currentLegProgress))
} else {
utterance = AVSpeechUtterance(string: modifiedInstruction.text)
}

boldtrn marked this conversation as resolved.
Show resolved Hide resolved
// Only localized languages will have a proper fallback voice
utterance.voice = AVSpeechSynthesisVoice(language: locale?.identifier ?? Locale.preferredLocalLanguageCountryCode)
}

// Only localized languages will have a proper fallback voice
if utterance?.voice == nil {
utterance?.voice = AVSpeechSynthesisVoice(language: locale?.identifier ?? Locale.preferredLocalLanguageCountryCode)
}

if let utterance = utterance {
speechSynth.speak(utterance)
}
speechSynth.speak(utterance)
}
}

Expand Down Expand Up @@ -264,8 +257,8 @@ public protocol VoiceControllerDelegate {

- parameter voiceController: The voice controller that will speak an instruction.
- parameter instruction: The spoken instruction that will be said.
- parameter routeProgress: The `RouteProgress` just before when the instruction is scheduled to be spoken.
- parameter routeProgress: The `RouteProgress` just before when the instruction is scheduled to be spoken. Could be `nil` if no progress is available or if it should be ignored.
**/
@objc(voiceController:willSpeakSpokenInstruction:routeProgress:)
optional func voiceController(_ voiceController: RouteVoiceController, willSpeak instruction: SpokenInstruction, routeProgress: RouteProgress) -> SpokenInstruction?
optional func voiceController(_ voiceController: RouteVoiceController, willSpeak instruction: SpokenInstruction, routeProgress: RouteProgress?) -> SpokenInstruction?
}
2 changes: 1 addition & 1 deletion MapboxNavigationTests/NavigationViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class TestableDayStyle: DayStyle {


class FakeVoiceController: RouteVoiceController {
override func speak(_ instruction: SpokenInstruction, with locale: Locale?) {
override func speak(_ instruction: SpokenInstruction, with locale: Locale?, ignoreProgress: Bool = false) {
//no-op
}

Expand Down
Loading