Factor out executable based TTS engines to separate subclasses.

Similar as done with SAPI / MSSP make the current implementation for executable
based TTS engines a base class and create derived classes for each supported
TTS. Removes the need for the implementation to know about the individual TTS
engines.

Add support for speaking directly (i.e. without going through a temporary wave
file, currently only used by espeak).

Change-Id: I59bbbd6ee4c2c009b2a8d8e0ab4a9b39ea723d6e
This commit is contained in:
Dominik Riebeling 2012-10-06 13:44:39 +02:00
parent 7f76bb48b3
commit 2c3b8bd1e2
8 changed files with 182 additions and 25 deletions

View file

@ -16,6 +16,7 @@
* *
****************************************************************************/ ****************************************************************************/
#include <QtCore>
#include "ttsbase.h" #include "ttsbase.h"
#include "ttsfestival.h" #include "ttsfestival.h"
@ -23,6 +24,9 @@
#include "ttssapi4.h" #include "ttssapi4.h"
#include "ttsmssp.h" #include "ttsmssp.h"
#include "ttsexes.h" #include "ttsexes.h"
#include "ttsespeak.h"
#include "ttsflite.h"
#include "ttsswift.h"
#if defined(Q_OS_MACX) #if defined(Q_OS_MACX)
#include "ttscarbon.h" #include "ttscarbon.h"
#endif #endif
@ -32,7 +36,6 @@ QMap<QString,QString> TTSBase::ttsList;
TTSBase::TTSBase(QObject* parent): EncTtsSettingInterface(parent) TTSBase::TTSBase(QObject* parent): EncTtsSettingInterface(parent)
{ {
} }
// static functions // static functions
@ -80,9 +83,15 @@ TTSBase* TTSBase::getTTS(QObject* parent,QString ttsName)
tts = new TTSCarbon(parent); tts = new TTSCarbon(parent);
else else
#endif #endif
// fix for OS other than WIN or LINUX if(ttsName == "espeak")
if (true) tts = new TTSEspeak(parent);
tts = new TTSExes(ttsName, parent); else if(ttsName == "flite")
tts = new TTSFlite(parent);
else if(ttsName == "swift")
tts = new TTSSwift(parent);
else if(ttsName == "user")
tts = new TTSExes(parent);
return tts; return tts;
} }

View file

@ -39,7 +39,7 @@ class TTSCarbon : public TTSBase
TTSCarbon(QObject *parent = NULL); TTSCarbon(QObject *parent = NULL);
//! Child class should generate a clip //! Child class should generate a clip
TTSStatus voice(QString text,QString wavfile, QString* errStr); TTSStatus voice(QString text, QString wavfile, QString* errStr);
//! Child class should do startup //! Child class should do startup
bool start(QString *errStr); bool start(QString *errStr);
//! child class should stop //! child class should stop
@ -51,7 +51,7 @@ class TTSCarbon : public TTSBase
bool configOk(); bool configOk();
//! Child class should generate and insertSetting(..) its settings //! Child class should generate and insertSetting(..) its settings
void generateSettings(); void generateSettings();
//! Chlid class should commit the Settings to permanent storage //! Child class should commit the Settings to permanent storage
void saveSettings(); void saveSettings();
Capabilities capabilities(); Capabilities capabilities();

View file

@ -0,0 +1,42 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2012 by Dominik Riebeling
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef TTSESPEAK_H
#define TTSESPEAK_H
#include <QtCore>
#include "ttsexes.h"
class TTSEspeak : public TTSExes
{
Q_OBJECT
public:
TTSEspeak(QObject* parent=NULL) : TTSExes(parent)
{
m_name = "espeak";
/* default to espeak */
m_TTSTemplate = "\"%exe\" %options -w \"%wavfile\" -- \"%text\"";
m_TTSSpeakTemplate = "\"%exe\" %options -- \"%text\"";
m_capabilities = TTSBase::CanSpeak;
}
};
#endif

