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 "rbsettings.h"
24 VoiceFileCreator::VoiceFileCreator(QObject
* parent
) :QObject(parent
)
26 m_wavtrimThreshold
=500;
29 void VoiceFileCreator::abort()
35 bool VoiceFileCreator::createVoiceFile()
39 emit
logItem(tr("Starting Voicefile generation"),LOGINFO
);
41 // test if tempdir exists
42 if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
44 QDir(QDir::tempPath()).mkdir("rbvoice");
46 m_path
= QDir::tempPath() + "/rbvoice/";
48 // read rockbox-info.txt
49 RockboxInfo
info(m_mountpoint
);
52 emit
logItem(tr("could not find rockbox-info.txt"),LOGERROR
);
57 QString target
= info
.target();
58 QString features
= info
.features();
59 QString version
= info
.version();
60 version
= version
.left(version
.indexOf("-")).remove(0,1);
62 //prepare download url
63 QUrl genlangUrl
= RbSettings::value(RbSettings::GenlangUrl
).toString()
64 +"?lang=" + m_lang
+ "&t=" + target
+ "&rev=" + version
+ "&f=" + features
;
66 qDebug() << "downloading " << genlangUrl
;
68 //download the correct genlang output
69 QTemporaryFile
*downloadFile
= new QTemporaryFile(this);
71 filename
= downloadFile
->fileName();
72 downloadFile
->close();
74 getter
= new HttpGet(this);
75 getter
->setFile(downloadFile
);
77 connect(getter
, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
78 connect(getter
, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(logProgress(int, int)));
79 connect(this, SIGNAL(aborted()), getter
, SLOT(abort()));
80 emit
logItem(tr("Downloading voice info.."),LOGINFO
);
81 getter
->getFile(genlangUrl
);
86 void VoiceFileCreator::downloadDone(bool error
)
88 qDebug() << "Voice creator::downloadDone, error:" << error
;
90 // update progress bar
91 emit
logProgress(1,1);
92 if(getter
->httpResponse() != 200 && !getter
->isCached()) {
93 emit
logItem(tr("Download error: received HTTP error %1.").arg(getter
->httpResponse()),LOGERROR
);
98 if(getter
->isCached())
99 emit
logItem(tr("Cached file used."), LOGINFO
);
102 emit
logItem(tr("Download error: %1").arg(getter
->errorString()),LOGERROR
);
107 emit
logItem(tr("Download finished."),LOGINFO
);
109 QCoreApplication::processEvents();
111 //open downloaded file
112 QFile
genlang(filename
);
113 if(!genlang
.open(QIODevice::ReadOnly
))
115 emit
logItem(tr("failed to open downloaded file"),LOGERROR
);
120 QCoreApplication::processEvents();
122 //read in downloaded file
123 emit
logItem(tr("Reading strings..."),LOGINFO
);
124 QTextStream
in(&genlang
);
125 in
.setCodec("UTF-8");
127 bool idfound
= false;
128 bool voicefound
=false;
131 QString line
= in
.readLine();
132 if(line
.contains("id:")) //ID found
134 id
= line
.remove("id:").remove('"').trimmed();
137 else if(line
.contains("voice:")) // voice found
139 voice
= line
.remove("voice:").remove('"').trimmed();
143 if(idfound
&& voicefound
)
145 TalkGenerator::TalkEntry entry
;
146 entry
.toSpeak
= voice
;
147 entry
.wavfilename
= m_path
+ "/" + id
+ ".wav";
148 entry
.talkfilename
= m_path
+ "/" + id
+ ".mp3"; //voicefont wants them with .mp3 extension
149 entry
.voiced
= false;
150 entry
.encoded
= false;
151 if(id
== "VOICE_PAUSE")
153 QFile::copy(":/builtin/VOICE_PAUSE.wav",m_path
+ "/VOICE_PAUSE.wav");
154 entry
.wavfilename
= m_path
+ "/VOICE_PAUSE.wav";
157 m_talkList
.append(entry
);
164 // check for empty list
165 if(m_talkList
.size() == 0)
167 emit
logItem(tr("The downloaded file was empty!"),LOGERROR
);
174 TalkGenerator
generator(this);
175 connect(&generator
,SIGNAL(done(bool)),this,SIGNAL(done(bool)));
176 connect(&generator
,SIGNAL(logItem(QString
,int)),this,SIGNAL(logItem(QString
,int)));
177 connect(&generator
,SIGNAL(logProgress(int,int)),this,SIGNAL(logProgress(int,int)));
178 connect(this,SIGNAL(aborted()),&generator
,SLOT(abort()));
180 if(generator
.process(&m_talkList
) == TalkGenerator::eERROR
)
183 emit
logProgress(0,1);
190 emit
logItem(tr("Creating voicefiles..."),LOGINFO
);
191 FILE* ids2
= fopen(filename
.toLocal8Bit(), "r");
195 emit
logItem(tr("Error opening downloaded file"),LOGERROR
);
200 FILE* output
= fopen(QString(m_mountpoint
+ "/.rockbox/langs/" + m_lang
+ ".voice").toLocal8Bit(), "wb");
204 emit
logItem(tr("Error opening output file"),LOGERROR
);
209 voicefont(ids2
,m_targetid
,m_path
.toLocal8Bit().data(), output
);
214 // Add Voice file to the install log
215 QSettings
installlog(m_mountpoint
+ "/.rockbox/rbutil.log", QSettings::IniFormat
, 0);
216 installlog
.beginGroup("selfcreated Voice");
217 installlog
.setValue("/.rockbox/langs/" + m_lang
+ ".voice",QDate::currentDate().toString("yyyyMMdd"));
218 installlog
.endGroup();
221 emit
logProgress(1,1);
222 emit
logItem(tr("successfully created."),LOGOK
);
227 //! \brief Cleans up Files potentially left in the temp dir
229 void VoiceFileCreator::cleanup()
231 emit
logItem(tr("Cleaning up.."),LOGINFO
);
233 for(int i
=0; i
< m_talkList
.size(); i
++)
235 if(QFile::exists(m_talkList
[i
].wavfilename
))
236 QFile::remove(m_talkList
[i
].wavfilename
);
237 if(QFile::exists(m_talkList
[i
].talkfilename
))
238 QFile::remove(m_talkList
[i
].talkfilename
);
240 QCoreApplication::processEvents();
242 emit
logItem(tr("Finished"),LOGINFO
);