Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

windows - Python 3.4 - Text to Speech with SAPI

I was trying to use this code to convert text to speech with Python 3.4, but since my computer's main language is not English (I'm using Win7x64) the voice and the accent are wrong (Because I want it to "speak" English).

import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello, it works!")

so, is there a way to change the voice/language (of the program, not the system)? Also, do you think there is a better way to do this? Perhaps a module that can work on every sytem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Chances are that your OS only came with one voice as it is. There are several ways you can get English sounding output using IPA (International Phonetic Language) and SVSFIsXML as a flag in your speak call... but I'm guessing you'd want something less complicated than that.

The first thing I'd do is grab an English voice if you don't have one already. (Check first by going into your control panel->speech recognition-> text to speech and look at your voice selection. If it says "Microsoft Anna - English (United States)" then, yes you already have an English voice.)

If not you'll have to grab another voice Microsoft Speech Platform - Runtime Languages (Version 11) . I highly recommend Microsoft Server Speech Text to Speech Voice (en-US, ZiraPro) as an English voice. You'll also want Microsoft Speech Platform - Software Development Kit (SDK) (Version 11).

Honestly, I just kind of install them all because I think it's cool.

Once you've got those all installed, what I've found to get the voices working is a neat registry hack I found at Voice Attack - Getting free alternate TTS voices working with Win7/8 64bit.

Basically what this entails is that you do some string replacement in your MS Speech Platform voices in your registry so that what you see in your

HKEY_LOCAL_MACHINESOFTWAREMicrosoftSpeech Serverv11.0Voices HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftSpeech Serverv11.0Voices

registries will wind up in:

HKEY_LOCAL_MACHINESOFTWAREMicrosoftSpeechVoices HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftSpeechVoices

Once that's done, go back to control panel and look at all the voices you installed. You should be able to test them all, even in different languages. If the voices aren't playing then the voices you installed weren't the right bit (x86 vs 64).

Now in python you'll have to make a SetVoice call. I've never in my life programmed in python, but I imagine the call you'd want would look something like speaker.SetVoice("Microsoft Server Speech Text to Speech Voice (en-US, ZiraPro)"). After you set the voice, that voice should be the one speaking all the time when you make a Speak call.

Now if you have gotten to this point and the voices played in the control panel but not in your code, it could be that your program is 32bit/64bit or something, and then you gotta run back, reinstall the opposite 32bit/64bit voices, run your reg edits again, and try running your application again.

A bit of work, but it'll pay off. If you do distribute your code, you'll have to make sure your voices are part of the client's registry, and messing with that can be a headache in itself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...