forked from Nekojimi/JackIt
56 lines
1.7 KiB
GDScript3
56 lines
1.7 KiB
GDScript3
|
extends Node
|
||
|
|
||
|
var cache: Dictionary = {}
|
||
|
|
||
|
@export_custom(PROPERTY_HINT_ENUM_SUGGESTION, "Ivy,Joanna,Joey,Justin,Kendra,Kimberly,Matthew,Salli,Amy,Brian,Emma,Russell,Nicole,Geraint,Aditi,Raveena") var voice_name: String = "Ivy"
|
||
|
|
||
|
const FILE_NAME_REPLACEMENT_CHARS: Dictionary = {
|
||
|
" ":"_",
|
||
|
",":"",
|
||
|
".":"",
|
||
|
"!":"",
|
||
|
"?":"",
|
||
|
";":"",
|
||
|
":":"",
|
||
|
}
|
||
|
|
||
|
@onready var http_request: HTTPRequest = $HTTPRequest
|
||
|
@onready var stream_player: AudioStreamPlayer = $AudioStreamPlayer
|
||
|
|
||
|
func get_tts_audio(text: String) -> AudioStreamMP3:
|
||
|
var sample_directory: String = "user://tts_samples/"
|
||
|
#var sample_directory_access: DirAccess = DirAccess.open(sample_directory)
|
||
|
if !DirAccess.dir_exists_absolute(sample_directory):
|
||
|
DirAccess.make_dir_recursive_absolute(sample_directory)
|
||
|
|
||
|
var file_name: String = text
|
||
|
for key in FILE_NAME_REPLACEMENT_CHARS.keys():
|
||
|
file_name = file_name.replace(key,FILE_NAME_REPLACEMENT_CHARS[key])
|
||
|
file_name = voice_name.to_lower() + "_" + file_name.left(128)
|
||
|
|
||
|
var file_path: String = sample_directory + file_name + ".mp3"
|
||
|
#if cache.has(file_name):
|
||
|
#file_path
|
||
|
|
||
|
if !FileAccess.file_exists(file_path):
|
||
|
http_request.download_file = file_path
|
||
|
var error: Error = http_request.request("https://api.streamelements.com/kappa/v2/speech?voice=%s&text=%s" % [voice_name, text.uri_encode()],[],HTTPClient.METHOD_GET)
|
||
|
if error != Error.OK:
|
||
|
return null
|
||
|
await http_request.request_completed
|
||
|
|
||
|
if FileAccess.file_exists(file_path):
|
||
|
var audio_stream: AudioStreamMP3 = load_mp3(file_path)
|
||
|
return audio_stream
|
||
|
|
||
|
return null
|
||
|
#cache[file_name] = file_path
|
||
|
|
||
|
|
||
|
# Copied from Godot Docs
|
||
|
func load_mp3(path) -> AudioStreamMP3:
|
||
|
var file = FileAccess.open(path, FileAccess.READ)
|
||
|
var sound = AudioStreamMP3.new()
|
||
|
sound.data = file.get_buffer(file.get_length())
|
||
|
return sound
|