Skip to content

Instantly share code, notes, and snippets.

@wizard1066
Created January 15, 2023 19:57
Show Gist options
  • Save wizard1066/a1258d4fac3f03496beaf126e359d007 to your computer and use it in GitHub Desktop.
Save wizard1066/a1258d4fac3f03496beaf126e359d007 to your computer and use it in GitHub Desktop.
import Foundation
import AVFoundation
import Combine
var nextPage = PassthroughSubject<Void,Never>()
var reporter = PassthroughSubject<String, Never>()
class speaking: NSObject, AVSpeechSynthesizerDelegate {
var synth:AVSpeechSynthesizer!
var lastWords = ""
override init() {
super.init()
synth = AVSpeechSynthesizer()
}
func speaker(words:[String]) {
lastWords = String(words.last!)
for word in words {
let utterance = AVSpeechUtterance(string: word)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
utterance.rate = 0.5
utterance.postUtteranceDelay = 0.1
synth.delegate = self
synth.speak(utterance)
}
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
didFinish utterance: AVSpeechUtterance) {
if utterance.speechString == lastWords {
nextPage.send()
}
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
didStart utterance: AVSpeechUtterance) {
reporter.send(utterance.speechString)
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment