WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 5.00 average. Display Modes
  #1  
Old 01-15-2009, 10: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
  #2  
Old 01-15-2009, 10:39 PM
Gladsomebeast Gladsomebeast is offline
Member
 
Join Date: Mar 2005
Location: Isla Vizta, CA
Posts: 397
What a fun tip.

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

viz.go()

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

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

import vizinfo
info = vizinfo.add('What should I say?')
info.scale([2,2])
textBox = info.add(viz.TEXTBOX)
textBox.setScale([2]*3)
textBox.overflow(viz.OVERFLOW_GROW)
textBox.message('I lovvvve yououououou')
speakButton = info.add(viz.BUTTON, 'Speak')

#used code from here: http://archives.seul.org/pygame/users/Jul-2008/msg00038.html
import win32com.client
tts4file = win32com.client.Dispatch('SAPI.SPVoice')
tts4file.Rate = -2
stream = win32com.client.Dispatch('SAPI.SpFileStream')
SSFM_CREATE_FOR_WRITE = 3

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

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'):
			try:
				os.remove(file)
			except:
				print 'Run script again without speaking to delete wav files'

vizact.onexit(cleanup)
Be sure to delete the .wav files this script leaves behind when done.

This Python Speech API wrapper code helped me figure this out.
http://archives.seul.org/pygame/user.../msg00038.html
__________________
Paul Elliott
WorldViz LLC
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Vizard and Augmented Reality realvision Vizard 4 04-04-2008 11:59 AM
Vizard won't run wouters Vizard 5 02-05-2008 12:12 PM
Fall 2007 release of Vizard R3 3 D Announcements 0 10-15-2007 05:50 PM
Matlab and Vizard brystewa Vizard 1 09-18-2006 04:11 PM
wxPython with Vizard farshizzo Vizard 18 09-29-2005 09:49 AM


All times are GMT -7. The time now is 06:36 PM.


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