Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

下一步

基本用法

文字轉語音的基本用法涉及以下一次性步驟:

  • 查詢系統以取得可用語音列表

  • 儲存您要使用的語音的 ID

獲得語音 ID 後,您可以用它來說出一些文字:

# One-time steps.
# Pick a voice. Here, we arbitrarily pick the first English voice.
var voices = DisplayServer.tts_get_voices_for_language("en")
var voice_id = voices[0]

# Say "Hello, world!".
DisplayServer.tts_speak("Hello, world!", voice_id)

# Say a longer sentence, and then interrupt it.
# Note that this method is asynchronous: execution proceeds to the next line immediately,
# before the voice finishes speaking.
var long_message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur"
DisplayServer.tts_speak(long_message, voice_id)

# Immediately stop the current text mid-sentence and say goodbye instead.
DisplayServer.tts_stop()
DisplayServer.tts_speak("Goodbye!", voice_id)

變換函式

Godot 包含文字轉語音功能。您可以在 DisplayServer 類別 <class_DisplayServer> 下找到它們。

Godot 依賴系統函式庫來實作文字轉語音功能。這些函式庫預設安裝在 Windows 和 macOS 上,但並非所有 Linux 發行版上。如果它們不存在,文字轉語音功能將無法運作。具體來說,「tts_get_voices()」方法將傳回一個空列表,表示沒有可用的語音。

Both Godot users on Linux and end-users on Linux running Godot games need to ensure that their system includes the system libraries for text-to-speech to work. Please consult the table below or your own distribution's documentation to determine what libraries you need to install.

用於各個發行版的單行安裝程式

Arch Linux

pacman -S speech-dispatcher festival espeakup

最佳實踐

就盲人玩家的理想玩家體驗而言,文字轉語音的最佳實踐是將輸出發送到玩家的螢幕閱讀器。這保留了使用者設定的語言、速度、音調等的選擇,並允許高級功能,例如允許玩家向後和向前滾動文字。截至目前,Godot 還沒有提供這種程度的整合。

根據 Godot 文字轉語音 API 的目前狀態,最佳實踐包括:

  • 開發遊戲時啟用文字轉語音,並確保一切聽起來都正確

  • 允許玩家控制使用哪種語音,並在遊戲會話中保存/保留該選擇

  • 允許玩家控制語速,並在遊戲會話中保存/保留該選擇

這為盲人玩家在不使用螢幕閱讀器時提供了最大的靈活性和舒適度,並最大限度地減少了讓他們感到沮喪和疏遠的機會。

遊戲與在地化

  • 當您呼叫“tts_speak”和“tts_stop”時,預計會出現延遲。實際延遲時間根據作業系統和機器規格而有所不同。這對於 Android 和 Web 來說尤其重要,其中一些語音依賴 Web 服務,實際播放時間取決於伺服器負載、網路延遲和其他因素。

  • 如果安裝和使用正確的語音,非英語文字也可以工作。在 Windows 上,您可以查閱「本文」中的說明,以在 Windows 上啟用其他語言語音。

  • 如果您選擇正確的語音,非 ASCII 字元(例如變音符號)將正確發音。

  • 盲人玩家使用多種螢幕閱讀器,包括 JAWS、NVDA、VoiceOver、Narrator 等。

  • Windows 文字轉語音 API 通常比其他系統上的同等 API 表現更好(例如,「tts_stop」後面跟著「tts_speak」立即說出新訊息)。