SpeechSynthesis.cpp [plain text]
#include "config.h"
#include "SpeechSynthesis.h"
#if ENABLE(SPEECH_SYNTHESIS)
#include "EventNames.h"
#include "PlatformSpeechSynthesisVoice.h"
#include "PlatformSpeechSynthesizer.h"
#include "SpeechSynthesisEvent.h"
#include "SpeechSynthesisUtterance.h"
#include "UserGestureIndicator.h"
#include <wtf/NeverDestroyed.h>
namespace WebCore {
Ref<SpeechSynthesis> SpeechSynthesis::create(WeakPtr<SpeechSynthesisClient> client)
{
return adoptRef(*new SpeechSynthesis(client));
}
SpeechSynthesis::SpeechSynthesis(WeakPtr<SpeechSynthesisClient> client)
: m_currentSpeechUtterance(nullptr)
, m_isPaused(false)
#if PLATFORM(IOS_FAMILY)
, m_restrictions(RequireUserGestureForSpeechStartRestriction)
#endif
, m_speechSynthesisClient(client)
{
if (m_speechSynthesisClient)
m_speechSynthesisClient->setObserver(makeWeakPtr(this));
}
void SpeechSynthesis::setPlatformSynthesizer(std::unique_ptr<PlatformSpeechSynthesizer> synthesizer)
{
m_platformSpeechSynthesizer = WTFMove(synthesizer);
m_voiceList.clear();
m_currentSpeechUtterance = nullptr;
m_utteranceQueue.clear();
m_isPaused = false;
m_speechSynthesisClient = nullptr;
}
void SpeechSynthesis::voicesDidChange()
{
m_voiceList.clear();
}
PlatformSpeechSynthesizer& SpeechSynthesis::ensurePlatformSpeechSynthesizer()
{
if (!m_platformSpeechSynthesizer)
m_platformSpeechSynthesizer = makeUnique<PlatformSpeechSynthesizer>(this);
return *m_platformSpeechSynthesizer;
}
const Vector<Ref<SpeechSynthesisVoice>>& SpeechSynthesis::getVoices()
{
if (!m_voiceList.isEmpty())
return m_voiceList;
for (auto& voice : m_speechSynthesisClient ? m_speechSynthesisClient->voiceList() : ensurePlatformSpeechSynthesizer().voiceList())
m_voiceList.append(SpeechSynthesisVoice::create(*voice));
return m_voiceList;
}
bool SpeechSynthesis::speaking() const
{
return m_currentSpeechUtterance;
}
bool SpeechSynthesis::pending() const
{
return m_utteranceQueue.size() > 1;
}
bool SpeechSynthesis::paused() const
{
return m_isPaused;
}
void SpeechSynthesis::startSpeakingImmediately(SpeechSynthesisUtterance& utterance)
{
utterance.setStartTime(MonotonicTime::now());
m_currentSpeechUtterance = &utterance;
m_isPaused = false;
if (utterance.text().isEmpty()) {
handleSpeakingCompleted(utterance, false);
return;
}
if (m_speechSynthesisClient)
m_speechSynthesisClient->speak(utterance.platformUtterance());
else
ensurePlatformSpeechSynthesizer().speak(utterance.platformUtterance());
}
void SpeechSynthesis::speak(SpeechSynthesisUtterance& utterance)
{
#if PLATFORM(IOS_FAMILY)
if (UserGestureIndicator::processingUserGesture())
removeBehaviorRestriction(RequireUserGestureForSpeechStartRestriction);
else if (userGestureRequiredForSpeechStart())
return;
#endif
m_utteranceQueue.append(utterance);
if (m_utteranceQueue.size() == 1)
startSpeakingImmediately(m_utteranceQueue.first());
}
void SpeechSynthesis::cancel()
{
RefPtr<SpeechSynthesisUtterance> current = m_currentSpeechUtterance;
m_utteranceQueue.clear();
if (m_speechSynthesisClient) {
m_speechSynthesisClient->cancel();
speakingErrorOccurred();
m_currentSpeechUtterance = nullptr;
} else if (m_platformSpeechSynthesizer) {
m_platformSpeechSynthesizer->cancel();
ASSERT(!m_currentSpeechUtterance);
}
current = nullptr;
}
void SpeechSynthesis::pause()
{
if (!m_isPaused) {
if (m_speechSynthesisClient)
m_speechSynthesisClient->pause();
else if (m_platformSpeechSynthesizer)
m_platformSpeechSynthesizer->pause();
}
}
void SpeechSynthesis::resume()
{
if (m_currentSpeechUtterance) {
if (m_speechSynthesisClient)
m_speechSynthesisClient->resume();
else if (m_platformSpeechSynthesizer)
m_platformSpeechSynthesizer->resume();
}
}
void SpeechSynthesis::fireEvent(const AtomString& type, SpeechSynthesisUtterance& utterance, unsigned long charIndex, const String& name)
{
utterance.dispatchEvent(SpeechSynthesisEvent::create(type, charIndex, (MonotonicTime::now() - utterance.startTime()).seconds(), name));
}
void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance& utterance, bool errorOccurred)
{
ASSERT(m_currentSpeechUtterance);
Ref<SpeechSynthesisUtterance> protect(utterance);
m_currentSpeechUtterance = nullptr;
fireEvent(errorOccurred ? eventNames().errorEvent : eventNames().endEvent, utterance, 0, String());
if (m_utteranceQueue.size()) {
Ref<SpeechSynthesisUtterance> firstUtterance = m_utteranceQueue.takeFirst();
ASSERT(&utterance == firstUtterance.ptr());
if (!m_utteranceQueue.isEmpty())
startSpeakingImmediately(m_utteranceQueue.first());
}
}
void SpeechSynthesis::boundaryEventOccurred(PlatformSpeechSynthesisUtterance& utterance, SpeechBoundary boundary, unsigned charIndex)
{
static NeverDestroyed<const String> wordBoundaryString(MAKE_STATIC_STRING_IMPL("word"));
static NeverDestroyed<const String> sentenceBoundaryString(MAKE_STATIC_STRING_IMPL("sentence"));
ASSERT(utterance.client());
switch (boundary) {
case SpeechBoundary::SpeechWordBoundary:
fireEvent(eventNames().boundaryEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), charIndex, wordBoundaryString);
break;
case SpeechBoundary::SpeechSentenceBoundary:
fireEvent(eventNames().boundaryEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), charIndex, sentenceBoundaryString);
break;
default:
ASSERT_NOT_REACHED();
}
}
void SpeechSynthesis::didStartSpeaking()
{
if (!m_currentSpeechUtterance)
return;
didStartSpeaking(*m_currentSpeechUtterance->platformUtterance());
}
void SpeechSynthesis::didFinishSpeaking()
{
if (!m_currentSpeechUtterance)
return;
didFinishSpeaking(*m_currentSpeechUtterance->platformUtterance());
}
void SpeechSynthesis::didPauseSpeaking()
{
if (!m_currentSpeechUtterance)
return;
didPauseSpeaking(*m_currentSpeechUtterance->platformUtterance());
}
void SpeechSynthesis::didResumeSpeaking()
{
if (!m_currentSpeechUtterance)
return;
didResumeSpeaking(*m_currentSpeechUtterance->platformUtterance());
}
void SpeechSynthesis::speakingErrorOccurred()
{
if (!m_currentSpeechUtterance)
return;
speakingErrorOccurred(*m_currentSpeechUtterance->platformUtterance());
}
void SpeechSynthesis::boundaryEventOccurred(bool wordBoundary, unsigned charIndex)
{
if (!m_currentSpeechUtterance)
return;
boundaryEventOccurred(*m_currentSpeechUtterance->platformUtterance(), wordBoundary ? SpeechBoundary::SpeechWordBoundary : SpeechBoundary::SpeechSentenceBoundary, charIndex);
}
void SpeechSynthesis::voicesChanged()
{
voicesDidChange();
}
void SpeechSynthesis::didStartSpeaking(PlatformSpeechSynthesisUtterance& utterance)
{
if (utterance.client())
fireEvent(eventNames().startEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), 0, String());
}
void SpeechSynthesis::didPauseSpeaking(PlatformSpeechSynthesisUtterance& utterance)
{
m_isPaused = true;
if (utterance.client())
fireEvent(eventNames().pauseEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), 0, String());
}
void SpeechSynthesis::didResumeSpeaking(PlatformSpeechSynthesisUtterance& utterance)
{
m_isPaused = false;
if (utterance.client())
fireEvent(eventNames().resumeEvent, 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);
}
}
#endif // ENABLE(SPEECH_SYNTHESIS)