乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Python 文本轉語音

       刮骨劍 2019-06-23

      文本轉語音,一般會用在無障礙開發(fā)。下面介紹如何使用Python實現(xiàn)將文本文件轉換成語音輸出。



      準備

      我測試使用的Python版本為2.7.10,如果你的版本是Python3.5的話,這里就不太適合了。
      在windows上進行測試的話,這里有兩種可選的方式:

      使用Speech API

      原理

      我們的想法是借助微軟的語音接口,所以我們肯定是要進行調用 相關的接口。所以我們需要安裝pywin32來幫助我們完成這一個底層的交互。

      示例代碼

      import win32com.client
      speaker = win32com.client.Dispatch("SAPI.SpVoice")
      speaker.Speak("Hello, it works!")

      小總結

      是的,調用接口來實現(xiàn)語音功能就是這么簡單,但是我們不得不來聊一聊這種方式的缺點。

      • 對中文支持的不夠好,僅僅是這一點,估計在中國沒幾個用它的了。

      • 還有就是語速不能很好的控制,詳細的API介紹可以參照這里API參考

      pyttsx方式

      原理

      pyttsx 是Python的一個關于文字轉語音方面的很不錯的庫。我們還可以借助pyttsx來實現(xiàn)在線朗讀rfc文件或者本地文件等等,最為關鍵的是,它對中文支持的還是不錯的。

      示例代碼

      # coding:utf-8
      import sys
      
      reload(sys)
      sys.setdefaultencoding('utf8')
      #    __author__ = '郭 璞'
      #    __date__ = '2016/8/6'
      #    __Desc__ = 文字轉語音輸出
      
      import pyttsx
      engine = pyttsx.init()
      engine.say('hello world')
      engine.say('你好,郭璞')
      engine.runAndWait()
      # 朗讀一次
      engine.endLoop()
      

      小總結

      使用pyttsx,我們可以借助其強大的API來實現(xiàn)我們基本的業(yè)務需求。很酷吧。

      pyttsx深入研究

      做完上面的小實驗,你肯定會覺得怎么這么不過癮呢?
      別擔心,下面我們就一起走進pyttsx的世界,深入的研究一下其工作原理吧。

      語音引擎工廠

      類似于設計模式中的“工廠模式”,pyttsx通過初始化來獲取語音引擎。當我們第一次調用init操作的時候,會返回一個pyttsx的engine對象,再次調用的時候,如果存在engine對象實例,就會使用現(xiàn)有的,否則再重新創(chuàng)建一個。

      pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine

      從方法聲明上來看,第一個參數(shù)指定的是語音驅動的名稱,這個在底層適合操作系統(tǒng)密切相關的。如下:

      • drivename:由pyttsx.driver模塊根據(jù)操作系統(tǒng)類型來調用,默認使用當前操作系統(tǒng)可以使用的最好的驅動

        • sapi5 - SAPI5 on Windows
        • nsss - NSSpeechSynthesizer on Mac OS X
        • espeak - eSpeak on every other platform
      • debug: 這第二個參數(shù)是指定要不要以調試狀態(tài)輸出,建議開發(fā)階段設置為True

      引擎接口

      要想很好的運用一個庫,不了解其API是不行的。下面來看看pyttsx。engine.Engine的引擎API。

      方法簽名 參數(shù)列表 返回值 簡單釋義
      connect(topic : string, cb : callable) topic:要描述的事件名稱;cb:回調函數(shù) → dict 在給定的topic上添加回調通知
      disconnect(token : dict) token:回調失聯(lián)的返回標記 Void 結束連接
      endLoop() None → None 簡單來說就是結束事件循環(huán)
      getProperty(name : string) name有這些枚舉值“rate, vioce,vioces,volumn → object 獲取當前引擎實例的屬性值
      setProperty(name : string) name有這些枚舉值“rate, vioce,vioces,volumn → object 設置當前引擎實例的屬性值
      say(text : unicode, name : string) text:要進行朗讀的文本數(shù)據(jù); name: 關聯(lián)發(fā)音人,一般用不到 → None 預設要朗讀的文本數(shù)據(jù),這也是“萬事俱備,只欠東風”中的“萬事俱備”
      runAndWait() None → None 這個方法就是“東風”了。當事件隊列中事件全部清空的時候返回
      startLoop([useDriverLoop : bool]) useDriverLoop:是否啟用驅動循環(huán) → None 開啟事件隊列

      元數(shù)據(jù)音調

      在pyttsx.voice.Voice中,處理合成器的發(fā)音。

      • age
        發(fā)音人的年齡,默認為None

      • gender
        以字符串為類型的發(fā)音人性別: male, female, or neutral.默認為None

      • id
        關于Voice的字符串確認信息. 通過 pyttsx.engine.Engine.setPropertyValue()來設置活動發(fā)音簽名. 這個屬性總是被定義。

      • languages
        發(fā)音支持的語言列表,如果沒有,則為一個空的列表。

      • name
        發(fā)音人名稱,默認為None.

      更多測試

      朗讀文本

      import pyttsx
      engine = pyttsx.init()
      engine.say('Sally sells seashells by the seashore.')
      engine.say('The quick brown fox jumped over the lazy dog.')
      engine.runAndWait()

      事件監(jiān)聽

      import pyttsx
      def onStart(name):
         print 'starting', name
      def onWord(name, location, length):
         print 'word', name, location, length
      def onEnd(name, completed):
         print 'finishing', name, completed
      engine = pyttsx.init()
      engine.say('The quick brown fox jumped over the lazy dog.')
      engine.runAndWait()

      打斷發(fā)音

      import pyttsx
      def onWord(name, location, length):
         print 'word', name, location, length
         if location > 10:
            engine.stop()
      engine = pyttsx.init()
      engine.say('The quick brown fox jumped over the lazy dog.')
      engine.runAndWait()

      更換發(fā)音人聲音

      engine = pyttsx.init()
      voices = engine.getProperty('voices')
      for voice in voices:
         engine.setProperty('voice', voice.id)
         engine.say('The quick brown fox jumped over the lazy dog.')
      engine.runAndWait()

      語速控制

      engine = pyttsx.init()
      rate = engine.getProperty('rate')
      engine.setProperty('rate', rate+50)
      engine.say('The quick brown fox jumped over the lazy dog.')
      engine.runAndWait()

      音量控制

      engine = pyttsx.init()
      volume = engine.getProperty('volume')
      engine.setProperty('volume', volume-0.25)
      engine.say('The quick brown fox jumped over the lazy dog.')
      engine.runAndWait()

      執(zhí)行一個事件驅動循環(huán)

      engine = pyttsx.init()
      def onStart(name):
         print 'starting', name
      def onWord(name, location, length):
         print 'word', name, location, length
      def onEnd(name, completed):
         print 'finishing', name, completed
         if name == 'fox':
            engine.say('What a lazy dog!', 'dog')
         elif name == 'dog':
            engine.endLoop()
      engine = pyttsx.init()
      engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
      engine.startLoop()

      使用一個外部的驅動循環(huán)

      engine = pyttsx.init()
      engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
      engine.startLoop(False)
      # engine.iterate() must be called inside externalLoop()
      externalLoop()
      engine.endLoop()

      總結

      看完了上面的講述,是不是感覺Python實現(xiàn)文本轉語音還是蠻簡單的?

      那么,快來嘗試嘗試吧。

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多