rbutilqt: made the sapi TTS more configurable, you can now select a specific voice...
[Rockbox.git] / rbutil / rbutilqt / voicefile.cpp
blob47b659a34757cd7581c2bf8b0164de5dbd94f5c3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
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
24 #define STATE_VOICE 2
27 VoiceFileCreator::VoiceFileCreator(QObject* parent) :QObject(parent)
29 m_wavtrimThreshold=500;
32 void VoiceFileCreator::abort()
34 m_abort = true;
37 bool VoiceFileCreator::createVoiceFile(ProgressloggerInterface* logger)
39 m_abort = false;
40 m_logger = 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);
56 return false;
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);
77 info.close();
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);
86 downloadFile->open();
87 filename = downloadFile->fileName();
88 downloadFile->close();
89 // get the real file.
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()));
99 return true;
103 void VoiceFileCreator::downloadRequestFinished(int id, bool error)
105 qDebug() << "Install::downloadRequestFinished" << id << error;
106 qDebug() << "error:" << getter->errorString();
108 downloadDone(error);
112 void VoiceFileCreator::downloadDone(bool error)
114 qDebug() << "Voice creator::downloadDone, error:" << error;
116 // update progress bar
117 int max = m_logger->getProgressMax();
118 if(max == 0) {
119 max = 100;
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);
125 m_logger->abort();
126 return;
128 if(getter->isCached()) m_logger->addItem(tr("Cached file used."), LOGINFO);
129 if(error) {
130 m_logger->addItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
131 m_logger->abort();
132 return;
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);
144 m_logger->abort();
145 return;
148 //tts
149 m_tts = getTTS(userSettings->value("tts").toString());
150 m_tts->setCfg(userSettings,deviceSettings);
152 QString errStr;
153 if(!m_tts->start(&errStr))
155 m_logger->addItem(errStr,LOGERROR);
156 m_logger->addItem(tr("Init of TTS engine failed"),LOGERROR);
157 m_logger->abort();
158 return;
161 // Encoder
162 m_enc = getEncoder(userSettings->value("encoder").toString());
163 m_enc->setUserCfg(userSettings);
165 if(!m_enc->start())
167 m_logger->addItem(tr("Init of Encoder engine failed"),LOGERROR);
168 m_tts->stop();
169 m_logger->abort();
170 return;
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");
180 QString id, voice;
181 bool idfound = false;
182 bool voicefound=false;
183 while (!in.atEnd())
185 QString line = in.readLine();
186 if(line.contains("id:")) //ID found
188 id = line.remove("id:").remove('"').trimmed();
189 idfound = true;
191 else if(line.contains("voice:")) // voice found
193 voice = line.remove("voice:").remove('"').trimmed();
194 voicefound=true;
197 if(idfound && voicefound)
199 voicepairs.append(QPair<QString,QString>(id,voice));
200 idfound=false;
201 voicefound=false;
204 genlang.close();
206 // check for empty list
207 if(voicepairs.size() == 0)
209 m_logger->addItem(tr("The downloaded file was empty!"),LOGERROR);
210 m_logger->abort();
211 m_tts->stop();
212 return;
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++)
222 if(m_abort)
224 m_logger->addItem("aborted.",LOGERROR);
225 m_logger->abort();
226 m_tts->stop();
227 return;
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";
236 // todo PAUSE
237 if(voicepairs.at(i).first == "VOICE_PAUSE")
239 QFile::copy(":/builtin/builtin/VOICE_PAUSE.wav",m_path + "/VOICE_PAUSE.wav");
242 else
244 if(toSpeak == "") continue;
246 m_logger->addItem(tr("creating ")+toSpeak,LOGINFO);
247 QApplication::processEvents();
248 m_tts->voice(toSpeak,wavname); // generate wav
251 // todo strip
252 char buffer[255];
254 wavtrim((char*)qPrintable(wavname),m_wavtrimThreshold,buffer,255);
256 // encode wav
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;
265 //make voicefile
266 FILE* ids2 = fopen(filename.toUtf8(), "r");
267 if (ids2 == NULL)
269 m_logger->addItem(tr("Error opening downloaded file"),LOGERROR);
270 m_logger->abort();
271 return;
274 FILE* output = fopen(QString(m_mountpoint + "/.rockbox/langs/" + m_lang + ".voice").toUtf8(), "wb");
275 if (output == NULL)
277 m_logger->addItem(tr("Error opening output file"),LOGERROR);
278 return;
281 voicefont(ids2,m_targetid,(char*)(const char*)m_path.toUtf8(), output);
283 //remove .mp3 files
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);
292 m_logger->abort();
295 void VoiceFileCreator::updateDataReadProgress(int read, int total)
297 m_logger->setProgressMax(total);
298 m_logger->setProgressValue(read);
299 //qDebug() << "progress:" << read << "/" << total;