home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-31 | 6.8 KB | 300 lines | [TEXT/CWIE] |
- /*
-
- File: SpokenCommandHandler.cp
- Project: Sprocket Framework 1.1 b1 of 2/26/96
- Contains: Class which implements serial number-based protection (ie only X copies allowed
- to being running at any one time)
- To Do: ?
-
- Sprocket Major Contributors:
- ----------------------------
- Dave Falkenburg, producer of Sprocket 1.0
- Bill Hayden, producer of Sprocket 1.1
- Steve Sisak, producer of the upcoming Sprocket 2.0
-
- Pete Alexander Dave Mark
- Eric Berdahl Gary Powell
- Marshall Clow Leonard Rosenthal
- Tim Craycroft Steve Sisak
- David denBoer Jon Summers
- Cameron Esfahani Randy Thelen
- Steve Falkenburg Dean Yu
- Nitin Ganatra
- Dave Hershey Apple Computer, Inc.
-
- Comments, Additions, or Corrections:
- ------------------------------------
- Bill Hayden, Nikol Software <nikol@codewell.com>
-
- */
-
-
- #include "SpokenCommandHandler.h"
- #include "Sprocket.h"
- #include <Gestalt.h>
-
- #include "UString.h"
- #include <AppleEvents.h>
-
- #include "AEHandling.h"
-
-
- pascal OSErr HandleSpeechDoneAppleEvent (AppleEvent *theAEevt, AppleEvent* reply, long refcon);
-
-
-
- /*****************************************************************************/
-
-
-
- TSpokenCommandHandler::TSpokenCommandHandler()
- {
- fListening = false;
- }
-
-
-
- /*****************************************************************************/
-
-
-
-
- OSErr TSpokenCommandHandler::ISpokenCommandHandler()
- {
- OSErr status;
- long attributes;
-
- // Make sure SpeechRecognition Toolbox is available
-
- status = Gestalt (gestaltSpeechRecognitionVersion, &attributes);
-
- // Must be at least version 1.5 to support Speech Recognition Toolbox API
-
- if (!status)
- if (attributes < 0x00000150)
- status = -1;
-
- // Open a Recognition System
-
- if (!status)
- status = SROpenRecognitionSystem (&fRecognitionSystem, kSRDefaultRecognitionSystemID);
-
- // We don't want the default feedback or listening modes
- if (false && !status)
- {
- short feedbackModes = kSRNoFeedbackNoListenModes;
- status = SRSetProperty (fRecognitionSystem, kSRFeedbackAndListeningModes,
- &feedbackModes, sizeof (feedbackModes));
- }
-
- // Create a recognizer with default speech source -- e.g. the desktop microphone
-
- if (!status)
- status = SRNewRecognizer (fRecognitionSystem, &fRecognizer, kSRDefaultSpeechSource);
-
- // Install an AppleEvent handler so recognizer can send us recognition results.
-
- if (!status)
- status = AEInstallEventHandler(kAESpeechSuite, kAESpeechDone,
- NewAEEventHandlerProc (HandleSpeechDoneAppleEvent), 0, false);
-
- // Make one language model, then make it active
-
- // Make a simple language model (LM)
-
- if (!status)
- status = SRNewLanguageModel (fRecognitionSystem, &fLanguageModel, "SR", clen ("SR"));
-
- // Use this LM in recognition
-
- if (!status)
- status = SRSetLanguageModel (fRecognizer, fLanguageModel);
-
- // To start listening, call StartListening()
-
- return status;
- }
-
-
-
- /*****************************************************************************/
-
-
-
-
- OSErr TSpokenCommandHandler::RegisterSpokenCommand( const char* commandText, CommandID command )
- {
- OSErr status;
-
- status = SRAddText (fLanguageModel, commandText, clen(commandText), command);
-
- if (!status)
- status = SRSetLanguageModel (fRecognizer, fLanguageModel);
-
- return status;
- }
-
-
-
- /*****************************************************************************/
-
-
-
- TSpokenCommandHandler::~TSpokenCommandHandler()
- {
- OSErr status;
-
- status = SRReleaseObject (fLanguageModel);
-
- status = SRStopListening (fRecognizer); // stop processing incoming sound
- status = SRReleaseObject (fRecognizer); // balance SRNewRecognizer call
- status = SRCloseRecognitionSystem (fRecognitionSystem); // balance SROpenRecognitionSystem call
-
- if (status)
- DebugNum(status);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- OSErr TSpokenCommandHandler::StartListening()
- {
- OSErr err;
-
-
- // start processing incoming sound
-
- err = SRStartListening(fRecognizer);
-
- if (!err)
- fListening = true;
-
- return err;
- }
-
-
-
- /*****************************************************************************/
-
-
-
- OSErr TSpokenCommandHandler::StopListening()
- {
- OSErr err;
-
-
- // stop processing incoming sound
-
- err = SRStopListening(fRecognizer);
-
- if (!err)
- fListening = false;
-
- return err;
- }
-
-
-
- /*****************************************************************************/
-
-
-
- Boolean TSpokenCommandHandler::IsListening()
- {
- return fListening;
- }
-
-
-
- /*****************************************************************************/
-
- /* Here's an AppleEvent handler for handling the kAESpeechDone event of the
- kAESpeechSuite -- i.e. the event indicating a recognition attempt was made.
- The keySpeechStatus parameter gives the status of the recognition. If it
- is noErr, then the keySpeechResult parameter gives a RecognitionResult with the
- various representations of the words the user spoke.
- */
-
- pascal OSErr HandleSpeechDoneAppleEvent (AppleEvent *theAEevt, AppleEvent* /* reply */, long /* refcon */)
- {
- long actualSize;
- DescType actualType;
- OSErr status = 0, recStatus = 0;
- SRRecognitionResult recResult;
- Str255 str;
- Size len;
-
-
- // Get status
-
- status = AEGetParamPtr(theAEevt, keySRSpeechStatus, typeInteger,
- &actualType, (Ptr)&recStatus, sizeof(status), &actualSize);
-
- // Get result
-
- if (!status && !recStatus)
- status = AEGetParamPtr(theAEevt, keySRSpeechResult, typeSRSpeechResult, &actualType,
- (Ptr)&recResult, sizeof(SRRecognitionResult), &actualSize);
-
- // Get text of words in result.
- // Could also get actual phrase, path, or LM objects using other
- // format property selectors. If we did that, we'd also want to
- // release those objects when we were done using them here.
-
- if (!status)
- {
- len = 255L;
- status = SRGetProperty (recResult, kSRTEXTFormat, str+1, &len);
- if (!status)
- {
- str[0] = len;
-
- // Release RecognitionResult since we are done with it!!!
- // The toolbox has given us a reference to this object, and
- // assumes we are still using it until we call SRReleaseObject
- // with it.
- }
- }
-
- if (!status)
- {
- SRLanguageModel LM;
- SRLanguageObject anObject;
-
- len = sizeof(SRLanguageModel);
- status = SRGetProperty (recResult, kSRLanguageModelFormat, &LM, &len);
- if (!status)
- {
- long myCommand;
- status = SRGetIndexedItem(LM, &anObject, 0);
-
- if (!status)
- {
- len = sizeof(long);
- status = SRGetProperty (anObject, kSRRefCon, &myCommand, &len);
-
- // Release RecognitionResult since we are done with it!!!
- // The toolbox has given us a reference to this object, and
- // assumes we are still using it until we call SRReleaseObject
- // with it.
-
- if (myCommand != 0)
- status = SendCommandToSelf(myCommand);
-
- SRReleaseObject (anObject);
- }
-
- SRReleaseObject (recResult);
- SRReleaseObject (LM);
- }
- }
-
- if (status)
- DebugMessage ("\pSpokenCommandHandler: Recognition error in AE Handler");
-
- return status;
- }
-