tts_windows.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**************************************************************************/
  2. /* tts_windows.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_windows.h"
  31. TTS_Windows *TTS_Windows::singleton = nullptr;
  32. void __stdcall TTS_Windows::speech_event_callback(WPARAM wParam, LPARAM lParam) {
  33. TTS_Windows *tts = TTS_Windows::get_singleton();
  34. SPEVENT event;
  35. while (tts->synth->GetEvents(1, &event, NULL) == S_OK) {
  36. if (tts->ids.has(event.ulStreamNum)) {
  37. if (event.eEventId == SPEI_START_INPUT_STREAM) {
  38. OS::get_singleton()->tts_post_utterance_event(OS::TTS_UTTERANCE_STARTED, tts->ids[event.ulStreamNum].id);
  39. } else if (event.eEventId == SPEI_END_INPUT_STREAM) {
  40. OS::get_singleton()->tts_post_utterance_event(OS::TTS_UTTERANCE_ENDED, tts->ids[event.ulStreamNum].id);
  41. tts->ids.erase(event.ulStreamNum);
  42. tts->_update_tts();
  43. } else if (event.eEventId == SPEI_WORD_BOUNDARY) {
  44. OS::get_singleton()->tts_post_utterance_event(OS::TTS_UTTERANCE_BOUNDARY, tts->ids[event.ulStreamNum].id, event.lParam - tts->ids[event.ulStreamNum].offset);
  45. }
  46. }
  47. }
  48. }
  49. void TTS_Windows::_update_tts() {
  50. if (!is_speaking() && !paused && queue.size() > 0) {
  51. OS::TTSUtterance &message = queue.front()->get();
  52. String text;
  53. DWORD flags = SPF_ASYNC | SPF_PURGEBEFORESPEAK | SPF_IS_XML;
  54. String pitch_tag = String("<pitch absmiddle=\"") + String::num_int64(message.pitch * 10 - 10, 10) + String("\">");
  55. text = pitch_tag + message.text + String("</pitch>");
  56. IEnumSpObjectTokens *cpEnum;
  57. ISpObjectToken *cpVoiceToken;
  58. ULONG ulCount = 0;
  59. ULONG stream_number = 0;
  60. ISpObjectTokenCategory *cpCategory;
  61. HRESULT hr = CoCreateInstance(CLSID_SpObjectTokenCategory, nullptr, CLSCTX_INPROC_SERVER, IID_ISpObjectTokenCategory, (void **)&cpCategory);
  62. if (SUCCEEDED(hr)) {
  63. hr = cpCategory->SetId(SPCAT_VOICES, false);
  64. if (SUCCEEDED(hr)) {
  65. hr = cpCategory->EnumTokens(nullptr, nullptr, &cpEnum);
  66. if (SUCCEEDED(hr)) {
  67. hr = cpEnum->GetCount(&ulCount);
  68. while (SUCCEEDED(hr) && ulCount--) {
  69. wchar_t *w_id = 0L;
  70. hr = cpEnum->Next(1, &cpVoiceToken, nullptr);
  71. cpVoiceToken->GetId(&w_id);
  72. if (String((const wchar_t *)w_id) == message.voice) {
  73. synth->SetVoice(cpVoiceToken);
  74. cpVoiceToken->Release();
  75. break;
  76. }
  77. cpVoiceToken->Release();
  78. }
  79. cpEnum->Release();
  80. }
  81. }
  82. cpCategory->Release();
  83. }
  84. UTData ut;
  85. ut.string = text;
  86. ut.offset = pitch_tag.length(); // Subtract injected <pitch> tag offset.
  87. ut.id = message.id;
  88. synth->SetVolume(message.volume);
  89. synth->SetRate(10.f * log10(message.rate) / log10(3.f));
  90. synth->Speak((LPCWSTR)ut.string.ptr(), flags, &stream_number);
  91. ids[stream_number] = ut;
  92. queue.pop_front();
  93. }
  94. }
  95. bool TTS_Windows::is_speaking() const {
  96. ERR_FAIL_COND_V(!synth, false);
  97. SPVOICESTATUS status;
  98. synth->GetStatus(&status, nullptr);
  99. return (status.dwRunningState == SPRS_IS_SPEAKING);
  100. }
  101. bool TTS_Windows::is_paused() const {
  102. ERR_FAIL_COND_V(!synth, false);
  103. return paused;
  104. }
  105. Array TTS_Windows::get_voices() const {
  106. Array list;
  107. IEnumSpObjectTokens *cpEnum;
  108. ISpObjectToken *cpVoiceToken;
  109. ISpDataKey *cpDataKeyAttribs;
  110. ULONG ulCount = 0;
  111. ISpObjectTokenCategory *cpCategory;
  112. HRESULT hr = CoCreateInstance(CLSID_SpObjectTokenCategory, nullptr, CLSCTX_INPROC_SERVER, IID_ISpObjectTokenCategory, (void **)&cpCategory);
  113. if (SUCCEEDED(hr)) {
  114. hr = cpCategory->SetId(SPCAT_VOICES, false);
  115. if (SUCCEEDED(hr)) {
  116. hr = cpCategory->EnumTokens(nullptr, nullptr, &cpEnum);
  117. if (SUCCEEDED(hr)) {
  118. hr = cpEnum->GetCount(&ulCount);
  119. while (SUCCEEDED(hr) && ulCount--) {
  120. hr = cpEnum->Next(1, &cpVoiceToken, nullptr);
  121. HRESULT hr_attr = cpVoiceToken->OpenKey(SPTOKENKEY_ATTRIBUTES, &cpDataKeyAttribs);
  122. if (SUCCEEDED(hr_attr)) {
  123. wchar_t *w_id = nullptr;
  124. wchar_t *w_lang = nullptr;
  125. wchar_t *w_name = nullptr;
  126. cpVoiceToken->GetId(&w_id);
  127. cpDataKeyAttribs->GetStringValue(L"Language", &w_lang);
  128. cpDataKeyAttribs->GetStringValue(nullptr, &w_name);
  129. LCID locale = wcstol(w_lang, nullptr, 16);
  130. int locale_chars = GetLocaleInfoW(locale, LOCALE_SISO639LANGNAME, nullptr, 0);
  131. int region_chars = GetLocaleInfoW(locale, LOCALE_SISO3166CTRYNAME, nullptr, 0);
  132. wchar_t *w_lang_code = new wchar_t[locale_chars];
  133. wchar_t *w_reg_code = new wchar_t[region_chars];
  134. GetLocaleInfoW(locale, LOCALE_SISO639LANGNAME, w_lang_code, locale_chars);
  135. GetLocaleInfoW(locale, LOCALE_SISO3166CTRYNAME, w_reg_code, region_chars);
  136. Dictionary voice_d;
  137. voice_d["id"] = String((const wchar_t *)w_id);
  138. if (w_name) {
  139. voice_d["name"] = String((const wchar_t *)w_name);
  140. } else {
  141. voice_d["name"] = voice_d["id"].operator String().replace("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\", "");
  142. }
  143. voice_d["language"] = String((const wchar_t *)w_lang_code) + "_" + String((const wchar_t *)w_reg_code);
  144. list.push_back(voice_d);
  145. delete[] w_lang_code;
  146. delete[] w_reg_code;
  147. cpDataKeyAttribs->Release();
  148. }
  149. cpVoiceToken->Release();
  150. }
  151. cpEnum->Release();
  152. }
  153. }
  154. cpCategory->Release();
  155. }
  156. return list;
  157. }
  158. void TTS_Windows::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) {
  159. ERR_FAIL_COND(!synth);
  160. if (p_interrupt) {
  161. stop();
  162. }
  163. if (p_text.empty()) {
  164. OS::get_singleton()->tts_post_utterance_event(OS::TTS_UTTERANCE_CANCELED, p_utterance_id);
  165. return;
  166. }
  167. OS::TTSUtterance message;
  168. message.text = p_text;
  169. message.voice = p_voice;
  170. message.volume = CLAMP(p_volume, 0, 100);
  171. message.pitch = CLAMP(p_pitch, 0.f, 2.f);
  172. message.rate = CLAMP(p_rate, 0.1f, 10.f);
  173. message.id = p_utterance_id;
  174. queue.push_back(message);
  175. if (is_paused()) {
  176. resume();
  177. } else {
  178. _update_tts();
  179. }
  180. }
  181. void TTS_Windows::pause() {
  182. ERR_FAIL_COND(!synth);
  183. if (!paused) {
  184. if (synth->Pause() == S_OK) {
  185. paused = true;
  186. }
  187. }
  188. }
  189. void TTS_Windows::resume() {
  190. ERR_FAIL_COND(!synth);
  191. synth->Resume();
  192. paused = false;
  193. }
  194. void TTS_Windows::stop() {
  195. ERR_FAIL_COND(!synth);
  196. SPVOICESTATUS status;
  197. synth->GetStatus(&status, nullptr);
  198. if (ids.has(status.ulCurrentStream)) {
  199. OS::get_singleton()->tts_post_utterance_event(OS::TTS_UTTERANCE_CANCELED, ids[status.ulCurrentStream].id);
  200. ids.erase(status.ulCurrentStream);
  201. }
  202. for (List<OS::TTSUtterance>::Element *E = queue.front(); E; E = E->next()) {
  203. OS::TTSUtterance &message = E->get();
  204. OS::get_singleton()->tts_post_utterance_event(OS::TTS_UTTERANCE_CANCELED, message.id);
  205. }
  206. queue.clear();
  207. synth->Speak(nullptr, SPF_PURGEBEFORESPEAK, nullptr);
  208. synth->Resume();
  209. paused = false;
  210. }
  211. TTS_Windows *TTS_Windows::get_singleton() {
  212. return singleton;
  213. }
  214. TTS_Windows::TTS_Windows() {
  215. singleton = this;
  216. CoInitialize(nullptr);
  217. if (SUCCEEDED(CoCreateInstance(CLSID_SpVoice, nullptr, CLSCTX_ALL, IID_ISpVoice, (void **)&synth))) {
  218. ULONGLONG event_mask = SPFEI(SPEI_END_INPUT_STREAM) | SPFEI(SPEI_START_INPUT_STREAM) | SPFEI(SPEI_WORD_BOUNDARY);
  219. synth->SetInterest(event_mask, event_mask);
  220. synth->SetNotifyCallbackFunction(&speech_event_callback, (WPARAM)(this), 0);
  221. print_verbose("Text-to-Speech: SAPI initialized.");
  222. } else {
  223. print_verbose("Text-to-Speech: Cannot initialize ISpVoice!");
  224. }
  225. }
  226. TTS_Windows::~TTS_Windows() {
  227. if (synth) {
  228. synth->Release();
  229. }
  230. singleton = nullptr;
  231. }