python 利用pyttsx3文字轉(zhuǎn)語音 | 您所在的位置:網(wǎng)站首頁 › seashells讀音 › python 利用pyttsx3文字轉(zhuǎn)語音 |
# -*- 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()
安裝 pip install pyttsx3語音引擎工廠 類似于設(shè)計模式中的“工廠模式”,pyttsx3通過初始化來獲取語音引擎。當(dāng)我們第一次調(diào)用init作的時候,會返回一個pyttsx3的engine對象,再次調(diào)用的時候,如果存在engine對象實例,就會使用現(xiàn)有的,否則再重新創(chuàng)建一個。 pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine從方法聲明上來看,第一個參數(shù)指定的是語音驅(qū)動的名稱,這個在底層適合作系統(tǒng)密切相關(guān)的。如下: ??????1.drivename:由pyttsx3.driver模塊根據(jù)作系統(tǒng)類型來調(diào)用,默認(rèn)使用當(dāng)前作系統(tǒng)可以使用的最好的驅(qū)動 ??????????? sapi5 - SAPI5 on Windows ??????????? nsss - NSSpeechSynthesizer on Mac OS X ??????????? espeak - eSpeak on every other platform ???????2.debug:?這第二個參數(shù)是指定要不要以調(diào)試狀態(tài)輸出,建議開發(fā)階段設(shè)置為True 引擎接口 要想很好的運用一個庫,不了解其API是不行的。下面來看看pyttsx3。engine.Engine的引擎API。 方法簽名 參數(shù)列表 返回值 簡單釋義 connect(topic : string, cb : callable)? topic:要描述的事件名稱;cb:回調(diào)函數(shù)? →?? dict? 在給定的topic上添加回調(diào)通知 disconnect(token : dict)? token:回調(diào)失聯(lián)的返回標(biāo)記? Void 結(jié)束連接 endLoop() None → None? 簡單來說就是結(jié)束事件循環(huán) getProperty(name : string)? name有這些枚舉值“rate, vioce,vioces,volumn? → object? 獲取當(dāng)前引擎實例的屬性值 setProperty(name : string)? name有這些枚舉值“rate, vioce,vioces,volumn → object? 設(shè)置當(dāng)前引擎實例的屬性值 say(text : unicode, name : string)? text:要進行朗讀的文本數(shù)據(jù); name: 關(guān)聯(lián)發(fā)音人,一般用不到 → None 預(yù)設(shè)要朗讀的文本數(shù)據(jù),這也是“萬事俱備,只欠東風(fēng)”中的“萬事俱備” runAndWait() None → None ?這個方法就是“東風(fēng)”了。當(dāng)事件隊列中事件全部清空的時候返回 startLoop([useDriverLoop : bool])? useDriverLoop:是否啟用驅(qū)動循環(huán) → None? 開啟事件隊列元數(shù)據(jù)音調(diào) 在pyttsx3.voice.Voice中,處理合成器的發(fā)音。 age? 發(fā)音人的年齡,默認(rèn)為None gender? 以字符串為類型的發(fā)音人性別: male, female, or neutral.默認(rèn)為None id? 關(guān)于Voice的字符串確認(rèn)信息. 通過 pyttsx3.engine.Engine.setPropertyValue()來設(shè)置活動發(fā)音簽名. 這個屬性總是被定義。 languages? 發(fā)音支持的語言列表,如果沒有,則為一個空的列表。 name? 發(fā)音人名稱,默認(rèn)為None. 更多測試 朗讀文本 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()事件監(jiān)聽 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()打斷發(fā)音 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()更換發(fā)音人聲音 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()語速控制 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()音量控制 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()執(zhí)行一個事件驅(qū)動循環(huán) 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()使用一個外部的驅(qū)動循環(huán) 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()? |
今日新聞 |
推薦新聞 |
專題文章 |
CopyRight 2018-2019 實驗室設(shè)備網(wǎng) 版權(quán)所有 |