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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ttssapi.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "rbsettings.h"
|
2010-01-24 21:12:11 +00:00
|
|
|
#include "systeminfo.h"
|
2013-11-03 10:08:18 +00:00
|
|
|
#include "Logger.h"
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
TTSSapi::TTSSapi(QObject* parent) : TTSBase(parent)
|
|
|
|
{
|
2012-09-09 09:20:33 +00:00
|
|
|
m_TTSTemplate = "cscript //nologo \"%exe\" /language:%lang "
|
|
|
|
"/voice:\"%voice\" /speed:%speed \"%options\"";
|
|
|
|
m_TTSVoiceTemplate = "cscript //nologo \"%exe\" /language:%lang /listvoices";
|
|
|
|
m_TTSType = "sapi";
|
2011-09-26 21:28:05 +00:00
|
|
|
defaultLanguage = "english";
|
|
|
|
m_started = false;
|
2009-10-13 19:54:27 +00:00
|
|
|
}
|
|
|
|
|
2010-06-04 21:22:25 +00:00
|
|
|
TTSBase::Capabilities TTSSapi::capabilities()
|
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2009-10-13 19:54:27 +00:00
|
|
|
void TTSSapi::generateSettings()
|
|
|
|
{
|
|
|
|
// language
|
2012-05-19 14:49:48 +00:00
|
|
|
QMap<QString, QStringList> languages = SystemInfo::languages();
|
|
|
|
QStringList langs;
|
|
|
|
for(int i = 0; i < languages.values().size(); ++i) {
|
|
|
|
langs.append(languages.values().at(i).at(0));
|
|
|
|
}
|
2012-09-09 09:20:33 +00:00
|
|
|
EncTtsSetting* setting = new EncTtsSetting(this,
|
|
|
|
EncTtsSetting::eSTRINGLIST, tr("Language:"),
|
|
|
|
RbSettings::subValue(m_TTSType, RbSettings::TtsLanguage),
|
|
|
|
langs);
|
2009-10-13 19:54:27 +00:00
|
|
|
connect(setting,SIGNAL(dataChanged()),this,SLOT(updateVoiceList()));
|
|
|
|
insertSetting(eLANGUAGE,setting);
|
|
|
|
// voice
|
2012-09-09 09:20:33 +00:00
|
|
|
setting = new EncTtsSetting(this,
|
|
|
|
EncTtsSetting::eSTRINGLIST, tr("Voice:"),
|
|
|
|
RbSettings::subValue(m_TTSType, RbSettings::TtsVoice),
|
|
|
|
getVoiceList(RbSettings::subValue(m_TTSType,
|
|
|
|
RbSettings::TtsLanguage).toString()),
|
|
|
|
EncTtsSetting::eREFRESHBTN);
|
2009-10-13 19:54:27 +00:00
|
|
|
connect(setting,SIGNAL(refresh()),this,SLOT(updateVoiceList()));
|
|
|
|
insertSetting(eVOICE,setting);
|
|
|
|
//speed
|
2012-09-09 09:20:33 +00:00
|
|
|
int speed = RbSettings::subValue(m_TTSType, RbSettings::TtsSpeed).toInt();
|
2011-04-22 18:58:01 +00:00
|
|
|
if(speed > 10 || speed < -10)
|
|
|
|
speed = 0;
|
2012-09-09 09:20:33 +00:00
|
|
|
insertSetting(eSPEED, new EncTtsSetting(this,
|
|
|
|
EncTtsSetting::eINT, tr("Speed:"), speed, -10, 10));
|
2009-10-13 19:54:27 +00:00
|
|
|
// options
|
2012-09-09 09:20:33 +00:00
|
|
|
insertSetting(eOPTIONS, new EncTtsSetting(this,
|
|
|
|
EncTtsSetting::eSTRING, tr("Options:"),
|
|
|
|
RbSettings::subValue(m_TTSType, RbSettings::TtsOptions)));
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTSSapi::saveSettings()
|
|
|
|
{
|
|
|
|
//save settings in user config
|
2012-09-09 09:20:33 +00:00
|
|
|
RbSettings::setSubValue(m_TTSType, RbSettings::TtsLanguage,
|
2010-03-03 22:26:00 +00:00
|
|
|
getSetting(eLANGUAGE)->current().toString());
|
2012-09-09 09:20:33 +00:00
|
|
|
RbSettings::setSubValue(m_TTSType, RbSettings::TtsVoice,
|
2010-03-03 22:26:00 +00:00
|
|
|
getSetting(eVOICE)->current().toString());
|
2012-09-09 09:20:33 +00:00
|
|
|
RbSettings::setSubValue(m_TTSType, RbSettings::TtsSpeed,
|
2010-03-03 22:26:00 +00:00
|
|
|
getSetting(eSPEED)->current().toInt());
|
2012-09-09 09:20:33 +00:00
|
|
|
RbSettings::setSubValue(m_TTSType, RbSettings::TtsOptions,
|
2010-03-03 22:26:00 +00:00
|
|
|
getSetting(eOPTIONS)->current().toString());
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
RbSettings::sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTSSapi::updateVoiceList()
|
|
|
|
{
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "updating voicelist";
|
2009-10-13 19:54:27 +00:00
|
|
|
QStringList voiceList = getVoiceList(getSetting(eLANGUAGE)->current().toString());
|
|
|
|
getSetting(eVOICE)->setList(voiceList);
|
|
|
|
if(voiceList.size() > 0) getSetting(eVOICE)->setCurrent(voiceList.at(0));
|
|
|
|
else getSetting(eVOICE)->setCurrent("");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTSSapi::start(QString *errStr)
|
|
|
|
{
|
|
|
|
|
2012-09-09 09:20:33 +00:00
|
|
|
m_TTSOpts = RbSettings::subValue(m_TTSType, RbSettings::TtsOptions).toString();
|
|
|
|
m_TTSLanguage =RbSettings::subValue(m_TTSType, RbSettings::TtsLanguage).toString();
|
|
|
|
m_TTSVoice=RbSettings::subValue(m_TTSType, RbSettings::TtsVoice).toString();
|
|
|
|
m_TTSSpeed=RbSettings::subValue(m_TTSType, RbSettings::TtsSpeed).toString();
|
2009-10-13 19:54:27 +00:00
|
|
|
|
|
|
|
QFile::remove(QDir::tempPath() +"/sapi_voice.vbs");
|
|
|
|
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())
|
|
|
|
{
|
2012-01-11 19:18:26 +00:00
|
|
|
*errStr = tr("Could not copy the SAPI script");
|
2009-10-13 19:54:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// create the voice process
|
|
|
|
QString execstring = m_TTSTemplate;
|
|
|
|
execstring.replace("%exe",m_TTSexec);
|
|
|
|
execstring.replace("%options",m_TTSOpts);
|
|
|
|
execstring.replace("%lang",m_TTSLanguage);
|
|
|
|
execstring.replace("%voice",m_TTSVoice);
|
|
|
|
execstring.replace("%speed",m_TTSSpeed);
|
|
|
|
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Start:" << execstring;
|
2009-10-13 19:54:27 +00:00
|
|
|
voicescript = new QProcess(NULL);
|
|
|
|
//connect(voicescript,SIGNAL(readyReadStandardError()),this,SLOT(error()));
|
|
|
|
voicescript->start(execstring);
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "wait for process";
|
2009-10-13 19:54:27 +00:00
|
|
|
if(!voicescript->waitForStarted())
|
|
|
|
{
|
2012-01-11 19:18:26 +00:00
|
|
|
*errStr = tr("Could not start SAPI process");
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_ERROR() << "starting process timed out!";
|
2009-10-13 19:54:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!voicescript->waitForReadyRead(300))
|
|
|
|
{
|
|
|
|
*errStr = voicescript->readAllStandardError();
|
|
|
|
if(*errStr != "")
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
voicestream = new QTextStream(voicescript);
|
|
|
|
voicestream->setCodec("UTF16-LE");
|
|
|
|
|
2011-09-26 21:28:05 +00:00
|
|
|
m_started = true;
|
2009-10-13 19:54:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-26 21:28:05 +00:00
|
|
|
QString TTSSapi::voiceVendor(void)
|
|
|
|
{
|
|
|
|
bool keeprunning = m_started;
|
|
|
|
QString vendor;
|
|
|
|
if(!m_started) {
|
|
|
|
QString error;
|
|
|
|
start(&error);
|
|
|
|
}
|
|
|
|
*voicestream << "QUERY\tVENDOR\r\n";
|
|
|
|
voicestream->flush();
|
|
|
|
while((vendor = voicestream->readLine()).isEmpty())
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "TTS vendor:" << vendor;
|
2011-09-26 21:28:05 +00:00
|
|
|
if(!keeprunning) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
return vendor;
|
|
|
|
}
|
2009-10-13 19:54:27 +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
|
2012-09-09 09:20:33 +00:00
|
|
|
QString execstring = m_TTSVoiceTemplate;
|
2009-10-13 19:54:27 +00:00
|
|
|
execstring.replace("%exe",m_TTSexec);
|
|
|
|
execstring.replace("%lang",language);
|
|
|
|
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Start:" << execstring;
|
2009-10-13 19:54:27 +00:00
|
|
|
voicescript = new QProcess(NULL);
|
|
|
|
voicescript->start(execstring);
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "wait for process";
|
2012-01-11 19:18:26 +00:00
|
|
|
if(!voicescript->waitForStarted()) {
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "process startup timed out!";
|
2009-10-13 19:54:27 +00:00
|
|
|
return result;
|
2012-01-11 19:18:26 +00:00
|
|
|
}
|
2009-10-13 19:54:27 +00:00
|
|
|
voicescript->closeWriteChannel();
|
|
|
|
voicescript->waitForReadyRead();
|
|
|
|
|
|
|
|
QString dataRaw = voicescript->readAllStandardError().data();
|
2012-09-08 18:46:20 +00:00
|
|
|
if(dataRaw.startsWith("Error")) {
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Error:" << dataRaw;
|
2012-09-08 18:46:20 +00:00
|
|
|
}
|
|
|
|
result = dataRaw.split(";",QString::SkipEmptyParts);
|
2009-10-13 19:54:27 +00:00
|
|
|
if(result.size() > 0)
|
|
|
|
{
|
|
|
|
result.sort();
|
|
|
|
result.removeFirst();
|
|
|
|
for(int i = 0; i< result.size();i++)
|
|
|
|
{
|
|
|
|
result[i] = result.at(i).simplified();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete voicescript;
|
2010-03-03 22:26:00 +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 );
|
2009-10-13 19:54:27 +00:00
|
|
|
QFile::remove(QDir::tempPath() +"/sapi_voice.vbs");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TTSStatus TTSSapi::voice(QString text,QString wavfile, QString *errStr)
|
|
|
|
{
|
|
|
|
(void) errStr;
|
2010-09-26 11:43:34 +00:00
|
|
|
QString query = "SPEAK\t"+wavfile+"\t"+text;
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "voicing" << query;
|
2010-09-26 11:43:34 +00:00
|
|
|
// append newline to query. Done now to keep debug output more readable.
|
|
|
|
query.append("\r\n");
|
2009-10-13 19:54:27 +00:00
|
|
|
*voicestream << query;
|
|
|
|
*voicestream << "SYNC\tbla\r\n";
|
|
|
|
voicestream->flush();
|
2012-02-22 21:31:42 +00:00
|
|
|
// do NOT poll the output with readLine(), this causes sync issues!
|
|
|
|
voicescript->waitForReadyRead();
|
2010-06-04 21:22:25 +00:00
|
|
|
|
2012-01-17 19:18:47 +00:00
|
|
|
if(!QFileInfo(wavfile).isFile()) {
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_ERROR() << "output file does not exist:" << wavfile;
|
2012-01-17 19:18:47 +00:00
|
|
|
return FatalError;
|
|
|
|
}
|
2009-10-13 19:54:27 +00:00
|
|
|
return NoError;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTSSapi::stop()
|
|
|
|
{
|
|
|
|
*voicestream << "QUIT\r\n";
|
|
|
|
voicestream->flush();
|
|
|
|
voicescript->waitForFinished();
|
|
|
|
delete voicestream;
|
|
|
|
delete voicescript;
|
2010-03-03 22:26:00 +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 );
|
2009-10-13 19:54:27 +00:00
|
|
|
QFile::remove(QDir::tempPath() +"/sapi_voice.vbs");
|
2011-09-26 21:28:05 +00:00
|
|
|
m_started = false;
|
2009-10-13 19:54:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTSSapi::configOk()
|
|
|
|
{
|
2012-09-09 09:20:33 +00:00
|
|
|
if(RbSettings::subValue(m_TTSType, RbSettings::TtsVoice).toString().isEmpty())
|
2009-10-13 19:54:27 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-03 22:26:00 +00:00
|
|
|
|