Python:音声処理、RFCドキュメントまたはローカルテキストファイルのオンライン朗読を実現


本文は主にpythonを用いてテキストを音声に変換する方法を説明し、一つの小さな例を例に、pytTSでローカルの方件を朗読したり、RFCドキュメントをオンラインで朗読したりすることを書いた.もちろん修正して、オンライン朗読ニュースを作ることもできるが、中国語小説を読む小さなプログラムを実現したいと思っていたが、現在は中国語に非常によくサポートされていることは発見されていない.しかも無料の音声処理エンジンで、TTSで英語を1つ実現するしかなく、聴力の練習に使われていると思っています.
  1、準備:
       a.pytTSをダウンロードし、 http://sourceforge.net/projects/uncassist/files/pyTTS/pyTTS%203.0/
       b.  SpeechSDK 51のダウンロード:ダウンロード
       c.  SpeechSDK 51 patchをダウンロードして、中国語と日本語をサポートして、この例は使用していないで、ダウンロードします.
 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

テスト1:
    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. 

テスト2:<>タグペアの内容は印刷のみで、朗読せず、各プロトコルドキュメント番号は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;

  このブログにはオーディオファイルがアップロードされません.興味のある人は自分で実行して聴くことができます.