tts_linux.cpp 9.1 KB

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