home *** CD-ROM | disk | FTP | other *** search
/ Shocking The Web CD-ROM / SHOCK_CD.ISO / pc / tutorial / devglry / digidrms / shockwav / uscq.dcr / 00001_Main.ls next >
Encoding:
Text File  |  1996-04-15  |  6.4 KB  |  283 lines

  1. global ASRObj, gListening, gStatusMsg, MainLM, CapitalLM, kStartQuiz, kMapChannel, kWaveChannel, kCredits, kKeyWords, gWaveState, gPhrase, gIDList
  2.  
  3. on startMovie
  4.   cursor(4)
  5.   set kWaveChannel to 3
  6.   set kStartQuiz to 4
  7.   set kMapChannel to 6
  8.   set kCredits to 11
  9.   set gPhrase to EMPTY
  10.   set gIDList to EMPTY
  11.   put "Starting Up" into field "Status"
  12.   set gListening to 0
  13.   set the cpuHogTicks to 1
  14.   puppetSprite(kWaveChannel, 1)
  15.   puppetSprite(kStartQuiz, 1)
  16.   puppetSprite(kMapChannel, 1)
  17.   updateStage()
  18.   openXLib("ShockTalk")
  19.   if objectp(ASRObj) then
  20.     ASRObj(mdispose)
  21.   end if
  22.   set ASRObj to ASRXObject(mnew, factory("ASRCallBack"), 2)
  23.   set result to value(ASRObj)
  24.   if result < 0 then
  25.     put result into field "Status"
  26.     if result = -5551 then
  27.       alert("This demo requires PlainTalk‚Ñ¢ Speech Recognition extension version 1.2.1 or later. Please install the Speech Recognition extension before running this demo.")
  28.     else
  29.       if result = -5132 then
  30.         alert("Either the microphone is not plugged in or the Sound Input source is incorrect. Please check and try again.")
  31.       else
  32.         alert("Error" && result && "trying to initialize ShockTalk.")
  33.       end if
  34.     end if
  35.   else
  36.     set MainLM to createLM("<Main>", field "Lexicon")
  37.     set CapitalLM to birth(script "aLanguage", "<Capital>")
  38.     mAddItems(CapitalLM, field "States", 2)
  39.     mAddPhrase(MainLM, "<Capital>", 13)
  40.     mRelease(CapitalLM)
  41.     mSetLanguage(MainLM)
  42.     mRelease(MainLM)
  43.     put " " into field "Status"
  44.   end if
  45.   cursor(-1)
  46.   updateStage()
  47. end
  48.  
  49. on queue phrase, idList
  50.   set gPhrase to phrase
  51.   set gIDList to idList
  52. end
  53.  
  54. on idle
  55.   if gListening then
  56.     if gPhrase <> EMPTY then
  57.       doRecognize(gPhrase, gIDList)
  58.       set gPhrase to EMPTY
  59.       set gIDList to EMPTY
  60.     end if
  61.     showStatus()
  62.   end if
  63. end
  64.  
  65. on showStatus
  66.   set status to ASRObj(mStatus)
  67.   if status = 1 then
  68.     cursor(-1)
  69.     put gStatusMsg into field "Status"
  70.   else
  71.     if status = 2 then
  72.       animateWave()
  73.     end if
  74.   end if
  75.   updateStage()
  76. end
  77.  
  78. on stopMovie
  79.   puppetSprite(kWaveChannel, 0)
  80.   puppetSprite(kMapChannel, 0)
  81.   puppetSprite(kStartQuiz, 0)
  82.   if objectp(ASRObj) then
  83.     if gListening then
  84.       ASRObj(mStopListening)
  85.     end if
  86.     ASRObj(mdispose)
  87.     set ASRObj to 0
  88.   end if
  89.   closeXLib()
  90.   clearGlobals()
  91. end
  92.  
  93. on createLM lmName, textField
  94.   if objectp(ASRObj) then
  95.     set tmpLM to birth(script "aLanguage", lmName)
  96.     mAddField(tmpLM, textField)
  97.   end if
  98.   return tmpLM
  99. end
  100.  
  101. on startListening
  102.   global gState
  103.   if objectp(ASRObj) then
  104.     ASRObj(mStartListening)
  105.     set gListening to 1
  106.     set the castNum of sprite kStartQuiz to the number of cast "stop.pic"
  107.     set gWaveState to 2
  108.     if ASRObj(mGetProperty, "lkmd") = 1 then
  109.       set gStatusMsg to "Push-To-Talk"
  110.     else
  111.       set gStatusMsg to "Talk Now"
  112.     end if
  113.   end if
  114.   set the visible of sprite 9 to 0
  115.   set gState to randomItem(field "States")
  116.   set gAttempt to 0
  117.   askQuestion()
  118.   updateStage()
  119. end
  120.  
  121. on stopListening
  122.   if objectp(ASRObj) then
  123.     if gListening then
  124.       ASRObj(mStopListening)
  125.       set gListening to 0
  126.       set the castNum of sprite kStartQuiz to the number of cast "start.pic"
  127.       set the castNum of sprite 3 to the number of cast "logo2.pic"
  128.     end if
  129.   end if
  130.   cursor(-1)
  131.   set the visible of sprite 9 to 1
  132.   showMap("United States.PIC")
  133.   put " " into field "Status"
  134.   set gStatusMsg to " "
  135.   updateStage()
  136. end
  137.  
  138. on toggleListening
  139.   if gListening then
  140.     stopListening()
  141.   else
  142.     startListening()
  143.   end if
  144. end
  145.  
  146. on doRecognize phrase, phraseID
  147.   global gState, gAttempt
  148.   set firstItem to item 1 of phraseID
  149.   if firstItem = 1 then
  150.     stopListening()
  151.   else
  152.     if firstItem = 2 then
  153.       stopListening()
  154.     else
  155.       if firstItem = 13 then
  156.         if item 2 of gState = phrase then
  157.           SayIt("Correct")
  158.           set gState to randomItem(field "States")
  159.           set gAttempt to 0
  160.           askQuestion()
  161.         else
  162.           checkAttempt()
  163.           askQuestion()
  164.         end if
  165.       else
  166.         if firstItem = -1 then
  167.           checkAttempt()
  168.         else
  169.           if firstItem = 3 then
  170.             giveAnswer()
  171.           end if
  172.           if firstItem <> 5 then
  173.             set gState to randomItem(field "States")
  174.             set gAttempt to 0
  175.           end if
  176.         end if
  177.         askQuestion()
  178.       end if
  179.     end if
  180.   end if
  181. end
  182.  
  183. on askQuestion
  184.   global gState, gAttempt
  185.   if gListening then
  186.     ASRObj(mStopListening)
  187.   end if
  188.   SayIt("What Capital")
  189.   set stateName to item 1 of gState
  190.   SayIt(stateName)
  191.   showMap(stateName & ".PIC")
  192.   updateStage()
  193.   if gListening then
  194.     ASRObj(mStartListening)
  195.   end if
  196. end
  197.  
  198. on giveAnswer
  199.   global gState, gAttempt
  200.   if gListening then
  201.     ASRObj(mStopListening)
  202.   end if
  203.   set stateName to item 1 of gState
  204.   set capitalName to item 2 of gState
  205.   SayIt("Capital of")
  206.   SayIt(stateName)
  207.   SayIt("is")
  208.   SayIt(capitalName)
  209.   if gListening then
  210.     ASRObj(mStartListening)
  211.   end if
  212. end
  213.  
  214. on checkAttempt
  215.   global gState, gAttempt
  216.   set gAttempt to gAttempt + 1
  217.   if gAttempt = 3 then
  218.     giveAnswer()
  219.     set gState to randomItem(field "States")
  220.     set gAttempt to 0
  221.   else
  222.     SayIt("Try Again")
  223.   end if
  224. end
  225.  
  226. on SayIt theText
  227.   set sndName to theText & ".AIFF"
  228.   preLoadCast(the number of cast sndName)
  229.   puppetSound(sndName)
  230.   updateStage()
  231.   repeat while 1 = soundBusy(1)
  232.     if (the ticks mod 5) = 0 then
  233.       animateWave()
  234.     end if
  235.   end repeat
  236.   puppetSound(0)
  237.   updateStage()
  238. end
  239.  
  240. on randomItem textField
  241.   global gState
  242.   set lineCount to the number of lines in textField
  243.   repeat while 1
  244.     set theItem to line random(lineCount) of textField
  245.     if theItem <> gState then
  246.       exit repeat
  247.     end if
  248.   end repeat
  249.   return theItem
  250. end
  251.  
  252. on sayAll textField, theItem
  253.   repeat with index = 1 to the number of lines in textField
  254.     SayIt(item theItem of line index of textField)
  255.   end repeat
  256. end
  257.  
  258. on toggleKeyWord
  259.   set keyCast to the castNum of sprite kKeyWords
  260.   set castName to the name of cast keyCast
  261.   if castName = "KWRDICON.PIC" then
  262.     set the castNum of sprite kKeyWords to the number of cast "KEYWORDS.PIC"
  263.   else
  264.     set the castNum of sprite kKeyWords to the number of cast "KWRDICON.PIC"
  265.   end if
  266. end
  267.  
  268. on animateWave
  269.   global gWaveState
  270.   put "Don't Talk" into field "Status"
  271.   set the castNum of sprite 3 to gWaveState + 245
  272.   updateStage()
  273.   set gWaveState to (gWaveState mod 3) + 1
  274. end
  275.  
  276. on showMap whichMap
  277.   set the castNum of sprite kMapChannel to the number of cast whichMap
  278. end
  279.  
  280. factory ASRCallBack
  281. method mRecognize phrase, idList
  282.   queue(phrase, idList)
  283.