2009-10-13 19:54:27 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 by Dominik Wenger
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-06-12 18:09:45 +00:00
|
|
|
#include <QtCore>
|
|
|
|
#include <QTcpSocket>
|
|
|
|
|
2009-10-13 19:54:27 +00:00
|
|
|
#include "ttsfestival.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "rbsettings.h"
|
2013-11-03 10:08:18 +00:00
|
|
|
#include "Logger.h"
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
TTSFestival::~TTSFestival()
|
|
|
|
{
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Destroying instance";
|
2009-10-13 19:54:27 +00:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2010-06-04 21:22:25 +00:00
|
|
|
TTSBase::Capabilities TTSFestival::capabilities()
|
|
|
|
{
|
|
|
|
return RunInParallel;
|
|
|
|
}
|
|
|
|
|
2009-10-13 19:54:27 +00:00
|
|
|
void TTSFestival::generateSettings()
|
|
|
|
{
|
|
|
|
// server path
|
2010-03-03 22:26:00 +00:00
|
|
|
QString exepath = RbSettings::subValue("festival-server",
|
|
|
|
RbSettings::TtsPath).toString();
|
2010-04-02 21:24:19 +00:00
|
|
|
if(exepath == "" ) exepath = Utils::findExecutable("festival");
|
2010-03-03 22:26:00 +00:00
|
|
|
insertSetting(eSERVERPATH,new EncTtsSetting(this,
|
|
|
|
EncTtsSetting::eSTRING, "Path to Festival server:",
|
|
|
|
exepath,EncTtsSetting::eBROWSEBTN));
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
// client path
|
2010-03-03 22:26:00 +00:00
|
|
|
QString clientpath = RbSettings::subValue("festival-client",
|
|
|
|
RbSettings::TtsPath).toString();
|
2010-04-02 21:24:19 +00:00
|
|
|
if(clientpath == "" ) clientpath = Utils::findExecutable("festival_client");
|
2009-10-13 19:54:27 +00:00
|
|
|
insertSetting(eCLIENTPATH,new EncTtsSetting(this,EncTtsSetting::eSTRING,
|
2010-03-03 22:26:00 +00:00
|
|
|
tr("Path to Festival client:"),
|
|
|
|
clientpath,EncTtsSetting::eBROWSEBTN));
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
// voice
|
2010-03-03 22:26:00 +00:00
|
|
|
EncTtsSetting* setting = new EncTtsSetting(this,
|
|
|
|
EncTtsSetting::eSTRINGLIST, tr("Voice:"),
|
|
|
|
RbSettings::subValue("festival", RbSettings::TtsVoice),
|
2010-03-30 17:45:23 +00:00
|
|
|
getVoiceList(), EncTtsSetting::eREFRESHBTN);
|
2009-10-13 19:54:27 +00:00
|
|
|
connect(setting,SIGNAL(refresh()),this,SLOT(updateVoiceList()));
|
|
|
|
connect(setting,SIGNAL(dataChanged()),this,SLOT(clearVoiceDescription()));
|
|
|
|
insertSetting(eVOICE,setting);
|
|
|
|
|
|
|
|
//voice description
|
|
|
|
setting = new EncTtsSetting(this,EncTtsSetting::eREADONLYSTRING,
|
|
|
|
tr("Voice description:"),"",EncTtsSetting::eREFRESHBTN);
|
|
|
|
connect(setting,SIGNAL(refresh()),this,SLOT(updateVoiceDescription()));
|
|
|
|
insertSetting(eVOICEDESC,setting);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTSFestival::saveSettings()
|
|
|
|
{
|
|
|
|
//save settings in user config
|
2010-03-03 22:26:00 +00:00
|
|
|
RbSettings::setSubValue("festival-server",
|
|
|
|
RbSettings::TtsPath,getSetting(eSERVERPATH)->current().toString());
|
|
|
|
RbSettings::setSubValue("festival-client",
|
|
|
|
RbSettings::TtsPath,getSetting(eCLIENTPATH)->current().toString());
|
|
|
|
RbSettings::setSubValue("festival",
|
|
|
|
RbSettings::TtsVoice,getSetting(eVOICE)->current().toString());
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
RbSettings::sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTSFestival::updateVoiceDescription()
|
|
|
|
{
|
|
|
|
// get voice Info with current voice and path
|
2010-03-30 17:45:23 +00:00
|
|
|
currentPath = getSetting(eSERVERPATH)->current().toString();
|
|
|
|
QString info = getVoiceInfo(getSetting(eVOICE)->current().toString());
|
|
|
|
currentPath = "";
|
2013-11-03 10:08:18 +00:00
|
|
|
|
2009-10-13 19:54:27 +00:00
|
|
|
getSetting(eVOICEDESC)->setCurrent(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTSFestival::clearVoiceDescription()
|
|
|
|
{
|
|
|
|
getSetting(eVOICEDESC)->setCurrent("");
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTSFestival::updateVoiceList()
|
|
|
|
{
|
2010-03-30 17:45:23 +00:00
|
|
|
currentPath = getSetting(eSERVERPATH)->current().toString();
|
|
|
|
QStringList voiceList = getVoiceList();
|
|
|
|
currentPath = "";
|
2013-11-03 10:08:18 +00:00
|
|
|
|
2009-10-13 19:54:27 +00:00
|
|
|
getSetting(eVOICE)->setList(voiceList);
|
|
|
|
if(voiceList.size() > 0) getSetting(eVOICE)->setCurrent(voiceList.at(0));
|
|
|
|
else getSetting(eVOICE)->setCurrent("");
|
|
|
|
}
|
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
void TTSFestival::startServer()
|
2009-10-13 19:54:27 +00:00
|
|
|
{
|
|
|
|
if(!configOk())
|
|
|
|
return;
|
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
if(serverProcess.state() != QProcess::Running)
|
|
|
|
{
|
|
|
|
QString path;
|
|
|
|
/* currentPath is set by the GUI - if it's set, it is the currently set
|
|
|
|
path in the configuration GUI; if it's not set, use the saved path */
|
|
|
|
if (currentPath == "")
|
|
|
|
path = RbSettings::subValue("festival-server",RbSettings::TtsPath).toString();
|
|
|
|
else
|
|
|
|
path = currentPath;
|
|
|
|
|
|
|
|
serverProcess.start(QString("%1 --server").arg(path));
|
|
|
|
serverProcess.waitForStarted();
|
|
|
|
|
|
|
|
/* A friendlier version of a spinlock */
|
2020-10-29 20:00:14 +00:00
|
|
|
while (serverProcess.processId() == 0 && serverProcess.state() != QProcess::Running)
|
2010-03-30 17:45:23 +00:00
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
|
|
|
|
|
|
|
|
if(serverProcess.state() == QProcess::Running)
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Server is up and running";
|
2010-03-30 17:45:23 +00:00
|
|
|
else
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_ERROR() << "Server failed to start, state:"
|
|
|
|
<< serverProcess.state();
|
2010-03-30 17:45:23 +00:00
|
|
|
}
|
2009-10-13 19:54:27 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
bool TTSFestival::ensureServerRunning()
|
2009-10-13 19:54:27 +00:00
|
|
|
{
|
|
|
|
if(serverProcess.state() != QProcess::Running)
|
|
|
|
{
|
2010-03-30 17:45:23 +00:00
|
|
|
startServer();
|
2009-10-13 19:54:27 +00:00
|
|
|
}
|
2010-03-30 17:45:23 +00:00
|
|
|
return serverProcess.state() == QProcess::Running;
|
2009-10-13 19:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TTSFestival::start(QString* errStr)
|
|
|
|
{
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Starting server with voice"
|
|
|
|
<< RbSettings::subValue("festival", RbSettings::TtsVoice).toString();
|
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
bool running = ensureServerRunning();
|
2009-10-13 19:54:27 +00:00
|
|
|
if (!RbSettings::subValue("festival",RbSettings::TtsVoice).toString().isEmpty())
|
2010-03-30 17:45:23 +00:00
|
|
|
{
|
|
|
|
/* There's no harm in using both methods to set the voice .. */
|
|
|
|
QString voiceSelect = QString("(voice.select '%1)\n")
|
|
|
|
.arg(RbSettings::subValue("festival", RbSettings::TtsVoice).toString());
|
|
|
|
queryServer(voiceSelect, 3000);
|
2013-11-03 10:08:18 +00:00
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
if(prologFile.open())
|
|
|
|
{
|
2013-01-27 10:57:46 +00:00
|
|
|
prologFile.write(voiceSelect.toLatin1());
|
2010-03-30 17:45:23 +00:00
|
|
|
prologFile.close();
|
|
|
|
prologPath = QFileInfo(prologFile).absoluteFilePath();
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Prolog created at" << prologPath;
|
2010-03-30 17:45:23 +00:00
|
|
|
}
|
2013-11-03 10:08:18 +00:00
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
}
|
2013-11-03 10:08:18 +00:00
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
if (!running)
|
|
|
|
(*errStr) = "Festival could not be started";
|
|
|
|
return running;
|
2009-10-13 19:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TTSFestival::stop()
|
|
|
|
{
|
|
|
|
serverProcess.terminate();
|
|
|
|
serverProcess.kill();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TTSStatus TTSFestival::voice(QString text, QString wavfile, QString* errStr)
|
|
|
|
{
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Voicing" << text << "->" << wavfile;
|
2009-10-13 19:54:27 +00:00
|
|
|
|
2010-03-03 22:26:00 +00:00
|
|
|
QString path = RbSettings::subValue("festival-client",
|
|
|
|
RbSettings::TtsPath).toString();
|
|
|
|
QString cmd = QString("%1 --server localhost --otype riff --ttw --withlisp"
|
2010-03-30 17:45:23 +00:00
|
|
|
" --output \"%2\" --prolog \"%3\" - ").arg(path).arg(wavfile).arg(prologPath);
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Client cmd:" << cmd;
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
QProcess clientProcess;
|
|
|
|
clientProcess.start(cmd);
|
2013-01-27 10:57:46 +00:00
|
|
|
clientProcess.write(QString("%1.\n").arg(text).toLatin1());
|
2009-10-13 19:54:27 +00:00
|
|
|
clientProcess.waitForBytesWritten();
|
|
|
|
clientProcess.closeWriteChannel();
|
|
|
|
clientProcess.waitForReadyRead();
|
|
|
|
QString response = clientProcess.readAll();
|
|
|
|
response = response.trimmed();
|
|
|
|
if(!response.contains("Utterance"))
|
|
|
|
{
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_WARNING() << "Could not voice string: " << response;
|
2009-10-13 19:54:27 +00:00
|
|
|
*errStr = tr("engine could not voice string");
|
|
|
|
return Warning;
|
|
|
|
/* do not stop the voicing process because of a single string
|
|
|
|
TODO: needs proper settings */
|
|
|
|
}
|
|
|
|
clientProcess.closeReadChannel(QProcess::StandardError);
|
|
|
|
clientProcess.closeReadChannel(QProcess::StandardOutput);
|
|
|
|
clientProcess.terminate();
|
|
|
|
clientProcess.kill();
|
|
|
|
|
|
|
|
return NoError;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTSFestival::configOk()
|
|
|
|
{
|
2010-03-30 17:45:23 +00:00
|
|
|
bool ret;
|
|
|
|
if (currentPath == "")
|
|
|
|
{
|
|
|
|
QString serverPath = RbSettings::subValue("festival-server",
|
|
|
|
RbSettings::TtsPath).toString();
|
|
|
|
QString clientPath = RbSettings::subValue("festival-client",
|
|
|
|
RbSettings::TtsPath).toString();
|
|
|
|
|
|
|
|
ret = QFileInfo(serverPath).isExecutable() &&
|
|
|
|
QFileInfo(clientPath).isExecutable();
|
|
|
|
if(RbSettings::subValue("festival",RbSettings::TtsVoice).toString().size() > 0
|
|
|
|
&& voices.size() > 0)
|
|
|
|
ret = ret && (voices.indexOf(RbSettings::subValue("festival",
|
|
|
|
RbSettings::TtsVoice).toString()) != -1);
|
|
|
|
}
|
2013-11-03 10:08:18 +00:00
|
|
|
else /* If we're currently configuring the server, we need to know that
|
2010-03-30 17:45:23 +00:00
|
|
|
the entered path is valid */
|
|
|
|
ret = QFileInfo(currentPath).isExecutable();
|
2013-11-03 10:08:18 +00:00
|
|
|
|
2009-10-13 19:54:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
QStringList TTSFestival::getVoiceList()
|
2009-10-13 19:54:27 +00:00
|
|
|
{
|
|
|
|
if(!configOk())
|
|
|
|
return QStringList();
|
|
|
|
|
|
|
|
if(voices.size() > 0)
|
|
|
|
{
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Using voice cache";
|
2009-10-13 19:54:27 +00:00
|
|
|
return voices;
|
|
|
|
}
|
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
QString response = queryServer("(voice.list)", 10000);
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
// get the 2nd line. It should be (<voice_name>, <voice_name>)
|
|
|
|
response = response.mid(response.indexOf('\n') + 1, -1);
|
|
|
|
response = response.left(response.indexOf('\n')).trimmed();
|
|
|
|
|
|
|
|
voices = response.mid(1, response.size()-2).split(' ');
|
|
|
|
|
|
|
|
voices.sort();
|
|
|
|
if (voices.size() == 1 && voices[0].size() == 0)
|
|
|
|
voices.removeAt(0);
|
|
|
|
if (voices.size() > 0)
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Voices:" << voices;
|
2009-10-13 19:54:27 +00:00
|
|
|
else
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_WARNING() << "No voices. Response was:" << response;
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
return voices;
|
|
|
|
}
|
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
QString TTSFestival::getVoiceInfo(QString voice)
|
2009-10-13 19:54:27 +00:00
|
|
|
{
|
|
|
|
if(!configOk())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
if(!getVoiceList().contains(voice))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
if(voiceDescriptions.contains(voice))
|
|
|
|
return voiceDescriptions[voice];
|
|
|
|
|
2010-03-03 22:26:00 +00:00
|
|
|
QString response = queryServer(QString("(voice.description '%1)").arg(voice),
|
2010-03-30 17:45:23 +00:00
|
|
|
10000);
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
if (response == "")
|
|
|
|
{
|
|
|
|
voiceDescriptions[voice]=tr("No description available");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-03 22:26:00 +00:00
|
|
|
response = response.remove(QRegExp("(description \"*\")",
|
|
|
|
Qt::CaseInsensitive, QRegExp::Wildcard));
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "voiceInfo w/o descr:" << response;
|
2009-10-13 19:54:27 +00:00
|
|
|
response = response.remove(')');
|
2020-07-27 20:31:26 +00:00
|
|
|
#if QT_VERSION >= 0x050e00
|
|
|
|
QStringList responseLines = response.split('(', Qt::SkipEmptyParts);
|
|
|
|
#else
|
2009-10-13 19:54:27 +00:00
|
|
|
QStringList responseLines = response.split('(', QString::SkipEmptyParts);
|
2020-07-27 20:31:26 +00:00
|
|
|
#endif
|
2009-10-13 19:54:27 +00:00
|
|
|
responseLines.removeAt(0); // the voice name itself
|
|
|
|
|
|
|
|
QString description;
|
|
|
|
foreach(QString line, responseLines)
|
|
|
|
{
|
|
|
|
line = line.remove('(');
|
|
|
|
line = line.simplified();
|
|
|
|
|
|
|
|
line[0] = line[0].toUpper(); // capitalize the key
|
|
|
|
|
|
|
|
int firstSpace = line.indexOf(' ');
|
|
|
|
if (firstSpace > 0)
|
|
|
|
{
|
2010-03-03 22:26:00 +00:00
|
|
|
// add a colon between the key and the value
|
|
|
|
line = line.insert(firstSpace, ':');
|
|
|
|
// capitalize the value
|
|
|
|
line[firstSpace+2] = line[firstSpace+2].toUpper();
|
2009-10-13 19:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
description += line + "\n";
|
|
|
|
}
|
|
|
|
voiceDescriptions[voice] = description.trimmed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return voiceDescriptions[voice];
|
|
|
|
}
|
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
QString TTSFestival::queryServer(QString query, int timeout)
|
2009-10-13 19:54:27 +00:00
|
|
|
{
|
|
|
|
if(!configOk())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// this operation could take some time
|
|
|
|
emit busy();
|
2013-11-03 10:08:18 +00:00
|
|
|
|
|
|
|
LOG_INFO() << "queryServer with" << query;
|
2009-10-13 19:54:27 +00:00
|
|
|
|
2010-03-30 17:45:23 +00:00
|
|
|
if (!ensureServerRunning())
|
|
|
|
{
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_ERROR() << "queryServer: ensureServerRunning failed";
|
2010-03-30 17:45:23 +00:00
|
|
|
emit busyEnd();
|
|
|
|
return "";
|
|
|
|
}
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
QString response;
|
|
|
|
|
|
|
|
QDateTime endTime;
|
|
|
|
if(timeout > 0)
|
|
|
|
endTime = QDateTime::currentDateTime().addMSecs(timeout);
|
|
|
|
|
|
|
|
/* Festival is *extremely* unreliable. Although at this
|
|
|
|
* point we are sure that SIOD is accepting commands,
|
|
|
|
* we might end up with an empty response. Hence, the loop.
|
|
|
|
*/
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
|
|
|
|
QTcpSocket socket;
|
|
|
|
|
|
|
|
socket.connectToHost("localhost", 1314);
|
|
|
|
socket.waitForConnected();
|
|
|
|
|
|
|
|
if(socket.state() == QAbstractSocket::ConnectedState)
|
|
|
|
{
|
2013-01-27 10:57:46 +00:00
|
|
|
socket.write(QString("%1\n").arg(query).toLatin1());
|
2009-10-13 19:54:27 +00:00
|
|
|
socket.waitForBytesWritten();
|
|
|
|
socket.waitForReadyRead();
|
|
|
|
|
|
|
|
response = socket.readAll().trimmed();
|
|
|
|
|
|
|
|
if (response != "LP" && response != "")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
socket.abort();
|
|
|
|
socket.disconnectFromHost();
|
|
|
|
|
|
|
|
if(timeout > 0 && QDateTime::currentDateTime() >= endTime)
|
|
|
|
{
|
|
|
|
emit busyEnd();
|
|
|
|
return "";
|
|
|
|
}
|
2010-03-03 22:26:00 +00:00
|
|
|
/* make sure we wait a little as we don't want to flood the server
|
|
|
|
* with requests */
|
2009-10-13 19:54:27 +00:00
|
|
|
QDateTime tmpEndTime = QDateTime::currentDateTime().addMSecs(500);
|
|
|
|
while(QDateTime::currentDateTime() < tmpEndTime)
|
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents);
|
|
|
|
}
|
|
|
|
if(response == "nil")
|
|
|
|
{
|
|
|
|
emit busyEnd();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList lines = response.split('\n');
|
|
|
|
if(lines.size() > 2)
|
|
|
|
{
|
2010-03-30 17:45:23 +00:00
|
|
|
lines.removeFirst(); /* should be LP */
|
|
|
|
lines.removeLast(); /* should be ft_StUfF_keyOK */
|
2009-10-13 19:54:27 +00:00
|
|
|
}
|
|
|
|
else
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_ERROR() << "Response too short:" << response;
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
emit busyEnd();
|
|
|
|
return lines.join("\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|