-
-
Save richimf/b042fe5037bbbd66513dc6125e02bacb to your computer and use it in GitHub Desktop.
[osx][swift]Example of Text-to-Speech as commandline program
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
// Text-to-Speech on OSX | |
// run: swift speech.swift "東京特許許可局" | |
// build: xcrun -sdk macosx swiftc speech.swift && ./speech "東京特許許可局" | |
// | |
// for OSX, use NSSpeechSYnthesizer in AppKit, | |
// (for iOS, use AVSpeechSynthesizer in AVFoundation) | |
import Foundation | |
import AppKit | |
// show the installed voice list with major attributes | |
for v in NSSpeechSynthesizer.availableVoices() { | |
print("\"\(v)\"") | |
let attrs = NSSpeechSynthesizer.attributesForVoice(v) | |
// swift < 2.1(from Xcode7.1): cannot interpolate string literals inside | |
if case let val? = attrs["VoiceName"] {print(" Name: \(val)")} | |
if case let val? = attrs["VoiceLanguage"] {print(" Lang: \(val)")} | |
if case let val? = attrs["VoideGender"] {print(" Gender: \(val)")} | |
if case let val? = attrs["VoiceAge"] {print(" Age: \(val)")} | |
//for (k, v) in attrs {print(" \(k)")} | |
} | |
// Code of text-to-speech | |
let loop = NSRunLoop.currentRunLoop() | |
let synth = NSSpeechSynthesizer() | |
func speech(text: String) { | |
synth.startSpeakingString(text) | |
let mode = loop.currentMode ?? NSDefaultRunLoopMode | |
while loop.runMode(mode, beforeDate: NSDate(timeIntervalSinceNow: 0.1)) | |
&& synth.speaking {} | |
} | |
// example: in default English | |
speech("Hello World!") | |
// example in Japanese | |
//synth.setVoice("com.apple.speech.synthesis.voice.kyoko.premium") | |
for v in NSSpeechSynthesizer.availableVoices() { | |
let attrs = NSSpeechSynthesizer.attributesForVoice(v) | |
if attrs["VoiceLanguage"] as? String == "ja-JP" { | |
synth.setVoice(v) | |
break | |
} | |
} | |
speech("こんにちは世界!") | |
// speech commandline arguments | |
for m in Process.arguments[1 ..< Process.arguments.count] { | |
speech(m) | |
} | |
//NEW IN SWIFT 3 | |
let loop = RunLoop.current | |
let synth = NSSpeechSynthesizer() | |
func speech(_ text: String) { | |
synth.startSpeaking(text) | |
synth.setVoice(<#T##voice: String?##String?#>) | |
let mode = loop.currentMode ?? RunLoopMode.defaultRunLoopMode | |
while loop.run(mode: mode, before: NSDate(timeIntervalSinceNow: 0.1) as Date) | |
&& synth.isSpeaking {} | |
} | |
// AVFoundation | |
func speech(text: String) { | |
// Line 1. Create an instance of AVSpeechSynthesizer. | |
let speechSynthesizer = AVSpeechSynthesizer() | |
// Line 2. Create an instance of AVSpeechUtterance and pass in a String to be spoken. | |
let speechUtterance: AVSpeechUtterance = AVSpeechUtterance(string: text) | |
//Line 3. Specify the speech utterance rate. 1 = speaking extremely the higher the values the slower speech patterns. The default rate, AVSpeechUtteranceDefaultSpeechRate is 0.5 | |
speechUtterance.rate = AVSpeechUtteranceMaximumSpeechRate / 2.5 | |
// Line 4. Specify the voice. It is explicitly set to English here, but it will use the device default if not specified. | |
speechUtterance.voice = AVSpeechSynthesisVoice(language: "es-MX") | |
// Line 5. Pass in the urrerance to the synthesizer to actually speak. | |
speechSynthesizer.speak(speechUtterance) | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment