本文主要講解如何使用python來實現將文本轉為語音,以一個小例子為例,寫了一下用pyTTS來朗讀本地方件或在線朗讀RFC文檔,當然也可以修改一下,做成在線朗讀新聞之類的,另本來想實現一個讀中文小說的小程序,目前沒有發現對中文支持得非常好的,且是免費的語音處理引擎,只能使用TTS實現一個英文的了,就當是用來練習聽力了。
1、準備:
? ? ?a. 下載pyTTS,?[http://sourceforge.net/projects/uncassist/files/pyTTS/pyTTS%203.0/](http://sourceforge.net/projects/uncassist/files/pyTTS/pyTTS%203.0/)
? ? ?b. ?下載SpeechSDK51:[下載](http://download.microsoft.com/download/B/4/3/B4314928-7B71-4336-9DE7-6FA4CF00B7B3/SpeechSDK51.exe)
? ? ?c. ?下載SpeechSDK51 patch,支持中文和日文,本例沒有使用,[下載](http://download.microsoft.com/download/B/4/3/B4314928-7B71-4336-9DE7-6FA4CF00B7B3/SpeechSDK51LangPack.exe)。
2、實現:
代碼:
~~~
#!/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:
~~~
[ttsinfo]
TTS_READ_RATE=-2 ;語速,默認為0,大于0表示快,小于0表示慢
TTS_READ_VOLUME=100 ;音量,0-100之間
TTS_READ_READER=MSMike ;朗讀者,取值MSSam、MSMary、MSMike
~~~
測試一:
~~~
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/](http://tools.ietf.org/rfc/)中查詢。
~~~
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;
~~~
此博客上傳不了音頻文件,有興趣的朋友可以自己運行一下收聽。
- 前言
- Python:實現文件歸檔
- Pyhon:按行輸出文件內容
- Python:讀文件和寫文件
- Python:實現一個小算法
- Python:通過命令行發送新浪微博
- Python:通過攝像頭實現的監控功能
- Python:通過攝像頭抓取圖像并自動上傳至新浪微博
- Python:簡單的攝像頭程序實現
- Python:日志模塊logging的應用
- Python:操作嵌入式數據庫SQLite
- Python:將句子中的單詞全部倒排過來,但單詞的字母順序不變
- Python:語音處理,實現在線朗讀RFC文檔或本地文本文件
- Python:通過計算階乘來學習lambda和reduce這兩個函數的使用
- Python:通過執行100萬次打印來比較C和python的性能,以及用C和python結合來解決性能問題的方法
- Python:使用matplotlib繪制圖表
- Python:使用pycha快速繪制辦公常用圖(餅圖、垂直直方圖、水平直方圖、散點圖等七種圖形)
- Python:使用pycha快速繪制辦公常用圖二(使用樣式定制個性化圖表)
- Python:監控鍵盤輸入、鼠標操作,并將捕獲到的信息記錄到文件中
- Python:通過獲取淘寶賬號和密碼的實驗,來看登陸方式選擇的重要性
- Python:通過獲取淘寶賬號和密碼的實驗,來看登陸方式選擇的重要性(二)
- Python:通過遠程監控用戶輸入來獲取淘寶賬號和密碼的實驗(一)
- Python:通過遠程監控用戶輸入來獲取淘寶賬號和密碼的實驗(二)
- Python:通過自定義系統級快捷鍵來控制程序運行
- Python:通過自定義系統級快捷鍵來控制程序開始或停止記錄日志(使用小技巧解決一個貌似無解的問題)
- Python:一個多功能的抓圖工具開發(附源碼)
- Python:程序發布方式簡介一(打包為可執行文件EXE)
- Python:新浪微博應用開發簡介(認證及授權部分)
- Python:程序最小化到托盤功能實現
- Python:實用抓圖工具開發介紹(含需求分析、設計、編碼、單元測試、打包、系統測試、發布各環節)
- Python:桌面氣泡提示功能實現
- Python:未來三個月的python學習計劃
- Python:pygame模塊及SDL庫簡介
- Python:獲取新浪微博用戶的收聽列表和粉絲列表
- Python:pygame游戲編程之旅一(Hello World)
- Python:pygame游戲編程之旅二(自由移動的小球)
- Python:pygame游戲編程之旅三(玩家控制的小球)
- Python:pygame游戲編程之旅四(游戲界面文字處理)
- Python:pygame游戲編程之旅五(游戲界面文字處理詳解)
- Python:pygame游戲編程之旅六(游戲中的聲音處理)
- Python:pygame游戲編程之旅七(pygame基礎知識講解1)
- Python:編程“八榮八恥”之我見
- Python:腳本的幾種執行方式
- wxPython:簡單的wxPython程序
- wxPython:簡單的wxPython程序的另一種寫法
- wxPython:應用程序對象介紹
- wxPython:輸出重定向
- wxPython:關閉wxPython程序
- wxPython:Frame類介紹
- wxPython:面板Panel的使用
- wxPython:工具欄、狀態欄、菜單實現
- wxPython:消息對話框MessageDialog
- wxPython:文本對話框TextEntryDialog
- wxPython:列表選擇框SingleChoiceDialog
- wxPython:事件處理介紹一
- wxPython:事件處理介紹二
- wxPython: 簡單的繪圖例子
- wxPython:狀態欄介紹
- wxPython:菜單介紹
- wxPython:文件對話框wx.FileDialog
- wxPython:顏色選擇對話框wx.ColourDialog
- wxPython:布局管理器sizer介紹
- wxPython:啟動畫面SplashScreen介紹
- wxPython:繪畫按鈕BitmapButton介紹
- wxPython:進度條Gauge介紹
- Python: 發送新浪微博(使用oauth2)
- Python:讀取新浪微博收聽列表
- Python:DNS客戶端實現