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))
49 void SpeechSynthesis::setPlatformSynthesizer(PlatformSpeechSynthesizer* synthesizer)
51 m_platformSpeechSynthesizer = synthesizer;
54 ExecutionContext* SpeechSynthesis::executionContext() const
56 return ContextLifecycleObserver::executionContext();
59 void SpeechSynthesis::voicesDidChange()
62 if (executionContext() && !executionContext()->activeDOMObjectsAreStopped())
63 dispatchEvent(Event::create(EventTypeNames::voiceschanged));
66 const HeapVector<Member<SpeechSynthesisVoice> >& SpeechSynthesis::getVoices()
68 if (m_voiceList.size())
71 // If the voiceList is empty, that's the cue to get the voices from the platform again.
72 const HeapVector<Member<PlatformSpeechSynthesisVoice> >& platformVoices = m_platformSpeechSynthesizer->voiceList();
73 size_t voiceCount = platformVoices.size();
74 for (size_t k = 0; k < voiceCount; k++)
75 m_voiceList.append(SpeechSynthesisVoice::create(platformVoices[k].get()));
80 bool SpeechSynthesis::speaking() const
82 // If we have a current speech utterance, then that means we're assumed to be in a speaking state.
83 // This state is independent of whether the utterance happens to be paused.
84 return currentSpeechUtterance();
87 bool SpeechSynthesis::pending() const
89 // This is true if there are any utterances that have not started.
90 // That means there will be more than one in the queue.
91 return m_utteranceQueue.size() > 1;
94 bool SpeechSynthesis::paused() const
99 void SpeechSynthesis::startSpeakingImmediately()
101 SpeechSynthesisUtterance* utterance = currentSpeechUtterance();
104 utterance->setStartTime(monotonicallyIncreasingTime());
106 m_platformSpeechSynthesizer->speak(utterance->platformUtterance());
109 void SpeechSynthesis::speak(SpeechSynthesisUtterance* utterance, ExceptionState& exceptionState)
112 exceptionState.throwTypeError("Invalid utterance argument");
116 m_utteranceQueue.append(utterance);
118 // If the queue was empty, speak this immediately.
119 if (m_utteranceQueue.size() == 1)
120 startSpeakingImmediately();
123 void SpeechSynthesis::cancel()
125 // Remove all the items from the utterance queue. The platform
126 // may still have references to some of these utterances and may
127 // fire events on them asynchronously.
128 m_utteranceQueue.clear();
129 m_platformSpeechSynthesizer->cancel();
132 void SpeechSynthesis::pause()
135 m_platformSpeechSynthesizer->pause();
138 void SpeechSynthesis::resume()
140 if (!currentSpeechUtterance())
142 m_platformSpeechSynthesizer->resume();
145 void SpeechSynthesis::fireEvent(const AtomicString& type, SpeechSynthesisUtterance* utterance, unsigned long charIndex, const String& name)
147 if (executionContext() && !executionContext()->activeDOMObjectsAreStopped())
148 utterance->dispatchEvent(SpeechSynthesisEvent::create(type, charIndex, (currentTime() - utterance->startTime()), name));
151 void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance* utterance, bool errorOccurred)
155 bool shouldStartSpeaking = false;
156 // If the utterance that completed was the one we're currently speaking,
157 // remove it from the queue and start speaking the next one.
158 if (utterance == currentSpeechUtterance()) {
159 m_utteranceQueue.removeFirst();
160 shouldStartSpeaking = !!m_utteranceQueue.size();
163 // Always fire the event, because the platform may have asynchronously
164 // sent an event on an utterance before it got the message that we
165 // canceled it, and we should always report to the user what actually
167 fireEvent(errorOccurred ? EventTypeNames::error : EventTypeNames::end, utterance, 0, String());
169 // Start the next utterance if we just finished one and one was pending.
170 if (shouldStartSpeaking && !m_utteranceQueue.isEmpty())
171 startSpeakingImmediately();
174 void SpeechSynthesis::boundaryEventOccurred(PlatformSpeechSynthesisUtterance* utterance, SpeechBoundary boundary, unsigned charIndex)
176 DEFINE_STATIC_LOCAL(const String, wordBoundaryString, ("word"));
177 DEFINE_STATIC_LOCAL(const String, sentenceBoundaryString, ("sentence"));
180 case SpeechWordBoundary:
181 fireEvent(EventTypeNames::boundary, static_cast<SpeechSynthesisUtterance*>(utterance->client()), charIndex, wordBoundaryString);
183 case SpeechSentenceBoundary:
184 fireEvent(EventTypeNames::boundary, static_cast<SpeechSynthesisUtterance*>(utterance->client()), charIndex, sentenceBoundaryString);
187 ASSERT_NOT_REACHED();
191 void SpeechSynthesis::didStartSpeaking(PlatformSpeechSynthesisUtterance* utterance)
193 if (utterance->client())
194 fireEvent(EventTypeNames::start, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
197 void SpeechSynthesis::didPauseSpeaking(PlatformSpeechSynthesisUtterance* utterance)
200 if (utterance->client())
201 fireEvent(EventTypeNames::pause, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
204 void SpeechSynthesis::didResumeSpeaking(PlatformSpeechSynthesisUtterance* utterance)
207 if (utterance->client())
208 fireEvent(EventTypeNames::resume, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
211 void SpeechSynthesis::didFinishSpeaking(PlatformSpeechSynthesisUtterance* utterance)
213 if (utterance->client())
214 handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance*>(utterance->client()), false);
217 void SpeechSynthesis::speakingErrorOccurred(PlatformSpeechSynthesisUtterance* utterance)
219 if (utterance->client())
220 handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance*>(utterance->client()), true);
223 SpeechSynthesisUtterance* SpeechSynthesis::currentSpeechUtterance() const
225 if (!m_utteranceQueue.isEmpty())
226 return m_utteranceQueue.first().get();
230 const AtomicString& SpeechSynthesis::interfaceName() const
232 return EventTargetNames::SpeechSynthesis;
235 void SpeechSynthesis::trace(Visitor* visitor)
237 visitor->trace(m_platformSpeechSynthesizer);
238 visitor->trace(m_voiceList);
239 visitor->trace(m_utteranceQueue);
240 PlatformSpeechSynthesizerClient::trace(visitor);
241 EventTargetWithInlineData::trace(visitor);