Debian Codegraph @

platform/linuxbsd/tts_linux.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* tts_linux.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "tts_linux.h"
32
33 #include "core/config/project_settings.h"
34 #include "servers/text/text_server.h"
35
36 TTS_Linux *TTS_Linux::singleton = nullptr;
37
38 void TTS_Linux::speech_init_thread_func(void *p_userdata) {
39 TTS_Linux *tts = (TTS_Linux *)p_userdata;
40 if (tts) {
41 MutexLock thread_safe_method(tts->_thread_safe_);
42 #ifdef SOWRAP_ENABLED
43 #ifdef DEBUG_ENABLED
44 int dylibloader_verbose = 1;
45 #else
46 int dylibloader_verbose = 0;
47 #endif
48 if (initialize_speechd(dylibloader_verbose) != 0) {
49 print_verbose("Text-to-Speech: Cannot load Speech Dispatcher library!");
50 } else {
51 if (!spd_open || !spd_set_notification_on || !spd_list_synthesis_voices || !free_spd_voices || !spd_set_synthesis_voice || !spd_set_volume || !spd_set_voice_pitch || !spd_set_voice_rate || !spd_set_data_mode || !spd_say || !spd_pause || !spd_resume || !spd_cancel) {
52 // There's no API to check version, check if functions are available instead.
53 print_verbose("Text-to-Speech: Unsupported Speech Dispatcher library version!");
54 return;
55 }
56 #else
57 {
58 #endif
59 CharString class_str;
60 String config_name = GLOBAL_GET("application/config/name");
61 if (config_name.length() == 0) {
62 class_str = "Godot_Engine";
63 } else {
64 class_str = config_name.utf8();
65 }
66 tts->synth = spd_open(class_str.get_data(), "Godot_Engine_Speech_API", "Godot_Engine", SPD_MODE_THREADED);
67 if (tts->synth) {
68 tts->synth->callback_end = &speech_event_callback;
69 tts->synth->callback_cancel = &speech_event_callback;
70 tts->synth->callback_im = &speech_event_index_mark;
71 spd_set_notification_on(tts->synth, SPD_END);
72 spd_set_notification_on(tts->synth, SPD_CANCEL);
73
74 print_verbose("Text-to-Speech: Speech Dispatcher initialized.");
75 } else {
76 print_verbose("Text-to-Speech: Cannot initialize Speech Dispatcher synthesizer!");
77 }
78 }
79 }
80 }
81
82 void TTS_Linux::speech_event_index_mark(size_t p_msg_id, size_t p_client_id, SPDNotificationType p_type, char *p_index_mark) {
83 TTS_Linux *tts = TTS_Linux::get_singleton();
84 if (tts) {
85 callable_mp(tts, &TTS_Linux::_speech_index_mark).call_deferred((int)p_msg_id, (int)p_type, String::utf8(p_index_mark));
86 }
87 }
88
89 void TTS_Linux::_speech_index_mark(int p_msg_id, int p_type, const String &p_index_mark) {
90 _THREAD_SAFE_METHOD_
91
92 if (ids.has(p_msg_id)) {
93 DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_BOUNDARY, ids[p_msg_id], p_index_mark.to_int());
94 }
95 }
96
97 void TTS_Linux::speech_event_callback(size_t p_msg_id, size_t p_client_id, SPDNotificationType p_type) {
98 TTS_Linux *tts = TTS_Linux::get_singleton();
99 if (tts) {
100 callable_mp(tts, &TTS_Linux::_speech_event).call_deferred((int)p_msg_id, (int)p_type);
101 }
102 }
103
104 void TTS_Linux::_load_voices() const {
105 if (!voices_loaded) {
106 SPDVoice **spd_voices = spd_list_synthesis_voices(synth);
107 if (spd_voices != nullptr) {
108 SPDVoice **voices_ptr = spd_voices;
109 while (*voices_ptr != nullptr) {
110 VoiceInfo vi;
111 vi.language = String::utf8((*voices_ptr)->language);
112 vi.variant = String::utf8((*voices_ptr)->variant);
113 voices[String::utf8((*voices_ptr)->name)] = vi;
114 voices_ptr++;
115 }
116 free_spd_voices(spd_voices);
117 }
118 voices_loaded = true;
119 }
120 }
121
122 void TTS_Linux::_speech_event(int p_msg_id, int p_type) {
123 _THREAD_SAFE_METHOD_
124
125 if (!paused && ids.has(p_msg_id)) {
126 if ((SPDNotificationType)p_type == SPD_EVENT_END) {
127 DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_ENDED, ids[p_msg_id]);
128 ids.erase(p_msg_id);
129 last_msg_id = -1;
130 speaking = false;
131 } else if ((SPDNotificationType)p_type == SPD_EVENT_CANCEL) {
132 DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, ids[p_msg_id]);
133 ids.erase(p_msg_id);
134 last_msg_id = -1;
135 speaking = false;
136 }
137 }
138 if (!speaking && queue.size() > 0) {
139 DisplayServer::TTSUtterance &message = queue.front()->get();
140
141 // Inject index mark after each word.
142 String text;
143 String language;
144
145 _load_voices();
146 const VoiceInfo *voice = voices.getptr(message.voice);
147 if (voice) {
148 language = voice->language;
149 }
150
151 PackedInt32Array breaks = TS->string_get_word_breaks(message.text, language);
152 int prev_end = -1;
153 for (int i = 0; i < breaks.size(); i += 2) {
154 const int start = breaks[i];
155 const int end = breaks[i + 1];
156 if (prev_end != -1 && prev_end != start) {
157 text += message.text.substr(prev_end, start - prev_end);
158 }
159 text += message.text.substr(start, end - start);
160 text += "<mark name=\"" + String::num_int64(end, 10) + "\"/>";
161 prev_end = end;
162 }
163
164 spd_set_synthesis_voice(synth, message.voice.utf8().get_data());
165 spd_set_volume(synth, message.volume * 2 - 100);
166 spd_set_voice_pitch(synth, (message.pitch - 1) * 100);
167 float rate = 0;
168 if (message.rate > 1.f) {
169 rate = std::log10(MIN(message.rate, 2.5f)) / std::log10(2.5f) * 100;
170 } else if (message.rate < 1.f) {
171 rate = std::log10(MAX(message.rate, 0.5f)) / std::log10(0.5f) * -100;
172 }
173 spd_set_voice_rate(synth, rate);
174 spd_set_data_mode(synth, SPD_DATA_SSML);
175 last_msg_id = spd_say(synth, SPD_TEXT, text.utf8().get_data());
176 ids[last_msg_id] = message.id;
177 DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_STARTED, message.id);
178
179 queue.pop_front();
180 speaking = true;
181 }
182 }
183
184 bool TTS_Linux::is_speaking() const {
185 return speaking;
186 }
187
188 bool TTS_Linux::is_paused() const {
189 return paused;
190 }
191
192 Array TTS_Linux::get_voices() const {
193 _THREAD_SAFE_METHOD_
194
195 ERR_FAIL_NULL_V(synth, Array());
196 _load_voices();
197
198 Array list;
199 for (const KeyValue<String, VoiceInfo> &E : voices) {
200 Dictionary voice_d;
201 voice_d["name"] = E.key;
202 voice_d["id"] = E.key;
203 voice_d["language"] = E.value.language + "_" + E.value.variant;
204 list.push_back(voice_d);
205 }
206
207 return list;
208 }
209
210 void TTS_Linux::speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int64_t p_utterance_id, bool p_interrupt) {
211 _THREAD_SAFE_METHOD_
212
213 ERR_FAIL_NULL(synth);
214 if (p_interrupt) {
215 stop();
216 }
217
218 if (p_text.is_empty()) {
219 DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, p_utterance_id);
220 return;
221 }
222
223 DisplayServer::TTSUtterance message;
224 message.text = p_text;
225 message.voice = p_voice;
226 message.volume = CLAMP(p_volume, 0, 100);
227 message.pitch = CLAMP(p_pitch, 0.f, 2.f);
228 message.rate = CLAMP(p_rate, 0.1f, 10.f);
229 message.id = p_utterance_id;
230 queue.push_back(message);
231
232 if (is_paused()) {
233 resume();
234 } else {
235 _speech_event(0, (int)SPD_EVENT_BEGIN);
236 }
237 }
238
239 void TTS_Linux::pause() {
240 _THREAD_SAFE_METHOD_
241
242 ERR_FAIL_NULL(synth);
243 if (spd_pause(synth) == 0) {
244 paused = true;
245 }
246 }
247
248 void TTS_Linux::resume() {
249 _THREAD_SAFE_METHOD_
250
251 ERR_FAIL_NULL(synth);
252 spd_resume(synth);
253 paused = false;
254 }
255
256 void TTS_Linux::stop() {
257 _THREAD_SAFE_METHOD_
258
259 ERR_FAIL_NULL(synth);
260 for (DisplayServer::TTSUtterance &message : queue) {
261 DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, message.id);
262 }
263 if ((last_msg_id != -1) && ids.has(last_msg_id)) {
264 DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, ids[last_msg_id]);
265 }
266 queue.clear();
267 ids.clear();
268 last_msg_id = -1;
269 spd_cancel(synth);
270 spd_resume(synth);
271 speaking = false;
272 paused = false;
273 }
274
275 TTS_Linux *TTS_Linux::get_singleton() {
276 return singleton;
277 }
278
279 TTS_Linux::TTS_Linux() {
280 singleton = this;
281 // Speech Dispatcher init can be slow, it might wait for helper process to start on background, so run it in the thread.
282 init_thread.start(speech_init_thread_func, this);
283 }
284
285 TTS_Linux::~TTS_Linux() {
286 init_thread.wait_to_finish();
287 if (synth) {
288 spd_close(synth);
289 }
290
291 singleton = nullptr;
292 }