tts_linux.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include "tts_linux.h"
  31. #include "core/config/project_settings.h"
  32. #include "servers/text_server.h"
  33. TTS_Linux *TTS_Linux::singleton = nullptr;
  34. void TTS_Linux::speech_init_thread_func(void *p_userdata) {
  35. TTS_Linux *tts = (TTS_Linux *)p_userdata;
  36. if (tts) {
  37. MutexLock thread_safe_method(tts->_thread_safe_);
  38. #ifdef SOWRAP_ENABLED
  39. #ifdef DEBUG_ENABLED
  40. int dylibloader_verbose = 1;
  41. #else
  42. int dylibloader_verbose = 0;
  43. #endif
  44. if (initialize_speechd(dylibloader_verbose) != 0) {
  45. print_verbose("Text-to-Speech: Cannot load Speech Dispatcher library!");
  46. } else {
  47. 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) {
  48. // There's no API to check version, check if functions are available instead.
  49. print_verbose("Text-to-Speech: Unsupported Speech Dispatcher library version!");
  50. return;
  51. }
  52. #else
  53. {
  54. #endif
  55. CharString class_str;
  56. String config_name = GLOBAL_GET("application/config/name");
  57. if (config_name.length() == 0) {
  58. class_str = "Godot_Engine";
  59. } else {
  60. class_str = config_name.utf8();
  61. }
  62. tts->synth = spd_open(class_str, "Godot_Engine_Speech_API", "Godot_Engine", SPD_MODE_THREADED);
  63. if (tts->synth) {
  64. tts->synth->callback_end = &speech_event_callback;
  65. tts->synth->callback_cancel = &speech_event_callback;
  66. tts->synth->callback_im = &speech_event_index_mark;
  67. spd_set_notification_on(tts->synth, SPD_END);
  68. spd_set_notification_on(tts->synth, SPD_CANCEL);
  69. print_verbose("Text-to-Speech: Speech Dispatcher initialized.");
  70. } else {
  71. print_verbose("Text-to-Speech: Cannot initialize Speech Dispatcher synthesizer!");
  72. }
  73. }
  74. }
  75. }
  76. void TTS_Linux::speech_event_index_mark(size_t p_msg_id, size_t p_client_id, SPDNotificationType p_type, char *p_index_mark) {
  77. TTS_Linux *tts = TTS_Linux::get_singleton();
  78. if (tts) {
  79. callable_mp(tts, &TTS_Linux::_speech_index_mark).call_deferred((int)p_msg_id, (int)p_type, String::utf8(p_index_mark));
  80. }
  81. }
  82. void TTS_Linux::_speech_index_mark(int p_msg_id, int p_type, const String &p_index_mark) {
  83. _THREAD_SAFE_METHOD_
  84. if (ids.has(p_msg_id)) {
  85. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_BOUNDARY, ids[p_msg_id], p_index_mark.to_int());
  86. }
  87. }
  88. void TTS_Linux::speech_event_callback(size_t p_msg_id, size_t p_client_id, SPDNotificationType p_type) {
  89. TTS_Linux *tts = TTS_Linux::get_singleton();
  90. if (tts) {
  91. callable_mp(tts, &TTS_Linux::_speech_event).call_deferred((int)p_msg_id, (int)p_type);
  92. }
  93. }
  94. void TTS_Linux::_load_voices() {
  95. if (!voices_loaded) {
  96. SPDVoice **spd_voices = spd_list_synthesis_voices(synth);
  97. if (spd_voices != nullptr) {
  98. SPDVoice **voices_ptr = spd_voices;
  99. while (*voices_ptr != nullptr) {
  100. VoiceInfo vi;
  101. vi.language = String::utf8((*voices_ptr)->language);
  102. vi.variant = String::utf8((*voices_ptr)->variant);
  103. voices[String::utf8((*voices_ptr)->name)] = vi;
  104. voices_ptr++;
  105. }
  106. free_spd_voices(spd_voices);
  107. }
  108. voices_loaded = true;
  109. }
  110. }
  111. void TTS_Linux::_speech_event(int p_msg_id, int p_type) {
  112. _THREAD_SAFE_METHOD_
  113. if (!paused && ids.has(p_msg_id)) {
  114. if ((SPDNotificationType)p_type == SPD_EVENT_END) {
  115. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_ENDED, ids[p_msg_id]);
  116. ids.erase(p_msg_id);
  117. last_msg_id = -1;
  118. speaking = false;
  119. } else if ((SPDNotificationType)p_type == SPD_EVENT_CANCEL) {
  120. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, ids[p_msg_id]);
  121. ids.erase(p_msg_id);
  122. last_msg_id = -1;
  123. speaking = false;
  124. }
  125. }
  126. if (!speaking && queue.size() > 0) {
  127. DisplayServer::TTSUtterance &message = queue.front()->get();
  128. // Inject index mark after each word.
  129. String text;
  130. String language;
  131. _load_voices();
  132. const VoiceInfo *voice = voices.getptr(message.voice);
  133. if (voice) {
  134. language = voice->language;
  135. }
  136. PackedInt32Array breaks = TS->string_get_word_breaks(message.text, language);
  137. int prev_end = -1;
  138. for (int i = 0; i < breaks.size(); i += 2) {
  139. const int start = breaks[i];
  140. const int end = breaks[i + 1];
  141. if (prev_end != -1 && prev_end != start) {
  142. text += message.text.substr(prev_end, start - prev_end);
  143. }
  144. text += message.text.substr(start, end - start);
  145. text += "<mark name=\"" + String::num_int64(end, 10) + "\"/>";
  146. prev_end = end;
  147. }
  148. spd_set_synthesis_voice(synth, message.voice.utf8().get_data());
  149. spd_set_volume(synth, message.volume * 2 - 100);
  150. spd_set_voice_pitch(synth, (message.pitch - 1) * 100);
  151. float rate = 0;
  152. if (message.rate > 1.f) {
  153. rate = log10(MIN(message.rate, 2.5f)) / log10(2.5f) * 100;
  154. } else if (message.rate < 1.f) {
  155. rate = log10(MAX(message.rate, 0.5f)) / log10(0.5f) * -100;
  156. }
  157. spd_set_voice_rate(synth, rate);
  158. spd_set_data_mode(synth, SPD_DATA_SSML);
  159. last_msg_id = spd_say(synth, SPD_TEXT, text.utf8().get_data());
  160. ids[last_msg_id] = message.id;
  161. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_STARTED, message.id);
  162. queue.pop_front();
  163. speaking = true;
  164. }
  165. }
  166. bool TTS_Linux::is_speaking() const {
  167. return speaking;
  168. }
  169. bool TTS_Linux::is_paused() const {
  170. return paused;
  171. }
  172. Array TTS_Linux::get_voices() const {
  173. _THREAD_SAFE_METHOD_
  174. ERR_FAIL_NULL_V(synth, Array());
  175. const_cast<TTS_Linux *>(this)->_load_voices();
  176. Array list;
  177. for (const KeyValue<String, VoiceInfo> &E : voices) {
  178. Dictionary voice_d;
  179. voice_d["name"] = E.key;
  180. voice_d["id"] = E.key;
  181. voice_d["language"] = E.value.language + "_" + E.value.variant;
  182. list.push_back(voice_d);
  183. }
  184. return list;
  185. }
  186. void TTS_Linux::speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, bool p_interrupt) {
  187. _THREAD_SAFE_METHOD_
  188. ERR_FAIL_NULL(synth);
  189. if (p_interrupt) {
  190. stop();
  191. }
  192. if (p_text.is_empty()) {
  193. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, p_utterance_id);
  194. return;
  195. }
  196. DisplayServer::TTSUtterance message;
  197. message.text = p_text;
  198. message.voice = p_voice;
  199. message.volume = CLAMP(p_volume, 0, 100);
  200. message.pitch = CLAMP(p_pitch, 0.f, 2.f);
  201. message.rate = CLAMP(p_rate, 0.1f, 10.f);
  202. message.id = p_utterance_id;
  203. queue.push_back(message);
  204. if (is_paused()) {
  205. resume();
  206. } else {
  207. _speech_event(0, (int)SPD_EVENT_BEGIN);
  208. }
  209. }
  210. void TTS_Linux::pause() {
  211. _THREAD_SAFE_METHOD_
  212. ERR_FAIL_NULL(synth);
  213. if (spd_pause(synth) == 0) {
  214. paused = true;
  215. }
  216. }
  217. void TTS_Linux::resume() {
  218. _THREAD_SAFE_METHOD_
  219. ERR_FAIL_NULL(synth);
  220. spd_resume(synth);
  221. paused = false;
  222. }
  223. void TTS_Linux::stop() {
  224. _THREAD_SAFE_METHOD_
  225. ERR_FAIL_NULL(synth);
  226. for (DisplayServer::TTSUtterance &message : queue) {
  227. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, message.id);
  228. }
  229. if ((last_msg_id != -1) && ids.has(last_msg_id)) {
  230. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, ids[last_msg_id]);
  231. }
  232. queue.clear();
  233. ids.clear();
  234. last_msg_id = -1;
  235. spd_cancel(synth);
  236. spd_resume(synth);
  237. speaking = false;
  238. paused = false;
  239. }
  240. TTS_Linux *TTS_Linux::get_singleton() {
  241. return singleton;
  242. }
  243. TTS_Linux::TTS_Linux() {
  244. singleton = this;
  245. // Speech Dispatcher init can be slow, it might wait for helper process to start on background, so run it in the thread.
  246. init_thread.start(speech_init_thread_func, this);
  247. }
  248. TTS_Linux::~TTS_Linux() {
  249. init_thread.wait_to_finish();
  250. if (synth) {
  251. spd_close(synth);
  252. }
  253. singleton = nullptr;
  254. }