2009-06-26 20:40:51 +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 "talkgenerator.h"
|
|
|
|
#include "rbsettings.h"
|
2010-01-24 21:12:11 +00:00
|
|
|
#include "systeminfo.h"
|
2009-06-26 20:40:51 +00:00
|
|
|
#include "wavtrim.h"
|
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
TalkGenerator::TalkGenerator(QObject* parent): QObject(parent)
|
2009-06-26 20:40:51 +00:00
|
|
|
{
|
2012-01-14 15:17:13 +00:00
|
|
|
|
2009-06-26 20:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Creates Talkfiles.
|
|
|
|
//!
|
|
|
|
TalkGenerator::Status TalkGenerator::process(QList<TalkEntry>* list,int wavtrimth)
|
|
|
|
{
|
2012-01-14 15:17:13 +00:00
|
|
|
m_abort = false;
|
2009-06-26 20:40:51 +00:00
|
|
|
QString errStr;
|
|
|
|
bool warnings = false;
|
|
|
|
|
|
|
|
//tts
|
|
|
|
emit logItem(tr("Starting TTS Engine"),LOGINFO);
|
|
|
|
m_tts = TTSBase::getTTS(this,RbSettings::value(RbSettings::Tts).toString());
|
|
|
|
if(!m_tts->start(&errStr))
|
|
|
|
{
|
|
|
|
emit logItem(errStr.trimmed(),LOGERROR);
|
|
|
|
emit logItem(tr("Init of TTS engine failed"),LOGERROR);
|
|
|
|
emit done(true);
|
|
|
|
return eERROR;
|
|
|
|
}
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
|
|
|
// Encoder
|
|
|
|
emit logItem(tr("Starting Encoder Engine"),LOGINFO);
|
2012-01-06 19:19:32 +00:00
|
|
|
m_enc = EncoderBase::getEncoder(this,SystemInfo::value(SystemInfo::CurEncoder).toString());
|
2009-06-26 20:40:51 +00:00
|
|
|
if(!m_enc->start())
|
|
|
|
{
|
|
|
|
emit logItem(tr("Init of Encoder engine failed"),LOGERROR);
|
|
|
|
emit done(true);
|
|
|
|
m_tts->stop();
|
|
|
|
return eERROR;
|
|
|
|
}
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
|
|
|
emit logProgress(0,0);
|
|
|
|
|
|
|
|
// Voice entries
|
|
|
|
emit logItem(tr("Voicing entries..."),LOGINFO);
|
|
|
|
Status voiceStatus= voiceList(list,wavtrimth);
|
|
|
|
if(voiceStatus == eERROR)
|
|
|
|
{
|
|
|
|
m_tts->stop();
|
|
|
|
m_enc->stop();
|
|
|
|
emit done(true);
|
|
|
|
return eERROR;
|
|
|
|
}
|
|
|
|
else if( voiceStatus == eWARNING)
|
|
|
|
warnings = true;
|
|
|
|
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
|
|
|
// Encoding Entries
|
|
|
|
emit logItem(tr("Encoding files..."),LOGINFO);
|
|
|
|
Status encoderStatus = encodeList(list);
|
|
|
|
if( encoderStatus == eERROR)
|
|
|
|
{
|
|
|
|
m_tts->stop();
|
|
|
|
m_enc->stop();
|
|
|
|
emit done(true);
|
|
|
|
return eERROR;
|
|
|
|
}
|
|
|
|
else if( voiceStatus == eWARNING)
|
|
|
|
warnings = true;
|
|
|
|
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
|
|
|
m_tts->stop();
|
|
|
|
m_enc->stop();
|
|
|
|
emit logProgress(1,1);
|
|
|
|
|
|
|
|
if(warnings)
|
|
|
|
return eWARNING;
|
|
|
|
return eOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Voices a List of string
|
|
|
|
//!
|
|
|
|
TalkGenerator::Status TalkGenerator::voiceList(QList<TalkEntry>* list,int wavtrimth)
|
|
|
|
{
|
2012-01-14 15:17:13 +00:00
|
|
|
int progressMax = list->size();
|
|
|
|
int m_progress = 0;
|
|
|
|
emit logProgress(m_progress,progressMax);
|
2009-06-26 20:40:51 +00:00
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
QStringList errors;
|
2010-06-04 21:22:25 +00:00
|
|
|
QStringList duplicates;
|
2009-06-26 20:40:51 +00:00
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
bool warnings = false;
|
2009-06-26 20:40:51 +00:00
|
|
|
for(int i=0; i < list->size(); i++)
|
|
|
|
{
|
2012-01-14 15:17:13 +00:00
|
|
|
if(m_abort)
|
|
|
|
{
|
|
|
|
emit logItem(tr("Voicing aborted"), LOGERROR);
|
|
|
|
return eERROR;
|
2011-10-01 19:48:58 +00:00
|
|
|
}
|
2009-06-26 20:40:51 +00:00
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
// skip duplicated wav entrys
|
2010-06-04 21:22:25 +00:00
|
|
|
if(!duplicates.contains(list->at(i).wavfilename))
|
|
|
|
duplicates.append(list->at(i).wavfilename);
|
2009-06-26 20:40:51 +00:00
|
|
|
else
|
|
|
|
{
|
2012-01-14 15:17:13 +00:00
|
|
|
qDebug() << "[TalkGenerator] duplicate skipped";
|
2009-06-26 20:40:51 +00:00
|
|
|
(*list)[i].voiced = true;
|
2012-01-14 15:17:13 +00:00
|
|
|
emit logProgress(++m_progress,progressMax);
|
2009-06-26 20:40:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-04-22 17:59:43 +00:00
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
// skip already voiced entrys
|
|
|
|
if(list->at(i).voiced == true)
|
|
|
|
{
|
|
|
|
emit logProgress(++m_progress,progressMax);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// skip entry whith empty text
|
|
|
|
if(list->at(i).toSpeak == "")
|
|
|
|
{
|
|
|
|
emit logProgress(++m_progress,progressMax);
|
|
|
|
continue;
|
2011-04-22 17:59:43 +00:00
|
|
|
}
|
2009-06-26 20:40:51 +00:00
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
// voice entry
|
2009-06-26 20:40:51 +00:00
|
|
|
QString error;
|
2012-01-15 11:24:54 +00:00
|
|
|
qDebug() << "[TalkGenerator] voicing: " << list->at(i).toSpeak
|
|
|
|
<< "to" << list->at(i).wavfilename;
|
2012-01-14 15:17:13 +00:00
|
|
|
TTSStatus status = m_tts->voice(list->at(i).toSpeak,list->at(i).wavfilename, &error);
|
|
|
|
if(status == Warning)
|
2009-06-26 20:40:51 +00:00
|
|
|
{
|
2012-01-14 15:17:13 +00:00
|
|
|
warnings = true;
|
|
|
|
emit logItem(tr("Voicing of %1 failed: %2").arg(list->at(i).toSpeak).arg(error),
|
|
|
|
LOGWARNING);
|
2009-06-26 20:40:51 +00:00
|
|
|
}
|
2012-01-14 15:17:13 +00:00
|
|
|
else if (status == FatalError)
|
|
|
|
{
|
|
|
|
emit logItem(tr("Voicing of %1 failed: %2").arg(list->at(i).toSpeak).arg(error),
|
|
|
|
LOGERROR);
|
|
|
|
return eERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
(*list)[i].voiced = true;
|
|
|
|
|
2012-01-15 11:24:54 +00:00
|
|
|
// wavtrim if needed
|
2012-01-14 15:17:13 +00:00
|
|
|
if(wavtrimth != -1)
|
2009-06-26 20:40:51 +00:00
|
|
|
{
|
|
|
|
char buffer[255];
|
2012-01-15 11:24:54 +00:00
|
|
|
if(wavtrim(list->at(i).wavfilename.toLocal8Bit().data(),
|
|
|
|
wavtrimth, buffer, 255))
|
|
|
|
{
|
|
|
|
qDebug() << "[TalkGenerator] wavtrim returned error on"
|
|
|
|
<< list->at(i).wavfilename;
|
|
|
|
return eERROR;
|
|
|
|
}
|
2009-06-26 20:40:51 +00:00
|
|
|
}
|
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
emit logProgress(++m_progress,progressMax);
|
|
|
|
QCoreApplication::processEvents();
|
2009-06-26 20:40:51 +00:00
|
|
|
}
|
2012-01-14 15:17:13 +00:00
|
|
|
if(warnings)
|
|
|
|
return eWARNING;
|
|
|
|
else
|
|
|
|
return eOK;
|
2009-06-26 20:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! \brief Encodes a List of strings
|
|
|
|
//!
|
|
|
|
TalkGenerator::Status TalkGenerator::encodeList(QList<TalkEntry>* list)
|
|
|
|
{
|
2010-06-04 21:22:25 +00:00
|
|
|
QStringList duplicates;
|
2009-06-26 20:40:51 +00:00
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
int progressMax = list->size();
|
|
|
|
int m_progress = 0;
|
|
|
|
emit logProgress(m_progress,progressMax);
|
2009-06-26 20:40:51 +00:00
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
for(int i=0; i < list->size(); i++)
|
2009-06-26 20:40:51 +00:00
|
|
|
{
|
2012-01-14 15:17:13 +00:00
|
|
|
if(m_abort)
|
2009-06-26 20:40:51 +00:00
|
|
|
{
|
2012-01-14 15:17:13 +00:00
|
|
|
emit logItem(tr("Encoding aborted"), LOGERROR);
|
|
|
|
return eERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
//skip non-voiced entrys
|
|
|
|
if(list->at(i).voiced == false)
|
|
|
|
{
|
2012-06-24 16:37:47 +00:00
|
|
|
qDebug() << "[TalkGenerator] non voiced entry detected:"
|
|
|
|
<< list->at(i).toSpeak;
|
2012-01-14 15:17:13 +00:00
|
|
|
emit logProgress(++m_progress,progressMax);
|
2009-06-26 20:40:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-01-14 15:17:13 +00:00
|
|
|
//skip duplicates
|
|
|
|
if(!duplicates.contains(list->at(i).talkfilename))
|
|
|
|
duplicates.append(list->at(i).talkfilename);
|
|
|
|
else
|
2009-06-26 20:40:51 +00:00
|
|
|
{
|
2012-01-14 15:17:13 +00:00
|
|
|
qDebug() << "[TalkGenerator] duplicate skipped";
|
|
|
|
(*list)[i].encoded = true;
|
|
|
|
emit logProgress(++m_progress,progressMax);
|
2009-06-26 20:40:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-06-04 21:22:25 +00:00
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
//encode entry
|
|
|
|
qDebug() << "[TalkGenerator] encoding " << list->at(i).wavfilename
|
|
|
|
<< "to" << list->at(i).talkfilename;
|
|
|
|
if(!m_enc->encode(list->at(i).wavfilename,list->at(i).talkfilename))
|
|
|
|
{
|
|
|
|
emit logItem(tr("Encoding of %1 failed").arg(
|
|
|
|
QFileInfo(list->at(i).wavfilename).baseName()), LOGERROR);
|
|
|
|
return eERROR;
|
2009-06-26 20:40:51 +00:00
|
|
|
}
|
2012-01-14 15:17:13 +00:00
|
|
|
(*list)[i].encoded = true;
|
|
|
|
emit logProgress(++m_progress,progressMax);
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
}
|
|
|
|
return eOK;
|
2009-06-26 20:40:51 +00:00
|
|
|
}
|
|
|
|
|
2012-01-14 15:17:13 +00:00
|
|
|
//! \brief slot, which is connected to the abort of the Logger.
|
|
|
|
//Sets a flag, so Creating Talkfiles ends at the next possible position
|
2009-06-26 20:40:51 +00:00
|
|
|
//!
|
|
|
|
void TalkGenerator::abort()
|
|
|
|
{
|
2012-01-14 15:17:13 +00:00
|
|
|
m_abort = true;
|
2009-06-26 20:40:51 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 19:48:58 +00:00
|
|
|
QString TalkGenerator::correctString(QString s)
|
|
|
|
{
|
|
|
|
QString corrected = s;
|
|
|
|
int i = 0;
|
|
|
|
int max = m_corrections.size();
|
|
|
|
while(i < max) {
|
|
|
|
corrected = corrected.replace(QRegExp(m_corrections.at(i).search,
|
|
|
|
m_corrections.at(i).modifier.contains("i")
|
|
|
|
? Qt::CaseInsensitive : Qt::CaseSensitive),
|
|
|
|
m_corrections.at(i).replace);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(corrected != s)
|
|
|
|
qDebug() << "[VoiceFileCreator] corrected string" << s << "to" << corrected;
|
|
|
|
|
|
|
|
return corrected;
|
2012-01-14 15:17:13 +00:00
|
|
|
m_abort = true;
|
2011-10-01 19:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TalkGenerator::setLang(QString name)
|
|
|
|
{
|
|
|
|
m_lang = name;
|
|
|
|
|
|
|
|
// re-initialize corrections list
|
|
|
|
m_corrections.clear();
|
|
|
|
QFile correctionsFile(":/builtin/voice-corrections.txt");
|
|
|
|
correctionsFile.open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
QString engine = RbSettings::value(RbSettings::Tts).toString();
|
|
|
|
TTSBase* tts = TTSBase::getTTS(this,RbSettings::value(RbSettings::Tts).toString());
|
|
|
|
QString vendor = tts->voiceVendor();
|
|
|
|
delete tts;
|
|
|
|
|
|
|
|
if(m_lang.isEmpty())
|
|
|
|
m_lang = "english";
|
|
|
|
qDebug() << "[TalkGenerator] building string corrections list for"
|
|
|
|
<< m_lang << engine << vendor;
|
|
|
|
QTextStream stream(&correctionsFile);
|
|
|
|
while(!stream.atEnd()) {
|
|
|
|
QString line = stream.readLine();
|
|
|
|
if(line.startsWith(" ") || line.length() < 10)
|
|
|
|
continue;
|
|
|
|
// separator is first character
|
|
|
|
QString separator = line.at(0);
|
|
|
|
line.remove(0, 1);
|
|
|
|
QStringList items = line.split(separator);
|
|
|
|
// we need to have at least 6 separate entries.
|
|
|
|
if(items.size() < 6)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QRegExp re_lang(items.at(0));
|
|
|
|
QRegExp re_engine(items.at(1));
|
|
|
|
QRegExp re_vendor(items.at(2));
|
|
|
|
if(!re_lang.exactMatch(m_lang)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!re_vendor.exactMatch(vendor)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!re_engine.exactMatch(engine)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
struct CorrectionItems co;
|
|
|
|
co.search = items.at(3);
|
|
|
|
co.replace = items.at(4);
|
|
|
|
// Qt uses backslash for back references, Perl uses dollar sign.
|
|
|
|
co.replace.replace(QRegExp("\\$(\\d+)"), "\\\\1");
|
|
|
|
co.modifier = items.at(5);
|
|
|
|
m_corrections.append(co);
|
|
|
|
}
|
|
|
|
correctionsFile.close();
|
|
|
|
}
|