歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python:語音處理,實現在線朗讀RFC文檔或本地文本文件

Python:語音處理,實現在線朗讀RFC文檔或本地文本文件

日期:2017/3/1 10:30:16   编辑:Linux編程
本文主要講解如何使用python來實現將文本轉為語音,以一個小例子為例,寫了一下用pyTTS來朗讀本地方件或在線朗讀RFC文檔,當然也可以修改一下,做成在線朗讀新聞之類的,另本來想實現一個讀中文小說的小程序,目前沒有發現對中文支持得非常好的,且是免費的語音處理引擎,只能使用TTS實現一個英文的了,就當是用來練習聽力了。

1、准備:

a. 下載pyTTS, http://sourceforge.net/projects/uncassist/files/pyTTS/pyTTS%203.0/

b. 下載SpeechSDK51:下載

c. 下載SpeechSDK51 patch,支持中文和日文,本例沒有使用,下載

2、實現:

代碼: [python]

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #程序說明:此程序實現了通過TTS將文本內容讀出來,提供了兩種方式,一種是讀本地文本文件,
  4. #另一種方式為在線讀RFC文檔,要屬入rfc編號,會將其內容逐行讀出來打印到終端,其中音量
  5. #大小,語速,朗讀者可能過配置文件來設置,測試了下基本還算清楚,發現免費的TTS引擎對中文
  6. #的支持均不是很好,所以本程序暫時沒有處理對中文文件的閱讀
  7. import pyTTS
  8. import ConfigParser
  9. def read_local_file(tts):
  10. '''''
  11. Function:朗讀本地文件
  12. Input:TTS對象
  13. Output: NONE
  14. Author: socrates
  15. Blog:http://blog.csdn.net/dyx1024
  16. Date:2012-02-19
  17. '''
  18. #輸入要朗讀的文本文件名
  19. file_name = raw_input("please input a text file name (for example: rfc4960.txt)").strip()
  20. try:
  21. fobj = open(file_name, 'r')
  22. except IOError, err:
  23. print('file open error: {0}'.format(err))
  24. return
  25. else:
  26. #逐行輸出並朗讀的文本內容
  27. for eachLine in fobj:
  28. print(eachLine)
  29. tts.Speak(eachLine)
  30. fobj.close()
  31. def read_online_rfc(tts):
  32. '''''
  33. Function:在線朗讀RFC文檔
  34. Input:TTS對象
  35. Output: NONE
  36. Author: socrates
  37. Blog:http://blog.csdn.net/dyx1024
  38. Date:2012-02-19
  39. '''
  40. import urllib
  41. #輸入要朗讀的RFC編號
  42. rfc_id = raw_input("please input a rfc number (for example: 4960):")
  43. #打開RCF文檔
  44. try:
  45. pager = urllib.urlopen("http://tools.ietf.org/rfc/rfc%s.txt" % rfc_id)
  46. except Exception, err:
  47. print("open url failed, ret = %s" % err.args[0])
  48. return
  49. #逐行讀取
  50. while True:
  51. if len(pager.readline()) == 0:
  52. break
  53. else:
  54. strtmp = pager.readline()
  55. print strtmp
  56. tts.Speak(strtmp)
  57. def Init_tts():
  58. '''''
  59. Function:初始化TTS引擎
  60. Input:NONE
  61. Output: NONE
  62. Author: socrates
  63. Blog:http://blog.csdn.net/dyx1024
  64. Date:2012-02-19
  65. '''
  66. tts_config = ConfigParser.ConfigParser()
  67. #讀取TTS相關配置文件
  68. try:
  69. tts_config.readfp(open('tts_config.ini'))
  70. except ConfigParser.Error:
  71. print 'read tts_config.ini failed.'
  72. #創建TTS對象
  73. tts = pyTTS.Create()
  74. #設置語速
  75. tts.Rate = int(tts_config.get("ttsinfo", "TTS_READ_RATE"))
  76. #設置音量
  77. tts.Volume = int(tts_config.get("ttsinfo", "TTS_READ_VOLUME"))
  78. #設置朗讀者
  79. tts.SetVoiceByName(tts_config.get("ttsinfo", "TTS_READ_READER"))
  80. return tts
  81. def show_menu():
  82. '''''
  83. Function:系統菜單
  84. Input:NONE
  85. Output: NONE
  86. Author: socrates
  87. Blog:http://blog.csdn.net/dyx1024
  88. Date:2012-02-19
  89. '''
  90. prompt = '''''
  91. l. read local file.
  92. 2. read rfc online.
  93. 3. exit
  94. please input your choice (1 or 2):
  95. '''
  96. command_name = {'1':read_local_file, '2':read_online_rfc}
  97. while True:
  98. while True:
  99. try:
  100. choice = raw_input(prompt).strip()[0]
  101. except (EOFError, KeyboardInterrupt, IndexError):
  102. choice = '3'
  103. if choice not in '123':
  104. print 'error input, try again'
  105. else:
  106. break
  107. if choice == '3':
  108. break
  109. command_name[choice](Init_tts())
  110. if __name__ == '__main__':
  111. show_menu()

配置文件tts_config.ini:

[plain]

  1. [ttsinfo]
  2. TTS_READ_RATE=-2 ;語速,默認為0,大於0表示快,小於0表示慢
  3. TTS_READ_VOLUME=100 ;音量,0-100之間
  4. TTS_READ_READER=MSMike ;朗讀者,取值MSSam、MSMary、MSMike

測試一:

[plain]

  1. l. read local file.
  2. 2. read rfc online.
  3. 3. exit
  4. please input your choice (1 or 2):
  5. 1
  6. please input a text file name (for example: rfc4960.txt)english.txt
  7. China says it condemns all acts of violence against innocent civilians
  8. 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.
  9. 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.
  10. "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]

  1. l. read local file.
  2. 2. read rfc online.
  3. 3. exit
  4. please input your choice (1 or 2):
  5. 2
  6. please input a rfc number (for example: 4960):330
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  8. <head>
  9. <meta http-equiv="Content-Style-Type" content="text/css" />
  10. <!-- JavaScript -->
  11. <script language="javascript1.1" src="/js/updated.js" type="text/javascript"></script>
  12. <link rel="icon" href="/ietf.ico" />
  13. <title>IETF Tools</title>
  14. <style type="text/css" >
  15. /* HTML element styles */
  16. background-color: white;
  17. padding: 0;
  18. }
  19. font-family: "Times New Roman", times, serif;
  20. margin: 0;
  21. h1 { font-size: 150%; }
  22. h4 { margin: 0.45em 0 0 0; }
  23. .menu form { margin: 0; }
  24. input.frugal,textarea.frugal {
  25. border-left: groove 2px #ccc;
Copyright © Linux教程網 All Rights Reserved