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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-06-09 21:39:57 +00:00
|
|
|
#include <QtCore>
|
2009-06-26 20:40:51 +00:00
|
|
|
#include "voicefile.h"
|
|
|
|
#include "utils.h"
|
2010-04-02 21:24:19 +00:00
|
|
|
#include "rockboxinfo.h"
|
2009-06-26 20:40:51 +00:00
|
|
|
#include "rbsettings.h"
|
2020-11-28 19:47:28 +00:00
|
|
|
#include "playerbuildinfo.h"
|
2012-06-09 21:39:57 +00:00
|
|
|
#include "ziputil.h"
|
2013-11-03 10:08:18 +00:00
|
|
|
#include "Logger.h"
|
2009-06-26 20:40:51 +00:00
|
|
|
|
|
|
|
VoiceFileCreator::VoiceFileCreator(QObject* parent) :QObject(parent)
|
|
|
|
{
|
|
|
|
m_wavtrimThreshold=500;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoiceFileCreator::abort()
|
|
|
|
{
|
|
|
|
m_abort = true;
|
|
|
|
emit aborted();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VoiceFileCreator::createVoiceFile()
|
|
|
|
{
|
|
|
|
m_talkList.clear();
|
|
|
|
m_abort = false;
|
|
|
|
emit logItem(tr("Starting Voicefile generation"),LOGINFO);
|
|
|
|
|
|
|
|
// test if tempdir exists
|
|
|
|
if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
|
|
|
|
{
|
|
|
|
QDir(QDir::tempPath()).mkdir("rbvoice");
|
|
|
|
}
|
|
|
|
m_path = QDir::tempPath() + "/rbvoice/";
|
|
|
|
|
|
|
|
// read rockbox-info.txt
|
|
|
|
RockboxInfo info(m_mountpoint);
|
2009-08-10 19:20:53 +00:00
|
|
|
if(!info.success())
|
2009-06-26 20:40:51 +00:00
|
|
|
{
|
|
|
|
emit logItem(tr("could not find rockbox-info.txt"),LOGERROR);
|
|
|
|
emit done(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QString target = info.target();
|
|
|
|
QString features = info.features();
|
2009-08-22 14:05:20 +00:00
|
|
|
m_targetid = info.targetID().toInt();
|
2012-05-23 20:59:56 +00:00
|
|
|
m_versionstring = info.version();
|
2012-06-03 14:13:04 +00:00
|
|
|
m_voiceformat = info.voicefmt();
|
2012-05-23 20:59:56 +00:00
|
|
|
QString version = m_versionstring.left(m_versionstring.indexOf("-")).remove("r");
|
2012-05-23 20:48:09 +00:00
|
|
|
|
2012-06-09 21:39:57 +00:00
|
|
|
// check if voicefile is present on target
|
|
|
|
QString fn = m_mountpoint + "/.rockbox/langs/voicestrings.zip";
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "searching for zipped voicestrings at" << fn;
|
2012-06-09 21:39:57 +00:00
|
|
|
if(QFileInfo(fn).isFile()) {
|
|
|
|
// search for binary voice strings file in archive
|
|
|
|
ZipUtil z(this);
|
|
|
|
if(z.open(fn)) {
|
|
|
|
QStringList contents = z.files();
|
|
|
|
int index;
|
|
|
|
for(index = 0; index < contents.size(); ++index) {
|
|
|
|
// strip any path, we don't know the structure in the zip
|
|
|
|
if(QFileInfo(contents.at(index)).baseName() == m_lang) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(index < contents.size()) {
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "extracting strings file from zip";
|
2012-06-09 21:39:57 +00:00
|
|
|
// extract strings
|
|
|
|
QTemporaryFile stringsfile;
|
|
|
|
stringsfile.open();
|
|
|
|
QString sfn = stringsfile.fileName();
|
|
|
|
// ZipUtil::extractArchive() only compares the filename.
|
|
|
|
if(z.extractArchive(sfn, QFileInfo(contents.at(index)).fileName())) {
|
|
|
|
emit logItem(tr("Extracted voice strings from installation"), LOGINFO);
|
|
|
|
|
|
|
|
stringsfile.seek(0);
|
|
|
|
QByteArray data = stringsfile.readAll();
|
|
|
|
const char* buf = data.constData();
|
|
|
|
// check file header
|
|
|
|
// header (4 bytes): cookie = 9a, version = 06, targetid, options
|
|
|
|
// subheader for each user. Only "core" for now.
|
|
|
|
// subheader (6 bytes): count (2bytes), size (2bytes), offset (2bytes)
|
|
|
|
if(buf[0] != (char)0x9a || buf[1] != 0x06 || buf[2] != m_targetid) {
|
|
|
|
emit logItem(tr("Extracted voice strings incompatible"), LOGINFO);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QMap<int, QString> voicestrings;
|
|
|
|
|
|
|
|
/* skip header */
|
|
|
|
int idx = 10;
|
|
|
|
do {
|
|
|
|
unsigned int id = ((unsigned char)buf[idx])<<8
|
|
|
|
| ((unsigned char)buf[idx+1]);
|
|
|
|
// need to use strlen here, since QString::size()
|
|
|
|
// returns number of characters, not bytes.
|
|
|
|
int len = strlen(&buf[idx + 2]);
|
|
|
|
voicestrings[id] = QString::fromUtf8(&buf[idx+2]);
|
|
|
|
idx += 2 + len + 1;
|
|
|
|
|
|
|
|
} while(idx < data.size());
|
|
|
|
|
|
|
|
stringsfile.close();
|
|
|
|
|
|
|
|
// create input file suitable for voicefont from strings.
|
|
|
|
QTemporaryFile voicefontlist;
|
|
|
|
voicefontlist.open();
|
|
|
|
m_filename = voicefontlist.fileName();
|
|
|
|
for(int i = 0; i < voicestrings.size(); ++i) {
|
|
|
|
QByteArray qba;
|
|
|
|
qba = QString("id: %1_%2\n")
|
|
|
|
.arg(voicestrings.keys().at(i) < 0x8000 ? "LANG" : "VOICE")
|
|
|
|
.arg(voicestrings.keys().at(i)).toUtf8();
|
|
|
|
voicefontlist.write(qba);
|
|
|
|
qba = QString("voice: \"%1\"\n").arg(
|
|
|
|
voicestrings[voicestrings.keys().at(i)]).toUtf8();
|
|
|
|
voicefontlist.write(qba);
|
|
|
|
}
|
|
|
|
voicefontlist.close();
|
|
|
|
|
|
|
|
// everything successful, now create the actual voice file.
|
|
|
|
create();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit logItem(tr("Could not retrieve strings from installation, downloading"), LOGINFO);
|
|
|
|
// if either no zip with voice strings is found or something went wrong
|
|
|
|
// retrieving the necessary files we'll end up here, trying to get the
|
|
|
|
// genlang output as previously from the webserver.
|
|
|
|
|
|
|
|
// prepare download url
|
2020-11-28 19:47:28 +00:00
|
|
|
QString genlang = PlayerBuildInfo::instance()->value(
|
|
|
|
PlayerBuildInfo::GenlangUrl).toString();
|
2011-04-02 08:59:00 +00:00
|
|
|
genlang.replace("%LANG%", m_lang);
|
|
|
|
genlang.replace("%TARGET%", target);
|
2012-05-23 20:59:56 +00:00
|
|
|
genlang.replace("%REVISION%", version);
|
2011-04-02 08:59:00 +00:00
|
|
|
genlang.replace("%FEATURES%", features);
|
|
|
|
QUrl genlangUrl(genlang);
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "downloading" << genlangUrl;
|
2009-06-26 20:40:51 +00:00
|
|
|
|
|
|
|
//download the correct genlang output
|
|
|
|
QTemporaryFile *downloadFile = new QTemporaryFile(this);
|
|
|
|
downloadFile->open();
|
2012-05-25 21:01:45 +00:00
|
|
|
m_filename = downloadFile->fileName();
|
2009-06-26 20:40:51 +00:00
|
|
|
downloadFile->close();
|
|
|
|
// get the real file.
|
|
|
|
getter = new HttpGet(this);
|
|
|
|
getter->setFile(downloadFile);
|
|
|
|
|
2020-12-16 16:34:28 +00:00
|
|
|
connect(getter, &HttpGet::done, this, &VoiceFileCreator::downloadDone);
|
|
|
|
connect(getter, &HttpGet::dataReadProgress, this, &VoiceFileCreator::logProgress);
|
|
|
|
connect(this, &VoiceFileCreator::aborted, getter, &HttpGet::abort);
|
2009-09-25 15:51:17 +00:00
|
|
|
emit logItem(tr("Downloading voice info..."),LOGINFO);
|
2009-06-26 20:40:51 +00:00
|
|
|
getter->getFile(genlangUrl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VoiceFileCreator::downloadDone(bool error)
|
|
|
|
{
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "download done, error:" << error;
|
2009-06-26 20:40:51 +00:00
|
|
|
|
|
|
|
// update progress bar
|
|
|
|
emit logProgress(1,1);
|
|
|
|
if(getter->httpResponse() != 200 && !getter->isCached()) {
|
2012-05-23 20:48:09 +00:00
|
|
|
emit logItem(tr("Download error: received HTTP error %1.")
|
|
|
|
.arg(getter->httpResponse()),LOGERROR);
|
2009-06-26 20:40:51 +00:00
|
|
|
emit done(true);
|
|
|
|
return;
|
|
|
|
}
|
2012-05-25 21:01:45 +00:00
|
|
|
|
|
|
|
if(getter->isCached())
|
2010-01-10 17:13:32 +00:00
|
|
|
emit logItem(tr("Cached file used."), LOGINFO);
|
2009-06-26 20:40:51 +00:00
|
|
|
if(error)
|
|
|
|
{
|
|
|
|
emit logItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
|
|
|
|
emit done(true);
|
|
|
|
return;
|
|
|
|
}
|
2012-05-25 21:01:45 +00:00
|
|
|
else
|
2010-01-10 17:13:32 +00:00
|
|
|
emit logItem(tr("Download finished."),LOGINFO);
|
2012-05-25 21:01:45 +00:00
|
|
|
|
2009-06-26 20:40:51 +00:00
|
|
|
QCoreApplication::processEvents();
|
2012-05-25 21:01:45 +00:00
|
|
|
create();
|
|
|
|
}
|
|
|
|
|
2009-06-26 20:40:51 +00:00
|
|
|
|
2012-05-25 21:01:45 +00:00
|
|
|
void VoiceFileCreator::create(void)
|
|
|
|
{
|
2009-06-26 20:40:51 +00:00
|
|
|
//open downloaded file
|
2012-05-25 21:01:45 +00:00
|
|
|
QFile genlang(m_filename);
|
2009-06-26 20:40:51 +00:00
|
|
|
if(!genlang.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
emit logItem(tr("failed to open downloaded file"),LOGERROR);
|
|
|
|
emit done(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//read in downloaded file
|
|
|
|
emit logItem(tr("Reading strings..."),LOGINFO);
|
|
|
|
QTextStream in(&genlang);
|
2020-07-25 18:28:05 +00:00
|
|
|
#if QT_VERSION < 0x060000
|
2009-06-26 20:40:51 +00:00
|
|
|
in.setCodec("UTF-8");
|
2020-07-25 18:28:05 +00:00
|
|
|
#else
|
|
|
|
in.setEncoding(QStringConverter::Utf8);
|
|
|
|
#endif
|
2009-06-26 20:40:51 +00:00
|
|
|
QString id, voice;
|
|
|
|
bool idfound = false;
|
|
|
|
bool voicefound=false;
|
2011-10-01 19:48:58 +00:00
|
|
|
bool useCorrection = RbSettings::value(RbSettings::UseTtsCorrections).toBool();
|
2009-06-26 20:40:51 +00:00
|
|
|
while (!in.atEnd())
|
|
|
|
{
|
|
|
|
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();
|
2012-04-26 18:42:18 +00:00
|
|
|
voice = voice.remove("<").remove(">");
|
2009-06-26 20:40:51 +00:00
|
|
|
voicefound=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(idfound && voicefound)
|
|
|
|
{
|
|
|
|
TalkGenerator::TalkEntry entry;
|
|
|
|
entry.toSpeak = voice;
|
|
|
|
entry.wavfilename = m_path + "/" + id + ".wav";
|
2011-10-01 19:48:58 +00:00
|
|
|
//voicefont wants them with .mp3 extension
|
|
|
|
entry.talkfilename = m_path + "/" + id + ".mp3";
|
2009-06-26 20:40:51 +00:00
|
|
|
entry.voiced = false;
|
|
|
|
entry.encoded = false;
|
|
|
|
if(id == "VOICE_PAUSE")
|
|
|
|
{
|
|
|
|
QFile::copy(":/builtin/VOICE_PAUSE.wav",m_path + "/VOICE_PAUSE.wav");
|
|
|
|
entry.wavfilename = m_path + "/VOICE_PAUSE.wav";
|
|
|
|
entry.voiced = true;
|
2012-04-06 13:55:48 +00:00
|
|
|
m_talkList.append(entry);
|
|
|
|
}
|
|
|
|
else if(entry.toSpeak.isEmpty()) {
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_WARNING() << "Empty voice string for ID" << id;
|
2012-04-06 13:55:48 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_talkList.append(entry);
|
2009-06-26 20:40:51 +00:00
|
|
|
}
|
|
|
|
idfound=false;
|
|
|
|
voicefound=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
genlang.close();
|
|
|
|
|
|
|
|
// check for empty list
|
|
|
|
if(m_talkList.size() == 0)
|
|
|
|
{
|
|
|
|
emit logItem(tr("The downloaded file was empty!"),LOGERROR);
|
|
|
|
emit done(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate files
|
|
|
|
{
|
|
|
|
TalkGenerator generator(this);
|
2011-10-01 19:48:58 +00:00
|
|
|
// set language for string correction. If not set no correction will be made.
|
|
|
|
if(useCorrection)
|
|
|
|
generator.setLang(m_lang);
|
2020-12-16 16:34:28 +00:00
|
|
|
connect(&generator, &TalkGenerator::done, this, &VoiceFileCreator::done);
|
|
|
|
connect(&generator, &TalkGenerator::logItem, this, &VoiceFileCreator::logItem);
|
|
|
|
connect(&generator, &TalkGenerator::logProgress, this, &VoiceFileCreator::logProgress);
|
|
|
|
connect(this, &VoiceFileCreator::aborted, &generator, &TalkGenerator::abort);
|
2012-06-09 21:39:57 +00:00
|
|
|
|
2012-01-15 11:24:54 +00:00
|
|
|
if(generator.process(&m_talkList, m_wavtrimThreshold) == TalkGenerator::eERROR)
|
2009-06-26 20:40:51 +00:00
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
emit logProgress(0,1);
|
|
|
|
emit done(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-06-09 21:39:57 +00:00
|
|
|
|
2009-06-26 20:40:51 +00:00
|
|
|
//make voicefile
|
|
|
|
emit logItem(tr("Creating voicefiles..."),LOGINFO);
|
2012-05-25 21:01:45 +00:00
|
|
|
FILE* ids2 = fopen(m_filename.toLocal8Bit(), "r");
|
2020-11-19 19:36:57 +00:00
|
|
|
if (ids2 == nullptr)
|
2009-06-26 20:40:51 +00:00
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
emit logItem(tr("Error opening downloaded file"),LOGERROR);
|
|
|
|
emit done(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-01 22:06:52 +00:00
|
|
|
FILE* output = fopen(QString(m_mountpoint + "/.rockbox/langs/" + m_lang
|
|
|
|
+ ".voice").toLocal8Bit(), "wb");
|
2020-11-19 19:36:57 +00:00
|
|
|
if (output == nullptr)
|
2009-06-26 20:40:51 +00:00
|
|
|
{
|
|
|
|
cleanup();
|
2010-04-01 22:06:52 +00:00
|
|
|
fclose(ids2);
|
2009-06-26 20:40:51 +00:00
|
|
|
emit logItem(tr("Error opening output file"),LOGERROR);
|
|
|
|
emit done(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Running voicefont, format" << m_voiceformat;
|
2012-06-03 14:13:04 +00:00
|
|
|
voicefont(ids2,m_targetid,m_path.toLocal8Bit().data(), output, m_voiceformat);
|
2010-04-01 22:06:52 +00:00
|
|
|
// ids2 and output are closed by voicefont().
|
2009-06-26 20:40:51 +00:00
|
|
|
|
|
|
|
//cleanup
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
// Add Voice file to the install log
|
2020-11-19 19:36:57 +00:00
|
|
|
QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, nullptr);
|
2012-05-23 20:48:09 +00:00
|
|
|
installlog.beginGroup(QString("Voice (self created, %1)").arg(m_lang));
|
|
|
|
installlog.setValue("/.rockbox/langs/" + m_lang + ".voice", m_versionstring);
|
2009-06-26 20:40:51 +00:00
|
|
|
installlog.endGroup();
|
|
|
|
installlog.sync();
|
|
|
|
|
|
|
|
emit logProgress(1,1);
|
|
|
|
emit logItem(tr("successfully created."),LOGOK);
|
2012-05-23 20:48:09 +00:00
|
|
|
|
2009-06-26 20:40:51 +00:00
|
|
|
emit done(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Cleans up Files potentially left in the temp dir
|
|
|
|
//!
|
|
|
|
void VoiceFileCreator::cleanup()
|
|
|
|
{
|
2009-09-25 15:51:17 +00:00
|
|
|
emit logItem(tr("Cleaning up..."),LOGINFO);
|
2009-06-26 20:40:51 +00:00
|
|
|
|
|
|
|
for(int i=0; i < m_talkList.size(); i++)
|
|
|
|
{
|
|
|
|
if(QFile::exists(m_talkList[i].wavfilename))
|
|
|
|
QFile::remove(m_talkList[i].wavfilename);
|
|
|
|
if(QFile::exists(m_talkList[i].talkfilename))
|
|
|
|
QFile::remove(m_talkList[i].talkfilename);
|
|
|
|
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
}
|
|
|
|
emit logItem(tr("Finished"),LOGINFO);
|
2012-01-08 11:50:04 +00:00
|
|
|
|
2009-06-26 20:40:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|