View file

@ -16,23 +16,24 @@
* *
****************************************************************************/ ****************************************************************************/
#include <QtCore>
#include "ttsexes.h" #include "ttsexes.h"
#include "utils.h" #include "utils.h"
#include "rbsettings.h" #include "rbsettings.h"
TTSExes::TTSExes(QString name,QObject* parent) : TTSBase(parent) TTSExes::TTSExes(QObject* parent) : TTSBase(parent)
{ {
m_name = name; /* default to espeak */
m_name = "espeak";
m_TemplateMap["espeak"] = "\"%exe\" %options -w \"%wavfile\" -- \"%text\""; m_capabilities = TTSBase::CanSpeak;
m_TemplateMap["flite"] = "\"%exe\" %options -o \"%wavfile\" -t \"%text\""; m_TTSTemplate = "\"%exe\" %options -w \"%wavfile\" -- \"%text\"";
m_TemplateMap["swift"] = "\"%exe\" %options -o \"%wavfile\" -- \"%text\""; m_TTSSpeakTemplate = "\"%exe\" %options -- \"%text\"";
} }
TTSBase::Capabilities TTSExes::capabilities() TTSBase::Capabilities TTSExes::capabilities()
{ {
return RunInParallel; return m_capabilities;
} }
void TTSExes::generateSettings() void TTSExes::generateSettings()
@ -46,9 +47,9 @@ void TTSExes::generateSettings()
void TTSExes::saveSettings() void TTSExes::saveSettings()
{ {
RbSettings::setSubValue(m_name,RbSettings::TtsPath, RbSettings::setSubValue(m_name, RbSettings::TtsPath,
getSetting(eEXEPATH)->current().toString()); getSetting(eEXEPATH)->current().toString());
RbSettings::setSubValue(m_name,RbSettings::TtsOptions, RbSettings::setSubValue(m_name, RbSettings::TtsOptions,
getSetting(eOPTIONS)->current().toString()); getSetting(eOPTIONS)->current().toString());
RbSettings::sync(); RbSettings::sync();
} }
@ -56,16 +57,15 @@ void TTSExes::saveSettings()
void TTSExes::loadSettings(void) void TTSExes::loadSettings(void)
{ {
m_TTSexec = RbSettings::subValue(m_name,RbSettings::TtsPath).toString(); m_TTSexec = RbSettings::subValue(m_name, RbSettings::TtsPath).toString();
if(m_TTSexec.isEmpty()) m_TTSexec = Utils::findExecutable(m_name); if(m_TTSexec.isEmpty()) m_TTSexec = Utils::findExecutable(m_name);
m_TTSOpts = RbSettings::subValue(m_name,RbSettings::TtsOptions).toString(); m_TTSOpts = RbSettings::subValue(m_name, RbSettings::TtsOptions).toString();
} }
bool TTSExes::start(QString *errStr) bool TTSExes::start(QString *errStr)
{ {
loadSettings(); loadSettings();
m_TTSTemplate = m_TemplateMap.value(m_name);
QFileInfo tts(m_TTSexec); QFileInfo tts(m_TTSexec);
if(tts.exists()) if(tts.exists())
@ -79,10 +79,26 @@ bool TTSExes::start(QString *errStr)
} }
} }
TTSStatus TTSExes::voice(QString text,QString wavfile, QString *errStr) TTSStatus TTSExes::voice(QString text, QString wavfile, QString *errStr)
{ {
(void) errStr; (void) errStr;
QString execstring = m_TTSTemplate; QString execstring;
if(wavfile.isEmpty() && m_capabilities & TTSBase::CanSpeak) {
if(m_TTSSpeakTemplate.isEmpty()) {
qDebug() << "[TTSExes] internal error: TTS announces CanSpeak "
"but template empty!";
return FatalError;
}
execstring = m_TTSSpeakTemplate;
}
else if(wavfile.isEmpty()) {
qDebug() << "[TTSExes] no output file passed to voice() "
"but TTS can't speak directly.";
return FatalError;
}
else {
execstring = m_TTSTemplate;
}
execstring.replace("%exe",m_TTSexec); execstring.replace("%exe",m_TTSexec);
execstring.replace("%options",m_TTSOpts); execstring.replace("%options",m_TTSOpts);
@ -91,7 +107,7 @@ TTSStatus TTSExes::voice(QString text,QString wavfile, QString *errStr)
QProcess::execute(execstring); QProcess::execute(execstring);
if(!QFileInfo(wavfile).isFile()) { if(!wavfile.isEmpty() && !QFileInfo(wavfile).isFile()) {
qDebug() << "[TTSExes] output file does not exist:" << wavfile; qDebug() << "[TTSExes] output file does not exist:" << wavfile;
return FatalError; return FatalError;
} }

View file

@ -21,6 +21,7 @@
#ifndef TTSEXES_H #ifndef TTSEXES_H
#define TTSEXES_H #define TTSEXES_H
#include <QtCore>
#include "ttsbase.h" #include "ttsbase.h"
class TTSExes : public TTSBase class TTSExes : public TTSBase
@ -33,7 +34,7 @@ class TTSExes : public TTSBase
Q_OBJECT Q_OBJECT
public: public:
TTSExes(QString name,QObject* parent=NULL); TTSExes(QObject* parent=NULL);
TTSStatus voice(QString text, QString wavfile, QString *errStr); TTSStatus voice(QString text, QString wavfile, QString *errStr);
bool start(QString *errStr); bool start(QString *errStr);
bool stop() {return true;} bool stop() {return true;}
@ -47,11 +48,14 @@ class TTSExes : public TTSBase
private: private:
void loadSettings(void); void loadSettings(void);
protected:
QString m_TTSTemplate;
QString m_TTSSpeakTemplate;
QString m_name; QString m_name;
QString m_TTSexec; QString m_TTSexec;
QString m_TTSOpts; QString m_TTSOpts;
QString m_TTSTemplate; TTSBase::Capabilities m_capabilities;
QMap<QString,QString> m_TemplateMap;
}; };
#endif #endif

