Search code examples
iosswiftuislideravspeechsynthesizer

Real time rate and pitch adjustments Swift


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 {

    }
}

Solution

  • 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. enter image description here

    1. Create your AVSpeechUtterance instances with their rate and pitchMultiplier properties.
    2. Add each one of them in an array that will represent the queue to be spoken.
    3. Make a loop inside the previous queue with the synthesizer to read out every elements.

    Now, if you want to change the property values in real-time, see the steps hereafter once one of your sliders moves:

    1. Get the current spoken utterance thanks to the AVSpeechSynthesizerDelegate protocol.
    2. Run the stopSpeaking synthesizer method that will remove from the queue the utterances that haven't been spoken yet.
    3. Create the previous removed utterances with the new property values.
    4. Redo steps 2/ and 3/ to resume where you stopped with these updated values.

    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.