summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/voicefile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/voicefile.cpp')
-rw-r--r--rbutil/rbutilqt/voicefile.cpp301
1 files changed, 0 insertions, 301 deletions
diff --git a/rbutil/rbutilqt/voicefile.cpp b/rbutil/rbutilqt/voicefile.cpp
deleted file mode 100644
index 4de9b87f34..0000000000
--- a/rbutil/rbutilqt/voicefile.cpp
+++ /dev/null
@@ -1,301 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include "voicefile.h"
21#include "utils.h"
22#include "rbsettings.h"
23
24#define STATE_INVALID 0
25#define STATE_PHRASE 1
26#define STATE_VOICE 2
27
28
29VoiceFileCreator::VoiceFileCreator(QObject* parent) :QObject(parent)
30{
31 m_wavtrimThreshold=500;
32}
33
34void VoiceFileCreator::abort()
35{
36 m_abort = true;
37}
38
39bool VoiceFileCreator::createVoiceFile(ProgressloggerInterface* logger)
40{
41 m_abort = false;
42 m_logger = logger;
43 m_logger->addItem(tr("Starting Voicefile generation"),LOGINFO);
44
45 // test if tempdir exists
46 if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
47 {
48 QDir(QDir::tempPath()).mkdir("rbvoice");
49 }
50
51 m_path = QDir::tempPath() + "/rbvoice/";
52
53 // read rockbox-info.txt
54 RockboxInfo info(m_mountpoint);
55 if(!info.open())
56 {
57 m_logger->addItem(tr("could not find rockbox-info.txt"),LOGERROR);
58 m_logger->setFinished();
59 emit done(false);
60 return false;
61 }
62
63 QString target = info.target();
64 QString features = info.features();
65 QString version = info.version();
66 version = version.left(version.indexOf("-")).remove(0,1);
67
68 //prepare download url
69 QUrl genlangUrl = RbSettings::value(RbSettings::GenlangUrl).toString()
70 +"?lang=" + m_lang + "&t=" + target + "&rev=" + version + "&f=" + features;
71
72 qDebug() << "downloading " << genlangUrl;
73
74 //download the correct genlang output
75 QTemporaryFile *downloadFile = new QTemporaryFile(this);
76 downloadFile->open();
77 filename = downloadFile->fileName();
78 downloadFile->close();
79 // get the real file.
80 getter = new HttpGet(this);
81 getter->setFile(downloadFile);
82
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()));
86
87 getter->getFile(genlangUrl);
88 return true;
89 }
90
91
92void VoiceFileCreator::downloadDone(bool error)
93{
94 qDebug() << "Voice creator::downloadDone, error:" << error;
95
96 // update progress bar
97 int max = m_logger->getProgressMax();
98 if(max == 0) {
99 max = 100;
100 m_logger->setProgressMax(max);
101 }
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();
106 emit done(false);
107 return;
108 }
109 if(getter->isCached()) m_logger->addItem(tr("Cached file used."), LOGINFO);
110 if(error) {
111 m_logger->addItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
112 m_logger->setFinished();
113 emit done(false);
114 return;
115 }
116 else m_logger->addItem(tr("Download finished."),LOGOK);
117 QCoreApplication::processEvents();
118
119
120 m_logger->setProgressMax(0);
121 //open downloaded file
122 QFile genlang(filename);
123 if(!genlang.open(QIODevice::ReadOnly))
124 {
125 m_logger->addItem(tr("failed to open downloaded file"),LOGERROR);
126 m_logger->setFinished();
127 emit done(false);
128 return;
129 }
130
131 //tts
132 m_tts = TTSBase::getTTS(this,RbSettings::value(RbSettings::Tts).toString());
133
134 QString errStr;
135 if(!m_tts->start(&errStr))
136 {
137 m_logger->addItem(errStr,LOGERROR);
138 m_logger->addItem(tr("Init of TTS engine failed"),LOGERROR);
139 m_logger->setFinished();
140 emit done(false);
141 return;
142 }
143
144 // Encoder
145 m_enc = EncBase::getEncoder(this,RbSettings::value(RbSettings::CurEncoder).toString());
146
147 if(!m_enc->start())
148 {
149 m_logger->addItem(tr("Init of Encoder engine failed"),LOGERROR);
150 m_tts->stop();
151 m_logger->setFinished();
152 emit done(false);
153 return;
154 }
155
156 QCoreApplication::processEvents();
157 connect(m_logger,SIGNAL(aborted()),this,SLOT(abort()));
158
159 //read in downloaded file
160 QList<QPair<QString,QString> > voicepairs;
161 QTextStream in(&genlang);
162 in.setCodec("UTF-8");
163 QString id, voice;
164 bool idfound = false;
165 bool voicefound=false;
166 while (!in.atEnd())
167 {
168 QString line = in.readLine();
169 if(line.contains("id:")) //ID found
170 {
171 id = line.remove("id:").remove('"').trimmed();
172 idfound = true;
173 }
174 else if(line.contains("voice:")) // voice found
175 {
176 voice = line.remove("voice:").remove('"').trimmed();
177 voicefound=true;
178 }
179
180 if(idfound && voicefound)
181 {
182 voicepairs.append(QPair<QString,QString>(id,voice));
183 idfound=false;
184 voicefound=false;
185 }
186 }
187 genlang.close();
188
189 // check for empty list
190 if(voicepairs.size() == 0)
191 {
192 m_logger->addItem(tr("The downloaded file was empty!"),LOGERROR);
193 m_logger->setFinished();
194 m_tts->stop();
195 emit done(false);
196 return;
197 }
198
199 m_logger->setProgressMax(voicepairs.size());
200 m_logger->setProgressValue(0);
201
202 // create voice clips
203 QStringList mp3files;
204 for(int i=0; i< voicepairs.size(); i++)
205 {
206 if(m_abort)
207 {
208 m_logger->addItem("aborted.",LOGERROR);
209 m_logger->setFinished();
210 m_tts->stop();
211 emit done(false);
212 return;
213 }
214
215 m_logger->setProgressValue(i);
216
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";
220
221 // todo PAUSE
222 if(voicepairs.at(i).first == "VOICE_PAUSE")
223 {
224 QFile::copy(":/builtin/builtin/VOICE_PAUSE.wav",m_path + "/VOICE_PAUSE.wav");
225
226 }
227 else
228 {
229 if(toSpeak == "") continue;
230
231 m_logger->addItem(tr("creating ")+toSpeak,LOGINFO);
232 QCoreApplication::processEvents();
233
234 // TODO: add support for aborting the operation
235 QString errStr;
236 m_tts->voice(toSpeak,wavname, &errStr); // generate wav
237 }
238
239 // todo strip
240 char buffer[255];
241
242 wavtrim((char*)qPrintable(wavname),m_wavtrimThreshold,buffer,255);
243
244 // encode wav
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;
250 }
251
252
253 //make voicefile
254 FILE* ids2 = fopen(filename.toUtf8(), "r");
255 if (ids2 == NULL)
256 {
257 m_logger->addItem(tr("Error opening downloaded file"),LOGERROR);
258 m_logger->setFinished();
259 emit done(false);
260 return;
261 }
262
263 FILE* output = fopen(QString(m_mountpoint + "/.rockbox/langs/" + m_lang + ".voice").toUtf8(), "wb");
264 if (output == NULL)
265 {
266 m_logger->addItem(tr("Error opening output file"),LOGERROR);
267 emit done(false);
268 return;
269 }
270
271 voicefont(ids2,m_targetid,(char*)(const char*)m_path.toUtf8(), output);
272
273 //remove .mp3 files
274 for(int i=0;i< mp3files.size(); i++)
275 {
276 QFile::remove(mp3files.at(i));
277 }
278
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();
284 installlog.sync();
285
286 m_logger->setProgressMax(100);
287 m_logger->setProgressValue(100);
288 m_logger->addItem(tr("successfully created."),LOGOK);
289 m_logger->setFinished();
290
291 emit done(true);
292}
293
294void VoiceFileCreator::updateDataReadProgress(int read, int total)
295{
296 m_logger->setProgressMax(total);
297 m_logger->setProgressValue(read);
298 //qDebug() << "progress:" << read << "/" << total;
299
300}
301