How Python implements text-to-speech

Source: Internet
Author: User

Get ready

I tested a python version of 2.7.10, and if your version is Python3.5, it's not a good place to be.

Using the speech API

Principle

Our idea is to use Microsoft's voice interface, so we're definitely going to call the relevant interface. So we need to install Pywin32 to help us complete this bottom-up interaction.

Sample code

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

Small summary

Yes, it's so easy to invoke the interface to implement speech, but we have to talk about the drawbacks of this approach.

The Chinese support is not good enough, just this point, it is estimated that few in China to use it.

And the speed is not very good control

PYTTSX Way

Principle

PYTTSX is a great library of Python for text-to-speech. We can also use PYTTSX to read the RFC files or local files, and so on, most crucially, it is still good for Chinese support.

Sample code

12345678910111213141516 # coding:utf-8import sysreload(sys)sys.setdefaultencoding(‘utf8‘)# __author__ = ‘郭 璞‘# __date__ = ‘2016/8/6‘# __Desc__ = 文字转语音输出import pyttsxengine = pyttsx.init()engine.say(‘hello world‘)engine.say(‘你好,郭璞‘)engine.runAndWait()# 朗读一次engine.endLoop()

Small summary

With PYTTSX, we can use its powerful API to achieve our basic business needs. It's cool.

PYTTSX in-depth research

After doing the little experiment above, you will certainly feel how so not enjoyable?
Don't worry, let's go into Pyttsx's world together, and delve into how it works.

Voice engine Factory

Similar to "Factory mode" in design mode, PYTTSX is initialized to get the speech engine. When we call the init operation for the first time, we return a PYTTSX engine object, and when it is called again, if there is an Engine object instance, it will use the existing one, or else recreate it.

1 pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine

From a method declaration, the first parameter specifies the name of the voice driver, which is closely related to the underlying operating system. As follows:

1.drivename: called by the Pyttsx.driver module according to the operating system type, by default using the best driver that the current operating system can use

Sapi5-sapi5 on Windows

Nsss-nsspeechsynthesizer on Mac OS X

Espeak-espeak on every other platform

2.debug: This second parameter specifies whether you want to output in debug state, the recommended development stage is set to True

Engine interface

To use a library well, it is not possible to understand its API. Let's take a look at pyttsx. Engine. Engine API.

Method signature Parameter list return value Simple explanation
Connect (topic:string, cb:callable) Topic: The name of the event to describe; CB: callback function →dict Adds a callback notification on a given topic
Disconnect (token:dict) Token: Return token for callback mismatch Void End Connection
Endloop () None →none In short, it's an end event loop.
GetProperty (name:string) Name has these enumeration values "rate, vioce,vioces,volumn →object Gets the property value of the current engine instance
SetProperty (name:string) Name has these enumeration values "rate, vioce,vioces,volumn →object Set property values for the current engine instance
Say (Text:unicode, name:string) Text: The textual data to be read aloud; Name: Associated pronunciation people, generally not used →none Preset to read the text data, which is "Everything is ready, only the east wind" in the "Everything is ready"
Runandwait () None →none This method is the "East Wind". Returns when all events in the event queue are emptied
Startloop ([Usedriverloop:bool]) Usedriverloop: Whether drive cycle is enabled →none Turn on event queuing

Meta data tones

In Pyttsx.voice.Voice, handle the pronunciation of the synth.

Age

Pronunciation of the age of the person, default to None

Gender

Pronunciation by String type human gender: Male, female, or neutral. Default is None

Id

String acknowledgment information about the voice. Use Pyttsx.engine.Engine.setPropertyValue () to set the active pronunciation signature. This property is always defined.

Languages

The list of languages supported by the pronunciation, if not, is an empty list.

Name

Pronunciation person name, default to None.

More Tests

Read aloud text

12345 importpyttsxengine =pyttsx.init()engine.say(‘Sally sells seashells by the seashore.‘)engine.say(‘The quick brown fox jumped over the lazy dog.‘)engine.runAndWait()

Event Monitoring

12345678910 import pyttsxdef onStart(name): print ‘starting‘, namedef onWord(name, location, length): print ‘word‘, name, location, lengthdef onEnd(name, completed): print ‘finishing‘, name, completedengine = pyttsx.init()engine.say(‘The quick brown fox jumped over the lazy dog.‘)engine.runAndWait()

Interrupt pronunciation

12345678 import pyttsx def onword (name, location, length):   print ' word ' , name, location, length   if location > 10 :   engine.stop () engine = pyttsx.init () engine.say ( ) engine.runandwait ()

Change the voice of the Speaker

123456 engine = pyttsx.init () voices = engine.getproperty ( ' voices ' ) for voice in voices:   engine.setproperty ( , voice. id   engine.say ( Code class= "py string" > ' The quick brown fox jumped over the "the lazy dog. ' ) engine.runandwait ()

Speed control

12345 engine =pyttsx.init()rate =engine.getProperty(‘rate‘)engine.setProperty(‘rate‘, rate+50)engine.say(‘The quick brown fox jumped over the lazy dog.‘)engine.runAndWait()

Volume control

12345 engine =pyttsx.init()volume =engine.getProperty(‘volume‘)engine.setProperty(‘volume‘, volume-0.25)engine.say(‘The quick brown fox jumped over the lazy dog.‘)engine.runAndWait()

Perform an event-driven loop

1234567891011121314 engine = pyttsx.init()def onStart(name): print ‘starting‘, namedef onWord(name, location, length): print ‘word‘, name, location, lengthdef onEnd(name, completed): print ‘finishing‘, name, completed if name == ‘fox‘: engine.say(‘What a lazy dog!‘, ‘dog‘) elif name == ‘dog‘: engine.endLoop()engine = pyttsx.init()engine.say(‘The quick brown fox jumped over the lazy dog.‘, ‘fox‘)engine.startLoop()

Use an external drive loop

123456 engine =pyttsx.init()engine.say(‘The quick brown fox jumped over the lazy dog.‘, ‘fox‘)engine.startLoop(False)# engine.iterate() must be called inside externalLoop()externalLoop()engine.endLoop()

Summarize

The above is how Python implementation of text to speech all the content, read the above, is not the sense of Python to achieve text to speech or pretty simple? Well, let's try it. I hope this article will help you learn python.

Transferred from: http://www.jb51.net/article/90067.htm

How Python implements text-to-speech

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.