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 #define STATE_INVALID 0
25 #define STATE_PHRASE 1
29 VoiceFileCreator::VoiceFileCreator(QObject
* parent
) :QObject(parent
)
31 m_wavtrimThreshold
=500;
34 void VoiceFileCreator::abort()
39 bool VoiceFileCreator::createVoiceFile(ProgressloggerInterface
* logger
)
43 m_logger
->addItem(tr("Starting Voicefile generation"),LOGINFO
);
45 // test if tempdir exists
46 if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
48 QDir(QDir::tempPath()).mkdir("rbvoice");
51 m_path
= QDir::tempPath() + "/rbvoice/";
53 // read rockbox-info.txt
54 RockboxInfo
info(m_mountpoint
);
57 m_logger
->addItem(tr("could not find rockbox-info.txt"),LOGERROR
);
58 m_logger
->setFinished();
63 QString target
= info
.target();
64 QString features
= info
.features();
65 QString version
= info
.version();
66 version
= version
.left(version
.indexOf("-")).remove(0,1);
68 //prepare download url
69 QUrl genlangUrl
= RbSettings::value(RbSettings::GenlangUrl
).toString()
70 +"?lang=" + m_lang
+ "&t=" + target
+ "&rev=" + version
+ "&f=" + features
;
72 qDebug() << "downloading " << genlangUrl
;
74 //download the correct genlang output
75 QTemporaryFile
*downloadFile
= new QTemporaryFile(this);
77 filename
= downloadFile
->fileName();
78 downloadFile
->close();
80 getter
= new HttpGet(this);
81 getter
->setFile(downloadFile
);
83 connect(getter
, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
84 connect(getter
, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
85 connect(m_logger
, SIGNAL(aborted()), getter
, SLOT(abort()));
87 getter
->getFile(genlangUrl
);
92 void VoiceFileCreator::downloadDone(bool error
)
94 qDebug() << "Voice creator::downloadDone, error:" << error
;
96 // update progress bar
97 int max
= m_logger
->getProgressMax();
100 m_logger
->setProgressMax(max
);
102 m_logger
->setProgressValue(max
);
103 if(getter
->httpResponse() != 200 && !getter
->isCached()) {
104 m_logger
->addItem(tr("Download error: received HTTP error %1.").arg(getter
->httpResponse()),LOGERROR
);
105 m_logger
->setFinished();
109 if(getter
->isCached()) m_logger
->addItem(tr("Cached file used."), LOGINFO
);
111 m_logger
->addItem(tr("Download error: %1").arg(getter
->errorString()),LOGERROR
);
112 m_logger
->setFinished();
116 else m_logger
->addItem(tr("Download finished."),LOGOK
);
117 QCoreApplication::processEvents();
120 m_logger
->setProgressMax(0);
121 //open downloaded file
122 QFile
genlang(filename
);
123 if(!genlang
.open(QIODevice::ReadOnly
))
125 m_logger
->addItem(tr("failed to open downloaded file"),LOGERROR
);
126 m_logger
->setFinished();
132 m_tts
= TTSBase::getTTS(this,RbSettings::value(RbSettings::Tts
).toString());
135 if(!m_tts
->start(&errStr
))
137 m_logger
->addItem(errStr
,LOGERROR
);
138 m_logger
->addItem(tr("Init of TTS engine failed"),LOGERROR
);
139 m_logger
->setFinished();
145 m_enc
= EncBase::getEncoder(this,RbSettings::value(RbSettings::CurEncoder
).toString());
149 m_logger
->addItem(tr("Init of Encoder engine failed"),LOGERROR
);
151 m_logger
->setFinished();
156 QCoreApplication::processEvents();
157 connect(m_logger
,SIGNAL(aborted()),this,SLOT(abort()));
159 //read in downloaded file
160 QList
<QPair
<QString
,QString
> > voicepairs
;
161 QTextStream
in(&genlang
);
162 in
.setCodec("UTF-8");
164 bool idfound
= false;
165 bool voicefound
=false;
168 QString line
= in
.readLine();
169 if(line
.contains("id:")) //ID found
171 id
= line
.remove("id:").remove('"').trimmed();
174 else if(line
.contains("voice:")) // voice found
176 voice
= line
.remove("voice:").remove('"').trimmed();
180 if(idfound
&& voicefound
)
182 voicepairs
.append(QPair
<QString
,QString
>(id
,voice
));
189 // check for empty list
190 if(voicepairs
.size() == 0)
192 m_logger
->addItem(tr("The downloaded file was empty!"),LOGERROR
);
193 m_logger
->setFinished();
199 m_logger
->setProgressMax(voicepairs
.size());
200 m_logger
->setProgressValue(0);
202 // create voice clips
203 QStringList mp3files
;
204 for(int i
=0; i
< voicepairs
.size(); i
++)
208 m_logger
->addItem("aborted.",LOGERROR
);
209 m_logger
->setFinished();
215 m_logger
->setProgressValue(i
);
217 QString wavname
= m_path
+ "/" + voicepairs
.at(i
).first
+ ".wav";
218 QString toSpeak
= voicepairs
.at(i
).second
;
219 QString encodedname
= m_path
+ "/" + voicepairs
.at(i
).first
+".mp3";
222 if(voicepairs
.at(i
).first
== "VOICE_PAUSE")
224 QFile::copy(":/builtin/builtin/VOICE_PAUSE.wav",m_path
+ "/VOICE_PAUSE.wav");
229 if(toSpeak
== "") continue;
231 m_logger
->addItem(tr("creating ")+toSpeak
,LOGINFO
);
232 QCoreApplication::processEvents();
234 // TODO: add support for aborting the operation
236 m_tts
->voice(toSpeak
,wavname
, &errStr
); // generate wav
242 wavtrim((char*)qPrintable(wavname
),m_wavtrimThreshold
,buffer
,255);
245 m_enc
->encode(wavname
,encodedname
);
246 // remove the wav file
247 QFile::remove(wavname
);
248 // remember the mp3 file for later removing
249 mp3files
<< encodedname
;
254 FILE* ids2
= fopen(filename
.toUtf8(), "r");
257 m_logger
->addItem(tr("Error opening downloaded file"),LOGERROR
);
258 m_logger
->setFinished();
263 FILE* output
= fopen(QString(m_mountpoint
+ "/.rockbox/langs/" + m_lang
+ ".voice").toUtf8(), "wb");
266 m_logger
->addItem(tr("Error opening output file"),LOGERROR
);
271 voicefont(ids2
,m_targetid
,(char*)(const char*)m_path
.toUtf8(), output
);
274 for(int i
=0;i
< mp3files
.size(); i
++)
276 QFile::remove(mp3files
.at(i
));
279 // Add Voice file to the install log
280 QSettings
installlog(m_mountpoint
+ "/.rockbox/rbutil.log", QSettings::IniFormat
, 0);
281 installlog
.beginGroup("selfcreated Voice");
282 installlog
.setValue("/.rockbox/langs/" + m_lang
+ ".voice",QDate::currentDate().toString("yyyyMMdd"));
283 installlog
.endGroup();
286 m_logger
->setProgressMax(100);
287 m_logger
->setProgressValue(100);
288 m_logger
->addItem(tr("successfully created."),LOGOK
);
289 m_logger
->setFinished();
294 void VoiceFileCreator::updateDataReadProgress(int read
, int total
)
296 m_logger
->setProgressMax(total
);
297 m_logger
->setProgressValue(read
);
298 //qDebug() << "progress:" << read << "/" << total;