跳到主要內容

簡介 python 文字轉語音

 ​

簡介

pyttsx3 是一個用來做「文字轉語音(Text-to-Speech, TTS)」的 Python 套件,可以讓程式把文字直接唸出來,讓電腦「開口說話」的工具。

特徵 :
.完全離線文字轉語音轉換
.從系統安裝的不同語音中進行選擇
.控制語速/語速
.調整音量
.將語音音訊儲存為文件
.簡單、強大且直覺的 API

參照: https://pypi.org/project/pyttsx3/

https://pyttsx3.readthedocs.io/en/latest/

它不會自己生成語音,而是透過呼叫系統內建語音引擎

  • macOS:NSSpeechSynthesizer
  • Windows:SAPI5
  • Linux:eSpeak

因此,聲音的品質取決於作業系統

安裝所需套件

# 如果需要使用麥克風,建議安裝此套件

pip3 install pyaudio

註:

macOS pyaudio 如出現問題 ->

note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for pyaudio

原因 ->

pyaudio 需要編譯 C 擴展,但你的系統缺少底層音訊函式庫

macOS 處理->

步驟 1:安裝 PortAudio

brew install portaudio

步驟 2:重新安裝 PyAudio

pip3 install pyaudio

# 「文字轉語音(Text-to-Speech, TTS)」套件

pip3 install pyttsx3

範例說明(英文, 簡中, 繁中)

import pyttsx3

# 本身不做語音合成,它是呼叫系統內建語音引擎
engine = pyttsx3.init() # Initialize the pyttsx3 engine


# 發音調整
# 語速 以每分鐘字數 為單位
engine.setProperty('rate', 150)
# 音量 取值範圍為0.0 到 1.0
engine.setProperty('volume', 1.0)

### 語音合成
engine.say("Hello, this is a test of the pyttsx3 library.")


# 切換中文聲音
# 簡體中文範例
# 列出所有語音,找到中文的 voice 後設定
voices = engine.getProperty('voices')
for voice in voices:
    print(voice.id, voice.name)
    # 找到中文 voice 後設定, 我的電腦顯示 簡體中文
    if 'zh' in voice.id:  # Look for a Chinese voice
        engine.setProperty('voice', voice.id)
        break
# 因為是 簡體中文 voice, 所以我講文字改為簡體中文
engine.say("你好,这是中文语音测试")


"""
繁體中文範例
我直接指定 mac 繁體中文 voice:
Ting-Ting
Mei-Jia
"""
engine.setProperty('voice', "com.apple.speech.synthesis.voice.Mei-Jia")
engine.say("你好,這是中文語音測試")

engine.runAndWait() # 呼叫函數讓引擎處理語音

終端機

pyttsx3 Library 讓我們能夠靈活地在 Python 中將文字轉換為語音,並完全控制語音的速度、音量和音色。無論是在開發需要音訊回饋的項目,還是想添加一個互動功能,這些屬性都能讓我們使語音聽起來完全符合我們的預期。

留言

這個網誌中的熱門文章

初探 Vue 呼叫 API 出現 CORS 跨來源資源共享 問題原因

提要:   在 {初探Vue 與 Spring boot 的對話} 專案 ,前端 Vue 應用程式 串接 後端 API 伺服器 ,axios 呼叫 API 時出現以下,”無法取得回應內容 (No 'Access-Control-Allow-Origin' header is present on the requested resource):” 錯誤訊息,根據查找相關資料 ,出現以下原因。 瀏覽器開發工具 錯誤訊息 畫面 錯誤原因: “ Access to XMLHttpRequest at ” from origin ‘http://localhost:8080’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource” 瀏覽器為了安全考量,實施了同源政策。 當您的前端應用程式 (http://localhost:8080) 嘗試呼叫一個不同來源 (不同協議、不同域名或不同埠號) 的 API 伺服器 (http://localhost:8088) 時,瀏覽器會主動阻止這個請求,除非伺服器明確地允許這個跨來源的存取。 同源政策限制(Same-Origin Policy): 同源政策限制了程式碼和不同網域資源間的互動,同源是指兩份網頁具備相同協定、埠號(如果有指定)以及主機位置 範例: 表列哪些 URL 與 URL http://www.example.com/api/p1 屬於同源: URL                                                   | 結果   | 原因 --------------------------------------------------------------------- http://www.example.com/api/p2     |...

初探 Vue 與 Spring boot 的對話之Frontend (Vue-Frontend)

  Front-end Vue 使用 REST API 建立 Vite 專案 可參考 { Vue 3 初探}  文章 danny@Danny-Yu projects % npm create vite@latest Need to install the following packages: create-vite@8.2.0 Ok to proceed? (y) y > npx > "create-vite" │ ◇   Project name: │   vue-frontend │ ◇   Select a framework: │   Vue │ ◇   Select a variant: │   TypeScript │ ◇   Use rolldown-vite (Experimental)?: │   No │ ◇   Install with npm and start now? │   Yes │ ◇   Scaffolding project in /Users/danny/Desktop/projects/vue-frontend... │ ◇   Installing dependencies with npm... added 47 packages, and audited 48 packages in 27s 6 packages are looking for funding   run `npm fund` for details found 0 vulnerabilities │ ◇   Starting dev server... > vue-frontend@0.0.0 dev > vite   VITE v7.2.4   ready in 411 ms   ➜   Local:   http://localhost:5173/   ➜   Network: use --host to expose   ➜   press h + enter to show...

初探 Spring 中的循環依賴

原因: 當兩個或多個 bean 直接或間接地相互依賴時, 就會出現 Circular Dependency (循環依賴) 如: Bean A -> Bean B -> Bean A import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class BeanA {          @Autowired     private BeanB beanB;     public String sayHi() {         return "Hi! 我是 Class A.";     } } import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class BeanB {          @Autowired     private BeanA beanA;     public String sayHi() {         return "Hi! 我是 Class B.";     } } 編譯時不會出現問題 danny@Danny-Yu demo % mvn clean install -Dmaven.test.skip=true                           ... ... [INFO] Installing /Users/danny/Desktop/projects/demo/target/dem...