rbutil: improve talkfile generation. remove the options to keep and overwrite the...
[kugel-rb.git] / rbutil / rbutilqt / talkfile.cpp
blob37a584e28cb3d54816eb21736a6e371d751eea0f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
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 "talkfile.h"
22 TalkFileCreator::TalkFileCreator(QObject* parent): QObject(parent)
27 bool TalkFileCreator::createTalkFiles(ProgressloggerInterface* logger)
29 m_abort = false;
30 m_logger = logger;
31 m_logger->addItem(tr("Starting Talk file generation"),LOGINFO);
33 //tts
34 m_tts = TTSBase::getTTS(settings->curTTS());
35 m_tts->setCfg(settings);
37 QString errStr;
38 if(!m_tts->start(&errStr))
40 m_logger->addItem(errStr,LOGERROR);
41 m_logger->addItem(tr("Init of TTS engine failed"),LOGERROR);
42 m_logger->abort();
43 return false;
46 // Encoder
47 m_enc = EncBase::getEncoder(settings->curEncoder());
48 m_enc->setCfg(settings);
50 if(!m_enc->start())
52 m_logger->addItem(tr("Init of Encoder engine failed"),LOGERROR);
53 m_logger->abort();
54 m_tts->stop();
55 return false;
58 QCoreApplication::processEvents();
60 connect(logger,SIGNAL(aborted()),this,SLOT(abort()));
61 m_logger->setProgressMax(0);
63 QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags;
64 if(m_recursive)
65 flags = QDirIterator::Subdirectories;
67 QDirIterator it(m_dir,flags);
68 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
69 installlog.beginGroup("talkfiles");
70 // iterate over all entrys
71 while (it.hasNext())
73 if(m_abort)
75 m_logger->addItem(tr("Talk file creation aborted"),LOGERROR);
76 m_logger->abort();
77 m_tts->stop();
78 return false;
81 QCoreApplication::processEvents();
82 QFileInfo fileInf = it.fileInfo();
83 QString toSpeak;
84 QString filename;
85 QString wavfilename;
86 QString filepath;
88 QString path = fileInf.filePath();
89 qDebug() << path;
91 if( path.endsWith("..") || path.endsWith(".") || path.endsWith(".talk") )
93 it.next();
94 continue;
97 //! if it is a dir
98 if(fileInf.isDir())
100 // skip entry if folder talking isnt enabled
101 if(m_talkFolders == false)
103 it.next();
104 continue;
107 toSpeak = fileInf.fileName();
109 filepath = fileInf.filePath() + "/";
110 filename = "_dirname.talk";
111 qDebug() << "toSpeak: " << toSpeak << "filename: " << filename << " path: " <<filepath;
113 else // if it is a file
115 // skip entry if file talking isnt enabled
116 if(m_talkFiles == false)
118 it.next();
119 continue;
122 // create toSpeak string
123 if(m_stripExtensions)
124 toSpeak = fileInf.baseName();
125 else
126 toSpeak = fileInf.fileName();
127 // create filename and path
128 filepath = fileInf.absolutePath();
129 filename = fileInf.fileName() + ".talk";
133 wavfilename = QDir::tempPath()+ "/"+ filename + ".wav";
135 QFileInfo filenameInf(filepath+filename);
136 QFileInfo wavfilenameInf(wavfilename);
138 //! the actual generation of the .talk files
139 if(!filenameInf.exists() || m_overwriteTalk)
141 if(!wavfilenameInf.exists() || m_overwriteWav)
143 m_logger->addItem(tr("Voicing of %1").arg(toSpeak),LOGINFO);
144 if(!m_tts->voice(toSpeak,wavfilename))
146 m_logger->addItem(tr("Voicing of %s failed").arg(toSpeak),LOGERROR);
147 m_logger->abort();
148 m_tts->stop();
149 m_enc->stop();
151 return false;
153 QCoreApplication::processEvents();
155 m_logger->addItem(tr("Encoding of %1").arg(toSpeak),LOGINFO);
156 if(!m_enc->encode(wavfilename,filepath+filename))
158 m_logger->addItem(tr("Encoding of %1 failed").arg(wavfilename),LOGERROR);
159 m_logger->abort();
160 m_tts->stop();
161 m_enc->stop();
163 return false;
165 QCoreApplication::processEvents();
168 //! remove the intermedia wav file, if requested
169 QString now = QDate::currentDate().toString("yyyyMMdd");
170 if(m_removeWav)
172 QFile wavfile(wavfilename);
173 wavfile.remove();
174 installlog.remove(wavfilename);
176 else
177 installlog.setValue(wavfilename.remove(0,m_mountpoint.length()),now);
179 //! add the .talk file to the install log
180 installlog.setValue(QString(filepath+filename).remove(0,m_mountpoint.length()),now);
181 it.next();
184 installlog.endGroup();
185 m_tts->stop();
186 m_logger->addItem(tr("Finished creating Talk files"),LOGOK);
187 m_logger->setProgressMax(1);
188 m_logger->setProgressValue(1);
189 m_logger->abort();
191 return true;
195 void TalkFileCreator::abort()
197 m_abort = true;