2008-01-11 23:59:12 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 by Dominik Wenger
|
2008-06-30 22:09:45 +00:00
|
|
|
* $Id$
|
2008-01-11 23:59:12 +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 "voicefile.h"
|
2009-03-21 16:30:40 +00:00
|
|
|
#include "utils.h"
|
2008-01-11 23:59:12 +00:00
|
|
|
|
|
|
|
#define STATE_INVALID 0
|
|
|
|
#define STATE_PHRASE 1
|
|
|
|
#define STATE_VOICE 2
|
|
|
|
|
|
|
|
|
|
|
|
VoiceFileCreator::VoiceFileCreator(QObject* parent) :QObject(parent)
|
|
|
|
{
|
2008-01-19 18:33:33 +00:00
|
|
|
m_wavtrimThreshold=500;
|
2008-01-11 23:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoiceFileCreator::abort()
|
|
|
|
{
|
|
|
|
m_abort = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VoiceFileCreator::createVoiceFile(ProgressloggerInterface* logger)
|
|
|
|
{
|
|
|
|
m_abort = false;
|
|
|
|
m_logger = logger;
|
2008-01-13 13:51:33 +00:00
|
|
|
m_logger->addItem(tr("Starting Voicefile generation"),LOGINFO);
|
2008-01-11 23:59:12 +00:00
|
|
|
|
|
|
|
// test if tempdir exists
|
|
|
|
if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
|
|
|
|
{
|
|
|
|
QDir(QDir::tempPath()).mkdir("rbvoice");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_path = QDir::tempPath() + "/rbvoice/";
|
|
|
|
|
|
|
|
// read rockbox-info.txt
|
2009-03-21 16:30:40 +00:00
|
|
|
RockboxInfo info(m_mountpoint);
|
|
|
|
if(!info.open())
|
2008-01-11 23:59:12 +00:00
|
|
|
{
|
2009-03-21 16:30:40 +00:00
|
|
|
m_logger->addItem(tr("could not find rockbox-info.txt"),LOGERROR);
|
2008-02-03 11:45:35 +00:00
|
|
|
m_logger->abort();
|
2008-02-09 18:58:14 +00:00
|
|
|
emit done(false);
|
2008-01-11 23:59:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-21 16:30:40 +00:00
|
|
|
QString target = info.target();
|
|
|
|
QString features = info.features();
|
|
|
|
QString version = info.version();
|
|
|
|
version = version.left(version.indexOf("-")).remove(0,1);
|
|
|
|
|
2008-01-11 23:59:12 +00:00
|
|
|
//prepare download url
|
2008-01-25 00:12:25 +00:00
|
|
|
QUrl genlangUrl = settings->genlangUrl() +"?lang=" +m_lang+"&t="+target+"&rev="+version+"&f="+features;
|
2008-01-11 23:59:12 +00:00
|
|
|
|
|
|
|
qDebug() << "downloading " << genlangUrl;
|
|
|
|
|
|
|
|
//download the correct genlang output
|
|
|
|
QTemporaryFile *downloadFile = new QTemporaryFile(this);
|
|
|
|
downloadFile->open();
|
|
|
|
filename = downloadFile->fileName();
|
|
|
|
downloadFile->close();
|
|
|
|
// get the real file.
|
|
|
|
getter = new HttpGet(this);
|
|
|
|
getter->setFile(downloadFile);
|
2008-04-01 18:02:56 +00:00
|
|
|
|
2008-01-11 23:59:12 +00:00
|
|
|
connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
|
|
|
|
connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
|
|
|
|
connect(m_logger, SIGNAL(aborted()), getter, SLOT(abort()));
|
2008-04-01 18:02:56 +00:00
|
|
|
|
|
|
|
getter->getFile(genlangUrl);
|
2008-01-11 23:59:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VoiceFileCreator::downloadDone(bool error)
|
|
|
|
{
|
|
|
|
qDebug() << "Voice creator::downloadDone, error:" << error;
|
|
|
|
|
|
|
|
// update progress bar
|
|
|
|
int max = m_logger->getProgressMax();
|
|
|
|
if(max == 0) {
|
|
|
|
max = 100;
|
|
|
|
m_logger->setProgressMax(max);
|
|
|
|
}
|
|
|
|
m_logger->setProgressValue(max);
|
|
|
|
if(getter->httpResponse() != 200 && !getter->isCached()) {
|
|
|
|
m_logger->addItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
|
|
|
|
m_logger->abort();
|
2008-02-09 18:58:14 +00:00
|
|
|
emit done(false);
|
2008-01-11 23:59:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(getter->isCached()) m_logger->addItem(tr("Cached file used."), LOGINFO);
|
|
|
|
if(error) {
|
|
|
|
m_logger->addItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
|
|
|
|
m_logger->abort();
|
2008-02-09 18:58:14 +00:00
|
|
|
emit done(false);
|
2008-01-11 23:59:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else m_logger->addItem(tr("Download finished."),LOGOK);
|
2008-02-10 18:25:49 +00:00
|
|
|
QCoreApplication::processEvents();
|
2008-01-11 23:59:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
m_logger->setProgressMax(0);
|
|
|
|
//open downloaded file
|
|
|
|
QFile genlang(filename);
|
|
|
|
if(!genlang.open(QIODevice::ReadOnly))
|
|
|
|
{
|
2008-01-13 13:51:33 +00:00
|
|
|
m_logger->addItem(tr("failed to open downloaded file"),LOGERROR);
|
2008-01-11 23:59:12 +00:00
|
|
|
m_logger->abort();
|
2008-02-09 18:58:14 +00:00
|
|
|
emit done(false);
|
2008-01-11 23:59:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//tts
|
2008-02-12 21:45:50 +00:00
|
|
|
m_tts = TTSBase::getTTS(settings->curTTS());
|
2008-01-25 00:12:25 +00:00
|
|
|
m_tts->setCfg(settings);
|
2008-01-11 23:59:12 +00:00
|
|
|
|
2008-01-19 18:33:33 +00:00
|
|
|
QString errStr;
|
|
|
|
if(!m_tts->start(&errStr))
|
2008-01-11 23:59:12 +00:00
|
|
|
{
|
2008-01-19 18:33:33 +00:00
|
|
|
m_logger->addItem(errStr,LOGERROR);
|
2008-01-13 13:51:33 +00:00
|
|
|
m_logger->addItem(tr("Init of TTS engine failed"),LOGERROR);
|
2008-01-11 23:59:12 +00:00
|
|
|
m_logger->abort();
|
2008-02-09 18:58:14 +00:00
|
|
|
emit done(false);
|
2008-01-11 23:59:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encoder
|
2008-02-13 18:11:09 +00:00
|
|
|
m_enc = EncBase::getEncoder(settings->curEncoder());
|
2008-01-25 00:12:25 +00:00
|
|
|
m_enc->setCfg(settings);
|
2008-01-11 23:59:12 +00:00
|
|
|
|
|
|
|
if(!m_enc->start())
|
|
|
|
{
|
2008-01-13 13:51:33 +00:00
|
|
|
m_logger->addItem(tr("Init of Encoder engine failed"),LOGERROR);
|
2008-01-11 23:59:12 +00:00
|
|
|
m_tts->stop();
|
|
|
|
m_logger->abort();
|
2008-02-09 18:58:14 +00:00
|
|
|
emit done(false);
|
2008-01-11 23:59:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-10 18:25:49 +00:00
|
|
|
QCoreApplication::processEvents();
|
2008-01-11 23:59:12 +00:00
|
|
|
connect(m_logger,SIGNAL(aborted()),this,SLOT(abort()));
|
2008-01-13 13:51:33 +00:00
|
|
|
|
|
|
|
//read in downloaded file
|
|
|
|
QList<QPair<QString,QString> > voicepairs;
|
2008-01-11 23:59:12 +00:00
|
|
|
QTextStream in(&genlang);
|
|
|
|
in.setCodec("UTF-8");
|
2008-01-13 13:51:33 +00:00
|
|
|
QString id, voice;
|
|
|
|
bool idfound = false;
|
|
|
|
bool voicefound=false;
|
2008-01-11 23:59:12 +00:00
|
|
|
while (!in.atEnd())
|
|
|
|
{
|
2008-01-13 13:51:33 +00:00
|
|
|
QString line = in.readLine();
|
|
|
|
if(line.contains("id:")) //ID found
|
|
|
|
{
|
|
|
|
id = line.remove("id:").remove('"').trimmed();
|
|
|
|
idfound = true;
|
|
|
|
}
|
|
|
|
else if(line.contains("voice:")) // voice found
|
|
|
|
{
|
|
|
|
voice = line.remove("voice:").remove('"').trimmed();
|
|
|
|
voicefound=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(idfound && voicefound)
|
|
|
|
{
|
|
|
|
voicepairs.append(QPair<QString,QString>(id,voice));
|
|
|
|
idfound=false;
|
|
|
|
voicefound=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
genlang.close();
|
|
|
|
|
|
|
|
// check for empty list
|
|
|
|
if(voicepairs.size() == 0)
|
|
|
|
{
|
|
|
|
m_logger->addItem(tr("The downloaded file was empty!"),LOGERROR);
|
|
|
|
m_logger->abort();
|
|
|
|
m_tts->stop();
|
2008-02-09 18:58:14 +00:00
|
|
|
emit done(false);
|
2008-01-13 13:51:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_logger->setProgressMax(voicepairs.size());
|
|
|
|
m_logger->setProgressValue(0);
|
|
|
|
|
|
|
|
// create voice clips
|
|
|
|
QStringList mp3files;
|
|
|
|
for(int i=0; i< voicepairs.size(); i++)
|
|
|
|
{
|
2008-01-11 23:59:12 +00:00
|
|
|
if(m_abort)
|
|
|
|
{
|
|
|
|
m_logger->addItem("aborted.",LOGERROR);
|
2008-01-13 13:51:33 +00:00
|
|
|
m_logger->abort();
|
|
|
|
m_tts->stop();
|
2008-02-09 18:58:14 +00:00
|
|
|
emit done(false);
|
2008-01-13 13:51:33 +00:00
|
|
|
return;
|
2008-01-11 23:59:12 +00:00
|
|
|
}
|
|
|
|
|
2008-01-13 13:51:33 +00:00
|
|
|
m_logger->setProgressValue(i);
|
|
|
|
|
|
|
|
QString wavname = m_path + "/" + voicepairs.at(i).first + ".wav";
|
|
|
|
QString toSpeak = voicepairs.at(i).second;
|
|
|
|
QString encodedname = m_path + "/" + voicepairs.at(i).first +".mp3";
|
2008-01-11 23:59:12 +00:00
|
|
|
|
|
|
|
// todo PAUSE
|
2008-01-13 13:51:33 +00:00
|
|
|
if(voicepairs.at(i).first == "VOICE_PAUSE")
|
2008-01-11 23:59:12 +00:00
|
|
|
{
|
|
|
|
QFile::copy(":/builtin/builtin/VOICE_PAUSE.wav",m_path + "/VOICE_PAUSE.wav");
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-13 13:51:33 +00:00
|
|
|
if(toSpeak == "") continue;
|
2008-01-11 23:59:12 +00:00
|
|
|
|
2008-02-10 18:25:49 +00:00
|
|
|
m_logger->addItem(tr("creating ")+toSpeak,LOGINFO);
|
|
|
|
QCoreApplication::processEvents();
|
2009-03-27 19:18:14 +00:00
|
|
|
|
|
|
|
// TODO: add support for aborting the operation
|
|
|
|
QString errStr;
|
|
|
|
m_tts->voice(toSpeak,wavname, &errStr); // generate wav
|
2008-01-11 23:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo strip
|
|
|
|
char buffer[255];
|
|
|
|
|
2008-01-19 18:33:33 +00:00
|
|
|
wavtrim((char*)qPrintable(wavname),m_wavtrimThreshold,buffer,255);
|
2008-01-11 23:59:12 +00:00
|
|
|
|
|
|
|
// encode wav
|
|
|
|
m_enc->encode(wavname,encodedname);
|
|
|
|
// remove the wav file
|
|
|
|
QFile::remove(wavname);
|
|
|
|
// remember the mp3 file for later removing
|
2008-02-10 18:25:49 +00:00
|
|
|
mp3files << encodedname;
|
2008-01-11 23:59:12 +00:00
|
|
|
}
|
|
|
|
|
2008-01-13 13:51:33 +00:00
|
|
|
|
2008-01-11 23:59:12 +00:00
|
|
|
//make voicefile
|
|
|
|
FILE* ids2 = fopen(filename.toUtf8(), "r");
|
|
|
|
if (ids2 == NULL)
|
|
|
|
{
|
2008-01-13 13:51:33 +00:00
|
|
|
m_logger->addItem(tr("Error opening downloaded file"),LOGERROR);
|
2008-01-11 23:59:12 +00:00
|
|
|
m_logger->abort();
|
2008-02-09 18:58:14 +00:00
|
|
|
emit done(false);
|
2008-01-11 23:59:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* output = fopen(QString(m_mountpoint + "/.rockbox/langs/" + m_lang + ".voice").toUtf8(), "wb");
|
|
|
|
if (output == NULL)
|
|
|
|
{
|
2008-02-09 18:58:14 +00:00
|
|
|
m_logger->addItem(tr("Error opening output file"),LOGERROR);
|
|
|
|
emit done(false);
|
2008-01-11 23:59:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
voicefont(ids2,m_targetid,(char*)(const char*)m_path.toUtf8(), output);
|
|
|
|
|
|
|
|
//remove .mp3 files
|
|
|
|
for(int i=0;i< mp3files.size(); i++)
|
|
|
|
{
|
|
|
|
QFile::remove(mp3files.at(i));
|
|
|
|
}
|
2008-06-28 16:44:59 +00:00
|
|
|
|
|
|
|
// Add Voice file to the install log
|
|
|
|
QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
|
|
|
|
installlog.beginGroup("selfcreated Voice");
|
|
|
|
installlog.setValue("/.rockbox/langs/" + m_lang + ".voice",QDate::currentDate().toString("yyyyMMdd"));
|
|
|
|
installlog.endGroup();
|
|
|
|
installlog.sync();
|
|
|
|
|
2008-01-11 23:59:12 +00:00
|
|
|
m_logger->setProgressMax(100);
|
|
|
|
m_logger->setProgressValue(100);
|
2008-01-13 13:51:33 +00:00
|
|
|
m_logger->addItem(tr("successfully created."),LOGOK);
|
2008-02-09 18:58:14 +00:00
|
|
|
m_logger->abort();
|
|
|
|
|
|
|
|
emit done(true);
|
2008-01-11 23:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoiceFileCreator::updateDataReadProgress(int read, int total)
|
|
|
|
{
|
|
|
|
m_logger->setProgressMax(total);
|
|
|
|
m_logger->setProgressValue(read);
|
|
|
|
//qDebug() << "progress:" << read << "/" << total;
|
|
|
|
|
|
|
|
}
|
|
|
|
|