2007-12-15 13:13:57 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 by Dominik Wenger
|
2008-06-30 22:09:45 +00:00
|
|
|
* $Id$
|
2007-12-15 13:13:57 +00:00
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "tts.h"
|
|
|
|
|
2008-01-29 21:54:43 +00:00
|
|
|
|
2008-02-12 21:45:50 +00:00
|
|
|
// static variables
|
|
|
|
QMap<QString,QString> TTSBase::ttsList;
|
|
|
|
QMap<QString,TTSBase*> TTSBase::ttsCache;
|
2007-12-15 13:13:57 +00:00
|
|
|
|
2008-02-12 21:45:50 +00:00
|
|
|
// static functions
|
|
|
|
void TTSBase::initTTSList()
|
2007-12-15 13:13:57 +00:00
|
|
|
{
|
|
|
|
ttsList["espeak"] = "Espeak TTS Engine";
|
|
|
|
ttsList["flite"] = "Flite TTS Engine";
|
|
|
|
ttsList["swift"] = "Swift TTS Engine";
|
|
|
|
#if defined(Q_OS_WIN)
|
2008-02-11 17:39:07 +00:00
|
|
|
ttsList["sapi"] = "Sapi TTS Engine";
|
2007-12-15 13:13:57 +00:00
|
|
|
#endif
|
2008-02-12 21:45:50 +00:00
|
|
|
|
2007-12-15 13:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// function to get a specific encoder
|
2008-02-12 21:45:50 +00:00
|
|
|
TTSBase* TTSBase::getTTS(QString ttsName)
|
2007-12-15 13:13:57 +00:00
|
|
|
{
|
|
|
|
// check cache
|
|
|
|
if(ttsCache.contains(ttsName))
|
|
|
|
return ttsCache.value(ttsName);
|
2008-02-06 21:51:35 +00:00
|
|
|
|
|
|
|
TTSBase* tts;
|
2007-12-15 13:13:57 +00:00
|
|
|
if(ttsName == "sapi")
|
|
|
|
{
|
|
|
|
tts = new TTSSapi();
|
|
|
|
ttsCache[ttsName] = tts;
|
|
|
|
return tts;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tts = new TTSExes(ttsName);
|
2008-02-12 21:45:50 +00:00
|
|
|
ttsCache[ttsName] = tts;
|
2007-12-15 13:13:57 +00:00
|
|
|
return tts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the list of encoders, nice names
|
2008-02-12 21:45:50 +00:00
|
|
|
QStringList TTSBase::getTTSList()
|
2007-12-15 13:13:57 +00:00
|
|
|
{
|
|
|
|
// init list if its empty
|
2008-02-06 21:51:35 +00:00
|
|
|
if(ttsList.count() == 0)
|
|
|
|
initTTSList();
|
2007-12-15 13:13:57 +00:00
|
|
|
|
2008-02-06 21:51:35 +00:00
|
|
|
return ttsList.keys();
|
2007-12-15 13:13:57 +00:00
|
|
|
}
|
|
|
|
|
2008-02-12 21:45:50 +00:00
|
|
|
// get nice name of a specific tts
|
|
|
|
QString TTSBase::getTTSName(QString tts)
|
2008-02-06 21:51:35 +00:00
|
|
|
{
|
|
|
|
if(ttsList.isEmpty())
|
|
|
|
initTTSList();
|
|
|
|
return ttsList.value(tts);
|
|
|
|
}
|
2007-12-15 13:13:57 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* TTS Base
|
|
|
|
**********************************************************************/
|
2008-01-29 21:54:43 +00:00
|
|
|
TTSBase::TTSBase(): QObject()
|
2007-12-15 13:13:57 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* General TTS Exes
|
|
|
|
**********************************************************************/
|
2008-01-29 21:54:43 +00:00
|
|
|
TTSExes::TTSExes(QString name) : TTSBase()
|
2007-12-15 13:13:57 +00:00
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
|
2008-08-03 20:20:28 +00:00
|
|
|
m_TemplateMap["espeak"] = "\"%exe\" %options -w \"%wavfile\" \"%text\"";
|
|
|
|
m_TemplateMap["flite"] = "\"%exe\" %options -o \"%wavfile\" \"%text\"";
|
|
|
|
m_TemplateMap["swift"] = "\"%exe\" %options -o \"%wavfile\" \"%text\"";
|
2007-12-15 13:13:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-08-02 16:40:16 +00:00
|
|
|
void TTSExes::setCfg(RbSettings* sett)
|
|
|
|
{
|
|
|
|
// call function of base class
|
|
|
|
TTSBase::setCfg(sett);
|
|
|
|
|
|
|
|
// if the config isnt OK, try to autodetect
|
|
|
|
if(!configOk())
|
|
|
|
{
|
|
|
|
QString exepath;
|
|
|
|
//try autodetect tts
|
|
|
|
#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
|
|
|
|
QStringList path = QString(getenv("PATH")).split(":", QString::SkipEmptyParts);
|
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
QStringList path = QString(getenv("PATH")).split(";", QString::SkipEmptyParts);
|
|
|
|
#endif
|
|
|
|
qDebug() << path;
|
|
|
|
for(int i = 0; i < path.size(); i++)
|
|
|
|
{
|
|
|
|
QString executable = QDir::fromNativeSeparators(path.at(i)) + "/" + m_name;
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
executable += ".exe";
|
|
|
|
QStringList ex = executable.split("\"", QString::SkipEmptyParts);
|
|
|
|
executable = ex.join("");
|
|
|
|
#endif
|
|
|
|
qDebug() << executable;
|
|
|
|
if(QFileInfo(executable).isExecutable())
|
|
|
|
{
|
|
|
|
exepath= QDir::toNativeSeparators(executable);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings->setTTSPath(m_name,exepath);
|
|
|
|
settings->sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-01-19 18:33:33 +00:00
|
|
|
bool TTSExes::start(QString *errStr)
|
2007-12-15 13:13:57 +00:00
|
|
|
{
|
2008-01-25 00:12:25 +00:00
|
|
|
m_TTSexec = settings->ttsPath(m_name);
|
|
|
|
m_TTSOpts = settings->ttsOptions(m_name);
|
2007-12-15 13:13:57 +00:00
|
|
|
|
|
|
|
m_TTSTemplate = m_TemplateMap.value(m_name);
|
|
|
|
|
|
|
|
QFileInfo tts(m_TTSexec);
|
|
|
|
if(tts.exists())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-19 18:33:33 +00:00
|
|
|
*errStr = tr("TTS executable not found");
|
2007-12-15 13:13:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTSExes::voice(QString text,QString wavfile)
|
|
|
|
{
|
|
|
|
QString execstring = m_TTSTemplate;
|
|
|
|
|
|
|
|
execstring.replace("%exe",m_TTSexec);
|
|
|
|
execstring.replace("%options",m_TTSOpts);
|
|
|
|
execstring.replace("%wavfile",wavfile);
|
|
|
|
execstring.replace("%text",text);
|
|
|
|
//qDebug() << "voicing" << execstring;
|
|
|
|
QProcess::execute(execstring);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTSExes::showCfg()
|
|
|
|
{
|
2008-02-09 18:58:14 +00:00
|
|
|
#ifndef CONSOLE
|
2008-01-29 21:54:43 +00:00
|
|
|
TTSExesGui gui;
|
2008-02-09 18:58:14 +00:00
|
|
|
#else
|
|
|
|
TTSExesGuiCli gui;
|
|
|
|
#endif
|
2008-01-29 21:54:43 +00:00
|
|
|
gui.setCfg(settings);
|
|
|
|
gui.showCfg(m_name);
|
2007-12-15 13:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TTSExes::configOk()
|
|
|
|
{
|
2008-01-25 00:12:25 +00:00
|
|
|
QString path = settings->ttsPath(m_name);
|
2007-12-15 13:13:57 +00:00
|
|
|
|
|
|
|
if (QFileInfo(path).exists())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* TTS Sapi
|
|
|
|
**********************************************************************/
|
2008-01-29 21:54:43 +00:00
|
|
|
TTSSapi::TTSSapi() : TTSBase()
|
2007-12-15 13:13:57 +00:00
|
|
|
{
|
2008-01-19 18:33:33 +00:00
|
|
|
m_TTSTemplate = "cscript //nologo \"%exe\" /language:%lang /voice:\"%voice\" /speed:%speed \"%options\"";
|
2007-12-15 13:13:57 +00:00
|
|
|
defaultLanguage ="english";
|
2008-02-11 17:39:07 +00:00
|
|
|
m_sapi4 =false;
|
2007-12-15 13:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-19 18:33:33 +00:00
|
|
|
bool TTSSapi::start(QString *errStr)
|
2007-12-15 13:13:57 +00:00
|
|
|
{
|
|
|
|
|
2008-01-25 00:12:25 +00:00
|
|
|
m_TTSOpts = settings->ttsOptions("sapi");
|
|
|
|
m_TTSLanguage =settings->ttsLang("sapi");
|
|
|
|
m_TTSVoice=settings->ttsVoice("sapi");
|
2008-02-11 17:39:07 +00:00
|
|
|
m_TTSSpeed=QString("%1").arg(settings->ttsSpeed("sapi"));
|
|
|
|
m_sapi4 = settings->ttsUseSapi4();
|
|
|
|
|
2008-01-19 18:33:33 +00:00
|
|
|
QFile::remove(QDir::tempPath() +"/sapi_voice.vbs");
|
2007-12-17 21:35:25 +00:00
|
|
|
QFile::copy(":/builtin/sapi_voice.vbs",QDir::tempPath() + "/sapi_voice.vbs");
|
2007-12-15 13:48:42 +00:00
|
|
|
m_TTSexec = QDir::tempPath() +"/sapi_voice.vbs";
|
|
|
|
|
2007-12-15 13:13:57 +00:00
|
|
|
QFileInfo tts(m_TTSexec);
|
|
|
|
if(!tts.exists())
|
2008-01-19 18:33:33 +00:00
|
|
|
{
|
|
|
|
*errStr = tr("Could not copy the Sapi-script");
|
2007-12-15 13:13:57 +00:00
|
|
|
return false;
|
2008-01-19 18:33:33 +00:00
|
|
|
}
|
2007-12-15 13:13:57 +00:00
|
|
|
// create the voice process
|
|
|
|
QString execstring = m_TTSTemplate;
|
|
|
|
execstring.replace("%exe",m_TTSexec);
|
|
|
|
execstring.replace("%options",m_TTSOpts);
|
|
|
|
execstring.replace("%lang",m_TTSLanguage);
|
2008-01-19 18:33:33 +00:00
|
|
|
execstring.replace("%voice",m_TTSVoice);
|
|
|
|
execstring.replace("%speed",m_TTSSpeed);
|
|
|
|
|
2008-02-11 17:39:07 +00:00
|
|
|
if(m_sapi4)
|
|
|
|
execstring.append(" /sapi4 ");
|
|
|
|
|
2007-12-15 13:13:57 +00:00
|
|
|
qDebug() << "init" << execstring;
|
|
|
|
voicescript = new QProcess(NULL);
|
2008-01-19 18:33:33 +00:00
|
|
|
//connect(voicescript,SIGNAL(readyReadStandardError()),this,SLOT(error()));
|
|
|
|
|
2007-12-15 13:13:57 +00:00
|
|
|
voicescript->start(execstring);
|
|
|
|
if(!voicescript->waitForStarted())
|
2008-01-19 18:33:33 +00:00
|
|
|
{
|
|
|
|
*errStr = tr("Could not start the Sapi-script");
|
2007-12-15 13:13:57 +00:00
|
|
|
return false;
|
2008-01-19 18:33:33 +00:00
|
|
|
}
|
|
|
|
|
2008-05-10 15:23:15 +00:00
|
|
|
if(!voicescript->waitForReadyRead(300))
|
2008-01-19 18:33:33 +00:00
|
|
|
{
|
|
|
|
*errStr = voicescript->readAllStandardError();
|
|
|
|
if(*errStr != "")
|
|
|
|
return false;
|
|
|
|
}
|
2007-12-15 13:13:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-19 18:33:33 +00:00
|
|
|
|
|
|
|
QStringList TTSSapi::getVoiceList(QString language)
|
|
|
|
{
|
|
|
|
QStringList result;
|
|
|
|
|
|
|
|
QFile::copy(":/builtin/sapi_voice.vbs",QDir::tempPath() + "/sapi_voice.vbs");
|
|
|
|
m_TTSexec = QDir::tempPath() +"/sapi_voice.vbs";
|
|
|
|
|
|
|
|
QFileInfo tts(m_TTSexec);
|
|
|
|
if(!tts.exists())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// create the voice process
|
2008-02-11 17:39:07 +00:00
|
|
|
QString execstring = "cscript //nologo \"%exe\" /language:%lang /listvoices";
|
2008-01-19 18:33:33 +00:00
|
|
|
execstring.replace("%exe",m_TTSexec);
|
|
|
|
execstring.replace("%lang",language);
|
2008-02-11 17:39:07 +00:00
|
|
|
|
|
|
|
if(settings->ttsUseSapi4())
|
|
|
|
execstring.append(" /sapi4 ");
|
|
|
|
|
2008-01-19 18:33:33 +00:00
|
|
|
qDebug() << "init" << execstring;
|
|
|
|
voicescript = new QProcess(NULL);
|
|
|
|
voicescript->start(execstring);
|
|
|
|
if(!voicescript->waitForStarted())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
voicescript->waitForReadyRead();
|
|
|
|
|
|
|
|
QString dataRaw = voicescript->readAllStandardError().data();
|
|
|
|
result = dataRaw.split(",",QString::SkipEmptyParts);
|
|
|
|
result.sort();
|
|
|
|
result.removeFirst();
|
2008-02-11 17:39:07 +00:00
|
|
|
for(int i = 0; i< result.size();i++)
|
|
|
|
{
|
|
|
|
result[i] = result.at(i).simplified();
|
|
|
|
}
|
|
|
|
|
2008-01-19 18:33:33 +00:00
|
|
|
|
|
|
|
delete voicescript;
|
|
|
|
QFile::setPermissions(QDir::tempPath() +"/sapi_voice.vbs",QFile::ReadOwner |QFile::WriteOwner|QFile::ExeOwner
|
|
|
|
|QFile::ReadUser| QFile::WriteUser| QFile::ExeUser
|
|
|
|
|QFile::ReadGroup |QFile::WriteGroup |QFile::ExeGroup
|
|
|
|
|QFile::ReadOther |QFile::WriteOther |QFile::ExeOther );
|
|
|
|
QFile::remove(QDir::tempPath() +"/sapi_voice.vbs");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-15 13:13:57 +00:00
|
|
|
bool TTSSapi::voice(QString text,QString wavfile)
|
|
|
|
{
|
|
|
|
QString query = "SPEAK\t"+wavfile+"\t"+text+"\r\n";
|
|
|
|
qDebug() << "voicing" << query;
|
2008-02-25 22:02:55 +00:00
|
|
|
voicescript->write(query.toLocal8Bit());
|
2007-12-15 13:13:57 +00:00
|
|
|
voicescript->write("SYNC\tbla\r\n");
|
|
|
|
voicescript->waitForReadyRead();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTSSapi::stop()
|
|
|
|
{
|
|
|
|
QString query = "QUIT\r\n";
|
2008-02-25 22:02:55 +00:00
|
|
|
voicescript->write(query.toLocal8Bit());
|
2007-12-15 13:13:57 +00:00
|
|
|
voicescript->waitForFinished();
|
|
|
|
delete voicescript;
|
2008-01-19 18:33:33 +00:00
|
|
|
QFile::setPermissions(QDir::tempPath() +"/sapi_voice.vbs",QFile::ReadOwner |QFile::WriteOwner|QFile::ExeOwner
|
|
|
|
|QFile::ReadUser| QFile::WriteUser| QFile::ExeUser
|
|
|
|
|QFile::ReadGroup |QFile::WriteGroup |QFile::ExeGroup
|
|
|
|
|QFile::ReadOther |QFile::WriteOther |QFile::ExeOther );
|
|
|
|
QFile::remove(QDir::tempPath() +"/sapi_voice.vbs");
|
2007-12-15 13:13:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TTSSapi::showCfg()
|
|
|
|
{
|
2008-02-09 18:58:14 +00:00
|
|
|
#ifndef CONSOLE
|
2008-01-29 21:54:43 +00:00
|
|
|
TTSSapiGui gui(this);
|
2008-02-09 18:58:14 +00:00
|
|
|
#else
|
|
|
|
TTSSapiGuiCli gui(this);
|
|
|
|
#endif
|
2008-01-29 21:54:43 +00:00
|
|
|
gui.setCfg(settings);
|
|
|
|
gui.showCfg();
|
2007-12-15 13:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TTSSapi::configOk()
|
|
|
|
{
|
2008-02-13 19:16:46 +00:00
|
|
|
if(settings->ttsVoice("sapi").isEmpty())
|
|
|
|
return false;
|
2007-12-15 13:48:42 +00:00
|
|
|
return true;
|
2007-12-15 13:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|