set ASRObj to ASRXObject(mnew, factory("ASRCallBack"), 2)
set result to value(ASRObj)
if result < 0 then
put result into field "Status"
if result = -5551 then
alert("This demo requires PlainTalk‚Ñ¢ Speech Recognition extension version 1.2.1 or later. Please install the Speech Recognition extension before running this demo.")
else
if result = -5132 then
alert("Either the microphone is not plugged in or the Sound Input source is incorrect. Please check and try again.")
else
alert("Error" && result && "trying to initialize ShockTalk.")
end if
end if
else
set MainLM to createLM("<Main>", field "Lexicon")
set CapitalLM to birth(script "aLanguage", "<Capital>")
mAddItems(CapitalLM, field "States", 2)
mAddPhrase(MainLM, "<Capital>", 13)
mRelease(CapitalLM)
mSetLanguage(MainLM)
mRelease(MainLM)
put " " into field "Status"
end if
cursor(-1)
updateStage()
end
on queue phrase, idList
set gPhrase to phrase
set gIDList to idList
end
on idle
if gListening then
if gPhrase <> EMPTY then
doRecognize(gPhrase, gIDList)
set gPhrase to EMPTY
set gIDList to EMPTY
end if
showStatus()
end if
end
on showStatus
set status to ASRObj(mStatus)
if status = 1 then
cursor(-1)
put gStatusMsg into field "Status"
else
if status = 2 then
animateWave()
end if
end if
updateStage()
end
on stopMovie
puppetSprite(kWaveChannel, 0)
puppetSprite(kMapChannel, 0)
puppetSprite(kStartQuiz, 0)
if objectp(ASRObj) then
if gListening then
ASRObj(mStopListening)
end if
ASRObj(mdispose)
set ASRObj to 0
end if
closeXLib()
clearGlobals()
end
on createLM lmName, textField
if objectp(ASRObj) then
set tmpLM to birth(script "aLanguage", lmName)
mAddField(tmpLM, textField)
end if
return tmpLM
end
on startListening
global gState
if objectp(ASRObj) then
ASRObj(mStartListening)
set gListening to 1
set the castNum of sprite kStartQuiz to the number of cast "stop.pic"
set gWaveState to 2
if ASRObj(mGetProperty, "lkmd") = 1 then
set gStatusMsg to "Push-To-Talk"
else
set gStatusMsg to "Talk Now"
end if
end if
set the visible of sprite 9 to 0
set gState to randomItem(field "States")
set gAttempt to 0
askQuestion()
updateStage()
end
on stopListening
if objectp(ASRObj) then
if gListening then
ASRObj(mStopListening)
set gListening to 0
set the castNum of sprite kStartQuiz to the number of cast "start.pic"
set the castNum of sprite 3 to the number of cast "logo2.pic"
end if
end if
cursor(-1)
set the visible of sprite 9 to 1
showMap("United States.PIC")
put " " into field "Status"
set gStatusMsg to " "
updateStage()
end
on toggleListening
if gListening then
stopListening()
else
startListening()
end if
end
on doRecognize phrase, phraseID
global gState, gAttempt
set firstItem to item 1 of phraseID
if firstItem = 1 then
stopListening()
else
if firstItem = 2 then
stopListening()
else
if firstItem = 13 then
if item 2 of gState = phrase then
SayIt("Correct")
set gState to randomItem(field "States")
set gAttempt to 0
askQuestion()
else
checkAttempt()
askQuestion()
end if
else
if firstItem = -1 then
checkAttempt()
else
if firstItem = 3 then
giveAnswer()
end if
if firstItem <> 5 then
set gState to randomItem(field "States")
set gAttempt to 0
end if
end if
askQuestion()
end if
end if
end if
end
on askQuestion
global gState, gAttempt
if gListening then
ASRObj(mStopListening)
end if
SayIt("What Capital")
set stateName to item 1 of gState
SayIt(stateName)
showMap(stateName & ".PIC")
updateStage()
if gListening then
ASRObj(mStartListening)
end if
end
on giveAnswer
global gState, gAttempt
if gListening then
ASRObj(mStopListening)
end if
set stateName to item 1 of gState
set capitalName to item 2 of gState
SayIt("Capital of")
SayIt(stateName)
SayIt("is")
SayIt(capitalName)
if gListening then
ASRObj(mStartListening)
end if
end
on checkAttempt
global gState, gAttempt
set gAttempt to gAttempt + 1
if gAttempt = 3 then
giveAnswer()
set gState to randomItem(field "States")
set gAttempt to 0
else
SayIt("Try Again")
end if
end
on SayIt theText
set sndName to theText & ".AIFF"
preLoadCast(the number of cast sndName)
puppetSound(sndName)
updateStage()
repeat while 1 = soundBusy(1)
if (the ticks mod 5) = 0 then
animateWave()
end if
end repeat
puppetSound(0)
updateStage()
end
on randomItem textField
global gState
set lineCount to the number of lines in textField
repeat while 1
set theItem to line random(lineCount) of textField
if theItem <> gState then
exit repeat
end if
end repeat
return theItem
end
on sayAll textField, theItem
repeat with index = 1 to the number of lines in textField
SayIt(item theItem of line index of textField)
end repeat
end
on toggleKeyWord
set keyCast to the castNum of sprite kKeyWords
set castName to the name of cast keyCast
if castName = "KWRDICON.PIC" then
set the castNum of sprite kKeyWords to the number of cast "KEYWORDS.PIC"
else
set the castNum of sprite kKeyWords to the number of cast "KWRDICON.PIC"
end if
end
on animateWave
global gWaveState
put "Don't Talk" into field "Status"
set the castNum of sprite 3 to gWaveState + 245
updateStage()
set gWaveState to (gWaveState mod 3) + 1
end
on showMap whichMap
set the castNum of sprite kMapChannel to the number of cast whichMap