Fix case in the constant
[maemo-rb.git] / rbutil / rbutilqt / voicefile.cpp
blob4de9b87f342334109e5c3f942cbe989bec80807f
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 "voicefile.h"
21 #include "utils.h"
22 #include "rbsettings.h"
24 #define STATE_INVALID 0
25 #define STATE_PHRASE 1
26 #define STATE_VOICE 2
29 VoiceFileCreator::VoiceFileCreator(QObject* parent) :QObject(parent)
31 m_wavtrimThreshold=500;
34 void VoiceFileCreator::abort()
36 m_abort = true;
39 bool VoiceFileCreator::createVoiceFile(ProgressloggerInterface* logger)
41 m_abort = false;
42 m_logger = logger;
43 m_logger->addItem(tr("Starting Voicefile generation"),LOGINFO);
45 // test if tempdir exists
46 if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
48 QDir(QDir::tempPath()).mkdir("rbvoice");
51 m_path = QDir::tempPath() + "/rbvoice/";
53 // read rockbox-info.txt
54 RockboxInfo info(m_mountpoint);
55 if(!info.open())
57 m_logger->addItem(tr("could not find rockbox-info.txt"),LOGERROR);
58 m_logger->setFinished();
59 emit done(false);
60 return false;
63 QString target = info.target();
64 QString features = info.features();
65 QString version = info.version();
66 version = version.left(version.indexOf("-")).remove(0,1);
68 //prepare download url
69 QUrl genlangUrl = RbSettings::value(RbSettings::GenlangUrl).toString()
70 +"?lang=" + m_lang + "&t=" + target + "&rev=" + version + "&f=" + features;
72 qDebug() << "downloading " << genlangUrl;
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);
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()));
87 getter->getFile(genlangUrl);
88 return true;
92 void VoiceFileCreator::downloadDone(bool error)
94 qDebug() << "Voice creator::downloadDone, error:" << error;
96 // update progress bar
97 int max = m_logger->getProgressMax();
98 if(max == 0) {
99 max = 100;
100 m_logger->setProgressMax(max);
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;
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;
116 else m_logger->addItem(tr("Download finished."),LOGOK);
117 QCoreApplication::processEvents();
120 m_logger->setProgressMax(0);
121 //open downloaded file
122 QFile genlang(filename);
123 if(!genlang.open(QIODevice::ReadOnly))
125 m_logger->addItem(tr("failed to open downloaded file"),LOGERROR);
126 m_logger->setFinished();
127 emit done(false);
128 return;
131 //tts
132 m_tts = TTSBase::getTTS(this,RbSettings::value(RbSettings::Tts).toString());
134 QString errStr;
135 if(!m_tts->start(&errStr))
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;
144 // Encoder
145 m_enc = EncBase::getEncoder(this,RbSettings::value(RbSettings::CurEncoder).toString());
147 if(!m_enc->start())
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;
156 QCoreApplication::processEvents();
157 connect(m_logger,SIGNAL(aborted()),this,SLOT(abort()));
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())
168 QString line = in.readLine();
169 if(line.contains("id:")) //ID found
171 id = line.remove("id:").remove('"').trimmed();
172 idfound = true;
174 else if(line.contains("voice:")) // voice found
176 voice = line.remove("voice:").remove('"').trimmed();
177 voicefound=true;
180 if(idfound && voicefound)
182 voicepairs.append(QPair<QString,QString>(id,voice));
183 idfound=false;
184 voicefound=false;
187 genlang.close();
189 // check for empty list
190 if(voicepairs.size() == 0)
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;
199 m_logger->setProgressMax(voicepairs.size());
200 m_logger->setProgressValue(0);
202 // create voice clips
203 QStringList mp3files;
204 for(int i=0; i< voicepairs.size(); i++)
206 if(m_abort)
208 m_logger->addItem("aborted.",LOGERROR);
209 m_logger->setFinished();
210 m_tts->stop();
211 emit done(false);
212 return;
215 m_logger->setProgressValue(i);
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";
221 // todo PAUSE
222 if(voicepairs.at(i).first == "VOICE_PAUSE")
224 QFile::copy(":/builtin/builtin/VOICE_PAUSE.wav",m_path + "/VOICE_PAUSE.wav");
227 else
229 if(toSpeak == "") continue;
231 m_logger->addItem(tr("creating ")+toSpeak,LOGINFO);
232 QCoreApplication::processEvents();
234 // TODO: add support for aborting the operation
235 QString errStr;
236 m_tts->voice(toSpeak,wavname, &errStr); // generate wav
239 // todo strip
240 char buffer[255];
242 wavtrim((char*)qPrintable(wavname),m_wavtrimThreshold,buffer,255);
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;
253 //make voicefile
254 FILE* ids2 = fopen(filename.toUtf8(), "r");
255 if (ids2 == NULL)
257 m_logger->addItem(tr("Error opening downloaded file"),LOGERROR);
258 m_logger->setFinished();
259 emit done(false);
260 return;
263 FILE* output = fopen(QString(m_mountpoint + "/.rockbox/langs/" + m_lang + ".voice").toUtf8(), "wb");
264 if (output == NULL)
266 m_logger->addItem(tr("Error opening output file"),LOGERROR);
267 emit done(false);
268 return;
271 voicefont(ids2,m_targetid,(char*)(const char*)m_path.toUtf8(), output);
273 //remove .mp3 files
274 for(int i=0;i< mp3files.size(); i++)
276 QFile::remove(mp3files.at(i));
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();
286 m_logger->setProgressMax(100);
287 m_logger->setProgressValue(100);
288 m_logger->addItem(tr("successfully created."),LOGOK);
289 m_logger->setFinished();
291 emit done(true);
294 void VoiceFileCreator::updateDataReadProgress(int read, int total)
296 m_logger->setProgressMax(total);
297 m_logger->setProgressValue(read);
298 //qDebug() << "progress:" << read << "/" << total;