Python:语音处理,实现在线朗读RFC文档或本地文本文件
来源:互联网 发布:数据库安全测试研究 编辑:程序博客网 时间:2024/06/02 23:35
本文主要讲解如何使用python来实现将文本转为语音,以一个小例子为例,写了一下用pyTTS来朗读本地方件或在线朗读RFC文档,当然也可以修改一下,做成在线朗读新闻之类的,另本来想实现一个读中文小说的小程序,目前没有发现对中文支持得非常好的,且是免费的语音处理引擎,只能使用TTS实现一个英文的了,就当是用来练习听力了。
1、准备:
a. 下载pyTTS, http://sourceforge.net/projects/uncassist/files/pyTTS/pyTTS%203.0/
b. 下载SpeechSDK51:下载
c. 下载SpeechSDK51 patch,支持中文和日文,本例没有使用,下载。
2、实现:
代码:
#!/usr/bin/env python# -*- coding: utf-8 -*-#程序说明:此程序实现了通过TTS将文本内容读出来,提供了两种方式,一种是读本地文本文件,#另一种方式为在线读RFC文档,要属入rfc编号,会将其内容逐行读出来打印到终端,其中音量#大小,语速,朗读者可能过配置文件来设置,测试了下基本还算清楚,发现免费的TTS引擎对中文#的支持均不是很好,所以本程序暂时没有处理对中文文件的阅读import pyTTSimport ConfigParserdef 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 ttsdef 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): 1please input a text file name (for example: rfc4960.txt)english.txtChina says it condemns all acts of violence against innocent civiliansBEIJING - 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/中查询。
l. read local file. 2. read rfc online. 3. exit please input your choice (1 or 2): 2please 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:语音处理,实现在线朗读RFC文档或本地文本文件
- python语音朗读
- 在线RFC文档
- TTS实现文字语音朗读
- Android 轻松实现语音朗读
- iOS 如何实现 语音朗读
- ios7实现语音朗读功能
- Java实现语音朗读电子书
- python 3 系统文本语音朗读api
- Windows 10 Python 语音合成朗读文本
- VC用TTS实现文字语音朗读
- VC用TTS实现文字语音朗读
- 使用文本语音开发包实现语音朗读功能
- android 中文朗读 语音朗读 tts朗读
- 使用java技术实现IBM VIAVOICE 语音朗读技术
- 【转】VC用TTS实现文字语音朗读
- C#语音朗读文本 — TTS的实现
- 使用讯飞语音实现语音识别,朗读文字
- Eclipse导入(打开)工程
- Android_在EditView中插入图片~
- Directx
- 11年7月以来做过的
- 一个整数阶乘N!末尾有多少个0
- Python:语音处理,实现在线朗读RFC文档或本地文本文件
- Android 开发环境的设置
- 全局变量、常量、程序代码、静态变量的位置关系
- 整理:Windows XP系统安装SQL Server 2000企业版并启用网络功能
- Jdeveloper 基于form认证中错误页重定向问题
- 自定义asp.net mvc 3目录结构
- 调试常用的 __FILE__, __FUNCTION__, __LINE__
- touch pro2 T7373 直刷 安卓2.2
- 共用体的使用实例