Fix FS#8196 - Gather Runtime Data > User Rating not working
[Rockbox.git] / rbutil / rbutilqt / talkfile.cpp
blobe13647deb21514d2d779e2fc99a7cf995dfd49b6
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)
28 bool TalkFileCreator::initEncoder()
30 QFileInfo enc(m_EncExec);
31 if(enc.exists())
33 return true;
35 else
37 return false;
42 bool TalkFileCreator::createTalkFiles(ProgressloggerInterface* logger)
44 m_abort = false;
45 m_logger = logger;
46 m_logger->addItem("Starting Talk file generation",LOGINFO);
48 if(m_curTTS == "sapi")
49 m_tts = new TTSSapi();
50 else
51 m_tts = new TTSExes();
53 m_tts->setTTSexe(m_TTSexec);
54 m_tts->setTTsOpts(m_TTSOpts);
55 m_tts->setTTsLanguage(m_TTSLanguage);
56 m_tts->setTTsTemplate(m_curTTSTemplate);
58 if(!m_tts->start())
60 m_logger->addItem("Init of TTS engine failed",LOGERROR);
61 return false;
63 if(!initEncoder())
65 m_logger->addItem("Init of encoder failed",LOGERROR);
66 m_tts->stop();
67 return false;
69 QApplication::processEvents();
71 connect(logger,SIGNAL(aborted()),this,SLOT(abort()));
72 m_logger->setProgressMax(0);
73 QDirIterator it(m_dir,QDirIterator::Subdirectories);
74 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
75 installlog.beginGroup("talkfiles");
76 // iterate over all entrys
77 while (it.hasNext())
79 if(m_abort)
81 m_logger->addItem("Talk file creation aborted",LOGERROR);
82 m_tts->stop();
83 return false;
86 QApplication::processEvents();
87 QFileInfo fileInf = it.fileInfo();
88 QString toSpeak;
89 QString filename;
90 QString wavfilename;
92 //! skip dotdot and .talk files
93 if(fileInf.fileName() == ".." || fileInf.suffix() == "talk")
95 it.next();
96 continue;
98 //! if it is a dir
99 if(fileInf.isDir())
101 // skip entry if folder talking isnt enabled
102 if(m_talkFolders == false)
104 it.next();
105 continue;
107 toSpeak = fileInf.absoluteDir().dirName();
108 filename = fileInf.absolutePath() + "/_dirname.talk";
110 else // if it is a file
112 // skip entry if file talking isnt enabled
113 if(m_talkFiles == false)
115 it.next();
116 continue;
118 if(m_stripExtensions)
119 toSpeak = fileInf.baseName();
120 else
121 toSpeak = fileInf.fileName();
122 filename = fileInf.absoluteFilePath() + ".talk";
124 wavfilename = filename + ".wav";
126 QFileInfo filenameInf(filename);
127 QFileInfo wavfilenameInf(wavfilename);
129 //! the actual generation of the .talk files
130 if(!filenameInf.exists() || m_overwriteTalk)
132 if(!wavfilenameInf.exists() || m_overwriteWav)
134 m_logger->addItem("Voicing of " + toSpeak,LOGINFO);
135 if(!m_tts->voice(toSpeak,wavfilename))
137 m_logger->addItem("Voicing of " + toSpeak + " failed",LOGERROR);
138 m_logger->abort();
139 m_tts->stop();
140 return false;
143 m_logger->addItem("Encoding of " + toSpeak,LOGINFO);
144 if(!encode(wavfilename,filename))
146 m_logger->addItem("Encoding of " + wavfilename + " failed",LOGERROR);
147 m_logger->abort();
148 m_tts->stop();
149 return false;
153 //! remove the intermedia wav file, if requested
154 QString now = QDate::currentDate().toString("yyyyMMdd");
155 if(m_removeWav)
157 QFile wavfile(wavfilename);
158 wavfile.remove();
159 installlog.remove(wavfilename);
161 else
162 installlog.setValue(wavfilename.remove(0,m_mountpoint.length()),now);
164 //! add the .talk file to the install log
165 installlog.setValue(filename.remove(0,m_mountpoint.length()),now);
166 it.next();
169 installlog.endGroup();
170 m_tts->stop();
171 m_logger->addItem("Finished creating Talk files",LOGOK);
172 m_logger->setProgressMax(1);
173 m_logger->setProgressValue(1);
174 m_logger->abort();
176 return true;
180 void TalkFileCreator::abort()
182 m_abort = true;
185 bool TalkFileCreator::encode(QString input,QString output)
187 qDebug() << "encoding..";
188 QString execstring = m_curEncTemplate;
190 execstring.replace("%exe",m_EncExec);
191 execstring.replace("%options",m_EncOpts);
192 execstring.replace("%input",input);
193 execstring.replace("%output",output);
194 qDebug() << execstring;
195 QProcess::execute(execstring);
196 return true;
200 bool TTSSapi::start()
202 QFileInfo tts(m_TTSexec);
203 if(!tts.exists())
204 return false;
206 // create the voice process
207 QString execstring = m_TTSTemplate;
208 execstring.replace("%exe",m_TTSexec);
209 execstring.replace("%options",m_TTSOpts);
210 execstring.replace("%lang",m_TTSLanguage);
211 qDebug() << "init" << execstring;
212 voicescript = new QProcess(NULL);
213 voicescript->start(execstring);
214 if(!voicescript->waitForStarted())
215 return false;
216 return true;
219 bool TTSSapi::voice(QString text,QString wavfile)
221 QString query = "SPEAK\t"+wavfile+"\t"+text+"\r\n";
222 qDebug() << "voicing" << query;
223 voicescript->write(query.toLocal8Bit());
224 voicescript->write("SYNC\tbla\r\n");
225 voicescript->waitForReadyRead();
226 return true;
229 bool TTSSapi::stop()
231 QString query = "QUIT\r\n";
232 voicescript->write(query.toLocal8Bit());
233 voicescript->waitForFinished();
234 delete voicescript;
235 return true;
238 bool TTSExes::start()
240 QFileInfo tts(m_TTSexec);
241 qDebug() << "ttsexe init";
242 if(tts.exists())
244 return true;
246 else
248 return false;
252 bool TTSExes::voice(QString text,QString wavfile)
254 QString execstring = m_TTSTemplate;
256 execstring.replace("%exe",m_TTSexec);
257 execstring.replace("%options",m_TTSOpts);
258 execstring.replace("%wavfile",wavfile);
259 execstring.replace("%text",text);
260 qDebug() << "voicing" << execstring;
261 QProcess::execute(execstring);
262 return true;