Lisp HUG Maillist Archive

Cocoa speech recognition

I've been trying to get Coca speech recognition working fully on Mac OS X 10.5.2.

So far, I've succeeded in getting a microphone to appear on-screen, display the commands I've fed it and react to sounds by showing changes in sound level.

I have not succeeded in getting it to show signs of actually recognizing any commands, however, even though I've tried command phrases that are consistently recognized when I test speech recognition with Apple's speakable commands.

Here's my code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(objc:ensure-objc-initialized
  :modules
  '("/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation"
    "/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa"))

(defmacro @ (&body body) `(objc:invoke ,@body))

(defun can (an-object a-method)
  (objc:can-invoke-p an-object a-method))


;;; Speech synthesis used for a simple response
(defparameter talker (@ (@ "NSSpeechSynthesizer" "alloc") "init"))

(defun say (it) (@ talker "startSpeakingString:" it))


(defparameter commands (vector "Tell me a joke." "Quit" "Tex" "What time is it?"))

(defparameter recognizer (@ (@ "NSSpeechRecognizer" "alloc") "init"))


;;; A delegate object class to handle speechRecognizer:didRecognizeCommand:
(objc:define-objc-class hero (objc:standard-objc-object)
                   ()
                   (:objc-class-name "Hero"))

(objc:define-objc-method ("speechRecognizer:didRecognizeCommand:" :void)
                    ((self hero)
                     (sender objc:objc-object-pointer)
                     (this objc:objc-object-pointer))
  (say this))

(defparameter my-hero (@ (@ "Hero" "alloc") "init"))

(@ recognizer "setCommands:" commands)

(@ recognizer "setDelegate:" my-hero) ; sets recognizer delegate

(defun start-listening () (@ recognizer "startListening"))
(defun stop-listening () (@ recognizer "stopListening"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

From the testing I've done, my recognizer is successfully assigned its commands and its delegate object. The following expression produces the expected result (saying "hello"):

(@ (@ recognizer "delegate") "speechRecognizer:didRecognizeCommand:" recognizer "hello")

So the delegate object (my-hero) has a method that responds as expected.

Clues, anyone? I was going to try subclassing NSSpeechRecognizer and/or telling it to init its super-class, but those don't seem to be likely fixes or, for that matter, things LispWorks even allows.

Laughing Water
Updated at: 2020-12-10 08:44 UTC