How does Python implement text-to-speech conversion,

Source: Internet
Author: User

How does Python implement text-to-speech conversion,

Preparation

The Python version I tested is 2.7.10. If your Python version is Python3.5, It is not suitable here.

Use the Speech API

Principle

Our idea is to use Microsoft's Voice interface, so we must call related interfaces. Therefore, we need to install pywin32 to help us complete this underlying interaction.

Sample Code

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

Summary

Yes, it is so simple to call an interface to implement the voice function, but we have to talk about the disadvantages of this method.

This is not enough to support Chinese. It is estimated that few Chinese users use it.

Also, the speed cannot be well controlled.

Pyttsx Mode

Principle

Pyttsx is a good Python library for text-to-speech conversion. We can also use pyttsx to read rfc files or local files online. The most important thing is that it supports Chinese well.

Sample Code

# Coding: utf-8import sysreload (sys) sys. setdefaultencoding ('utf8 ') # _ author _ = 'Guo pu' # _ date _ = '2017/6' # _ Desc _ = Text to Speech Output import pyttsxengine = pyttsx. init () engine. say ('Hello World') engine. say ('hello, Guo pu') engine. runAndWait () # Read the engine once. endLoop ()

Summary

With pyttsx, we can use its powerful APIs to meet our basic business needs. It's cool.

Pyttsx

After completing the small experiment above, you will surely feel that you are not addicted to it?
Don't worry. Let's take a look at the pyttsx world.

Speech Engine Factory

Similar to the "factory mode" in the design mode, pyttsx obtains the voice engine through initialization. When we call the init operation for the first time, a pyttsx engine object will be returned. When we call it again, if an engine object instance exists, the existing one will be used; otherwise, a new one will be created.

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

From the method declaration, the first parameter specifies the name of The Voice driver, which is suitable for operating systems closely at the underlying layer. As follows:

1. drivename:The pyttsx. driver Module calls the driver Based on the operating system type. By default, the best driver available for the current operating system is used.

Sapi5-SAPI5 on Windows

Nsss-NSSpeechSynthesizer on Mac OS X

Espeak-eSpeak on every other platform

2. debug:The second parameter specifies whether to output data in the debugging status. We recommend that you set this parameter to True in the development phase.

Engine Interface

If you want to use a library well, you won't be able to understand its API. Next let's take a look at pyttsx. Engine. Engine engine API.

Method Signature Parameter List Return Value Simple Interpretation
Connect (topic: string, cb: callable) Topic: name of the event to be described; cb: callback function → Dict Add a callback notification to a specified topic.
Disconnect (token: dict) Token: The returned tag of callback disconnection Void End connection
EndLoop () None → None Simply put, it is to end the event loop.
GetProperty (name: string) Name has these enumerated values: "rate, vice, vioces, volumn → Object Get the attribute value of the current engine instance
SetProperty (name: string) Name has these enumerated values: "rate, vice, vioces, volumn → Object Set the attribute value of the current engine instance
Say (text: unicode, name: string) Text: text data to be read; name: associated speaker, generally unavailable → None Pre-set the text data to be read, which is also the "everything is ready, nothing is left blank"
RunAndWait () None → None This method is Dongfeng. Returned when all events in the event queue are cleared
StartLoop ([useDriverLoop: bool]) UseDriverLoop: whether to enable drive Loop → None Enable event queue

Metadata tone

In pyttsx. voice. Voice, process the synthesizer pronunciation.

Age

Specifies the age of the speaker. The default value is None.

Gender

The speaker Gender of the string type: male, female, or neutral. The default value is None.

Id

For Voice string confirmation information, use pyttsx. engine. Engine. setPropertyValue () to set the active pronunciation signature. This attribute is always defined.

Ages

List of languages supported by pronunciation. If not, it is an empty list.

Name

Name of the speaker. The default value is None.

More Tests

Read text

import pyttsxengine = pyttsx.init()engine.say('Sally sells seashells by the seashore.')engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

Event listening

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

import pyttsxdef onWord(name, location, length): print 'word', name, location, length if location > 10: engine.stop()engine = pyttsx.init()engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

Change speaker voice

engine = pyttsx.init()voices = engine.getProperty('voices')for voice in voices: engine.setProperty('voice', voice.id) engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

Speed Control

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

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()

Execute an event-driven Loop

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

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()

Summary

The above is how Python implements all the content of text-to-speech conversion. After reading the above descriptions, is it quite simple to implement text-to-speech conversion in Python? So, let's try it. I hope this article will help you learn Python.

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.