[EFL] Utilize espeak as a synthesizer back-end for WebSpeech
authork.czech@samsung.com <k.czech@samsung.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 26 Aug 2014 11:25:49 +0000 (11:25 +0000)
committerk.czech@samsung.com <k.czech@samsung.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 26 Aug 2014 11:25:49 +0000 (11:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=136127

Reviewed by Gyuyoung Kim.

.:

Add build support for espeak.

* Source/cmake/FindEspeak.cmake: Added.
* Source/cmake/OptionsEfl.cmake: Add Espeak dependency.

Source/WebCore:

Utilize espeak API to have support for speak, cancel and initializeVoiceList.

* PlatformEfl.cmake:
* platform/efl/PlatformSpeechSynthesisProviderEfl.cpp:
(WebCore::PlatformSpeechSynthesisProviderEfl::PlatformSpeechSynthesisProviderEfl):
(WebCore::PlatformSpeechSynthesisProviderEfl::convertRateToEspeakValue):
(WebCore::PlatformSpeechSynthesisProviderEfl::convertVolumeToEspeakValue):
(WebCore::PlatformSpeechSynthesisProviderEfl::convertPitchToEspeakValue):
(WebCore::PlatformSpeechSynthesisProviderEfl::voiceName):
(WebCore::PlatformSpeechSynthesisProviderEfl::engineInit):
(WebCore::PlatformSpeechSynthesisProviderEfl::currentVoice):
(WebCore::PlatformSpeechSynthesisProviderEfl::initializeVoiceList):
(WebCore::PlatformSpeechSynthesisProviderEfl::speak):
(WebCore::PlatformSpeechSynthesisProviderEfl::cancel):
(WebCore::PlatformSpeechSynthesisProviderEfl::fireSpeechEvent):
* platform/efl/PlatformSpeechSynthesisProviderEfl.h:
* platform/efl/PlatformSpeechSynthesizerEfl.cpp:
(WebCore::PlatformSpeechSynthesizer::PlatformSpeechSynthesizer):
(WebCore::PlatformSpeechSynthesizer::initializeVoiceList):
(WebCore::PlatformSpeechSynthesizer::pause):
(WebCore::PlatformSpeechSynthesizer::resume):
(WebCore::PlatformSpeechSynthesizer::speak):
(WebCore::PlatformSpeechSynthesizer::cancel):

Tools:

Add espeak dependency.

* efl/install-dependencies:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@172956 268f45cc-cd09-0410-ab3c-d52691b4dbfc

ChangeLog
Source/WebCore/ChangeLog
Source/WebCore/PlatformEfl.cmake
Source/WebCore/platform/efl/PlatformSpeechSynthesisProviderEfl.cpp
Source/WebCore/platform/efl/PlatformSpeechSynthesisProviderEfl.h
Source/WebCore/platform/efl/PlatformSpeechSynthesizerEfl.cpp
Source/cmake/FindEspeak.cmake [new file with mode: 0644]
Source/cmake/OptionsEfl.cmake
Tools/ChangeLog
Tools/efl/install-dependencies

index fea3871..8a0549a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2014-08-26  Krzysztof Czech  <k.czech@samsung.com>
+
+        [EFL] Utilize espeak as a synthesizer back-end for WebSpeech
+        https://bugs.webkit.org/show_bug.cgi?id=136127
+
+        Reviewed by Gyuyoung Kim.
+
+        Add build support for espeak.
+
+        * Source/cmake/FindEspeak.cmake: Added.
+        * Source/cmake/OptionsEfl.cmake: Add Espeak dependency.
+
 2014-08-26  Ryuan Choi  <ryuan.choi@samsung.com>
 
         [EFL] Build break using clang
index 455169b..38268b6 100644 (file)
@@ -1,3 +1,34 @@
+2014-08-26  Krzysztof Czech  <k.czech@samsung.com>
+
+        [EFL] Utilize espeak as a synthesizer back-end for WebSpeech
+        https://bugs.webkit.org/show_bug.cgi?id=136127
+
+        Reviewed by Gyuyoung Kim.
+
+        Utilize espeak API to have support for speak, cancel and initializeVoiceList.
+
+        * PlatformEfl.cmake:
+        * platform/efl/PlatformSpeechSynthesisProviderEfl.cpp:
+        (WebCore::PlatformSpeechSynthesisProviderEfl::PlatformSpeechSynthesisProviderEfl):
+        (WebCore::PlatformSpeechSynthesisProviderEfl::convertRateToEspeakValue):
+        (WebCore::PlatformSpeechSynthesisProviderEfl::convertVolumeToEspeakValue):
+        (WebCore::PlatformSpeechSynthesisProviderEfl::convertPitchToEspeakValue):
+        (WebCore::PlatformSpeechSynthesisProviderEfl::voiceName):
+        (WebCore::PlatformSpeechSynthesisProviderEfl::engineInit):
+        (WebCore::PlatformSpeechSynthesisProviderEfl::currentVoice):
+        (WebCore::PlatformSpeechSynthesisProviderEfl::initializeVoiceList):
+        (WebCore::PlatformSpeechSynthesisProviderEfl::speak):
+        (WebCore::PlatformSpeechSynthesisProviderEfl::cancel):
+        (WebCore::PlatformSpeechSynthesisProviderEfl::fireSpeechEvent):
+        * platform/efl/PlatformSpeechSynthesisProviderEfl.h:
+        * platform/efl/PlatformSpeechSynthesizerEfl.cpp:
+        (WebCore::PlatformSpeechSynthesizer::PlatformSpeechSynthesizer):
+        (WebCore::PlatformSpeechSynthesizer::initializeVoiceList):
+        (WebCore::PlatformSpeechSynthesizer::pause):
+        (WebCore::PlatformSpeechSynthesizer::resume):
+        (WebCore::PlatformSpeechSynthesizer::speak):
+        (WebCore::PlatformSpeechSynthesizer::cancel):
+
 2014-08-26  Ryuan Choi  <ryuan.choi@samsung.com>
 
         [EFL] Build break using clang
index a4ec315..5afc810 100644 (file)
@@ -481,6 +481,12 @@ if (ENABLE_ACCESSIBILITY)
 endif ()
 
 if (ENABLE_SPEECH_SYNTHESIS)
+    list(APPEND WebCore_INCLUDE_DIRECTORIES
+        ${ESPEAK_INCLUDE_DIRS}
+    )
+    list(APPEND WebCore_LIBRARIES
+        ${ESPEAK_LIBRARIES}
+    )
     list(APPEND WebCore_SOURCES
         platform/efl/PlatformSpeechSynthesizerEfl.cpp
         platform/efl/PlatformSpeechSynthesisProviderEfl.cpp
index d1a66e7..015a392 100644 (file)
 #include <PlatformSpeechSynthesisUtterance.h>
 #include <PlatformSpeechSynthesisVoice.h>
 #include <PlatformSpeechSynthesizer.h>
+#include <wtf/text/CString.h>
 
 namespace WebCore {
 
-PlatformSpeechSynthesisProviderEfl::PlatformSpeechSynthesisProviderEfl(PlatformSpeechSynthesizer* platformSpeechSynthesizer)
-    : m_platformSpeechSynthesizer(platformSpeechSynthesizer)
+PlatformSpeechSynthesisProviderEfl::PlatformSpeechSynthesisProviderEfl(PlatformSpeechSynthesizer* client)
+    : m_isEngineStarted(false)
+    , m_platformSpeechSynthesizer(client)
 {
 }
 
@@ -44,10 +46,86 @@ PlatformSpeechSynthesisProviderEfl::~PlatformSpeechSynthesisProviderEfl()
 {
 }
 
+int PlatformSpeechSynthesisProviderEfl::convertRateToEspeakValue(float rate) const
+{
+    // The normal value that Espeak expects is 175, minimum is 80 and maximum 450
+    return espeakRATE_NORMAL * rate;
+}
+
+int PlatformSpeechSynthesisProviderEfl::convertVolumeToEspeakValue(float volume) const
+{
+    // 0 = silence, 100 = normal, greater values may produce distortion
+    return volume * 100;
+}
+
+int PlatformSpeechSynthesisProviderEfl::convertPitchToEspeakValue(float pitch) const
+{
+    // 0 = minimum, 50 = normal, 100 = maximum
+    return pitch * 50;
+}
+
+String PlatformSpeechSynthesisProviderEfl::voiceName(PassRefPtr<PlatformSpeechSynthesisUtterance> utterance) const
+{
+    if (!m_platformSpeechSynthesizer)
+        return String();
+
+    if (!utterance->lang().isEmpty()) {
+        const String& language = utterance->lang();
+        const Vector<RefPtr<PlatformSpeechSynthesisVoice>>& voiceList = m_platformSpeechSynthesizer->voiceList();
+        for (const auto& voice : voiceList) {
+            // Espeak adds an empty character at the beginning of the language
+            unsigned length = voice->lang().length();
+            String lang = voice->lang().substring(1, length);
+            if (equalIgnoringCase(language, lang))
+                return voice->name();
+        }
+    }
+
+    espeak_VOICE* espeakVoice = currentVoice();
+    ASSERT(espeakVoice);
+    return ASCIILiteral(espeakVoice->name);
+}
+
+bool PlatformSpeechSynthesisProviderEfl::engineInit()
+{
+    if (!m_isEngineStarted) {
+        if (!(m_isEngineStarted = espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 0, 0, 0) != EE_INTERNAL_ERROR))
+            return false;
+        espeak_SetVoiceByName("default");
+    }
+    return true;
+}
+
+espeak_VOICE* PlatformSpeechSynthesisProviderEfl::currentVoice() const
+{
+    return espeak_GetCurrentVoice();
+}
+
 void PlatformSpeechSynthesisProviderEfl::initializeVoiceList(Vector<RefPtr<PlatformSpeechSynthesisVoice>>& voiceList)
 {
-    UNUSED_PARAM(voiceList);
-    notImplemented();
+    if (!engineInit()) {
+        fireSpeechEvent(SpeechError);
+        return;
+    }
+
+    espeak_VOICE* espeakVoice = currentVoice();
+    ASSERT(espeakVoice);
+    String currentLanguage = ASCIILiteral(espeakVoice->languages);
+
+    const espeak_VOICE** voices = espeak_ListVoices(nullptr);
+    if (!voices) {
+        fireSpeechEvent(SpeechError);
+        return;
+    }
+
+    // Voices array is terminated by the nullptr
+    for (int i = 0; voices[i]; i++) {
+        const espeak_VOICE* voice = voices[i];
+        String id = ASCIILiteral(voice->identifier);
+        String name = ASCIILiteral(voice->name);
+        String language = ASCIILiteral(voice->languages);
+        voiceList.append(PlatformSpeechSynthesisVoice::create(id, name, language, true, language == currentLanguage));
+    }
 }
 
 void PlatformSpeechSynthesisProviderEfl::pause()
@@ -62,13 +140,63 @@ void PlatformSpeechSynthesisProviderEfl::resume()
 
 void PlatformSpeechSynthesisProviderEfl::speak(PassRefPtr<PlatformSpeechSynthesisUtterance> utterance)
 {
-    UNUSED_PARAM(utterance);
-    notImplemented();
+    if (!engineInit() || !utterance) {
+        fireSpeechEvent(SpeechError);
+        return;
+    }
+
+    m_utterance = utterance;
+    String voice = voiceName(m_utterance);
+    espeak_SetVoiceByName(voice.utf8().data());
+    espeak_SetParameter(espeakRATE, convertRateToEspeakValue(m_utterance->rate()), 0);
+    espeak_SetParameter(espeakVOLUME, convertVolumeToEspeakValue(m_utterance->volume()), 0);
+    espeak_SetParameter(espeakPITCH, convertPitchToEspeakValue(m_utterance->pitch()), 0);
+
+    String textToRead = m_utterance->text();
+    espeak_ERROR err = espeak_Synth(textToRead.utf8().data(), textToRead.length(), 0, POS_CHARACTER, 0, espeakCHARS_AUTO, 0, nullptr);
+    if (err == EE_INTERNAL_ERROR) {
+        fireSpeechEvent(SpeechError);
+        m_utterance = nullptr;
+        return;
+    }
+
+    fireSpeechEvent(SpeechStart);
 }
 
 void PlatformSpeechSynthesisProviderEfl::cancel()
 {
-    notImplemented();
+    if (!m_isEngineStarted || !m_utterance)
+        return;
+
+    if (espeak_Cancel() == EE_INTERNAL_ERROR) {
+        fireSpeechEvent(SpeechError);
+        m_utterance = nullptr;
+        return;
+    }
+    fireSpeechEvent(SpeechCancel);
+    m_utterance = nullptr;
+}
+
+void PlatformSpeechSynthesisProviderEfl::fireSpeechEvent(SpeechEvent speechEvent)
+{
+    switch (speechEvent) {
+    case SpeechStart:
+        m_platformSpeechSynthesizer->client()->didStartSpeaking(m_utterance);
+        break;
+    case SpeechPause:
+        m_platformSpeechSynthesizer->client()->didPauseSpeaking(m_utterance);
+        break;
+    case SpeechResume:
+        m_platformSpeechSynthesizer->client()->didResumeSpeaking(m_utterance);
+        break;
+    case SpeechError:
+        m_isEngineStarted = false;
+    case SpeechCancel:
+        m_platformSpeechSynthesizer->client()->speakingErrorOccurred(m_utterance);
+        break;
+    default:
+        ASSERT_NOT_REACHED();
+    };
 }
 
 } // namespace WebCore
index 318f6d8..06d5f1e 100644 (file)
 
 #if ENABLE(SPEECH_SYNTHESIS)
 
+#include <speak_lib.h>
 #include <wtf/PassRefPtr.h>
 #include <wtf/Vector.h>
+#include <wtf/text/WTFString.h>
 
 namespace WebCore {
 
@@ -39,6 +41,14 @@ class PlatformSpeechSynthesisVoice;
 
 class PlatformSpeechSynthesisProviderEfl {
 public:
+    enum SpeechEvent {
+        SpeechError,
+        SpeechCancel,
+        SpeechPause,
+        SpeechResume,
+        SpeechStart
+    };
+
     explicit PlatformSpeechSynthesisProviderEfl(PlatformSpeechSynthesizer*);
     ~PlatformSpeechSynthesisProviderEfl();
 
@@ -48,7 +58,19 @@ public:
     void speak(PassRefPtr<PlatformSpeechSynthesisUtterance>);
     void cancel();
 private:
+    bool engineInit();
+
+    int convertRateToEspeakValue(float) const;
+    int convertVolumeToEspeakValue(float) const;
+    int convertPitchToEspeakValue(float) const;
+
+    espeak_VOICE* currentVoice() const;
+    String voiceName(PassRefPtr<PlatformSpeechSynthesisUtterance>) const;
+    void fireSpeechEvent(SpeechEvent);
+
+    bool m_isEngineStarted;
     PlatformSpeechSynthesizer* m_platformSpeechSynthesizer;
+    RefPtr<PlatformSpeechSynthesisUtterance> m_utterance;
 };
 
 } // namespace WebCore
index c6bade1..467c7e2 100644 (file)
@@ -38,7 +38,6 @@ PlatformSpeechSynthesizer::PlatformSpeechSynthesizer(PlatformSpeechSynthesizerCl
     : m_voiceListIsInitialized(false)
     , m_speechSynthesizerClient(client)
     , m_platformSpeechWrapper(std::make_unique<PlatformSpeechSynthesisProviderEfl>(this))
-
 {
 }
 
@@ -48,26 +47,31 @@ PlatformSpeechSynthesizer::~PlatformSpeechSynthesizer()
 
 void PlatformSpeechSynthesizer::initializeVoiceList()
 {
+    ASSERT(m_platformSpeechWrapper);
     m_platformSpeechWrapper->initializeVoiceList(m_voiceList);
 }
 
 void PlatformSpeechSynthesizer::pause()
 {
+    ASSERT(m_platformSpeechWrapper);
     m_platformSpeechWrapper->pause();
 }
 
 void PlatformSpeechSynthesizer::resume()
 {
+    ASSERT(m_platformSpeechWrapper);
     m_platformSpeechWrapper->resume();
 }
 
 void PlatformSpeechSynthesizer::speak(PassRefPtr<PlatformSpeechSynthesisUtterance> utterance)
 {
+    ASSERT(m_platformSpeechWrapper);
     m_platformSpeechWrapper->speak(utterance);
 }
 
 void PlatformSpeechSynthesizer::cancel()
 {
+    ASSERT(m_platformSpeechWrapper);
     m_platformSpeechWrapper->cancel();
 }
 
diff --git a/Source/cmake/FindEspeak.cmake b/Source/cmake/FindEspeak.cmake
new file mode 100644 (file)
index 0000000..d273b61
--- /dev/null
@@ -0,0 +1,42 @@
+# - Try to find Espeak
+# Once done, this will define
+#
+#  ESPEAK_INCLUDE_DIRS - the Espeak include drectories
+#  ESPEAK_LIBRARIES - link these to use Espeak
+#
+# Copyright (C) 2014 Samsung Electronics
+#
+# 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 THE COPYRIGHT HOLDER AND ITS CONTRIBUTORS ``A
+# 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 THE COPYRIGHT HOLDER OR ITS
+# 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.
+
+find_path(ESPEAK_INCLUDE_DIRS
+    NAMES speak_lib.h
+    PATH_SUFFIXES espeak
+)
+
+find_library(ESPEAK_LIBRARIES
+    NAMES espeak
+)
+
+mark_as_advanced(
+    ESPEAK_INCLUDE_DIRS
+    ESPEAK_LIBRARIES
+)
index b12823a..62006da 100644 (file)
@@ -312,6 +312,7 @@ else ()
 endif ()
 
 if (ENABLE_SPEECH_SYNTHESIS)
+    find_package(Espeak REQUIRED)
     add_definitions(-DENABLE_SPEECH_SYNTHESIS=1)
 endif ()
 
index bb5cfda..875fca6 100644 (file)
@@ -1,3 +1,14 @@
+2014-08-26  Krzysztof Czech  <k.czech@samsung.com>
+
+        [EFL] Utilize espeak as a synthesizer back-end for WebSpeech
+        https://bugs.webkit.org/show_bug.cgi?id=136127
+
+        Reviewed by Gyuyoung Kim.
+
+        Add espeak dependency.
+
+        * efl/install-dependencies:
+
 2014-08-25  Alexey Proskuryakov  <ap@apple.com>
 
         Remove a little more accidentally landed code from r172891.
index b25a359..845d3c5 100755 (executable)
@@ -39,6 +39,7 @@ function installDependenciesWithApt {
         libc++abi-dev \
         libdbus-1-dev \
         libenchant-dev \
+        libespeak-dev \
         libfaad-dev \
         libffi-dev \
         libfreetype6-dev \