Hi!
    I am doing an update on the Python version of getting voices set. I also
use the registry in the module called InstallSpeech.py posted at the end of 
the
explanation which uses the proper declarations and such.
    I hopefully made it easier to understand this time. The first 
explanation uses most of the stuff needed for making games.

    Original posting of the Python creation of speech which also has the
needed load of the module for speech once created which is used only when
the executable file has been made and you are not near any computer that has
windoweyes installed or a windows speech package.

    For when the python file is first compiled you must be on a computer
that has all the speech packages installed on besides Python.
Eventually all the needed software is loaded using the generated python
file, or windoweyes.py file where the extension .py is called a module and
is imported as a module.

    Python has this unique ability to generate a python file of the object
you created and it is done automatically in the background. Once you have
run the program and created the object, it also has a generated copy in a
sub folder called gen, which is inside the comtypes sub folder which are all
inside the python lib site packages sub folder.
This is done using the imported module below:
import win32com.client

    So below is the python code to do all the needed stuff and what is
needed to import for most games and such.
Note: the sign called the pound or number sign, # is the python version for
a comment line.
Remember that all python code is indent based.

# Load Modules
Note: The statement word "from" is used to import a specific group of sub
modules,
Note2: The * means all sub modules/variables inside that module called.
Note3: Using the key word "from" reduces the name size inside the program.
import pygame #The game module which has video and sound drivers.
from pygame.locals import * #The contstants and variables of Python.
from pygame.event import Event
#Note: Above if from was not used it would be pygame.event.MethodName, thus
"from" shortens the variable name.
import random, sys, os
import time
from math import * #All math properties and methods.
#Import the client side commands and there constants name list:
from win32com.client import constants
import win32com.client
#The module that creates the generated file.
from comtypes.client import CreateObject
#If you did not use the from command above the code would have to be:
#comtypes.client.CreateObject each time you wanted to create an object.

# Fetch Generated Speech, if not trap the error:
#Fetch Windoweyes Engine:
try:
    from comtypes.gen import WindowEyes
except:
    print " Windoweyes Not Generated Yet "
#Fetch SAPI Windows Voices:
try:
    from comtypes.gen import SpeechLib
except:
    print " Speech Lib Not Generated Yet "
#Fix for no video device on XP machines placed in as Pygame 1.9.1 bug fix.
os.environ['SDL_VIDEODRIVER']='windib'

INIFile = 0
#Set Windoweyes voice parameter defaults:
# Below is the Windoweyes application model under the object model list:
#Trap error if computer did not install Windoweyes:
try:
    WeTts = CreateObject( "windoweyes.application")
#Same As:
#    WeTts = win32com.client.Dispatch("WindowEyes.Application")
#But the above does not generate the Windoweyes.py module needed for the
executable.

#Now Make dictionary Keys For Speech:
    WeScreenVoice = {"v": WeTts.ActiveSettings.Screen.Tone, "rl":
WeTts.ActiveSettings.Screen.Rate, "pl": WeTts.ActiveSettings.Screen.Pitch,
"vl": WeTts.ActiveSettings.Screen.Volume}
    WeTtsDict[ "Org"] = WeScreenVoice
    WeVoices = True
    INIFile = WeTts.INIFile( myIniFile)
except:
    print
    print "Error!"
    print " No Windoweyes Voice Engine!"
    print

SapiVoices = False
#Creating or Fetch The SAPI Engine:
    tts = CreateObject( "SAPI.SpVoice") )
# Below works but does not create generated SpeechLib.py needed for
executable.
#tts = win32com.client.Dispatch("SAPI.SpVoice") #, constants.SVSFlagsAsync)

Below is a check module that does the needed checks for both voices:
#InstallSpeech.py

from comtypes.client import CreateObject
import _winreg
_voice=0
_voiceCount = 0
_voices = []
ttsSapi = None
ttsWe = None

def check4Object( objectName):
    #Test If Object Exist.
    try:
        r=_winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,objectName)
        r.Close()
        return True
    except:
        return False

def initObject( objectName):
    #INITIALIZE OBJECCT!
    try:
        tts = CreateObject( objectName)
        return tts
    except:
        return None

if check4Object( "SAPI.SPVoice"):
    print "SAPI Engine Installed"
    try:
        ttsSapi = initObject( "SAPI.SPVoice")
        _voiceCount = len(ttsSapi.GetVoices())
        print " Number of voices: %d " % _voiceCount
        for v in range(_voiceCount):
            _voices.append( ttsSapi.GetVoices()[v])
            print " %d) %s " % (v, ttsSapi.GetVoices()[v].GetDescription())
    except:
        print "SAPI Engine Creation Failed!"
else:
    print "No SAPI Engine Installed."

if check4Object( "Windoweyes.Application"):
    print "Windoweyes Engine Installed"
    try:
        ttsWe = initObject( "Windoweyes.Application")
        print " The Windoweyes Version Is: %s " % ttsWe.version
    except:
        print "Windoweyes Engine Creation Failed!"
else:
    print "No Windoweyes Engine Installed."


---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

_______________________________________________
Any views or opinions presented in this email are solely those of the author 
and do not necessarily represent those of Ai Squared.

For membership options, visit 
http://lists.window-eyes.com/options.cgi/scripting-window-eyes.com/archive%40mail-archive.com.
For subscription options, visit 
http://lists.window-eyes.com/listinfo.cgi/scripting-window-eyes.com
List archives can be found at 
http://lists.window-eyes.com/private.cgi/scripting-window-eyes.com

Reply via email to