-
Notifications
You must be signed in to change notification settings - Fork 54
/
SpeechSynthesisService.cpp
214 lines (173 loc) · 6.75 KB
/
SpeechSynthesisService.cpp
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
/* -*- 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 "SpeechSynthesisService.h"
#include <android/log.h>
#include "nsXULAppAPI.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/dom/nsSynthVoiceRegistry.h"
#include "mozilla/jni/Utils.h"
#include "mozilla/Preferences.h"
#define ALOG(args...) \
__android_log_print(ANDROID_LOG_INFO, "GeckoSpeechSynthesis", ##args)
namespace mozilla {
namespace dom {
StaticRefPtr<SpeechSynthesisService> SpeechSynthesisService::sSingleton;
class AndroidSpeechCallback final : public nsISpeechTaskCallback {
public:
AndroidSpeechCallback() {}
NS_DECL_ISUPPORTS
NS_IMETHOD OnResume() override { return NS_OK; }
NS_IMETHOD OnPause() override { return NS_OK; }
NS_IMETHOD OnCancel() override {
java::SpeechSynthesisService::Stop();
return NS_OK;
}
NS_IMETHOD OnVolumeChanged(float aVolume) override { return NS_OK; }
private:
~AndroidSpeechCallback() {}
};
NS_IMPL_ISUPPORTS(AndroidSpeechCallback, nsISpeechTaskCallback)
NS_IMPL_ISUPPORTS(SpeechSynthesisService, nsISpeechService)
void SpeechSynthesisService::Setup() {
ALOG("SpeechSynthesisService::Setup");
if (!Preferences::GetBool("media.webspeech.synth.enabled") ||
Preferences::GetBool("media.webspeech.synth.test")) {
return;
}
if (!jni::IsAvailable()) {
NS_WARNING("Failed to initialize speech synthesis");
return;
}
Init();
java::SpeechSynthesisService::InitSynth();
}
// nsISpeechService
NS_IMETHODIMP
SpeechSynthesisService::Speak(const nsAString& aText, const nsAString& aUri,
float aVolume, float aRate, float aPitch,
nsISpeechTask* aTask) {
if (mTask) {
NS_WARNING("Service only supports one speech task at a time.");
return NS_ERROR_NOT_AVAILABLE;
}
RefPtr<AndroidSpeechCallback> callback = new AndroidSpeechCallback();
nsresult rv = aTask->Setup(callback);
if (NS_FAILED(rv)) {
return rv;
}
jni::String::LocalRef utteranceId =
java::SpeechSynthesisService::Speak(aUri, aText, aRate, aPitch, aVolume);
if (!utteranceId) {
return NS_ERROR_NOT_AVAILABLE;
}
mTaskUtteranceId = utteranceId->ToCString();
mTask = aTask;
mTaskTextLength = aText.Length();
mTaskTextOffset = 0;
return NS_OK;
}
SpeechSynthesisService* SpeechSynthesisService::GetInstance(bool aCreate) {
if (XRE_GetProcessType() != GeckoProcessType_Default) {
MOZ_ASSERT(
false,
"SpeechSynthesisService can only be started on main gecko process");
return nullptr;
}
if (!sSingleton && aCreate) {
sSingleton = new SpeechSynthesisService();
sSingleton->Setup();
ClearOnShutdown(&sSingleton);
}
return sSingleton;
}
already_AddRefed<SpeechSynthesisService>
SpeechSynthesisService::GetInstanceForService() {
MOZ_ASSERT(NS_IsMainThread());
RefPtr<SpeechSynthesisService> sapiService = GetInstance();
return sapiService.forget();
}
// JNI
void SpeechSynthesisService::RegisterVoice(jni::String::Param aUri,
jni::String::Param aName,
jni::String::Param aLocale,
bool aIsNetwork, bool aIsDefault) {
nsSynthVoiceRegistry* registry = nsSynthVoiceRegistry::GetInstance();
SpeechSynthesisService* service = SpeechSynthesisService::GetInstance(false);
// This service can only speak one utterance at a time, so we set
// aQueuesUtterances to true in order to track global state and schedule
// access to this service.
DebugOnly<nsresult> rv =
registry->AddVoice(service, aUri->ToString(), aName->ToString(),
aLocale->ToString(), !aIsNetwork, true);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to add voice");
if (aIsDefault) {
DebugOnly<nsresult> rv = registry->SetDefaultVoice(aUri->ToString(), true);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to set voice as default");
}
}
void SpeechSynthesisService::DoneRegisteringVoices() {
nsSynthVoiceRegistry* registry = nsSynthVoiceRegistry::GetInstance();
registry->NotifyVoicesChanged();
}
void SpeechSynthesisService::DispatchStart(jni::String::Param aUtteranceId) {
if (sSingleton) {
MOZ_ASSERT(sSingleton->mTaskUtteranceId.Equals(aUtteranceId->ToCString()));
nsCOMPtr<nsISpeechTask> task = sSingleton->mTask;
if (task) {
sSingleton->mTaskStartTime = TimeStamp::Now();
DebugOnly<nsresult> rv = task->DispatchStart();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Unable to dispatch start");
}
}
}
void SpeechSynthesisService::DispatchEnd(jni::String::Param aUtteranceId) {
if (sSingleton) {
// In API older than 23, we will sometimes call this function
// without providing an utterance ID.
MOZ_ASSERT(!aUtteranceId ||
sSingleton->mTaskUtteranceId.Equals(aUtteranceId->ToCString()));
nsCOMPtr<nsISpeechTask> task = sSingleton->mTask;
sSingleton->mTask = nullptr;
if (task) {
TimeStamp startTime = sSingleton->mTaskStartTime;
DebugOnly<nsresult> rv =
task->DispatchEnd((TimeStamp::Now() - startTime).ToSeconds(),
sSingleton->mTaskTextLength);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Unable to dispatch start");
}
}
}
void SpeechSynthesisService::DispatchError(jni::String::Param aUtteranceId) {
if (sSingleton) {
MOZ_ASSERT(sSingleton->mTaskUtteranceId.Equals(aUtteranceId->ToCString()));
nsCOMPtr<nsISpeechTask> task = sSingleton->mTask;
sSingleton->mTask = nullptr;
if (task) {
TimeStamp startTime = sSingleton->mTaskStartTime;
DebugOnly<nsresult> rv =
task->DispatchError((TimeStamp::Now() - startTime).ToSeconds(),
sSingleton->mTaskTextOffset);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Unable to dispatch start");
}
}
}
void SpeechSynthesisService::DispatchBoundary(jni::String::Param aUtteranceId,
int32_t aStart, int32_t aEnd) {
if (sSingleton) {
MOZ_ASSERT(sSingleton->mTaskUtteranceId.Equals(aUtteranceId->ToCString()));
nsCOMPtr<nsISpeechTask> task = sSingleton->mTask;
if (task) {
TimeStamp startTime = sSingleton->mTaskStartTime;
sSingleton->mTaskTextOffset = aStart;
DebugOnly<nsresult> rv = task->DispatchBoundary(
NS_LITERAL_STRING("word"), (TimeStamp::Now() - startTime).ToSeconds(),
aStart, aEnd - aStart, 1);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Unable to dispatch boundary");
}
}
}
} // namespace dom
} // namespace mozilla