2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "modules/speech/SpeechSynthesis.h"
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "core/dom/ExecutionContext.h"
31 #include "modules/speech/SpeechSynthesisEvent.h"
32 #include "platform/speech/PlatformSpeechSynthesisVoice.h"
33 #include "wtf/CurrentTime.h"
37 SpeechSynthesis* SpeechSynthesis::create(ExecutionContext* context)
39 return adoptRefCountedGarbageCollectedWillBeNoop(new SpeechSynthesis(context));
42 SpeechSynthesis::SpeechSynthesis(ExecutionContext* context)
43 : ContextLifecycleObserver(context)
44 , m_platformSpeechSynthesizer(PlatformSpeechSynthesizer::create(this))
47 ScriptWrappable::init(this);
50 void SpeechSynthesis::setPlatformSynthesizer(PlatformSpeechSynthesizer* synthesizer)
52 m_platformSpeechSynthesizer = synthesizer;
55 ExecutionContext* SpeechSynthesis::executionContext() const
57 return ContextLifecycleObserver::executionContext();
60 void SpeechSynthesis::voicesDidChange()
63 if (executionContext() && !executionContext()->activeDOMObjectsAreStopped())
64 dispatchEvent(Event::create(EventTypeNames::voiceschanged));
67 const HeapVector<Member<SpeechSynthesisVoice> >& SpeechSynthesis::getVoices()
69 if (m_voiceList.size())
72 // If the voiceList is empty, that's the cue to get the voices from the platform again.
73 const HeapVector<Member<PlatformSpeechSynthesisVoice> >& platformVoices = m_platformSpeechSynthesizer->voiceList();
74 size_t voiceCount = platformVoices.size();
75 for (size_t k = 0; k < voiceCount; k++)
76 m_voiceList.append(SpeechSynthesisVoice::create(platformVoices[k].get()));
81 bool SpeechSynthesis::speaking() const
83 // If we have a current speech utterance, then that means we're assumed to be in a speaking state.
84 // This state is independent of whether the utterance happens to be paused.
85 return currentSpeechUtterance();
88 bool SpeechSynthesis::pending() const
90 // This is true if there are any utterances that have not started.
91 // That means there will be more than one in the queue.
92 return m_utteranceQueue.size() > 1;
95 bool SpeechSynthesis::paused() const
100 void SpeechSynthesis::startSpeakingImmediately()
102 SpeechSynthesisUtterance* utterance = currentSpeechUtterance();
105 utterance->setStartTime(monotonicallyIncreasingTime());
107 m_platformSpeechSynthesizer->speak(utterance->platformUtterance());
110 void SpeechSynthesis::speak(SpeechSynthesisUtterance* utterance, ExceptionState& exceptionState)
113 exceptionState.throwTypeError("Invalid utterance argument");
117 m_utteranceQueue.append(utterance);
119 // If the queue was empty, speak this immediately.
120 if (m_utteranceQueue.size() == 1)
121 startSpeakingImmediately();
124 void SpeechSynthesis::cancel()
126 // Remove all the items from the utterance queue. The platform
127 // may still have references to some of these utterances and may
128 // fire events on them asynchronously.
129 m_utteranceQueue.clear();
130 m_platformSpeechSynthesizer->cancel();
133 void SpeechSynthesis::pause()
136 m_platformSpeechSynthesizer->pause();
139 void SpeechSynthesis::resume()
141 if (!currentSpeechUtterance())
143 m_platformSpeechSynthesizer->resume();
146 void SpeechSynthesis::fireEvent(const AtomicString& type, SpeechSynthesisUtterance* utterance, unsigned long charIndex, const String& name)
148 if (executionContext() && !executionContext()->activeDOMObjectsAreStopped())
149 utterance->dispatchEvent(SpeechSynthesisEvent::create(type, charIndex, (currentTime() - utterance->startTime()), name));
152 void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance* utterance, bool errorOccurred)
156 bool didJustFinishCurrentUtterance = false;
157 // If the utterance that completed was the one we're currently speaking,
158 // remove it from the queue and start speaking the next one.
159 if (utterance == currentSpeechUtterance()) {
160 m_utteranceQueue.removeFirst();
161 didJustFinishCurrentUtterance = true;
164 // Always fire the event, because the platform may have asynchronously
165 // sent an event on an utterance before it got the message that we
166 // canceled it, and we should always report to the user what actually
168 fireEvent(errorOccurred ? EventTypeNames::error : EventTypeNames::end, utterance, 0, String());
170 // Start the next utterance if we just finished one and one was pending.
171 if (didJustFinishCurrentUtterance && !m_utteranceQueue.isEmpty() && !utterance->startTime())
172 startSpeakingImmediately();
175 void SpeechSynthesis::boundaryEventOccurred(PlatformSpeechSynthesisUtterance* utterance, SpeechBoundary boundary, unsigned charIndex)
177 DEFINE_STATIC_LOCAL(const String, wordBoundaryString, ("word"));
178 DEFINE_STATIC_LOCAL(const String, sentenceBoundaryString, ("sentence"));
181 case SpeechWordBoundary:
182 fireEvent(EventTypeNames::boundary, static_cast<SpeechSynthesisUtterance*>(utterance->client()), charIndex, wordBoundaryString);
184 case SpeechSentenceBoundary:
185 fireEvent(EventTypeNames::boundary, static_cast<SpeechSynthesisUtterance*>(utterance->client()), charIndex, sentenceBoundaryString);
188 ASSERT_NOT_REACHED();
192 void SpeechSynthesis::didStartSpeaking(PlatformSpeechSynthesisUtterance* utterance)
194 if (utterance->client())
195 fireEvent(EventTypeNames::start, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
198 void SpeechSynthesis::didPauseSpeaking(PlatformSpeechSynthesisUtterance* utterance)
201 if (utterance->client())
202 fireEvent(EventTypeNames::pause, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
205 void SpeechSynthesis::didResumeSpeaking(PlatformSpeechSynthesisUtterance* utterance)
208 if (utterance->client())
209 fireEvent(EventTypeNames::resume, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
212 void SpeechSynthesis::didFinishSpeaking(PlatformSpeechSynthesisUtterance* utterance)
214 if (utterance->client())
215 handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance*>(utterance->client()), false);
218 void SpeechSynthesis::speakingErrorOccurred(PlatformSpeechSynthesisUtterance* utterance)
220 if (utterance->client())
221 handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance*>(utterance->client()), true);
224 SpeechSynthesisUtterance* SpeechSynthesis::currentSpeechUtterance() const
226 if (!m_utteranceQueue.isEmpty())
227 return m_utteranceQueue.first().get();
231 const AtomicString& SpeechSynthesis::interfaceName() const
233 return EventTargetNames::SpeechSynthesis;
236 void SpeechSynthesis::trace(Visitor* visitor)
238 visitor->trace(m_platformSpeechSynthesizer);
239 visitor->trace(m_voiceList);
240 visitor->trace(m_utteranceQueue);
241 PlatformSpeechSynthesizerClient::trace(visitor);
242 EventTargetWithInlineData::trace(visitor);