/* file: SpeechEvent.c author: Michael J. Norton date: 07/06/2000 oreillynet.com C source file contains the exported AE speech done event handler. Requirements: Carbon SDK (available from www.apple.com Apple Developer Connection */ #include #include #include #include "SpeechEvent.h" #include "LangModel.h" /* private entries */ static OSErr ExtractResult (SRLanguageModel resultLM, SRRecognizer recognizer); /* ExtractResult ------------- OSErr ExtractResult (SRLanguageModel resultLM, SRRecognizer recognizer) Function extracts the Language Model result that was provided by the recognizer. note: see The Speech Recognition Manager Revealed */ static OSErr ExtractResult (SRLanguageModel resultLM, SRRecognizer recognizer) { OSErr status = noErr; if (resultLM && recognizer) { long refcon; long propertySize = sizeof (refcon); /* get the refcon of the root object*/ status = SRGetProperty (resultLM, kSRRefCon, &refcon, &propertySize); /* is the resultLM a subset of our top language model or is it the rejection word, "???" ? */ if (!status && refcon == kTopLMRefcon) { SRLanguageObject languageObject; propertySize = sizeof (languageObject); /* The resultLM contains either an SRPath or an SRPhrase. The SRPhrases in gTopLanguageModel */ /* were created by the SRAddText calls in BuildLanguageModel. The SRPath in */ /* gTopLanguageModel was created in AddFancierLanguageModel. We use the refcon property */ /* we set in our language model building routines to distinguish between the results */ /* We expect our result language model to contain only 1 item, a phrase or a path; get it */ status = SRGetIndexedItem (resultLM, &languageObject, 0); if (!status) { long refcon; propertySize = sizeof (refcon); /* get the refcon of the object at the root of our language model */ status = SRGetProperty (languageObject, kSRRefCon, &refcon, &propertySize); if (!status) { /* speak and display our response using the feedback character. Use */ /* the refcon as an index into our response array */ status = SRSpeakAndDrawText (recognizer, kResponseList[refcon], strlen (kResponseList[refcon])); } /* if !status */ /* always SRReleaseÉ what we SRGotÉ */ status = SRReleaseObject (languageObject); } } } return status; } /* exported entries */ /* note: see Listing 1-11, Speech Recognition Manager */ pascal OSErr MyHandleAESpeechDone(const AppleEvent *theAEevt, AppleEvent* reply, UInt32 refcon) { long actualSize; DescType actualType; OSErr recStatus = 0, myErr; SRRecognitionResult recResult; SRRecognizer recognizer; /* Step 1. get the recognition result status*/ myErr = AEGetParamPtr(theAEevt, keySRSpeechStatus, typeShortInteger, &actualType, (Ptr) &recStatus, sizeof(recStatus), &actualSize); /* Step 2. get the SRrecognizer */ if (!myErr && !recStatus) { myErr = AEGetParamPtr(theAEevt,keySRRecognizer, typeSRRecognizer, &actualType, (Ptr) &recognizer, sizeof(recognizer), &actualSize ); /* Step 3. get the SRRecognitionResult */ if (!myErr) { myErr = AEGetParamPtr(theAEevt, keySRSpeechResult, typeSRSpeechResult, &actualType, (Ptr) &recResult, sizeof(recResult), &actualSize); /* Step 4. extract the language model from the result */ if (!myErr) { SRLanguageModel resultLM; long propertySize = sizeof(resultLM); myErr = SRGetProperty(recResult, kSRLanguageModelFormat, &resultLM, &propertySize); /* process the language model */ /* run handler here*/ myErr = ExtractResult(resultLM, recognizer); /* release the Language Model */ SRReleaseObject(resultLM); } /* release the recognition result */ SRReleaseObject(recResult); } } return noErr; }