home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Sound / Speech Recognition sample / _source / MainFunctions.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-15  |  5.3 KB  |  320 lines  |  [TEXT/CWIE]

  1. #ifndef __MAINFUNC__
  2. #include "MainFunctions.h"
  3. #endif
  4.  
  5. extern     SpeechChannel        theSpeechChan;
  6. extern    Boolean                gDone;
  7.  
  8. /*
  9.     This is a dispatcher for dealing with what the user said.  Add your functions to the switch
  10.     statement, add an entry in the 'LANG' resource, and it should "just work".
  11. */
  12. OSErr        ParseRefCon            (SpeechInfoPtr theSpeechInfo, CommandPtr theCommand)
  13. {
  14.     LanguageModelListPtr    language;
  15.     long                    type            = 0;
  16.     OSErr                    theErr            = noErr;
  17.     short                    word            = 0;
  18.     Boolean                    done            = false,
  19.                             found            = false;
  20.  
  21.     type = theCommand->type;
  22.     word = theCommand->ID;
  23.  
  24.     PlaySndCommand (8192);        /* Play the conformation tones */
  25.  
  26.     switch (type) {
  27.         case 'LANG':
  28.             language = theSpeechInfo->languages;
  29.             while (done == false) {
  30.                 if (language->resID != word) {
  31.                     if (language->nextLanguage != nil) {
  32.                         language = language->nextLanguage;
  33.                     }
  34.                     else {
  35.                         done = true;
  36.                         found = false;
  37.                     }
  38.                 }
  39.                 else {
  40.                     done = true;
  41.                     found = true;
  42.                 }
  43.             }
  44.             if (found == true) {
  45.                 theErr = SRSetLanguageModel (theSpeechInfo->theRecognizer, language->theLanguage);
  46.             }
  47.             break;
  48.         case 'chan':
  49.             theErr = ChannelCommand (word);
  50.             break;
  51.         case 'lght':
  52.             theErr = LightCommand (word);
  53.             break;
  54.         case 'scpt':
  55.             theErr = ScriptCommand (word);
  56.             break;
  57.         case 'call':
  58.             theErr = PhoneCommand (word);
  59.             break;
  60.         case 'game':
  61.             theErr = GameCommand (word);
  62.             break;
  63.         case 'stro':
  64.             theErr = StereoCommand (word);
  65.             break;
  66.         case 'tv  ':
  67.             theErr = TVCommand (word);
  68.             break;
  69.         case 'vcr ':
  70.             theErr = VCRCommand (word);
  71.             break;
  72.         case 'ldsk':
  73.             theErr = LaserdiskCommand (word);
  74.             break;
  75.         case 'vol ':
  76.             theErr = VolumeCommand (word);
  77.             break;
  78.         case 'snd ':
  79.             theErr = PlaySndCommand (word);
  80.             break;
  81.         case 'quit':
  82.             theErr = QuitCommand (word);
  83.             break;
  84.     }
  85.  
  86.     return theErr;
  87. }
  88.  
  89. OSErr    ChannelCommand        (long commandNumber)
  90. {
  91.     OSErr        theErr        = noErr;
  92.  
  93.     switch (commandNumber) {
  94.         case 0:
  95.             break;
  96.         case 1:
  97.             break;
  98.         default:
  99.             ;
  100.     }
  101.  
  102.     return theErr;
  103. }
  104.  
  105. OSErr    LightCommand        (long commandNumber)
  106. {
  107.     OSErr        theErr        = noErr;
  108.  
  109.     switch (commandNumber) {
  110.         case 255:
  111.             break;
  112.         case 25:
  113.             break;
  114.         case 0:
  115.             break;
  116.         default:
  117.             ;
  118.     }
  119.  
  120.     return theErr;
  121. }
  122.  
  123. OSErr    ScriptCommand        (long commandNumber)
  124. {
  125.     ComponentInstance    cmptInstance    = 0;
  126.     AEDesc                desc            = {0, nil};
  127.     OSAID                scriptID        = 0,
  128.                         resultingOSAID    = 0;
  129.     OSErr                theErr            = noErr;
  130.  
  131.     cmptInstance = OpenDefaultComponent (kOSAComponentType, 'scpt');
  132.     if (cmptInstance == nil)
  133.         DebugStr ("\pOpenDefaultComponent failed, do you have AppleScript installed?");
  134.     desc.descriptorType = 'scpt';
  135.  
  136.     desc.dataHandle = Get1Resource ('scpt', commandNumber);
  137.     theErr = ResError ();
  138.     if (desc.dataHandle == nil || theErr != noErr)
  139.         DebugStr ("\pGet1Resource failed");
  140.  
  141.     theErr = OSALoad (cmptInstance, &desc, kOSAModeNull, &scriptID);
  142.     if (theErr != noErr)
  143.         DebugStr ("\pOSALoad failed");
  144.  
  145.     ReleaseResource (desc.dataHandle);
  146.     theErr = OSAExecute (cmptInstance, scriptID, kOSANullScript, kOSAModeNull, &resultingOSAID);
  147.     if (theErr != noErr)
  148.         DebugStr ("\pOSAExecute failed");
  149.  
  150.     theErr = OSADispose (cmptInstance, scriptID);
  151.     if (theErr != noErr)
  152.         DebugStr ("\pOSADispose failed");
  153.  
  154.     theErr = CloseComponent (cmptInstance);
  155.     if (theErr != noErr)
  156.         DebugStr ("\pCloseComponent failed");
  157.  
  158.     return theErr;
  159. }
  160.  
  161. OSErr    PhoneCommand        (long commandNumber)
  162. {
  163.     OSErr        theErr        = noErr;
  164.  
  165.     switch (commandNumber) {
  166.         default:
  167.             ;
  168.     }
  169.  
  170.     return theErr;
  171. }
  172.  
  173. OSErr    GameCommand            (long commandNumber)
  174. {
  175.     OSErr        theErr        = noErr;
  176.  
  177.     switch (commandNumber) {
  178.         default:
  179.             ;
  180.     }
  181.  
  182.     return theErr;
  183. }
  184.  
  185. OSErr    StereoCommand        (long commandNumber)
  186. {
  187.     OSErr        theErr        = noErr;
  188.  
  189.     switch (commandNumber) {
  190.         case 0:
  191.             break;
  192.         case 1:
  193.             break;
  194.         default:
  195.             ;
  196.     }
  197.  
  198.     return theErr;
  199. }
  200.  
  201. OSErr    TVCommand            (long commandNumber)
  202. {
  203.     OSErr        theErr        = noErr;
  204.  
  205.     switch (commandNumber) {
  206.         case 0:
  207.             break;
  208.         case 1:
  209.             break;
  210.         default:
  211.             ;
  212.     }
  213.  
  214.     return theErr;
  215. }
  216.  
  217. OSErr    LaserdiskCommand    (long commandNumber)
  218. {
  219.     OSErr        theErr        = noErr;
  220.  
  221.     switch (commandNumber) {
  222.         case 0:
  223.             break;
  224.         case 1:
  225.             break;
  226.         case 2:
  227.             break;
  228.         case 3:
  229.             break;
  230.         case 4:
  231.             break;
  232.         default:
  233.             ;
  234.     }
  235.  
  236.     return theErr;
  237. }
  238.  
  239. OSErr    VCRCommand            (long commandNumber)
  240. {
  241.     OSErr        theErr        = noErr;
  242.  
  243.     switch (commandNumber) {
  244.         case 0:
  245.             break;
  246.         case 1:
  247.             break;
  248.         case 2:
  249.             break;
  250.         case 3:
  251.             break;
  252.         case 4:
  253.             break;
  254.         case 5:
  255.             break;
  256.         case 6:
  257.             break;
  258.         default:
  259.             ;
  260.     }
  261.  
  262.     return theErr;
  263. }
  264.  
  265. OSErr    VolumeCommand        (long commandNumber)
  266. {
  267.     OSErr        theErr        = noErr;
  268.  
  269.     switch (commandNumber) {
  270.         case 0:
  271.             break;
  272.         case 1:
  273.             break;
  274.         case 2:
  275.             break;
  276.         default:
  277.             ;
  278.     }
  279.  
  280.     return theErr;
  281. }
  282.  
  283. OSErr    PlaySndCommand        (long commandNumber)
  284. {
  285.     Handle        theSound    = nil;
  286.     OSErr        theErr        = noErr;
  287.  
  288.     switch (commandNumber) {
  289.         default:
  290.             theSound = GetResource ('snd ', commandNumber);
  291.             theErr = ResError ();
  292.             HLock (theSound);
  293.             if (theSound != nil && theErr == noErr) {
  294.                 SndPlay (nil, (SndListHandle)theSound, false);
  295.                 HUnlock (theSound);
  296.                 ReleaseResource (theSound);
  297.             }
  298.     }
  299.  
  300.     return theErr;
  301. }
  302.  
  303. OSErr    QuitCommand            (long commandNumber)
  304. {
  305. #pragma unused (commandNumber)
  306.  
  307.     Ptr            goodbye        = "You're welcome, good bye.";
  308.     short        goodbyeLen    = 25;
  309.     OSErr        theErr        = noErr;
  310.  
  311.     theErr = SpeakBuffer (theSpeechChan, (char *)goodbye, goodbyeLen, 0);
  312.     if (theErr != noErr) {
  313.         DebugStr ("\pSpeakBuffer failed");
  314.     }
  315.  
  316.     gDone = true;
  317.  
  318.     return theErr;
  319. }
  320.