2019-10-09 21:10:14 +00:00
|
|
|
tool
|
2019-09-24 19:21:05 +00:00
|
|
|
extends Node
|
2019-09-17 15:12:17 +00:00
|
|
|
|
2019-12-31 14:54:13 +00:00
|
|
|
var TTS
|
2019-09-17 15:12:17 +00:00
|
|
|
|
2020-01-15 01:54:40 +00:00
|
|
|
var tts
|
2019-10-12 22:03:26 +00:00
|
|
|
|
|
|
|
func _ready():
|
2020-01-10 21:06:13 +00:00
|
|
|
if OS.get_name() == "Server" or OS.has_feature("JavaScript"):
|
2020-01-09 21:54:05 +00:00
|
|
|
return
|
2019-12-30 18:57:19 +00:00
|
|
|
elif Engine.has_singleton("AndroidTTS"):
|
|
|
|
tts = Engine.get_singleton("AndroidTTS")
|
2020-01-10 14:49:43 +00:00
|
|
|
else:
|
|
|
|
TTS = preload("godot-tts.gdns")
|
|
|
|
if TTS and (TTS.can_instance() or Engine.editor_hint):
|
2019-12-31 14:54:13 +00:00
|
|
|
tts = TTS.new()
|
2019-10-12 22:03:26 +00:00
|
|
|
else:
|
2019-10-16 13:01:34 +00:00
|
|
|
print_debug("TTS not available!")
|
2019-09-17 15:12:17 +00:00
|
|
|
|
2020-05-19 12:49:28 +00:00
|
|
|
func _get_min_rate():
|
|
|
|
if OS.has_feature('JavaScript'):
|
|
|
|
return 0.1
|
|
|
|
elif Engine.has_singleton("AndroidTTS"):
|
|
|
|
return 0.1
|
|
|
|
elif tts != null:
|
|
|
|
return tts.min_rate
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
var min_rate setget , _get_min_rate
|
|
|
|
|
|
|
|
func _get_max_rate():
|
|
|
|
if OS.has_feature('JavaScript'):
|
|
|
|
return 10
|
|
|
|
elif Engine.has_singleton("AndroidTTS"):
|
|
|
|
return 10.0
|
|
|
|
elif tts != null:
|
|
|
|
return tts.max_rate
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
var max_rate setget , _get_max_rate
|
|
|
|
|
|
|
|
func _get_normal_rate():
|
|
|
|
if OS.has_feature('JavaScript'):
|
|
|
|
return 1
|
|
|
|
elif Engine.has_singleton("AndroidTTS"):
|
|
|
|
return 1.0
|
|
|
|
elif tts != null:
|
|
|
|
return tts.normal_rate
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
var normal_rate setget , _get_normal_rate
|
|
|
|
|
2019-12-31 14:54:13 +00:00
|
|
|
var javascript_rate = 50
|
|
|
|
|
2020-05-19 13:10:02 +00:00
|
|
|
func _set_rate(rate):
|
2020-05-19 12:49:28 +00:00
|
|
|
if rate < self.min_rate:
|
|
|
|
rate = self.min_rate
|
|
|
|
elif rate > self.max_rate:
|
|
|
|
rate = self.max_rate
|
2020-05-19 13:10:02 +00:00
|
|
|
if Engine.has_singleton("AndroidTTS"):
|
|
|
|
return tts.set_rate(rate)
|
|
|
|
elif tts != null:
|
2019-10-12 22:03:26 +00:00
|
|
|
tts.rate = rate
|
2019-12-31 14:54:13 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
javascript_rate = rate
|
2019-09-27 18:07:35 +00:00
|
|
|
|
2020-05-19 13:10:02 +00:00
|
|
|
func _get_rate():
|
|
|
|
if Engine.has_singleton("AndroidTTS"):
|
|
|
|
return tts.get_rate()
|
|
|
|
elif tts != null:
|
2019-10-12 22:03:26 +00:00
|
|
|
return tts.rate
|
2019-12-31 14:54:13 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
return javascript_rate
|
2019-10-12 22:03:26 +00:00
|
|
|
else:
|
|
|
|
return 0
|
2019-09-27 18:07:35 +00:00
|
|
|
|
2020-05-19 13:10:02 +00:00
|
|
|
var rate setget _set_rate, _get_rate
|
2019-10-09 21:10:14 +00:00
|
|
|
|
2020-05-19 12:49:28 +00:00
|
|
|
func _get_rate_percentage():
|
|
|
|
return range_lerp(self.rate, self.min_rate, self.max_rate, 0, 100)
|
|
|
|
|
|
|
|
func _set_rate_percentage(v):
|
|
|
|
self.rate = range_lerp(v, 0, 100, self.min_rate, self.max_rate)
|
|
|
|
|
|
|
|
var rate_percentage setget _set_rate_percentage, _get_rate_percentage
|
|
|
|
|
|
|
|
func _get_normal_rate_percentage():
|
|
|
|
return range_lerp(self.normal_rate, self.min_rate, self.max_rate, 0, 100)
|
|
|
|
|
|
|
|
var normal_rate_percentage setget , _get_rate_percentage
|
|
|
|
|
2019-09-17 15:12:17 +00:00
|
|
|
func speak(text, interrupt := true):
|
2019-10-12 22:03:26 +00:00
|
|
|
if tts != null:
|
|
|
|
tts.speak(text, interrupt)
|
2019-12-31 14:54:13 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
var code = """
|
|
|
|
let utterance = new SpeechSynthesisUtterance("%s")
|
|
|
|
utterance.rate = %s
|
2020-05-19 12:49:28 +00:00
|
|
|
""" % [text.replace("\n", " "), javascript_rate]
|
2019-12-31 14:54:13 +00:00
|
|
|
if interrupt:
|
|
|
|
code += """
|
|
|
|
window.speechSynthesis.cancel()
|
|
|
|
"""
|
|
|
|
code += "window.speechSynthesis.speak(utterance)"
|
|
|
|
JavaScript.eval(code)
|
2020-03-19 18:36:21 +00:00
|
|
|
else:
|
|
|
|
print_debug("%s: %s" % [text, interrupt])
|
2019-09-17 15:12:17 +00:00
|
|
|
|
|
|
|
func stop():
|
2019-10-12 22:03:26 +00:00
|
|
|
if tts != null:
|
|
|
|
tts.stop()
|
2019-12-31 14:54:13 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
JavaScript.eval("window.speechSynthesis.cancel()")
|
2019-09-24 23:06:57 +00:00
|
|
|
|
2019-10-09 21:10:14 +00:00
|
|
|
func get_is_rate_supported():
|
2020-05-19 12:49:28 +00:00
|
|
|
if Engine.has_singleton("AndroidTTS"):
|
|
|
|
return true
|
2019-12-31 14:54:13 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
return true
|
2019-12-30 18:57:19 +00:00
|
|
|
elif tts != null:
|
2019-10-12 22:03:26 +00:00
|
|
|
return tts.is_rate_supported()
|
|
|
|
else:
|
|
|
|
return false
|
2019-10-09 21:10:14 +00:00
|
|
|
|
|
|
|
var is_rate_supported setget , get_is_rate_supported
|
|
|
|
|
2020-05-19 13:56:41 +00:00
|
|
|
func _get_can_detect_screen_reader():
|
|
|
|
if Engine.has_singleton("AndroidTTS"):
|
2020-01-25 14:57:19 +00:00
|
|
|
return true
|
2020-05-19 13:56:41 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
return false
|
|
|
|
elif tts != null:
|
|
|
|
return tts.can_detect_screen_reader
|
|
|
|
return false
|
|
|
|
|
|
|
|
var can_detect_screen_reader setget , _get_can_detect_screen_reader
|
|
|
|
|
|
|
|
func _get_has_screen_reader():
|
|
|
|
if Engine.has_singleton("AndroidTTS"):
|
|
|
|
return tts.has_screen_reader()
|
|
|
|
elif OS.has_feature('JavaScript'):
|
2020-01-25 14:57:19 +00:00
|
|
|
return false
|
2020-05-19 13:56:41 +00:00
|
|
|
elif tts != null:
|
|
|
|
return tts.has_screen_reader
|
|
|
|
return false
|
2020-01-25 14:57:19 +00:00
|
|
|
|
2020-05-19 13:56:41 +00:00
|
|
|
var has_screen_reader setget , _get_has_screen_reader
|
2020-01-25 14:57:19 +00:00
|
|
|
|
2019-09-24 23:06:57 +00:00
|
|
|
func singular_or_plural(count, singular, plural):
|
|
|
|
if count == 1:
|
|
|
|
return singular
|
|
|
|
else:
|
|
|
|
return plural
|
2019-12-23 00:09:40 +00:00
|
|
|
|
|
|
|
func _exit_tree():
|
2020-01-09 21:54:05 +00:00
|
|
|
if not tts or not TTS:
|
|
|
|
return
|
2020-01-15 01:54:40 +00:00
|
|
|
tts.free()
|