tts_linux.cpp 9.0 KB

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