Debian Codegraph @

platform/windows/tts_windows.cpp

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