View file

@ -0,0 +1,43 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2012 by Dominik Riebeling
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef TTSFLITE_H
#define TTSFLITE_H
#include <QtCore>
#include "ttsexes.h"
class TTSFlite : public TTSExes
{
Q_OBJECT
public:
TTSFlite(QObject* parent=NULL) : TTSExes(parent)
{
m_name = "flite";
/* default to espeak */
m_TTSTemplate = "\"%exe\" %options -o \"%wavfile\" -t \"%text\"";
m_TTSSpeakTemplate = "";
m_capabilities = TTSBase::None;
}
};
#endif

View file

@ -0,0 +1,40 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2012 by Dominik Riebeling
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef TTSSWIFT_H
#define TTSSWIFT_H
#include <QtCore>
#include "ttsexes.h"
class TTSSwift : public TTSExes
{
Q_OBJECT
public:
TTSSwift(QObject* parent=NULL) : TTSExes(parent)
{
m_name = "swift";
m_TTSTemplate = "\"%exe\" %options -o \"%wavfile\" -- \"%text\"";
m_TTSSpeakTemplate = "";
m_capabilities = TTSBase::None;
}
};
#endif

View file

@ -111,7 +111,10 @@ HEADERS += \
base/encttssettings.h \ base/encttssettings.h \
base/ttsbase.h \ base/ttsbase.h \
base/ttsexes.h \ base/ttsexes.h \
base/ttsespeak.h \
base/ttsflite.h \
base/ttsfestival.h \ base/ttsfestival.h \
base/ttsswift.h \
base/ttssapi.h \ base/ttssapi.h \
base/ttssapi4.h \ base/ttssapi4.h \
base/ttsmssp.h \ base/ttsmssp.h \