######### fails on tts part for no obvious reason ##########################
# quiztalker.py by Bruce_Landon@douglas.bc.ca 10-22-06
# works with display only once then close and restart IDLE
# this should work most easily with glossary style questions
# question lines need to be preporocesed to wrap before being in the list to show correctly 
from ccm.env.objects import *
import random
import pyTTS

tts = pyTTS.Create()

# set the speech rate

tts.Rate = 4

# set the speech volume percentage (0-100%)

tts.Volume = 60
#get a list of all the available voice actors
#print tts.GetVoiceNames()
#explicitly set a voice from MS standard voices
#tts.SetVoiceByName('MSSam')
#tts.SetVoiceByName('MSMike')
#tts.SetVoiceByName('MSMary')
#explicitly set a voice from additional voices added
#tts.SetVoiceByName('CMUKal')
tts.SetVoiceByName('VW Kate')
#tts.SetVoiceByName('VW Paul')
#speak the text
#tts.Speak('This is the sound of my voice.')

#instructions="""
#You will now be shown a series
#of questions.  Answer each one
#by pressing the appropriate letter.

#Press any key to start.
#"""
instructions="Press any key to start."

questions=[
"""
001. The sequence of brain regions from
 oldest to newest is:
A) limbic system; brainstem; cerebral cortex.
B) brainstem; cerebral cortex; limbic system.
C) limbic system; cerebral cortex; brainstem.
D) brainstem; limbic system; cerebral cortex.
E) cerebral cortex; brainstem; limbic system.
""",
"""
002. Your conscious awareness of your
 own name and self-identity depends
 primarily on the normal functioning
 of your:
A) cerebellum.
B) amygdala.
C) hypothalamus.
D) sympathetic nervous system.
E) cerebral cortex.
""""
]


class MultipleChoiceEnvironment(ObjectEnvironment):
    def start(self):
        self.text=Object(isa='text',x=0.5,y=0.5)
        self.text.value=instructions
        tts.Speak(instructions)     # add voice
        self.currentQuestion=None
        self.answers=[]
        yield 'key'                 # wait for any key to be pressed
        self.text.value=None
        yield 0.5
        x=str(questions[0])
        # split into stem and choices
        stem=''
        choiceA=''
        choiceB=''
        choiceC=''
        choiceD=''
        choiceE=''
        stem=x[0:(x.find('\nA)'))]
        choiceA=x[(x.find('\nA)')):(x.find('\nB)'))]
        choiceB=x[(x.find('\nB)')):(x.find('\nC)'))]
        choiceC=x[(x.find('\nC)')):(x.find('\nD)'))]
        if choiceD.find('\nE)') > 0 :
            choiceD=x[(x.find('\nD)')):(x.find('\nE)'))]
            choiceE=x[(x.find('\nD')):]
        else :
            choiceD=x[(x.find('\nD')):]
          
        self.text.value=questions[0]     # show the first question
        # say stem - translated in memory
        tts.Speak(stem)                  # add voice
        self.currentQuestion=0           # remember what question we are on
        # model makes a choice
        # say choices - checking in memory for closeness to translated stem
        if choiceA != '' :
            tts.Speak(choiceA)
        if choiceB != '' :
            tts.Speak(choiceB)
        if choiceC != '' :
            tts.Speak(choiceC)
        if choiceD != '' :
            tts.Speak(choiceD)
        if choiceE != '' :
            tts.Speak(choiceE)

    @ccm.parallelize
    def press(self,key): #fix to add in model
        if self.currentQuestion is None: return    # make sure we are waiting
                                                   # for a response
        if key not in 'abcdeABCDE': return      # make sure it's a valid key        

        self.answers.append(key)         # remember the answer
        self.text.value=None
        nextQuestion=self.currentQuestion+1
        self.currentQuestion=None
        if nextQuestion<len(questions):      # more questions to ask?
            yield 0.5 # wait 0.5 seconds
            x=str(questions[nextQuestion])
            # split into stem and choices
            stem=''
            choiceA=''
            choiceB=''
            choiceC=''
            choiceD=''
            choiceE=''
            stem=x[0:(x.find('\nA)'))]
            choiceA=x[(x.find('\nA)')):(x.find('\nB)'))]
            choiceB=x[(x.find('\nB)')):(x.find('\nC)'))]
            choiceC=x[(x.find('\nC)')):(x.find('\nD)'))]
            if choiceD.find('\nE)') > 0 :
                choiceD=x[(x.find('\nD)')):(x.find('\nE)'))]
                choiceE=x[(x.find('\nD')):]
            else :
                choiceD=x[(x.find('\nD')):]
                      
            self.text.value=questions[nextQuestion]  # show the next question
            # say stem - translated in memory
            tts.Speak(stem)     # add voice
            # brief pause
            self.currentQuestion=nextQuestion
            # model makes a choice
            if choiceA != '' :
                tts.Speak(choiceA)
            if choiceB != '' :
                tts.Speak(choiceB)
            if choiceC != '' :
                tts.Speak(choiceC)
            if choiceD != '' :
                tts.Speak(choiceD)
            if choiceE != '' :
                tts.Speak(choiceE)
        else:
            log.answers=self.answers  # record the answers
            self.stop()


######### actr model for reading and making a choice ##########################

log=ccm.log()
env=MultipleChoiceEnvironment(log=log)

import ccm.display
ccm.display.Display(env)
env.run(rate=1)  # to run at real-time
env.stop()
#env.run() #does not work as text in IDLE


