文本转语音
基本用法
在能够使用文本转语音进行基本操作前,需要执行一次以下步骤:
在 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 类中找到。
Godot 的文本转语音功能依赖于系统库。Windows 和 macOS 上默认安装了这些库,但并不是所有 Linux 发行版都有安装。如果这些库不存在,那么文本转语音功能就无法正常工作,此时 tts_get_voices()
返回的列表为空,表示没有可用的语音。
使用 Linux 的 Godot 用户以及使用 Linux 运行 Godot 游戏的最终用户都需要确保自己的操作系统中包含了相关的系统库,这样文本转语音才能正常工作。要确定需要安装哪些库,请参考下面的表格或者对应发行版的文档。
针对各个发行版的单行命令
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 平台尤为明显,因为部分语音基于 Web 服务,播放的实际时间取决于服务器负载、网络延迟等因素。
非英语文本需要安装并使用正确语音后才能正常工作。在 Windows 上,可以按照这篇文章的内容来启用额外语言的语音。
元音变音等非 ASCII 字符的正确发音需要选择正确的语音。
盲人玩家使用的屏幕阅读器有很多种,包括 JAWS、NVDA、VoiceOver、Narrator 等。
Windows 文本转语音 API 的性能通常比其他系统中对应的 API 要好(例如
tts_stop
后跟tts_speak
能够立即朗读新消息)。