1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsISupports.h"
#include "SapiService.h"
#include "nsServiceManagerUtils.h"
#include "nsWin32Locale.h"
#include "GeckoProfiler.h"
#include "nsEscape.h"
#include "mozilla/dom/nsSynthVoiceRegistry.h"
#include "mozilla/dom/nsSpeechTask.h"
#include "mozilla/Preferences.h"
namespace mozilla {
namespace dom {
StaticRefPtr<SapiService> SapiService::sSingleton;
class SapiCallback final : public nsISpeechTaskCallback
{
public:
SapiCallback(nsISpeechTask* aTask, ISpVoice* aSapiClient,
uint32_t aTextOffset, uint32_t aSpeakTextLen)
: mTask(aTask)
, mSapiClient(aSapiClient)
, mTextOffset(aTextOffset)
, mSpeakTextLen(aSpeakTextLen)
, mCurrentIndex(0)
, mStreamNum(0)
{
mStartingTime = GetTickCount();
}
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(SapiCallback, nsISpeechTaskCallback)
NS_DECL_NSISPEECHTASKCALLBACK
ULONG GetStreamNum() const { return mStreamNum; }
void SetStreamNum(ULONG aValue) { mStreamNum = aValue; }
void OnSpeechEvent(const SPEVENT& speechEvent);
private:
~SapiCallback() { }
// This pointer is used to dispatch events
nsCOMPtr<nsISpeechTask> mTask;
RefPtr<ISpVoice> mSapiClient;
uint32_t mTextOffset;
uint32_t mSpeakTextLen;
// Used for calculating the time taken to speak the utterance
double mStartingTime;
uint32_t mCurrentIndex;
ULONG mStreamNum;
};
NS_IMPL_CYCLE_COLLECTION(SapiCallback, mTask);
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SapiCallback)
NS_INTERFACE_MAP_ENTRY(nsISpeechTaskCallback)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISpeechTaskCallback)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(SapiCallback)
NS_IMPL_CYCLE_COLLECTING_RELEASE(SapiCallback)
NS_IMETHODIMP
SapiCallback::OnPause()
{
if (FAILED(mSapiClient->Pause())) {
return NS_ERROR_FAILURE;
}
if (!mTask) {
// When calling pause() on child porcess, it may not receive end event
// from chrome process yet.
return NS_ERROR_FAILURE;
}
mTask->DispatchPause(GetTickCount() - mStartingTime, mCurrentIndex);
return NS_OK;
}
NS_IMETHODIMP
SapiCallback::OnResume()
{
if (FAILED(mSapiClient->Resume())) {
return NS_ERROR_FAILURE;
}
if (!mTask) {
// When calling resume() on child porcess, it may not receive end event
// from chrome process yet.
return NS_ERROR_FAILURE;
}
mTask->DispatchResume(GetTickCount() - mStartingTime, mCurrentIndex);
return NS_OK;
}
NS_IMETHODIMP
SapiCallback::OnCancel()
{
// After cancel, mCurrentIndex may be updated.
// At cancel case, use mCurrentIndex for DispatchEnd.
mSpeakTextLen = 0;
// Purge all the previous utterances and speak an empty string
if (FAILED(mSapiClient->Speak(nullptr, SPF_PURGEBEFORESPEAK, nullptr))) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
NS_IMETHODIMP
SapiCallback::OnVolumeChanged(float aVolume)
{
mSapiClient->SetVolume(static_cast<USHORT>(aVolume * 100));
return NS_OK;
}
void
SapiCallback::OnSpeechEvent(const SPEVENT& speechEvent)
{
switch (speechEvent.eEventId) {
case SPEI_START_INPUT_STREAM:
mTask->DispatchStart();
break;
case SPEI_END_INPUT_STREAM:
if (mSpeakTextLen) {
mCurrentIndex = mSpeakTextLen;
}
mTask->DispatchEnd(GetTickCount() - mStartingTime, mCurrentIndex);
mTask = nullptr;
break;
case SPEI_TTS_BOOKMARK:
mCurrentIndex = static_cast<ULONG>(speechEvent.lParam) - mTextOffset;
mTask->DispatchBoundary(NS_LITERAL_STRING("mark"),
GetTickCount() - mStartingTime, mCurrentIndex);
break;
case SPEI_WORD_BOUNDARY:
mCurrentIndex = static_cast<ULONG>(speechEvent.lParam) - mTextOffset;
mTask->DispatchBoundary(NS_LITERAL_STRING("word"),
GetTickCount() - mStartingTime, mCurrentIndex);
break;
case SPEI_SENTENCE_BOUNDARY:
mCurrentIndex = static_cast<ULONG>(speechEvent.lParam) - mTextOffset;
mTask->DispatchBoundary(NS_LITERAL_STRING("sentence"),
GetTickCount() - mStartingTime, mCurrentIndex);
break;
default:
break;
}
}
// static
void __stdcall
SapiService::SpeechEventCallback(WPARAM aWParam, LPARAM aLParam)
{
RefPtr<ISpVoice> spVoice = (ISpVoice*) aWParam;
RefPtr<SapiService> service = (SapiService*) aLParam;
SPEVENT speechEvent;
while (spVoice->GetEvents(1, &speechEvent, nullptr) == S_OK) {
for (size_t i = 0; i < service->mCallbacks.Length(); i++) {
RefPtr<SapiCallback> callback = service->mCallbacks[i];
if (callback->GetStreamNum() == speechEvent.ulStreamNum) {
callback->OnSpeechEvent(speechEvent);
if (speechEvent.eEventId == SPEI_END_INPUT_STREAM) {
service->mCallbacks.RemoveElementAt(i);
}
break;
}
}
}
}
NS_INTERFACE_MAP_BEGIN(SapiService)
NS_INTERFACE_MAP_ENTRY(nsISpeechService)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISpeechService)
NS_INTERFACE_MAP_END
NS_IMPL_ADDREF(SapiService)
NS_IMPL_RELEASE(SapiService)
SapiService::SapiService()
: mInitialized(false)
{
}
SapiService::~SapiService()
{
}
bool
SapiService::Init()
{
PROFILER_LABEL_FUNC(js::ProfileEntry::Category::OTHER);
MOZ_ASSERT(!mInitialized);
if (Preferences::GetBool("media.webspeech.synth.test") ||
!Preferences::GetBool("media.webspeech.synth.enabled")) {
// When enabled, we shouldn't add OS backend (Bug 1160844)
return false;
}
// Get all the voices from sapi and register in the SynthVoiceRegistry
if (!RegisterVoices()) {
return false;
}
mInitialized = true;
return true;
}
already_AddRefed<ISpVoice>
SapiService::InitSapiInstance()
{
RefPtr<ISpVoice> spVoice;
if (FAILED(CoCreateInstance(CLSID_SpVoice, nullptr, CLSCTX_ALL, IID_ISpVoice,
getter_AddRefs(spVoice)))) {
return nullptr;
}
// Set interest for all the events we are interested in
ULONGLONG eventMask =
SPFEI(SPEI_START_INPUT_STREAM) |
SPFEI(SPEI_TTS_BOOKMARK) |
SPFEI(SPEI_WORD_BOUNDARY) |
SPFEI(SPEI_SENTENCE_BOUNDARY) |
SPFEI(SPEI_END_INPUT_STREAM);
if (FAILED(spVoice->SetInterest(eventMask, eventMask))) {
return nullptr;
}
// Set the callback function for receiving the events
spVoice->SetNotifyCallbackFunction(
(SPNOTIFYCALLBACK*) SapiService::SpeechEventCallback,
(WPARAM) spVoice.get(), (LPARAM) this);
return spVoice.forget();
}
bool
SapiService::RegisterVoices()
{
nsresult rv;
nsCOMPtr<nsISynthVoiceRegistry> registry =
do_GetService(NS_SYNTHVOICEREGISTRY_CONTRACTID);
if (!registry) {
return false;
}
RefPtr<ISpObjectTokenCategory> category;
if (FAILED(CoCreateInstance(CLSID_SpObjectTokenCategory, nullptr, CLSCTX_ALL,
IID_ISpObjectTokenCategory,
getter_AddRefs(category)))) {
return false;
}
if (FAILED(category->SetId(SPCAT_VOICES, FALSE))) {
return false;
}
RefPtr<IEnumSpObjectTokens> voiceTokens;
if (FAILED(category->EnumTokens(nullptr, nullptr,
getter_AddRefs(voiceTokens)))) {
return false;
}
while (true) {
RefPtr<ISpObjectToken> voiceToken;
if (voiceTokens->Next(1, getter_AddRefs(voiceToken), nullptr) != S_OK) {
break;
}
RefPtr<ISpDataKey> attributes;
if (FAILED(voiceToken->OpenKey(L"Attributes",
getter_AddRefs(attributes)))) {
continue;
}
WCHAR* language = nullptr;
if (FAILED(attributes->GetStringValue(L"Language", &language))) {
continue;
}
// Language attribute is LCID by hex. So we need convert to locale
// name.
nsAutoString hexLcid;
LCID lcid = wcstol(language, nullptr, 16);
CoTaskMemFree(language);
nsAutoString locale;
nsWin32Locale::GetXPLocale(lcid, locale);
WCHAR* description = nullptr;
if (FAILED(voiceToken->GetStringValue(nullptr, &description))) {
continue;
}
nsAutoString uri;
uri.AssignLiteral("urn:moz-tts:sapi:");
uri.Append(description);
uri.AppendLiteral("?");
uri.Append(locale);
// This service can only speak one utterance at a time, se we set
// aQueuesUtterances to true in order to track global state and schedule
// access to this service.
rv = registry->AddVoice(this, uri, nsDependentString(description), locale,
true, true);
CoTaskMemFree(description);
if (NS_FAILED(rv)) {
continue;
}
mVoices.Put(uri, voiceToken);
}
registry->NotifyVoicesChanged();
return true;
}
NS_IMETHODIMP
SapiService::Speak(const nsAString& aText, const nsAString& aUri,
float aVolume, float aRate, float aPitch,
nsISpeechTask* aTask)
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_AVAILABLE);
RefPtr<ISpObjectToken> voiceToken;
if (!mVoices.Get(aUri, getter_AddRefs(voiceToken))) {
return NS_ERROR_NOT_AVAILABLE;
}
RefPtr<ISpVoice> spVoice = InitSapiInstance();
if (!spVoice) {
return NS_ERROR_FAILURE;
}
if (FAILED(spVoice->SetVoice(voiceToken))) {
return NS_ERROR_FAILURE;
}
if (FAILED(spVoice->SetVolume(static_cast<USHORT>(aVolume * 100)))) {
return NS_ERROR_FAILURE;
}
// The max supported rate in SAPI engines is 3x, and the min is 1/3x. It is
// expressed by an integer. 0 being normal rate, -10 is 1/3 and 10 is 3x.
// Values below and above that are allowed, but the engine may clip the rate
// to its maximum capable value.
// "Each increment between -10 and +10 is logarithmically distributed such
// that incrementing or decrementing by 1 is multiplying or dividing the
// rate by the 10th root of 3"
// https://msdn.microsoft.com/en-us/library/ee431826(v=vs.85).aspx
long rate = aRate != 0 ? static_cast<long>(10 * log10(aRate) / log10(3)) : 0;
if (FAILED(spVoice->SetRate(rate))) {
return NS_ERROR_FAILURE;
}
// Set the pitch using xml
nsAutoString xml;
xml.AssignLiteral("<pitch absmiddle=\"");
// absmiddle doesn't allow float type
xml.AppendInt(static_cast<int32_t>(aPitch * 10.0f - 10.0f));
xml.AppendLiteral("\">");
uint32_t textOffset = xml.Length();
for (size_t i = 0; i < aText.Length(); i++) {
switch (aText[i]) {
case '&':
xml.AppendLiteral("&");
break;
case '<':
xml.AppendLiteral("<");
break;
case '>':
xml.AppendLiteral(">");
break;
default:
xml.Append(aText[i]);
break;
}
}
xml.AppendLiteral("</pitch>");
RefPtr<SapiCallback> callback =
new SapiCallback(aTask, spVoice, textOffset, aText.Length());
// The last three parameters doesn't matter for an indirect service
nsresult rv = aTask->Setup(callback, 0, 0, 0);
if (NS_FAILED(rv)) {
return rv;
}
ULONG streamNum;
if (FAILED(spVoice->Speak(xml.get(), SPF_ASYNC, &streamNum))) {
aTask->Setup(nullptr, 0, 0, 0);
return NS_ERROR_FAILURE;
}
callback->SetStreamNum(streamNum);
// streamNum reassigns same value when last stream is finished even if
// callback for stream end isn't called
// So we cannot use data hashtable and has to add it to vector at last.
mCallbacks.AppendElement(callback);
return NS_OK;
}
NS_IMETHODIMP
SapiService::GetServiceType(SpeechServiceType* aServiceType)
{
*aServiceType = nsISpeechService::SERVICETYPE_INDIRECT_AUDIO;
return NS_OK;
}
NS_IMETHODIMP
SapiService::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData)
{
return NS_OK;
}
SapiService*
SapiService::GetInstance()
{
MOZ_ASSERT(NS_IsMainThread());
if (XRE_GetProcessType() != GeckoProcessType_Default) {
MOZ_ASSERT(false,
"SapiService can only be started on main gecko process");
return nullptr;
}
if (!sSingleton) {
RefPtr<SapiService> service = new SapiService();
if (service->Init()) {
sSingleton = service;
}
}
return sSingleton;
}
already_AddRefed<SapiService>
SapiService::GetInstanceForService()
{
RefPtr<SapiService> sapiService = GetInstance();
return sapiService.forget();
}
void
SapiService::Shutdown()
{
if (!sSingleton) {
return;
}
sSingleton = nullptr;
}
} // namespace dom
} // namespace mozilla
|