View Single Post
  #1  
Old 01-15-2009, 09:15 AM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
Vizard tech tip: Text to Speech

By connecting to Microsoft's Speech API(SAPI) using their Component Object Model(COM) you can bring text to speech capability into Python and your Vizard script. This post descibes how to install pywin32, the Python for Windows extensions that you need for COM to work. There is also a sample script included that shows this being used within a Vizard environment.

Installation:

If you are running on Windows XP or Vista, SAPI5Speech should already be installed. This allows you to use text to speech on your Windows machine. Click here to download

Go to the pywin32 download page.

Download the pywin32 version of the installer for the version of Python used by Vizard. Use the table below to determine which version of Python your Vizard installation uses:

Vizard 2.x uses Python 2.3
Vizard 3.x uses Python 2.4

Run the installer. It should automatically detect Vizard's Python installation. If you have multiple Python installations on your computer, make sure you select the Vizard Python installation.

About voices:

If you are running Windows XP the default voice is 'Sam'. By installing SAPIVoice you can add the voices of 'Mary' and 'Mike' for use with XP. Follow this link to download the installer

Windows Vista provides the default voice of 'Anna'.

Speak method:

When you call the Speak method on your SAPI voice object you tell it whether or not to return immediately(asynchronously) or wait until the speech is completed(synchronously). Here, since we want rendering and user interaction to continue while the text is spoken we call it asynchronously and pass in a value of 1 after the text. If you remove that flag, the user is blocked out and everything will freeze until the speech is done.


Code:
#This is an example showing how to connect to the speech API within
#your Vizard script

import viz

viz.go()

#add the ground
viz.add('tut_ground.wrl')

#add some avatars and place them in different states
woman = viz.add('vcc_female.cfg', pos = [2,0,8], euler = [180,0,0])
woman.state(11)

man = viz.add('vcc_male.cfg', pos = [0,0,8], euler = [180,0,0])
man.state(9)

duck = viz.add('duck.cfg', pos = [-2,0,8], euler = [180,0,0])
duck.state(1)


#connect to the speech API using COM
import win32com.client
speak = win32com.client.Dispatch('Sapi.SpVoice')

#You can manipulate the volume and the rate of speech
#the range for the volume goes from 0(lowest) to 100(loudest)
VOLUME = 100
#the range for the rate of speech goes from -10(slowest) to 10(fastest)
RATE = -2

#set the rate and volume
speak.Rate = RATE
speak.Volume = VOLUME

#pass in a one after the text and script will continue without
#waiting for speak method to finish, otherwise rendering will freeze
speak.Speak("Hello.  Let's use text to speech in Vizard.  Click on an avatar.", 1) 

#say something when an avatar is picked
#again pass in a one after the speech to continue rendering
vizact.onpick(duck, speak.Speak,"You picked the duck. He looks like he's having a good time.",1)
vizact.onpick(man, speak.Speak,"You picked the man. Why is he looking around like that?",1)
vizact.onpick(woman, speak.Speak,"You picked the woman, she's getting a good workout.",1)

#disable mouse navigation so viewpoint doesn't move on mouseclick
viz.mouse(viz.OFF)
to change the voice to Mary or Mike if you have XP and have downloaded the voices use one of the following on the speak object

Code:
speak.Voice = speak.GetVoices('Name=Microsoft Mary').Item(0)
speak.Voice = speak.GetVoices('Name=Microsoft Mike').Item(0)
Reply With Quote