Search code examples
swiftmacosspeech

speech to text macOS swift playground


I am trying to get speech to text working on my mac in swift. I found a few articles but they are all for iOS. I'm trying to follow this one: http://www.appcoda.com/siri-speech-framework/

So far this is my code in Playground:

//: Playground - noun: a place where people can play

import Cocoa
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
import Speech

while true {
    microphoneButton.isEnabled = false  //2

    speechRecognizer.delegate = self  //3

    SFSpeechRecognizer.requestAuthorization { (authStatus) in  //4

        var isButtonEnabled = false

        switch authStatus {  //5
        case .authorized:
            isButtonEnabled = true

        case .denied:
            isButtonEnabled = false
            print("User denied access to speech recognition")

        case .restricted:
            isButtonEnabled = false
            print("Speech recognition restricted on this device")

        case .notDetermined:
            isButtonEnabled = false
            print("Speech recognition not yet authorized")
        }

        OperationQueue.main.addOperation() {
            self.microphoneButton.isEnabled = isButtonEnabled
        }
    }
}

It comes up with the error, "No such Module Speech"

My questions are, is it possible in macOS? Is it possible in playgrounds?

Thanks in advance, Jersh


Solution

  • It seems like the Speech library is only available on iOS, so you're out of luck with SFSpeechRecognizer. NSSpeechRecognizer could be an alternative but requires you to give a list of words for it to recognize from (rather than recognizing from any arbitrary word).