Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
文字轉語音
基本用法
文字轉語音的基本使用流程包含下列一次性步驟:
在 Godot 編輯器中啟用專案的文字轉語音(TTS)功能
向系統查詢可用語音列表
儲存你想使用的語音 ID
預設情況下,Godot 的專案層級文字轉語音功能是關閉的,以避免不必要的資源消耗。若要啟用此功能,請:
前往 專案 > 專案設定
確認有啟用 進階設定 開關
點選 音訊 > 一般
確定已勾選 文字轉語音 選項
若系統提示,請重新啟動 Godot。
文字轉語音會使用特定的語音。依使用者系統安裝的語音數量而定,可能有多種語音可用。取得語音 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)
// One-time steps.
// Pick a voice. Here, we arbitrarily pick the first English voice.
string[] voices = DisplayServer.TtsGetVoicesForLanguage("en");
string voiceId = voices[0];
// Say "Hello, world!".
DisplayServer.TtsSpeak("Hello, world!", voiceId);
// 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.
string longMessage = "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.TtsSpeak(longMessage, voiceId);
// Immediately stop the current text mid-sentence and say goodbye instead.
DisplayServer.TtsStop();
DisplayServer.TtsSpeak("Goodbye!", voiceId);
功能需求
Godot 內建文字轉語音功能。你可以在 DisplayServer 類別 下找到相關 API。
Godot 的文字轉語音功能依賴系統函式庫。這些函式庫在 Windows、macOS、Web、Android 與 iOS 上預設安裝,但並非所有 Linux 發行版都預先提供。若系統未安裝,文字轉語音將無法運作;具體而言,tts_get_voices() 會回傳空清單,表示沒有可用的語音。
不論是使用 Godot 的 Linux 開發者,還是在 Linux 上執行 Godot 遊戲的玩家,都需要確保系統已安裝相關的文字轉語音函式庫。請參考下表,或查閱你的 Linux 發行版文件,了解需要安裝哪些函式庫。
各發行版安裝指令
Arch Linux |
pacman -S speech-dispatcher festival espeakup
|
疑難排解
如果你在 var voice_id = voices[0] 這行遇到 Invalid get index '0' (on base: 'PackedStringArray'). 的錯誤,請檢查 voices 是否有內容。如果沒有:
所有系統:請確認專案設定已啟用 文字轉語音
Linux 使用者:請確認已安裝系統所需的文字轉語音函式庫
最佳實務
針對視障玩家的最佳文字轉語音實踐,是將輸出傳送至玩家的螢幕閱讀器。這樣能保留玩家設定的語言、速度、音高等,同時支援讓玩家前後瀏覽文字等進階功能。目前 Godot 尚未支援這樣的深度整合。
以目前 Godot 文字轉語音 API 能力來說,建議最佳做法如下:
開發時請啟用文字轉語音,並確認所有語音內容正確
讓玩家可選擇使用語音,並將選擇結果於遊戲存檔中保存
讓玩家可調整語速,並將設定保存於遊戲存檔
這樣能讓視障玩家在未啟用螢幕閱讀器時,依然享有最大的彈性與舒適體驗,並減少挫折感與疏離感。
注意事項與其他資訊
呼叫 tts_speak 或 tts_stop 時可能會有延遲。實際延遲時間會依作業系統及硬體規格而異。特別是在 Android 與 Web 上,有些語音仰賴網頁服務,播放時間會受到伺服器負載、網路延遲等因素影響。
如有正確安裝相對應語音,非英文文字也可朗讀。在 Windows 上,請參考 這篇文章 來啟用額外語言的語音。
選用正確語音時,像變音符號(umlaut)等非 ASCII 字元也能正確發音。
視障玩家常用的螢幕閱讀器有 JAWS、NVDA、VoiceOver、Narrator 等。
Windows 的文字轉語音 API 通常比其他系統表現更好,例如在呼叫 tts_stop 後接著呼叫 tts_speak,能立即朗讀新訊息。