I am setting up a tts app with AVSpeechSynthesizer
. I have to do real-time pitch and rate adjustments. I am using UISLider
for adjusting pitch and rate.
Here is my code:-
@IBAction func sl(_ sender: UISlider) {
if synthesizer.isSpeaking {
synthesizer.stopSpeaking(at: .immediate)
self.rate = sender.value
if currentRange.length > 0 {
let valuee = currentRange.length + currentRange.location
let neww = self.tvEditor.text.dropFirst(valuee)
self.tvEditor.text = String(neww)
synthesizer.speak(buildUtterance(for: rate, pitch: pitch, with: String(neww), language: self.preferredVoiceLanguageCode2 ?? "en"))
}
} else {
}
}
I may have understood your problem even if no details are provided: you can't take into account the new values of the rate
and pitchMultiplier
when the speech is running.
To explain the following details, I read this example that contains code snippets (ObjC, Swift) and illustrations.
AVSpeechUtterance
instances with their rate
and pitchMultiplier
properties.Now, if you want to change the property values in real-time, see the steps hereafter once one of your sliders moves:
AVSpeechSynthesizerDelegate
protocol.stopSpeaking
synthesizer method that will remove from the queue the utterances that haven't been spoken yet. The synthesizer queues all information to be spoken long before you ask for new values that don't impact the stored utterances: you must remove and recreate the utterances with their new property values to be spoken.
If the code example provided by the link above isn't enough, I suggest to take a look at this WWDC video detailed summary dealing with AVSpeechSynthesizer
.