python 利用pyttsx3文字轉語音

<code># -*- coding: utf-8 -*-
import pyttsx3
f = open("all.txt",'r')
line = f.readline()

engine = pyttsx3.init()
while line:
line = f.readline()
print(line, end = '')
engine.say(line)
engine.runAndWait()
f.close()
/<code>
<code>import win32com.client

speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("我是語音助手,小靈!")
/<code>

安裝

<code>pip install pyttsx3/<code>

語音引擎工廠

類似於設計模式中的“工廠模式”,pyttsx3通過初始化來獲取語音引擎。當我們第一次調用init操作的時候,會返回一個pyttsx3的engine對象,再次調用的時候,如果存在engine對象實例,就會使用現有的,否則再重新創建一個。

<code>pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine/<code>

從方法聲明上來看,第一個參數指定的是語音驅動的名稱,這個在底層適合操作系統密切相關的。如下:

1.drivename:由pyttsx3.driver模塊根據操作系統類型來調用,默認使用當前操作系統可以使用的最好的驅動

sapi5 - SAPI5 on Windows

nsss - NSSpeechSynthesizer on Mac OS X

espeak - eSpeak on every other platform

2.debug: 這第二個參數是指定要不要以調試狀態輸出,建議開發階段設置為True

引擎接口

要想很好的運用一個庫,不瞭解其API是不行的。下面來看看pyttsx3。engine.Engine的引擎API。


python 利用pyttsx3文字轉語音


元數據音調

在pyttsx3.voice.Voice中,處理合成器的發音。

age

發音人的年齡,默認為None

gender

以字符串為類型的發音人性別: male, female, or neutral.默認為None

id

關於Voice的字符串確認信息. 通過 pyttsx3.engine.Engine.setPropertyValue()來設置活動發音簽名. 這個屬性總是被定義。

languages

發音支持的語言列表,如果沒有,則為一個空的列表。

name

發音人名稱,默認為None.

更多測試

朗讀文本

<code>import pyttsx3
engine = pyttsx3.init()
engine.say('Sally sells seashells by the seashore.')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()/<code>

事件監聽

<code>import pyttsx3

def onStart(name):
print 'starting', name

def onWord(name, location, length):
print 'word', name, location, length

def onEnd(name, completed):
print 'finishing', name, completed

engine = pyttsx3.init()
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()/<code>

打斷髮音

<code>import pyttsx3

def onWord(name, location, length):
print('word', name, location, length)
if location > 10:
engine.stop()

engine = pyttsx3.init()
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()/<code>

更換髮音人聲音

<code>engine = pyttsx3.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()/<code>

語速控制

<code>engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate+50)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()/<code>

音量控制

<code>engine = pyttsx3.init()
volume = engine.getProperty('volume')
engine.setProperty('volume', volume-0.25)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()/<code>

執行一個事件驅動循環

<code>engine = pyttsx3.init()
def onStart(name):
print('starting', name)

def onWord(name, location, length):
print('word', name, location, length)

def onEnd(name, completed):
print('finishing', name, completed)
if name == 'fox':
engine.say('What a lazy dog!', 'dog')
elif name == 'dog':

engine.endLoop()

engine = pyttsx3.init()
engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
engine.startLoop()/<code>

使用一個外部的驅動循環

<code>engine = pyttsx3.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()/<code>


python 利用pyttsx3文字轉語音


分享到:


相關文章: