summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Wenger <domonoky@googlemail.com>2008-07-12 17:04:49 +0000
committerDominik Wenger <domonoky@googlemail.com>2008-07-12 17:04:49 +0000
commita9f50c35e684af8b6ca6061838b5ffa61f231c84 (patch)
tree9ab1ef438713772c70595ccbf74f35ed9151e48a
parentae055017169f699f262606fd307e836d456d2535 (diff)
downloadrockbox-a9f50c35e684af8b6ca6061838b5ffa61f231c84.tar.gz
rockbox-a9f50c35e684af8b6ca6061838b5ffa61f231c84.zip
rbutil: completly reworked Talkfile generation. Should be more stable and more maintainable.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18016 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--rbutil/rbutilqt/talkfile.cpp433
-rw-r--r--rbutil/rbutilqt/talkfile.h15
2 files changed, 345 insertions, 103 deletions
diff --git a/rbutil/rbutilqt/talkfile.cpp b/rbutil/rbutilqt/talkfile.cpp
index 4aa86fa21a..0c826e82aa 100644
--- a/rbutil/rbutilqt/talkfile.cpp
+++ b/rbutil/rbutilqt/talkfile.cpp
@@ -24,17 +24,25 @@ TalkFileCreator::TalkFileCreator(QObject* parent): QObject(parent)
24 24
25} 25}
26 26
27//! \brief Creates Talkfiles.
28//!
29//! \param logger A pointer to a Loggerobject
27bool TalkFileCreator::createTalkFiles(ProgressloggerInterface* logger) 30bool TalkFileCreator::createTalkFiles(ProgressloggerInterface* logger)
28{ 31{
29 m_abort = false; 32 m_abort = false;
30 m_logger = logger; 33 m_logger = logger;
34
35 QMultiMap<QString,QString> fileList;
36 QMultiMap<QString,QString> dirList;
37 QStringList toSpeakList;
38 QString errStr;
39
31 m_logger->addItem(tr("Starting Talk file generation"),LOGINFO); 40 m_logger->addItem(tr("Starting Talk file generation"),LOGINFO);
32 41
33 //tts 42 //tts
34 m_tts = TTSBase::getTTS(settings->curTTS()); 43 m_tts = TTSBase::getTTS(settings->curTTS());
35 m_tts->setCfg(settings); 44 m_tts->setCfg(settings);
36 45
37 QString errStr;
38 if(!m_tts->start(&errStr)) 46 if(!m_tts->start(&errStr))
39 { 47 {
40 m_logger->addItem(errStr.trimmed(),LOGERROR); 48 m_logger->addItem(errStr.trimmed(),LOGERROR);
@@ -59,141 +67,368 @@ bool TalkFileCreator::createTalkFiles(ProgressloggerInterface* logger)
59 67
60 connect(logger,SIGNAL(aborted()),this,SLOT(abort())); 68 connect(logger,SIGNAL(aborted()),this,SLOT(abort()));
61 m_logger->setProgressMax(0); 69 m_logger->setProgressMax(0);
62 70
71 // read in Maps of paths - file/dirnames
72 m_logger->addItem(tr("Reading Filelist..."),LOGINFO);
73 if(createDirAndFileMaps(m_dir,&dirList,&fileList) == false)
74 {
75 m_logger->addItem(tr("Talk file creation aborted"),LOGERROR);
76 doAbort(toSpeakList);
77 return false;
78 }
79
80 // create List of all Files/Dirs to speak
81 QMapIterator<QString, QString> dirIt(dirList);
82 while (dirIt.hasNext())
83 {
84 dirIt.next();
85 // insert only non dublicate dir entrys into list
86 if(!toSpeakList.contains(dirIt.value()))
87 {
88 qDebug() << "toSpeaklist dir:" << dirIt.value();
89 toSpeakList.append(dirIt.value());
90 }
91 }
92 QMapIterator<QString, QString> fileIt(fileList);
93 while (fileIt.hasNext())
94 {
95 fileIt.next();
96 // insert only non- dublictae file entrys into list
97 if(!toSpeakList.contains(fileIt.value()))
98 {
99 if(m_stripExtensions)
100 toSpeakList.append(stripExtension(fileIt.value()));
101 else
102 toSpeakList.append(fileIt.value());
103 }
104 }
105
106 // Voice entrys
107 m_logger->addItem(tr("Voicing entrys..."),LOGINFO);
108 if(voiceList(toSpeakList,&errStr) == false)
109 {
110 m_logger->addItem(errStr,LOGERROR);
111 doAbort(toSpeakList);
112 return false;
113 }
114
115 // Encoding Entrys
116 m_logger->addItem(tr("Encoding files..."),LOGINFO);
117 if(encodeList(toSpeakList,&errStr) == false)
118 {
119 m_logger->addItem(errStr,LOGERROR);
120 doAbort(toSpeakList);
121 return false;
122 }
123
124 // Copying talk files
125 m_logger->addItem(tr("Copying Talkfile for Dirs..."),LOGINFO);
126 if(copyTalkDirFiles(dirList,&errStr) == false)
127 {
128 m_logger->addItem(errStr,LOGERROR);
129 doAbort(toSpeakList);
130 return false;
131 }
132
133 //Copying file talk files
134 m_logger->addItem(tr("Copying Talkfile for Files..."),LOGINFO);
135 if(copyTalkFileFiles(fileList,&errStr) == false)
136 {
137 m_logger->addItem(errStr,LOGERROR);
138 doAbort(toSpeakList);
139 return false;
140 }
141
142 // Deleting left overs
143 if( !cleanup(toSpeakList))
144 return false;
145
146 m_tts->stop();
147 m_enc->stop();
148 m_logger->addItem(tr("Finished creating Talk files"),LOGOK);
149 m_logger->setProgressMax(1);
150 m_logger->setProgressValue(1);
151 m_logger->abort();
152
153 return true;
154}
155
156//! \brief resets the internal progress counter, and sets the Progressbar in the Logger
157//!
158//! \param max The maximum to shich the Progressbar is set.
159void TalkFileCreator::resetProgress(int max)
160{
161 m_progress = 0;
162 m_logger->setProgressMax(max);
163 m_logger->setProgressValue(m_progress);
164}
165
166//! \brief Strips everything after and including the last dot in a string. If there is no dot, nothing is changed
167//!
168//! \param filename The filename from which to strip the Extension
169//! \returns the modified string
170QString TalkFileCreator::stripExtension(QString filename)
171{
172 if(filename.lastIndexOf(".") != -1)
173 return filename.left(filename.lastIndexOf("."));
174 else
175 return filename;
176}
177
178//! \brief Does needed Tasks when we need to abort. Cleans up Files. Stops the Logger, Stops TTS and Encoder
179//!
180//! \param cleanupList List of filenames to give to cleanup()
181void TalkFileCreator::doAbort(QStringList cleanupList)
182{
183 cleanup(cleanupList);
184 m_logger->setProgressMax(1);
185 m_logger->setProgressValue(0);
186 m_logger->abort();
187 m_tts->stop();
188 m_enc->stop();
189}
190
191//! \brief Creates MultiMaps (paths -> File/dir names) of all Dirs and Files in a Folder.
192//! Depending on settings, either Dirs or Files can be ignored.
193//! Also recursion is controlled by settings
194//!
195//! \param startDir The dir where it beginns scanning
196//! \param dirMap The MulitMap where the dirs are stored
197//! \param filMap The MultiMap where Files are stored
198//! \returns true on Success, false if User aborted.
199bool TalkFileCreator::createDirAndFileMaps(QDir startDir,QMultiMap<QString,QString> *dirMap,QMultiMap<QString,QString> *fileMap)
200{
201 // create Iterator
63 QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags; 202 QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags;
64 if(m_recursive) 203 if(m_recursive)
65 flags = QDirIterator::Subdirectories; 204 flags = QDirIterator::Subdirectories;
66 205
67 QDirIterator it(m_dir,flags); 206 QDirIterator it(startDir,flags);
68 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0); 207
69 installlog.beginGroup("talkfiles"); 208 // read in Maps of paths - file/dirnames
70 // iterate over all entrys
71 while (it.hasNext()) 209 while (it.hasNext())
72 { 210 {
211 it.next();
73 if(m_abort) 212 if(m_abort)
74 { 213 {
75 m_logger->addItem(tr("Talk file creation aborted"),LOGERROR);
76 m_logger->abort();
77 m_tts->stop();
78 m_enc->stop();
79 return false; 214 return false;
80 } 215 }
81 216
82 QCoreApplication::processEvents();
83 QFileInfo fileInf = it.fileInfo(); 217 QFileInfo fileInf = it.fileInfo();
84 QString toSpeak; 218
85 QString filename; 219 // its a dir
86 QString wavfilename; 220 if(fileInf.isDir())
87 QString filepath;
88
89 QString path = fileInf.filePath();
90 qDebug() << path;
91
92 if( path.endsWith("..") || path.endsWith(".") || path.endsWith(".talk") )
93 {
94 it.next();
95 continue;
96 }
97
98 //! if it is a dir
99 if(fileInf.isDir())
100 { 221 {
101 // skip entry if folder talking isnt enabled 222 QDir dir = fileInf.dir();
102 if(m_talkFolders == false) 223
224 // insert into List
225 if(!dir.dirName().isEmpty() && m_talkFolders)
103 { 226 {
104 it.next(); 227 qDebug() << "Dir: " << dir.dirName() << " - " << dir.path();
105 continue; 228 dirMap->insert(dir.path(),dir.dirName());
106 } 229 }
107
108 toSpeak = fileInf.fileName();
109
110 filepath = fileInf.filePath() + "/";
111 filename = "_dirname.talk";
112 qDebug() << "toSpeak: " << toSpeak << "filename: " << filename << " path: " <<filepath;
113 } 230 }
114 else // if it is a file 231 else // its a File
115 { 232 {
116 // skip entry if file talking isnt enabled 233 // insert into List
117 if(m_talkFiles == false) 234 if( !fileInf.fileName().isEmpty() && !fileInf.fileName().endsWith(".talk") && m_talkFiles)
118 { 235 {
119 it.next(); 236 qDebug() << "File: " << fileInf.fileName() << " - " << fileInf.path();
120 continue; 237 fileMap->insert(fileInf.path(),fileInf.fileName());
121 } 238 }
122 239 }
123 // create toSpeak string 240 QCoreApplication::processEvents();
124 if(m_stripExtensions) 241 }
125 toSpeak = fileInf.baseName(); 242 return true;
126 else 243}
127 toSpeak = fileInf.fileName(); 244
128 // create filename and path 245//! \brief Voices a List of string to the temp dir. Progress is handled inside.
129 filepath = fileInf.absolutePath(); 246//!
130 filename = fileInf.fileName() + ".talk"; 247//! \param toSpeak QStringList with the Entrys to voice.
131 248//! \param errString pointer to where the Error cause is written
249//! \returns true on success, false on error or user abort
250bool TalkFileCreator::voiceList(QStringList toSpeak,QString* errString)
251{
252 resetProgress(toSpeak.size());
253
254 for(int i=0; i < toSpeak.size(); i++)
255 {
256 if(m_abort)
257 {
258 *errString = tr("Talk file creation aborted");
259 return false;
132 } 260 }
133 261
134 wavfilename = QDir::tempPath()+ "/"+ filename + ".wav"; 262 QString filename = QDir::tempPath()+ "/"+ toSpeak[i] + ".wav";
263
264 if(!m_tts->voice(toSpeak[i],filename))
265 {
266 *errString =tr("Voicing of %s failed").arg(toSpeak[i]);
267 return false;
268 }
269 m_logger->setProgressValue(++m_progress);
270 QCoreApplication::processEvents();
271 }
272 return true;
273}
135 274
136 QFileInfo filenameInf(filepath+"/"+filename);
137 QFileInfo wavfilenameInf(wavfilename);
138 275
139 //! the actual generation of the .talk files 276//! \brief Encodes a List of strings from/to the temp dir. Progress is handled inside.
140 if(!filenameInf.exists() || m_overwriteTalk) 277//! It expects the inputfile in the temp dir with the name in the List appended with ".wav"
278//!
279//! \param toSpeak QStringList with the Entrys to encode.
280//! \param errString pointer to where the Error cause is written
281//! \returns true on success, false on error or user abort
282bool TalkFileCreator::encodeList(QStringList toEncode,QString* errString)
283{
284 resetProgress(toEncode.size());
285 for(int i=0; i < toEncode.size(); i++)
286 {
287 if(m_abort)
141 { 288 {
142 if(!wavfilenameInf.exists() || m_overwriteWav) 289 *errString = tr("Talk file creation aborted");
143 { 290 return false;
144 m_logger->addItem(tr("Voicing of %1").arg(toSpeak),LOGINFO); 291 }
145 if(!m_tts->voice(toSpeak,wavfilename)) 292
146 { 293 QString wavfilename = QDir::tempPath()+ "/"+ toEncode[i] + ".wav";
147 m_logger->addItem(tr("Voicing of %s failed").arg(toSpeak),LOGERROR); 294 QString filename = QDir::tempPath()+ "/"+ toEncode[i] + ".talk";
148 m_logger->abort(); 295
149 m_tts->stop(); 296 if(!m_enc->encode(wavfilename,filename))
150 m_enc->stop(); 297 {
151 298 *errString =tr("Encoding of %1 failed").arg(filename);
152 return false; 299 return false;
153 } 300 }
154 QCoreApplication::processEvents(); 301 m_logger->setProgressValue(++m_progress);
155 } 302 QCoreApplication::processEvents();
156 m_logger->addItem(tr("Encoding of %1").arg(toSpeak),LOGINFO); 303 }
157 if(!m_enc->encode(wavfilename,filepath+"/"+filename)) 304 return true;
158 { 305}
159 m_logger->addItem(tr("Encoding of %1 failed").arg(wavfilename),LOGERROR);
160 m_logger->abort();
161 m_tts->stop();
162 m_enc->stop();
163 306
164 return false; 307//! \brief copys Talkfile for Dirs from the temp dir to the target. Progress and installlog is handled inside
165 } 308//!
166 QCoreApplication::processEvents(); 309//! \param dirMap a MultiMap of Paths -> Dirnames
310//! \param errString Pointer to a QString where the error cause is written.
311//! \returns true on success, false on error or user abort
312bool TalkFileCreator::copyTalkDirFiles(QMultiMap<QString,QString> dirMap,QString* errString)
313{
314 resetProgress(dirMap.size());
315
316 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
317 installlog.beginGroup("talkfiles");
318
319 QMapIterator<QString, QString> it(dirMap);
320 while (it.hasNext())
321 {
322 it.next();
323 if(m_abort)
324 {
325 *errString = tr("Talk file creation aborted");
326 return false;
167 } 327 }
328
329 QString source = QDir::tempPath()+ "/"+ it.value() + ".talk";
330 QString target = it.key() + "/" + "_dirname.talk";
331
332 // remove target if it exists, and if we should overwrite it
333 if(m_overwriteTalk && QFile::exists(target))
334 QFile::remove(target);
168 335
169 //! remove the intermedia wav file, if requested 336 // copying
337 if(!QFile::copy(source,target))
338 {
339 *errString = tr("Copying of %1 to %2 failed").arg(source).arg(target);
340 return false;
341 }
342
343 // add to installlog
170 QString now = QDate::currentDate().toString("yyyyMMdd"); 344 QString now = QDate::currentDate().toString("yyyyMMdd");
171 if(m_removeWav) 345 installlog.setValue(target.remove(0,m_mountpoint.length()),now);
346
347 m_logger->setProgressValue(++m_progress);
348 QCoreApplication::processEvents();
349 }
350 installlog.endGroup();
351 installlog.sync();
352 return true;
353}
354
355//! \brief copys Talkfile for Files from the temp dir to the target. Progress and installlog is handled inside
356//!
357//! \param fileMap a MultiMap of Paths -> Filenames
358//! \param errString Pointer to a QString where the error cause is written.
359//! \returns true on success, false on error or user abort
360bool TalkFileCreator::copyTalkFileFiles(QMultiMap<QString,QString> fileMap,QString* errString)
361{
362 resetProgress(fileMap.size());
363
364 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
365 installlog.beginGroup("talkfiles");
366
367 QMapIterator<QString, QString> it(fileMap);
368 while (it.hasNext())
369 {
370 it.next();
371 if(m_abort)
172 { 372 {
173 QFile wavfile(wavfilename); 373 *errString = tr("Talk file creation aborted");
174 wavfile.remove(); 374 return false;
175 installlog.remove(wavfilename);
176 } 375 }
376
377 QString source;
378 QString target = it.key() + "/" + it.value() + ".talk";
379
380 // correct source if we hav stripExtension enabled
381 if(m_stripExtensions)
382 source = QDir::tempPath()+ "/"+ stripExtension(it.value()) + ".talk";
177 else 383 else
178 installlog.setValue(wavfilename.remove(0,m_mountpoint.length()),now); 384 source = QDir::tempPath()+ "/"+ it.value() + ".talk";
179 385
180 //! add the .talk file to the install log 386 // remove target if it exists, and if we should overwrite it
181 installlog.setValue(QString(filepath+"/"+filename).remove(0,m_mountpoint.length()),now); 387 if(m_overwriteTalk && QFile::exists(target))
182 it.next(); 388 QFile::remove(target);
389
390 // copy file
391 qDebug() << "copying: " << source << " to " << target;
392 if(!QFile::copy(source,target))
393 {
394 *errString = tr("Copying of %1 to %2 failed").arg(source).arg(target);
395 return false;
396 }
397
398 // add to Install log
399 QString now = QDate::currentDate().toString("yyyyMMdd");
400 installlog.setValue(target.remove(0,m_mountpoint.length()),now);
401
402 m_logger->setProgressValue(++m_progress);
403 QCoreApplication::processEvents();
183 } 404 }
184
185 installlog.endGroup(); 405 installlog.endGroup();
186 m_tts->stop(); 406 installlog.sync();
187 m_enc->stop(); 407 return true;
188 m_logger->addItem(tr("Finished creating Talk files"),LOGOK); 408}
189 m_logger->setProgressMax(1);
190 m_logger->setProgressValue(1);
191 m_logger->abort();
192 409
193 return true;
194 410
411//! \brief Cleans up Files potentially left in the temp dir
412//!
413//! \param list List of file to try to delete in the temp dir. Function appends ".wav" and ".talk" to the filenames
414bool TalkFileCreator::cleanup(QStringList list)
415{
416 m_logger->addItem(tr("Cleaning up.."),LOGINFO);
417
418 for(int i=0; i < list.size(); i++)
419 {
420 if(QFile::exists(QDir::tempPath()+ "/"+ list[i] + ".wav"))
421 QFile::remove(QDir::tempPath()+ "/"+ list[i] + ".wav");
422 if(QFile::exists(QDir::tempPath()+ "/"+ list[i] + ".talk"))
423 QFile::remove(QDir::tempPath()+ "/"+ list[i] + ".talk");
424
425 QCoreApplication::processEvents();
426 }
427 return true;
195} 428}
196 429
430//! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
431//!
197void TalkFileCreator::abort() 432void TalkFileCreator::abort()
198{ 433{
199 m_abort = true; 434 m_abort = true;
diff --git a/rbutil/rbutilqt/talkfile.h b/rbutil/rbutilqt/talkfile.h
index f4e9b4c88e..d869c32880 100644
--- a/rbutil/rbutilqt/talkfile.h
+++ b/rbutil/rbutilqt/talkfile.h
@@ -44,8 +44,6 @@ public:
44 void setMountPoint(QString mountpoint) {m_mountpoint =mountpoint; } 44 void setMountPoint(QString mountpoint) {m_mountpoint =mountpoint; }
45 45
46 void setOverwriteTalk(bool ov) {m_overwriteTalk = ov;} 46 void setOverwriteTalk(bool ov) {m_overwriteTalk = ov;}
47 void setOverwriteWav(bool ov) {m_overwriteWav = ov;}
48 void setRemoveWav(bool ov) {m_removeWav = ov;}
49 void setRecursive(bool ov) {m_recursive = ov;} 47 void setRecursive(bool ov) {m_recursive = ov;}
50 void setStripExtensions(bool ov) {m_stripExtensions = ov;} 48 void setStripExtensions(bool ov) {m_stripExtensions = ov;}
51 void setTalkFolders(bool ov) {m_talkFolders = ov;} 49 void setTalkFolders(bool ov) {m_talkFolders = ov;}
@@ -55,16 +53,25 @@ private slots:
55 void abort(); 53 void abort();
56 54
57private: 55private:
56 bool cleanup(QStringList list);
57 QString stripExtension(QString filename);
58 void doAbort(QStringList cleanupList);
59 void resetProgress(int max);
60 bool createDirAndFileMaps(QDir startDir,QMultiMap<QString,QString> *dirMap,QMultiMap<QString,QString> *fileMap);
61 bool voiceList(QStringList toSpeak,QString* errString);
62 bool encodeList(QStringList toEncode,QString* errString);
63 bool copyTalkDirFiles(QMultiMap<QString,QString> dirMap,QString* errString);
64 bool copyTalkFileFiles(QMultiMap<QString,QString> fileMap,QString* errString);
65
58 TTSBase* m_tts; 66 TTSBase* m_tts;
59 EncBase* m_enc; 67 EncBase* m_enc;
60 RbSettings* settings; 68 RbSettings* settings;
61 69
62 QDir m_dir; 70 QDir m_dir;
63 QString m_mountpoint; 71 QString m_mountpoint;
72 int m_progress;
64 73
65 bool m_overwriteTalk; 74 bool m_overwriteTalk;
66 bool m_overwriteWav;
67 bool m_removeWav;
68 bool m_recursive; 75 bool m_recursive;
69 bool m_stripExtensions; 76 bool m_stripExtensions;
70 bool m_talkFolders; 77 bool m_talkFolders;