1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id: voicefile.h 15932 2007-12-15 13:13:57Z domonoky $
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 #define STATE_INVALID 0
23 #define STATE_PHRASE 1
27 VoiceFileCreator::VoiceFileCreator(QObject
* parent
) :QObject(parent
)
29 m_wavtrimThreshold
=500;
32 void VoiceFileCreator::abort()
37 bool VoiceFileCreator::createVoiceFile(ProgressloggerInterface
* logger
)
41 m_logger
->addItem(tr("Starting Voicefile generation"),LOGINFO
);
43 // test if tempdir exists
44 if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
46 QDir(QDir::tempPath()).mkdir("rbvoice");
49 m_path
= QDir::tempPath() + "/rbvoice/";
51 // read rockbox-info.txt
52 QFile
info(m_mountpoint
+"/.rockbox/rockbox-info.txt");
53 if(!info
.open(QIODevice::ReadOnly
))
55 m_logger
->addItem(tr("failed to open rockbox-info.txt"),LOGERROR
);
59 QString target
, features
,version
;
60 while (!info
.atEnd()) {
61 QString line
= info
.readLine();
63 if(line
.contains("Target:"))
65 target
= line
.remove("Target:").trimmed();
67 else if(line
.contains("Features:"))
69 features
= line
.remove("Features:").trimmed();
71 else if(line
.contains("Version:"))
73 version
= line
.remove("Version:").trimmed();
74 version
= version
.left(version
.indexOf("-")).remove(0,1);
79 //prepare download url
80 QUrl genlangUrl
= deviceSettings
->value("genlang_url").toString() +"?lang=" +m_lang
+"&t="+target
+"&rev="+version
+"&f="+features
;
82 qDebug() << "downloading " << genlangUrl
;
84 //download the correct genlang output
85 QTemporaryFile
*downloadFile
= new QTemporaryFile(this);
87 filename
= downloadFile
->fileName();
88 downloadFile
->close();
90 getter
= new HttpGet(this);
91 getter
->setProxy(m_proxy
);
92 getter
->setFile(downloadFile
);
93 getter
->getFile(genlangUrl
);
95 connect(getter
, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
96 connect(getter
, SIGNAL(downloadDone(int, bool)), this, SLOT(downloadRequestFinished(int, bool)));
97 connect(getter
, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
98 connect(m_logger
, SIGNAL(aborted()), getter
, SLOT(abort()));
103 void VoiceFileCreator::downloadRequestFinished(int id
, bool error
)
105 qDebug() << "Install::downloadRequestFinished" << id
<< error
;
106 qDebug() << "error:" << getter
->errorString();
112 void VoiceFileCreator::downloadDone(bool error
)
114 qDebug() << "Voice creator::downloadDone, error:" << error
;
116 // update progress bar
117 int max
= m_logger
->getProgressMax();
120 m_logger
->setProgressMax(max
);
122 m_logger
->setProgressValue(max
);
123 if(getter
->httpResponse() != 200 && !getter
->isCached()) {
124 m_logger
->addItem(tr("Download error: received HTTP error %1.").arg(getter
->httpResponse()),LOGERROR
);
128 if(getter
->isCached()) m_logger
->addItem(tr("Cached file used."), LOGINFO
);
130 m_logger
->addItem(tr("Download error: %1").arg(getter
->errorString()),LOGERROR
);
134 else m_logger
->addItem(tr("Download finished."),LOGOK
);
135 QApplication::processEvents();
138 m_logger
->setProgressMax(0);
139 //open downloaded file
140 QFile
genlang(filename
);
141 if(!genlang
.open(QIODevice::ReadOnly
))
143 m_logger
->addItem(tr("failed to open downloaded file"),LOGERROR
);
149 m_tts
= getTTS(userSettings
->value("tts").toString());
150 m_tts
->setCfg(userSettings
,deviceSettings
);
153 if(!m_tts
->start(&errStr
))
155 m_logger
->addItem(errStr
,LOGERROR
);
156 m_logger
->addItem(tr("Init of TTS engine failed"),LOGERROR
);
162 m_enc
= getEncoder(userSettings
->value("encoder").toString());
163 m_enc
->setUserCfg(userSettings
);
167 m_logger
->addItem(tr("Init of Encoder engine failed"),LOGERROR
);
173 QApplication::processEvents();
174 connect(m_logger
,SIGNAL(aborted()),this,SLOT(abort()));
176 //read in downloaded file
177 QList
<QPair
<QString
,QString
> > voicepairs
;
178 QTextStream
in(&genlang
);
179 in
.setCodec("UTF-8");
181 bool idfound
= false;
182 bool voicefound
=false;
185 QString line
= in
.readLine();
186 if(line
.contains("id:")) //ID found
188 id
= line
.remove("id:").remove('"').trimmed();
191 else if(line
.contains("voice:")) // voice found
193 voice
= line
.remove("voice:").remove('"').trimmed();
197 if(idfound
&& voicefound
)
199 voicepairs
.append(QPair
<QString
,QString
>(id
,voice
));
206 // check for empty list
207 if(voicepairs
.size() == 0)
209 m_logger
->addItem(tr("The downloaded file was empty!"),LOGERROR
);
215 m_logger
->setProgressMax(voicepairs
.size());
216 m_logger
->setProgressValue(0);
218 // create voice clips
219 QStringList mp3files
;
220 for(int i
=0; i
< voicepairs
.size(); i
++)
224 m_logger
->addItem("aborted.",LOGERROR
);
230 m_logger
->setProgressValue(i
);
232 QString wavname
= m_path
+ "/" + voicepairs
.at(i
).first
+ ".wav";
233 QString toSpeak
= voicepairs
.at(i
).second
;
234 QString encodedname
= m_path
+ "/" + voicepairs
.at(i
).first
+".mp3";
237 if(voicepairs
.at(i
).first
== "VOICE_PAUSE")
239 QFile::copy(":/builtin/builtin/VOICE_PAUSE.wav",m_path
+ "/VOICE_PAUSE.wav");
244 if(toSpeak
== "") continue;
246 m_logger
->addItem(tr("creating ")+toSpeak
,LOGINFO
);
247 QApplication::processEvents();
248 m_tts
->voice(toSpeak
,wavname
); // generate wav
254 wavtrim((char*)qPrintable(wavname
),m_wavtrimThreshold
,buffer
,255);
257 m_enc
->encode(wavname
,encodedname
);
258 // remove the wav file
259 QFile::remove(wavname
);
260 // remember the mp3 file for later removing
261 mp3files
<< encodedname
;
266 FILE* ids2
= fopen(filename
.toUtf8(), "r");
269 m_logger
->addItem(tr("Error opening downloaded file"),LOGERROR
);
274 FILE* output
= fopen(QString(m_mountpoint
+ "/.rockbox/langs/" + m_lang
+ ".voice").toUtf8(), "wb");
277 m_logger
->addItem(tr("Error opening output file"),LOGERROR
);
281 voicefont(ids2
,m_targetid
,(char*)(const char*)m_path
.toUtf8(), output
);
284 for(int i
=0;i
< mp3files
.size(); i
++)
286 QFile::remove(mp3files
.at(i
));
289 m_logger
->setProgressMax(100);
290 m_logger
->setProgressValue(100);
291 m_logger
->addItem(tr("successfully created."),LOGOK
);
295 void VoiceFileCreator::updateDataReadProgress(int read
, int total
)
297 m_logger
->setProgressMax(total
);
298 m_logger
->setProgressValue(read
);
299 //qDebug() << "progress:" << read << "/" << total;