1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Wenger
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "voicefile.h"
22 #include "rockboxinfo.h"
23 #include "rbsettings.h"
24 #include "systeminfo.h"
26 VoiceFileCreator::VoiceFileCreator(QObject
* parent
) :QObject(parent
)
28 m_wavtrimThreshold
=500;
31 void VoiceFileCreator::abort()
37 bool VoiceFileCreator::createVoiceFile()
41 emit
logItem(tr("Starting Voicefile generation"),LOGINFO
);
43 // test if tempdir exists
44 if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
46 QDir(QDir::tempPath()).mkdir("rbvoice");
48 m_path
= QDir::tempPath() + "/rbvoice/";
50 // read rockbox-info.txt
51 RockboxInfo
info(m_mountpoint
);
54 emit
logItem(tr("could not find rockbox-info.txt"),LOGERROR
);
59 QString target
= info
.target();
60 QString features
= info
.features();
61 QString version
= info
.version();
62 m_targetid
= info
.targetID().toInt();
63 version
= version
.left(version
.indexOf("-")).remove("r");
65 //prepare download url
66 QUrl genlangUrl
= SystemInfo::value(SystemInfo::GenlangUrl
).toString()
67 +"?lang=" + m_lang
+ "&t=" + target
+ "&rev=" + version
+ "&f=" + features
;
69 qDebug() << "downloading " << genlangUrl
;
71 //download the correct genlang output
72 QTemporaryFile
*downloadFile
= new QTemporaryFile(this);
74 filename
= downloadFile
->fileName();
75 downloadFile
->close();
77 getter
= new HttpGet(this);
78 getter
->setFile(downloadFile
);
80 connect(getter
, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
81 connect(getter
, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(logProgress(int, int)));
82 connect(this, SIGNAL(aborted()), getter
, SLOT(abort()));
83 emit
logItem(tr("Downloading voice info..."),LOGINFO
);
84 getter
->getFile(genlangUrl
);
89 void VoiceFileCreator::downloadDone(bool error
)
91 qDebug() << "Voice creator::downloadDone, error:" << error
;
93 // update progress bar
94 emit
logProgress(1,1);
95 if(getter
->httpResponse() != 200 && !getter
->isCached()) {
96 emit
logItem(tr("Download error: received HTTP error %1.").arg(getter
->httpResponse()),LOGERROR
);
101 if(getter
->isCached())
102 emit
logItem(tr("Cached file used."), LOGINFO
);
105 emit
logItem(tr("Download error: %1").arg(getter
->errorString()),LOGERROR
);
110 emit
logItem(tr("Download finished."),LOGINFO
);
112 QCoreApplication::processEvents();
114 //open downloaded file
115 QFile
genlang(filename
);
116 if(!genlang
.open(QIODevice::ReadOnly
))
118 emit
logItem(tr("failed to open downloaded file"),LOGERROR
);
123 QCoreApplication::processEvents();
125 //read in downloaded file
126 emit
logItem(tr("Reading strings..."),LOGINFO
);
127 QTextStream
in(&genlang
);
128 in
.setCodec("UTF-8");
130 bool idfound
= false;
131 bool voicefound
=false;
134 QString line
= in
.readLine();
135 if(line
.contains("id:")) //ID found
137 id
= line
.remove("id:").remove('"').trimmed();
140 else if(line
.contains("voice:")) // voice found
142 voice
= line
.remove("voice:").remove('"').trimmed();
146 if(idfound
&& voicefound
)
148 TalkGenerator::TalkEntry entry
;
149 entry
.toSpeak
= voice
;
150 entry
.wavfilename
= m_path
+ "/" + id
+ ".wav";
151 entry
.talkfilename
= m_path
+ "/" + id
+ ".mp3"; //voicefont wants them with .mp3 extension
152 entry
.voiced
= false;
153 entry
.encoded
= false;
154 if(id
== "VOICE_PAUSE")
156 QFile::copy(":/builtin/VOICE_PAUSE.wav",m_path
+ "/VOICE_PAUSE.wav");
157 entry
.wavfilename
= m_path
+ "/VOICE_PAUSE.wav";
160 m_talkList
.append(entry
);
167 // check for empty list
168 if(m_talkList
.size() == 0)
170 emit
logItem(tr("The downloaded file was empty!"),LOGERROR
);
177 TalkGenerator
generator(this);
178 connect(&generator
,SIGNAL(done(bool)),this,SIGNAL(done(bool)));
179 connect(&generator
,SIGNAL(logItem(QString
,int)),this,SIGNAL(logItem(QString
,int)));
180 connect(&generator
,SIGNAL(logProgress(int,int)),this,SIGNAL(logProgress(int,int)));
181 connect(this,SIGNAL(aborted()),&generator
,SLOT(abort()));
183 if(generator
.process(&m_talkList
) == TalkGenerator::eERROR
)
186 emit
logProgress(0,1);
193 emit
logItem(tr("Creating voicefiles..."),LOGINFO
);
194 FILE* ids2
= fopen(filename
.toLocal8Bit(), "r");
198 emit
logItem(tr("Error opening downloaded file"),LOGERROR
);
203 FILE* output
= fopen(QString(m_mountpoint
+ "/.rockbox/langs/" + m_lang
204 + ".voice").toLocal8Bit(), "wb");
209 emit
logItem(tr("Error opening output file"),LOGERROR
);
214 voicefont(ids2
,m_targetid
,m_path
.toLocal8Bit().data(), output
);
215 // ids2 and output are closed by voicefont().
220 // Add Voice file to the install log
221 QSettings
installlog(m_mountpoint
+ "/.rockbox/rbutil.log", QSettings::IniFormat
, 0);
222 installlog
.beginGroup("selfcreated Voice");
223 installlog
.setValue("/.rockbox/langs/" + m_lang
+ ".voice",
224 QDate::currentDate().toString("yyyyMMdd"));
225 installlog
.endGroup();
228 emit
logProgress(1,1);
229 emit
logItem(tr("successfully created."),LOGOK
);
234 //! \brief Cleans up Files potentially left in the temp dir
236 void VoiceFileCreator::cleanup()
238 emit
logItem(tr("Cleaning up..."),LOGINFO
);
240 for(int i
=0; i
< m_talkList
.size(); i
++)
242 if(QFile::exists(m_talkList
[i
].wavfilename
))
243 QFile::remove(m_talkList
[i
].wavfilename
);
244 if(QFile::exists(m_talkList
[i
].talkfilename
))
245 QFile::remove(m_talkList
[i
].talkfilename
);
247 QCoreApplication::processEvents();
249 emit
logItem(tr("Finished"),LOGINFO
);