Repository navigation
Use MapboxVoice instead of aws - #617
Conversation
There was a problem hiding this comment.
The step table needs this dependency: RouteTableViewCell → VisualInstructionFormatter → RouteStepFormatter → OSRMInstructionFormatter.
There was a problem hiding this comment.
Oh this was a mistake.
There was a problem hiding this comment.
Will the Voice API choose a voice based on the locale?
There was a problem hiding this comment.
Yeah, I'll add this to the list.
There was a problem hiding this comment.
Can we keep the structure with one TTS service per class?
For example, Polly doesn't speak Mandarin so extending RouteVoiceController with AMandarinSpeakingVoiceController shouldn't fall back to MapboxVoice then AVSpeech but directly to AVSpeech.
6bda83c to
e850c44
Compare
e850c44 to
7d574d4
Compare
9c71dbc to
0310bee
Compare
There was a problem hiding this comment.
Will remove once this PR is ready. It makes it easier for me to keep both while developing between branches.
cc34ef2 to
87d510e
Compare
|
@mapbox/navigation-ios this is ready for review. |
| } | ||
|
|
||
| spokenInstructionsForRoute.countLimit = 200 | ||
| sessionConfiguration.timeoutIntervalForRequest = timeoutIntervalForRequest; |
| for instruction in instructions { | ||
| guard spokenInstructionsForRoute.object(forKey: instruction.ssmlText as NSString) == nil else { continue } | ||
|
|
||
| cacheAndPlay(instruction: instruction, play: false) |
There was a problem hiding this comment.
The new code targeting the Mapbox Speech API should live in its own RouteVoiceController subclass for the following reasons:
- It keeps the failable network request and precaching code more modular.
- It makes the fallback to AVSpeechSynthesizer more clear.
- Even if the Mapbox Speech API is available out of the box, a developer may already be using a third-party speech recognition SDK that comes with its own speech synthesizer. Or they may be using a third-party speech synthesizer custom-tailored to a language that the Speech API doesn’t support.
We can have the navigation SDK use this new voice controller by default, but it should still be possible to swap in a different voice controller.
|
@1ec5 good call, updated. |
| import MapboxDirections | ||
|
|
||
| /** | ||
| `PollyVoiceController` extends the default `RouteVoiceController` by providing support for AWSPolly. `RouteVoiceController` will be used as a fallback during poor network conditions. |
There was a problem hiding this comment.
Typo: PollyVoiceController. Also, AWSPolly is no longer a direct dependency.
| `PollyVoiceController` extends the default `RouteVoiceController` by providing support for AWSPolly. `RouteVoiceController` will be used as a fallback during poor network conditions. | ||
| */ | ||
| @objc(MBMapboxSpeechController) | ||
| public class MapboxSpeechController: RouteVoiceController { |
There was a problem hiding this comment.
MapboxSpeechController and RouteVoiceController have dissimilar names, so the relationship is unclear. Some possibilities:
MapboxVoiceControllerinherits fromRouteVoiceControllerMapboxSpeechControllerinherits fromRouteSpeechControllerRemoteVoiceControllerinherits fromRouteVoiceController
| var urlSession: URLSession | ||
| var cacheURLSession: URLSession | ||
| var pollyTask: URLSessionDataTask? | ||
| var cachePollyTask: URLSessionDataTask? |
There was a problem hiding this comment.
Remove “Polly” from these property names.
| var cachePollyTask: URLSessionDataTask? | ||
| var spokenInstructionsForRoute = NSCache<NSString, NSData>() | ||
|
|
||
| var mapboxSpeechSynth: SpeechSynthesizer |
There was a problem hiding this comment.
Avoid abbreviating “synthesizer” as “synth”. It’s also a bit redundant that the class MapboxSpeechController in the module MapboxNavigation contains the property mapboxSpeechSynth.
| audioTask?.cancel() | ||
| let options = SpeechOptions(ssml: instruction.ssmlText) | ||
| if let locale = locale { | ||
| options.locale = locale |
There was a problem hiding this comment.
We should figure out a way to let this code know the value of RouteOptions.locale so it can automatically match the language of the SSML coming from the Directions API.
There was a problem hiding this comment.
The good news is that the default locale value is calculated in the same manner across libs. But yeah, its almost like we need a public LocaleManager class.
|
|
||
| let localizedErrorMessage = NSLocalizedString("FAILED_INSTRUCTION", bundle: .mapboxNavigation, value: "Unable to read instruction aloud.", comment: "Error message when the SDK is unable to read a spoken instruction.") | ||
|
|
||
| public init(locale: Locale, accessToken: String? = nil, host: String? = nil) { |
There was a problem hiding this comment.
I don’t think locale should be an argument on the initializer. A developer using just MapboxSpeechController in conjunction with RouteController (but not NavigationViewController) would need to manually set the locale, which runs counter to the goal of making localization as transparent as possible.
didPassSpokenInstructionPoint(_:) has access to the RouteProgress:
so RouteOptions is also easily accessible:
let routeProgresss = notification.userInfo![MBRouteControllerDidPassSpokenInstructionPointRouteProgressKey] as! RouteProgress
let routeOptions = routeProgress.route.routeOptions
…
cacheSpokenInstruction(instruction: instruction, locale: routeOptions.locale)Streamlining localization is also relevant to RouteVoiceController’s AVSpeechSynthesizer fallback, so I opened #933 to track the general issue.
|
|
||
| let utterance = AVSpeechUtterance(string: text) | ||
| speechSynth.delegate = self | ||
| speechSynth.speak(utterance) |
There was a problem hiding this comment.
The intent of this code was to demonstrate using AVSpeechSynthesizer as opposed to Polly. speechSynth (still present in this PR) is an instance of AVSpeechSynthesizer.
| let localizedErrorMessage = NSLocalizedString("FAILED_INSTRUCTION", bundle: .mapboxNavigation, value: "Unable to read instruction aloud.", comment: "Error message when the SDK is unable to read a spoken instruction.") | ||
|
|
||
| public init(accessToken: String? = nil, host: String? = nil) { | ||
| @objc public init(accessToken: String? = nil, host: String? = nil) { |
There was a problem hiding this comment.
This method needs documentation.
| @@ -9,7 +9,7 @@ import MapboxDirections | |||
| `MapboxVoiceController` extends the default `RouteVoiceController` by providing a more robust speech synthesizer via the Mapbox Speech API. `RouteVoiceController` will be used as a fallback during poor network conditions. | |||
There was a problem hiding this comment.
Is it the Mapbox Speech API or the Mapbox Voice API?
| /** | ||
| Speaks an instruction. | ||
|
|
||
| The cache is first checked to see if we have already downloaded the speech file. If not, the instruction is fetched and played. If there is an error anywhere along the way, the instruction will be spoken with the default speech synthesizer. |
There was a problem hiding this comment.
s/with the default speech synthesizer/by RouteVoiceController/
| /** | ||
| Fetches and plays an instruction. | ||
| */ | ||
| @objc open func fetch(instruction: SpokenInstruction) { |
There was a problem hiding this comment.
Set the argument’s label to _ instruction, so that the call site reads more naturally as “fetch modified instruction” instead of “fetch instruction modified instruction”.
| /** | ||
| Caches an instruction in an in-memory cache. | ||
| */ | ||
| @objc open func cacheSpokenInstruction(instruction: SpokenInstruction) { |
There was a problem hiding this comment.
cache(_ instruction: SpokenInstruction, for consistency with fetch(_:).
| | Vietnamese | ✅ | ❌ | | ||
|
|
||
| The `PollyVoiceController` is powered by Amazon Web Service's Polly product. If Polly supports a language, we will add support for it in [`PollyVoiceController`](https://github.com/mapbox/mapbox-navigation-ios/blob/1d74296aa4c6adc779193fad07f0c97de2f79e90/MapboxNavigation/PollyVoiceController.swift#L99). Reference [AWS Polly supported languages](https://docs.aws.amazon.com/polly/latest/dg/SupportedLanguage.html). | ||
| The `MapboxVoiceController` is powered by Amazon Web Service's Polly product. If Polly supports a language, we will add support for it in [`PollyVoiceController`](https://github.com/mapbox/mapbox-navigation-ios/blob/1d74296aa4c6adc779193fad07f0c97de2f79e90/MapboxNavigation/PollyVoiceController.swift#L99). Reference [AWS Polly supported languages](https://docs.aws.amazon.com/polly/latest/dg/SupportedLanguage.html). |
There was a problem hiding this comment.
Eventually, the MapboxVoiceController column should go away and this document should refer to the Mapbox Voice API documentation at the Mapbox API Documentation site.
| @@ -0,0 +1,251 @@ | |||
| // Generated by Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2) | |||
There was a problem hiding this comment.
Did you mean to check in the framework bundle?
|
@mapbox/navigation-ios this is ready for review. All server side API changes are ready and MapboxSpeech.swift v0.0.1 has been released. |
| @@ -1,7 +1,7 @@ | |||
| binary "https://www.mapbox.com/ios-sdk/Mapbox-iOS-SDK.json" ~> 3.7 | |||
| github "mapbox/MapboxDirections.swift" ~> 0.16.0 | |||
| github "mapbox/MapboxDirections.swift" "voiceLocale" | |||
There was a problem hiding this comment.
this has been or will be merged to master, right?
There was a problem hiding this comment.
@JThramer waiting mapbox/mapbox-directions-swift#235 before changing this.
| "$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/SDWebImage.framework", | ||
| "$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/Polyline.framework", | ||
| "$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/AWSPolly.framework", | ||
| "$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/AWSCore.framework", |
There was a problem hiding this comment.
We should remove this line

This removes the dependency on AWS and also makes it so anyone using MapboxCoreNavigation can get access to AWS Polly with only a Mapbox access token.
There is a lot do here, but so far so good.
/cc @1ec5 @ericrwolfe @frederoni @mapbox/navigation @willwhite