Skip to content

Instantly share code, notes, and snippets.

@Nexengineer
Created December 16, 2017 05:20
Show Gist options
  • Save Nexengineer/953bf1988441372a2c02a851e9af6eb2 to your computer and use it in GitHub Desktop.
Save Nexengineer/953bf1988441372a2c02a851e9af6eb2 to your computer and use it in GitHub Desktop.
This GIST is for converting Speech To Text.
import Foundation
import Speech
protocol SpeechToTextDelegate {
func conversionGoingOn(_ convertedString: String)
func conversionFinished(_ convertedString: String)
}
class SpeechToText: NSObject, SFSpeechRecognizerDelegate {
// Creating singleton
static let sharedIntance = SpeechToText()
private override init() {
super.init()
speechRecognizer?.delegate = self
getPermissionForSpeechRecognition()
}
var delegateSpeechToText: SpeechToTextDelegate?
// This is the speech recognizer
let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))
// This is a Request
var regRequest: SFSpeechAudioBufferRecognitionRequest?
var regTask: SFSpeechRecognitionTask?
let avEngine = AVAudioEngine()
var speechText = String()
var timer: Timer?
// Method for stop recognition
func stopRecognition() {
avEngine.stop()
regRequest?.endAudio()
avEngine.inputNode?.removeTap(onBus: 0)
speechText = ""
timer?.invalidate()
regRequest = nil
regTask = nil
}
// Method for starting recognition
func startRecognition() {
//Cancel task if already running
if regTask != nil {
regTask?.cancel()
regTask = nil
}
//Create and AVAudioSession for audio recording
let avAudioSession = AVAudioSession.sharedInstance()
do {
try avAudioSession.setCategory(AVAudioSessionCategoryRecord)
try avAudioSession.setMode(AVAudioSessionModeMeasurement)
try avAudioSession.setActive(true, with: .notifyOthersOnDeactivation)
} catch {
print("Audio Session is not active")
}
//Check the Audio input.
guard let inputEngineNode = avEngine.inputNode else {
fatalError("Some Error")
}
regRequest = SFSpeechAudioBufferRecognitionRequest()
guard let recognitionRequest = regRequest else {
fatalError("SFSpeechAudioBufferRecognitionRequest object is not created")
}
recognitionRequest.shouldReportPartialResults = true
//Start task of speech recognition
regTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
if result != nil {
DispatchQueue.main.async {
self.timer?.invalidate()
self.timer = nil
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(SpeechToText.timerStopped), userInfo: nil, repeats: false)
self.speechText = (result?.bestTranscription.formattedString)!
self.delegateSpeechToText?.conversionGoingOn(self.speechText)
}
}
})
//Set Formation of Audio Input
inputEngineNode.removeTap(onBus: 0)
let recordingFormat = inputEngineNode.outputFormat(forBus: 0)
inputEngineNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in
self.regRequest?.append(buffer)
}
avEngine.prepare()
do {
try avEngine.start()
} catch {
print("some error")
}
}
// When conversation stops
func timerStopped() {
delegateSpeechToText?.conversionFinished(self.speechText)
stopRecognition()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment