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