Created
January 10, 2019 00:10
-
-
Save myrickchow32/3eefc77f3269e396207a872b0c4f4467 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import AVFoundation | |
| class VoiceUtils { | |
| static var avSpeechSynthesizer = AVSpeechSynthesizer() | |
| static func textToSpeech(text: String, delegate: AVSpeechSynthesizerDelegate?) { | |
| /* | |
| Support langauges: | |
| Chinese (China) - zh-CN | |
| Chinese (Hong Kong SAR China) - zh-HK | |
| Chinese (Taiwan) - zh-TW | |
| English (Australia) - en-AU | |
| English (Ireland) - en-IE | |
| English (South Africa) - en-ZA | |
| English (United Kingdom) - en-GB | |
| English (United States) - en-US | |
| AVSpeechUtterance property | |
| voice, rate (Speed of speech), volume, pitchMultiplier | |
| /*! | |
| @property "rate" | |
| @abstract Changes the playback rate of the input signal | |
| @discussion | |
| A value of 2.0 results in the output audio playing one octave higher. | |
| A value of 0.5, results in the output audio playing one octave lower. | |
| Range: 0.5 -> 2.0 | |
| Default: 1.0 | |
| Mixer: AVAudioEnvironmentNode | |
| */ | |
| @property "pitchMultiplier" | |
| // [0.5 - 2] Default = 1 | |
| */ | |
| let utterance = AVSpeechUtterance(string: text) | |
| utterance.voice = AVSpeechSynthesisVoice(language: DataUtils.getTextToSpeechLang()) | |
| utterance.rate = Float(DataUtils.selectedVoiceSpeed.rawValue) ?? 1.0 | |
| utterance.pitchMultiplier = Float(DataUtils.selectedVoiceTone.rawValue) ?? 1.0 | |
| stopSpeech() | |
| avSpeechSynthesizer = AVSpeechSynthesizer() | |
| avSpeechSynthesizer.delegate = delegate | |
| avSpeechSynthesizer.speak(utterance) | |
| } | |
| static func isSpeechPlaying() -> Bool { | |
| return avSpeechSynthesizer.isSpeaking | |
| } | |
| static func isSpeechPaused() -> Bool { | |
| return avSpeechSynthesizer.isPaused | |
| } | |
| static func stopSpeech() { | |
| avSpeechSynthesizer.stopSpeaking(at: .immediate) | |
| } | |
| static func pauseSpeech() { | |
| avSpeechSynthesizer.pauseSpeaking(at: .immediate) | |
| } | |
| static func resumeSpeech() { | |
| avSpeechSynthesizer.continueSpeaking() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment