本文主要講解如何使用python來實現將文本轉為語音,以一個小例子為例,寫了一下用pyTTS來朗讀本地方件或在線朗讀RFC文檔,當然也可以修改一下,做成在線朗讀新聞之類的,另本來想實現一個讀中文小說的小程序,目前沒有發現對中文支持得非常好的,且是免費的語音處理引擎,只能使用TTS實現一個英文的了,就當是用來練習聽力了。
1、准備:
a. 下載pyTTS, http://sourceforge.net/projects/uncassist/files/pyTTS/pyTTS%203.0/
b. 下載SpeechSDK51:下載
c. 下載SpeechSDK51 patch,支持中文和日文,本例沒有使用,下載。
2、實現:
代碼:
[python]
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
-
- #程序說明:此程序實現了通過TTS將文本內容讀出來,提供了兩種方式,一種是讀本地文本文件,
- #另一種方式為在線讀RFC文檔,要屬入rfc編號,會將其內容逐行讀出來打印到終端,其中音量
- #大小,語速,朗讀者可能過配置文件來設置,測試了下基本還算清楚,發現免費的TTS引擎對中文
- #的支持均不是很好,所以本程序暫時沒有處理對中文文件的閱讀
-
- import pyTTS
- import ConfigParser
-
- def read_local_file(tts):
- '''''
- Function:朗讀本地文件
- Input:TTS對象
- Output: NONE
- Author: socrates
- Blog:http://blog.csdn.net/dyx1024
- Date:2012-02-19
- '''
-
- #輸入要朗讀的文本文件名
- file_name = raw_input("please input a text file name (for example: rfc4960.txt)").strip()
-
- try:
- fobj = open(file_name, 'r')
- except IOError, err:
- print('file open error: {0}'.format(err))
- return
- else:
- #逐行輸出並朗讀的文本內容
- for eachLine in fobj:
- print(eachLine)
- tts.Speak(eachLine)
- fobj.close()
-
- def read_online_rfc(tts):
- '''''
- Function:在線朗讀RFC文檔
- Input:TTS對象
- Output: NONE
- Author: socrates
- Blog:http://blog.csdn.net/dyx1024
- Date:2012-02-19
- '''
-
- import urllib
-
- #輸入要朗讀的RFC編號
- rfc_id = raw_input("please input a rfc number (for example: 4960):")
-
- #打開RCF文檔
- try:
- pager = urllib.urlopen("http://tools.ietf.org/rfc/rfc%s.txt" % rfc_id)
- except Exception, err:
- print("open url failed, ret = %s" % err.args[0])
- return
-
- #逐行讀取
- while True:
- if len(pager.readline()) == 0:
- break
- else:
- strtmp = pager.readline()
- print strtmp
- tts.Speak(strtmp)
-
- def Init_tts():
- '''''
- Function:初始化TTS引擎
- Input:NONE
- Output: NONE
- Author: socrates
- Blog:http://blog.csdn.net/dyx1024
- Date:2012-02-19
- '''
-
- tts_config = ConfigParser.ConfigParser()
-
- #讀取TTS相關配置文件
- try:
- tts_config.readfp(open('tts_config.ini'))
- except ConfigParser.Error:
- print 'read tts_config.ini failed.'
-
- #創建TTS對象
- tts = pyTTS.Create()
-
- #設置語速
- tts.Rate = int(tts_config.get("ttsinfo", "TTS_READ_RATE"))
-
- #設置音量
- tts.Volume = int(tts_config.get("ttsinfo", "TTS_READ_VOLUME"))
-
- #設置朗讀者
- tts.SetVoiceByName(tts_config.get("ttsinfo", "TTS_READ_READER"))
-
- return tts
-
- def show_menu():
- '''''
- Function:系統菜單
- Input:NONE
- Output: NONE
- Author: socrates
- Blog:http://blog.csdn.net/dyx1024
- Date:2012-02-19
- '''
-
- prompt = '''''
- l. read local file.
- 2. read rfc online.
- 3. exit
- please input your choice (1 or 2):
- '''
- command_name = {'1':read_local_file, '2':read_online_rfc}
-
- while True:
-
- while True:
-
- try:
- choice = raw_input(prompt).strip()[0]
- except (EOFError, KeyboardInterrupt, IndexError):
- choice = '3'
-
- if choice not in '123':
- print 'error input, try again'
- else:
- break
-
- if choice == '3':
- break
- command_name[choice](Init_tts())
-
-
- if __name__ == '__main__':
- show_menu()
-
-
-
-
-
配置文件tts_config.ini:
[plain]
- [ttsinfo]
- TTS_READ_RATE=-2 ;語速,默認為0,大於0表示快,小於0表示慢
- TTS_READ_VOLUME=100 ;音量,0-100之間
- TTS_READ_READER=MSMike ;朗讀者,取值MSSam、MSMary、MSMike
測試一:
[plain]
- l. read local file.
- 2. read rfc online.
- 3. exit
- please input your choice (1 or 2):
- 1
- please input a text file name (for example: rfc4960.txt)english.txt
- China says it condemns all acts of violence against innocent civilians
-
-
-
- BEIJING - China's negative vote on a draft resolution on Syria at the United Nations General Assembly on Thursday was consistent with China's independent foreign policy of peace and in the best interests of the Syrian situation, officials and experts said.
-
-
-
- China opposes armed intervention or forcing a so-called regime change in Syria, China's deputy permanent representative to the UN Wang Min said in explanatory remarks.
-
-
-
- "We condemn all acts of violence against innocent civilians and urge the government and all political factions of Syria to immediately and fully end all acts of violence, and quickly restore stability and the normal social order," Wang said.
測試二:<>標記對中的內容僅打印,不朗讀,各協議文檔編號可從
http://tools.ietf.org/rfc/中查詢。
[plain]
- l. read local file.
- 2. read rfc online.
- 3. exit
- please input your choice (1 or 2):
- 2
- please input a rfc number (for example: 4960):330
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <head>
-
- <meta http-equiv="Content-Style-Type" content="text/css" />
-
- <!-- JavaScript -->
-
- <script language="javascript1.1" src="/js/updated.js" type="text/javascript"></script>
-
- <link rel="icon" href="/ietf.ico" />
-
- <title>IETF Tools</title>
-
- <style type="text/css" >
-
- /* HTML element styles */
-
- background-color: white;
-
- padding: 0;
-
- }
-
- font-family: "Times New Roman", times, serif;
-
- margin: 0;
-
-
-
- h1 { font-size: 150%; }
-
- h4 { margin: 0.45em 0 0 0; }
-
- .menu form { margin: 0; }
-
- input.frugal,textarea.frugal {
-
- border-left: groove 2px #ccc;