Python Text to speech

programmer , In the eyes of others, it is often the existence of high and cold , In their eyes, the problems that can be solved with code will never consider other methods , In this article, let's use Python To upgrade the high cold level , Let's use code to “ speak
”.

install pyttsx3

install

Let's take a look Python How did you speak “ speak “ of .

Let's install the required libraries first pyttsx3:
pip install pyttsx3
Voice engine factory

Similar to in design mode “ Factory mode ”,pyttsx3 Get voice engine through initialization . When we first call init When operating , Will return a pyttsx3 of engine object , When called again , If present engine Object instance , Will use the existing , Otherwise, create another one .
pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine
From the point of view of method declaration , The first parameter specifies the name of the voice driver , This is closely related to the underlying suitable operating system . as follows :

      1.drivename: from pyttsx3.driver The module is called according to the operating system type , By default, the best driver available to the current operating system is used .

            sapi5 - SAPI5 on Windows

            nsss - NSSpeechSynthesizer on Mac OS X

            espeak - eSpeak on every other platform

      2.debug:  This second parameter specifies whether to output in debug state , It is recommended that the development phase be set to True.

Function description

In this section, let's look at it again pyttsx3.engine.Engine Corresponding function description in .

- connect

parameter :

  topic(string): The name of the event to describe ;

  cb(callable): Callback function .

Return value :

  dict

Function description :

  In a given topic Add callback notification on

- disconnect

parameter :

  token(dict): Callback lost return flag  

Return value :

  Void

Function description :

   End connection

- endLoop

Function description :

  Simply put, end the event loop

- getProperty

parameter :

  name(string):rate: Speech rate of words per minute , The default is per minute 200 individual .

   name(string):voice: String identifier of voice .

   name(string):voices: pyttsx3.voice.Voice Descriptor object list .

   name(string):volume: Volume size .

Return value :

  Object

Function description :

  Gets the property value of the current engine instance .

- setProperty

parameter :

  name(string):rate: Speech rate of words per minute , The default is per minute 200 individual .

   name(string):voice: String identifier of voice .

   name(string):volume: Volume size .

  value(int):rate: Set the number of words read per minute .

  value:voice: Set pronunciation , I'll talk about it later .

  valu(float):volume: Set volume ,0.0-1.0 between ; default 1.0.

Return value :

  Object

Function description :

  Gets the property value of the current engine instance .

- say

parameter :

  text(unicode): Text data to be read aloud .

  name(strin): Associated pronunciation person ( Usually not )

Function description :

  Preset text data to read

- runAndWait

Function description :

    Returns when all events in the event queue are cleared .

- startLoop

parameter :

   [useDriverLoop(bool)]: Whether drive cycle is enabled .

Function description :

    Open event queue

- stop

Function description :

    Stops the current speech and clears the command queue .

Simple Chinese pronunciation

First, let's learn the simplest “ speak ” method , We don't need to make any settings , Just pass in the text you want to read .
# Voice broadcast module import pyttsx3 # Module initialization engine = pyttsx3.init() print(' Ready to start voice broadcast ...')
engine.say(' I'm cold , I don't want to talk ~') # Wait for the voice broadcast to complete engine.runAndWait()
Set the pronunciation of the parameter

When setting the pronunciation mode of parameters , We need to pay attention to one thing , When the built-in sound selection function of the module is executed, it will not match the Chinese pronunciation , So let's try the default parameters in English .
# Voice broadcast module import pyttsx3 ​ # Module initialization engine = pyttsx3.init() print(' Ready to start voice broadcast ...')
​ # Set pronunciation rate , The default value is 200 rate = engine.getProperty('rate') engine.setProperty('rate',
rate - 50) # Set pronunciation size , Range is 0.0-1.0 volume = engine.getProperty('volume')
engine.setProperty('volume', 0.6) # Set default sound :voices[0].id On behalf of boys ,voices[1].id On behalf of girls
voices = engine.getProperty('voices') engine.setProperty('voice', voices[1].id)
# Add read text engine.say('Not everyone can become a great artist.') ​ # Wait for the voice broadcast to complete
engine.runAndWait()
Interesting Chinese dialect pronunciation

Although we can't change the tone in the built-in pronunciation mode , But after some searching, I found that , We can also call other voice broadcasts API, This perfectly solves the problem of changing pronunciation , And it can support many dialects , Let's take the voice broadcast we first heard as an example , The code is as follows :
# Voice broadcast module import pyttsx3 ​ msg = ''' Today I , Watch the snow drift in the cold night ​ Floating far away with a cooled heart ​ Chasing in the wind and rain , I can't tell the trace in the fog ​
The sky is vast, you and me ​ May change ( Who's not changing ) ​ How many times? , Facing coldness and ridicule ​ Never give up the ideal in my heart ​ In a daze , A sense of loss ​ It has faded unknowingly ​
Heart love ( Who understands me ) ​ Forgive me for my unruly life and love freedom ​ I'm afraid I'll fall one day ​ Betrayed the ideal , Anyone can ​ Even if one day only you share with me ''' # Module initialization
engine = pyttsx3.init() volume = engine.getProperty('volume') ​ # Standard Cantonese pronunciation
voices = engine.setProperty( 'voice',
"com.apple.speech.synthesis.voice.sin-ji") ​ # Mandarin pronunciation # voices =
engine.setProperty( # 'voice',
"com.apple.speech.synthesis.voice.ting-ting.premium") ​ # Taiwanese girls' Mandarin pronunciation # voices
= engine.setProperty( # 'voice', "com.apple.speech.synthesis.voice.mei-jia")
print(' Ready to start voice broadcast ...') # Enter voice broadcast words engine.setProperty('volume', 0.7)
engine.say(msg) ​ engine.runAndWait() engine.stop()
Isn't it interesting , Give me a compliment before you go ~

Technology
©2019-2020 Toolsou All rights reserved,
java Comparing attribute values between two objects utilize Python handle Excel data ——xlrd,xlwt library Bidirectional linked list Why? Python Not a future oriented programming language ?Python【 Assignment statement 】 Special lecture , You can't just a=b ah ! Suggest mastering ! utilize Python handle Excel data ——pandas library see SQL-SERVER Data volume and occupied space of database and each table PID The algorithm finally figured out the principle , It was so simple web Two front-end practical games ( Attached source code ) Beginners learn Python Be sure to know what his basic algorithms are ? What is the function ?