Skip to content

Instantly share code, notes, and snippets.

View acotilla91's full-sized avatar

Alejandro Cotilla acotilla91

View GitHub Profile
@acotilla91
acotilla91 / AVSpeechSynthesizer-Sample.swift
Last active May 31, 2018 06:21
AVSpeechSynthesizer | Text-to-Speech | iOS
let synth = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "Some text")
let voice = AVSpeechSynthesisVoice(language: "en-US")
synth.speak(utterance)
@acotilla91
acotilla91 / SpeechService.swift
Last active May 31, 2018 22:17
Utility class to communicate with Google Cloud Text-to-Speech API in Swift.
import UIKit
import AVFoundation
enum VoiceType: String {
case undefined
case waveNetFemale = "en-US-Wavenet-F"
case waveNetMale = "en-US-Wavenet-D"
case standardFemale = "en-US-Standard-E"
case standardMale = "en-US-Standard-D"
}
@IBAction func didPressSpeakButton(_ sender: Any) {
SpeechService.shared.speak(text: textView.text) {
// Finished speaking!
}
}
@IBAction func didPressSpeakButton(_ sender: Any) {
// Disable the "Speak" button while processing/speaking
speakButton.setTitle("Speaking...", for: .normal)
speakButton.isEnabled = false
speakButton.alpha = 0.6
// Choose voice type
var voiceType: VoiceType = .undefined
let category = voiceCategoryControl.titleForSegment(at: voiceCategoryControl.selectedSegmentIndex)
let gender = voiceGenderControl.titleForSegment(at: voiceGenderControl.selectedSegmentIndex)
@acotilla91
acotilla91 / SpeechRecognizer.xml
Created June 3, 2018 19:21
Roku SpeechRecognizer component header.
<component name="SpeechRecognizer" extends="Task" >
<script type="text/brightscript" uri="pkg:/components/SpeechRecognizer.brs"/>
<interface>
<!-- Callback listener -->
<field id="delegate" type="node"/>
<function name="startListening"/>
</interface>
@acotilla91
acotilla91 / SpeechRecognizer.brs
Created June 3, 2018 19:24
Roku SpeechRecognizer component implementation.
function startListening(params = invalid)
m.top.functionName = "runRecognizer"
m.top.control = "RUN"
end function
sub runRecognizer()
mic = createObject("roMicrophone")
' If can't record, there's no point on continuing
if not mic.CanRecord()
m.recognizer = CreateObject("roSGNode", "SpeechRecognizer")
m.recognizer.delegate = m.top
m.recognizer.callFunc("startListening", {})
function recognizer()
if m.recognizer = invalid
m.recognizer = CreateObject("roSGNode", "SpeechRecognizer")
m.recognizer.delegate = m.top
end if
return m.recognizer
end function
function onKeyEvent(key as String, press as Boolean) as Boolean
if key = "OK" and press
<component name="AppScene" extends="Scene">
<interface>
<!-- Speech Recognizer callbacks -->
<function name="speechRecognizerDidReceiveTranscript"/>
</interface>
<script type="text/brightscript" uri="pkg:/components/AppScene.brs"/>
</component>
function speechRecognizerDidReceiveTranscript(transcript as String)
m.label.text = transcript
m.infoLb.text = "Hold the OK button to start dictation, release it once you're done."
end function