Python:语音处理,实现在线朗读RFC文档或本地文本文件

最后更新于:2022-04-01 19:39:48

本文主要讲解如何使用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 IETF Tools