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
|
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "modules/speech/SpeechSynthesis.h"
#include "bindings/core/v8/ExceptionState.h"
#include "core/dom/ExecutionContext.h"
#include "modules/speech/SpeechSynthesisEvent.h"
#include "platform/speech/PlatformSpeechSynthesisVoice.h"
#include "wtf/CurrentTime.h"
namespace blink {
SpeechSynthesis* SpeechSynthesis::create(ExecutionContext* context)
{
return new SpeechSynthesis(context);
}
SpeechSynthesis::SpeechSynthesis(ExecutionContext* context)
: ContextLifecycleObserver(context)
, m_platformSpeechSynthesizer(PlatformSpeechSynthesizer::create(this))
, m_isPaused(false)
{
}
void SpeechSynthesis::setPlatformSynthesizer(PlatformSpeechSynthesizer* synthesizer)
{
m_platformSpeechSynthesizer = synthesizer;
}
ExecutionContext* SpeechSynthesis::executionContext() const
{
return ContextLifecycleObserver::executionContext();
}
void SpeechSynthesis::voicesDidChange()
{
m_voiceList.clear();
if (executionContext() && !executionContext()->activeDOMObjectsAreStopped())
dispatchEvent(Event::create(EventTypeNames::voiceschanged));
}
const HeapVector<Member<SpeechSynthesisVoice> >& SpeechSynthesis::getVoices()
{
if (m_voiceList.size())
return m_voiceList;
// If the voiceList is empty, that's the cue to get the voices from the platform again.
const HeapVector<Member<PlatformSpeechSynthesisVoice> >& platformVoices = m_platformSpeechSynthesizer->voiceList();
size_t voiceCount = platformVoices.size();
for (size_t k = 0; k < voiceCount; k++)
m_voiceList.append(SpeechSynthesisVoice::create(platformVoices[k].get()));
return m_voiceList;
}
bool SpeechSynthesis::speaking() const
{
// If we have a current speech utterance, then that means we're assumed to be in a speaking state.
// This state is independent of whether the utterance happens to be paused.
return currentSpeechUtterance();
}
bool SpeechSynthesis::pending() const
{
// This is true if there are any utterances that have not started.
// That means there will be more than one in the queue.
return m_utteranceQueue.size() > 1;
}
bool SpeechSynthesis::paused() const
{
return m_isPaused;
}
void SpeechSynthesis::startSpeakingImmediately()
{
SpeechSynthesisUtterance* utterance = currentSpeechUtterance();
ASSERT(utterance);
utterance->setStartTime(monotonicallyIncreasingTime());
m_isPaused = false;
m_platformSpeechSynthesizer->speak(utterance->platformUtterance());
}
void SpeechSynthesis::speak(SpeechSynthesisUtterance* utterance, ExceptionState& exceptionState)
{
if (!utterance) {
exceptionState.throwTypeError("Invalid utterance argument");
return;
}
m_utteranceQueue.append(utterance);
// If the queue was empty, speak this immediately.
if (m_utteranceQueue.size() == 1)
startSpeakingImmediately();
}
void SpeechSynthesis::cancel()
{
// Remove all the items from the utterance queue. The platform
// may still have references to some of these utterances and may
// fire events on them asynchronously.
m_utteranceQueue.clear();
m_platformSpeechSynthesizer->cancel();
}
void SpeechSynthesis::pause()
{
if (!m_isPaused)
m_platformSpeechSynthesizer->pause();
}
void SpeechSynthesis::resume()
{
if (!currentSpeechUtterance())
return;
m_platformSpeechSynthesizer->resume();
}
void SpeechSynthesis::fireEvent(const AtomicString& type, SpeechSynthesisUtterance* utterance, unsigned long charIndex, const String& name)
{
if (executionContext() && !executionContext()->activeDOMObjectsAreStopped())
utterance->dispatchEvent(SpeechSynthesisEvent::create(type, charIndex, (currentTime() - utterance->startTime()), name));
}
void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance* utterance, bool errorOccurred)
{
ASSERT(utterance);
bool shouldStartSpeaking = false;
// If the utterance that completed was the one we're currently speaking,
// remove it from the queue and start speaking the next one.
if (utterance == currentSpeechUtterance()) {
m_utteranceQueue.removeFirst();
shouldStartSpeaking = !!m_utteranceQueue.size();
}
// Always fire the event, because the platform may have asynchronously
// sent an event on an utterance before it got the message that we
// canceled it, and we should always report to the user what actually
// happened.
fireEvent(errorOccurred ? EventTypeNames::error : EventTypeNames::end, utterance, 0, String());
// Start the next utterance if we just finished one and one was pending.
if (shouldStartSpeaking && !m_utteranceQueue.isEmpty())
startSpeakingImmediately();
}
void SpeechSynthesis::boundaryEventOccurred(PlatformSpeechSynthesisUtterance* utterance, SpeechBoundary boundary, unsigned charIndex)
{
DEFINE_STATIC_LOCAL(const String, wordBoundaryString, ("word"));
DEFINE_STATIC_LOCAL(const String, sentenceBoundaryString, ("sentence"));
switch (boundary) {
case SpeechWordBoundary:
fireEvent(EventTypeNames::boundary, static_cast<SpeechSynthesisUtterance*>(utterance->client()), charIndex, wordBoundaryString);
break;
case SpeechSentenceBoundary:
fireEvent(EventTypeNames::boundary, static_cast<SpeechSynthesisUtterance*>(utterance->client()), charIndex, sentenceBoundaryString);
break;
default:
ASSERT_NOT_REACHED();
}
}
void SpeechSynthesis::didStartSpeaking(PlatformSpeechSynthesisUtterance* utterance)
{
if (utterance->client())
fireEvent(EventTypeNames::start, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
}
void SpeechSynthesis::didPauseSpeaking(PlatformSpeechSynthesisUtterance* utterance)
{
m_isPaused = true;
if (utterance->client())
fireEvent(EventTypeNames::pause, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
}
void SpeechSynthesis::didResumeSpeaking(PlatformSpeechSynthesisUtterance* utterance)
{
m_isPaused = false;
if (utterance->client())
fireEvent(EventTypeNames::resume, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
}
void SpeechSynthesis::didFinishSpeaking(PlatformSpeechSynthesisUtterance* utterance)
{
if (utterance->client())
handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance*>(utterance->client()), false);
}
void SpeechSynthesis::speakingErrorOccurred(PlatformSpeechSynthesisUtterance* utterance)
{
if (utterance->client())
handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance*>(utterance->client()), true);
}
SpeechSynthesisUtterance* SpeechSynthesis::currentSpeechUtterance() const
{
if (!m_utteranceQueue.isEmpty())
return m_utteranceQueue.first().get();
return 0;
}
const AtomicString& SpeechSynthesis::interfaceName() const
{
return EventTargetNames::SpeechSynthesis;
}
void SpeechSynthesis::trace(Visitor* visitor)
{
visitor->trace(m_platformSpeechSynthesizer);
visitor->trace(m_voiceList);
visitor->trace(m_utteranceQueue);
PlatformSpeechSynthesizerClient::trace(visitor);
RefCountedGarbageCollectedEventTargetWithInlineData<SpeechSynthesis>::trace(visitor);
ContextLifecycleObserver::trace(visitor);
}
} // namespace blink
|