WorldViz User Forum

WorldViz User Forum (
-   Vizard (
-   -   Vizard tech tip: Text to Speech (

Jeff 01-15-2009 09:15 AM

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.


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.


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

import viz


#add the ground

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

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

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

#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

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


speak.Voice = speak.GetVoices('Name=Microsoft Mary').Item(0)
speak.Voice = speak.GetVoices('Name=Microsoft Mike').Item(0)

Gladsomebeast 01-15-2009 09:39 PM

What a fun tip.

I got a mouth-flapping avatar speaking via the API after some fiddling around.
Check it out:

import viz


#add the ground

woman = viz.add('vcc_female.cfg', pos = [0,.2,1], euler = [180,0,0])

import vizinfo
info = vizinfo.add('What should I say?')
textBox = info.add(viz.TEXTBOX)
textBox.message('I lovvvve yououououou')
speakButton = info.add(viz.BUTTON, 'Speak')

#used code from here:
import win32com.client
tts4file = win32com.client.Dispatch('SAPI.SPVoice')
tts4file.Rate = -2
stream = win32com.client.Dispatch('SAPI.SpFileStream')

def writeSpeechToFile(textToSpeak, fileName):
        stream.Open(fileName, SSFM_CREATE_FOR_WRITE)
        tts4file.AudioOutputStream = stream
        tts4file.Speak(textToSpeak, 0)

count = 0
def speakWoman(textToSay):
        global count
        count += 1
        fileName = 'deleteMe' + str(count) + '.wav'
        writeSpeechToFile(textToSay, fileName)
        yield None
        woman.addAction(vizact.speak(fileName, scale=.005))
import viztask
def onSpeakButton():
        viztask.schedule( speakWoman(textBox.get()) )

vizact.onbuttonup(speakButton, onSpeakButton)

# Dont work first time cuz Vizard still has lock on files
def cleanup():
        import os
        files = os.listdir(os.getcwd())
        for file in files:
                if file.startswith('deleteMe'):
                                print 'Run script again without speaking to delete wav files'


Be sure to delete the .wav files this script leaves behind when done.

This Python Speech API wrapper code helped me figure this out.

All times are GMT -7. The time now is 01:09 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC