Skip to content

Instantly share code, notes, and snippets.

@b3ll
Created December 30, 2016 18:19
Show Gist options
  • Save b3ll/f3ba5db48e19e58ba2319324aaae1eb3 to your computer and use it in GitHub Desktop.
Save b3ll/f3ba5db48e19e58ba2319324aaae1eb3 to your computer and use it in GitHub Desktop.
For some reason the character range in the delegate is totally messed up… unless I'm using it incorrectly?
//
// AppDelegate.swift
// AVSpeechSynthesizerTest
//
// Created by Adam Bell on 12/30/16.
// Copyright © 2016 Adam Bell. All rights reserved.
//
import AVFoundation
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, AVSpeechSynthesizerDelegate {
var window: UIWindow?
let synthesizer = AVSpeechSynthesizer()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
synthesizer.delegate = self
synthesizer.speak(AVSpeechUtterance(string: "Phil Schiller says the MacBook Pro doesn't need an SD card slot"))
return true
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
let substring = (utterance.speechString as NSString).substring(with: characterRange)
print("\(substring)")
/**
Ends up printing out:
Phil
Schiller
says
the
Book Pro doesn't
d an SD
card
sl
t
instead of something like:
Phil
Schiller
says
the
MacBook Pro
doesn't
need
an
SD
card
slot
*/
}
}
@aclauss
Copy link

aclauss commented Jun 27, 2017

I’ve encountered the issue as well. This seems to be a bug in AVSpeechSynthesizer. The character range is wrong for certain words only and remains wrong afterwards. If the spoken text includes these words multiple times, the error will cumulate, so the character range will be soon totally off. The words which trigger this issue are different for each voice. So words which trigger the bug for one voice will be no problem for another one and vice versa. And the error for a character range can be also very different. I’ve encountered an offset between 1 and 18 characters for a single word compared to the real range. But also the length that is reported can be wrong. Sometimes even a single word is split into pieces. This makes it all very hard to correct the unreliable values from the character range.

Until Apple fixes this (this bug seems to exist for a long time, so I guess Apple did not receive enough bugmreports to take this bug serious), there’s no real solution for this. I guess the only „solution“ would be to break down the text into a single sentence per „utterage“ object, so for longer texts the error can not cumulate too much. But your example shows that even in a single sentence the error can get out of control.

If you’ve found any solution or workaround, I’d like to hear about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment