test test test
[kugel-rb.git] / rbutil / rbutilqt / base / voicefile.cpp
blob3b80c1ff1244243acf5148bacc87296d40b3d9f0
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 VoiceFileCreator::VoiceFileCreator(QObject* parent) :QObject(parent)
26 m_wavtrimThreshold=500;
29 void VoiceFileCreator::abort()
31 m_abort = true;
32 emit aborted();
35 bool VoiceFileCreator::createVoiceFile()
37 m_talkList.clear();
38 m_abort = false;
39 emit logItem(tr("Starting Voicefile generation"),LOGINFO);
41 // test if tempdir exists
42 if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
44 QDir(QDir::tempPath()).mkdir("rbvoice");
46 m_path = QDir::tempPath() + "/rbvoice/";
48 // read rockbox-info.txt
49 RockboxInfo info(m_mountpoint);
50 if(!info.open())
52 emit logItem(tr("could not find rockbox-info.txt"),LOGERROR);
53 emit done(true);
54 return false;
57 QString target = info.target();
58 QString features = info.features();
59 QString version = info.version();
60 version = version.left(version.indexOf("-")).remove(0,1);
62 //prepare download url
63 QUrl genlangUrl = RbSettings::value(RbSettings::GenlangUrl).toString()
64 +"?lang=" + m_lang + "&t=" + target + "&rev=" + version + "&f=" + features;
66 qDebug() << "downloading " << genlangUrl;
68 //download the correct genlang output
69 QTemporaryFile *downloadFile = new QTemporaryFile(this);
70 downloadFile->open();
71 filename = downloadFile->fileName();
72 downloadFile->close();
73 // get the real file.
74 getter = new HttpGet(this);
75 getter->setFile(downloadFile);
77 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
78 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(logProgress(int, int)));
79 connect(this, SIGNAL(aborted()), getter, SLOT(abort()));
80 emit logItem(tr("Downloading voice info.."),LOGINFO);
81 getter->getFile(genlangUrl);
82 return true;
86 void VoiceFileCreator::downloadDone(bool error)
88 qDebug() << "Voice creator::downloadDone, error:" << error;
90 // update progress bar
91 emit logProgress(1,1);
92 if(getter->httpResponse() != 200 && !getter->isCached()) {
93 emit logItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
94 emit done(true);
95 return;
98 if(getter->isCached())
99 emit logItem(tr("Cached file used."), LOGINFO);
100 if(error)
102 emit logItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
103 emit done(true);
104 return;
106 else
107 emit logItem(tr("Download finished."),LOGINFO);
109 QCoreApplication::processEvents();
111 //open downloaded file
112 QFile genlang(filename);
113 if(!genlang.open(QIODevice::ReadOnly))
115 emit logItem(tr("failed to open downloaded file"),LOGERROR);
116 emit done(true);
117 return;
120 QCoreApplication::processEvents();
122 //read in downloaded file
123 emit logItem(tr("Reading strings..."),LOGINFO);
124 QTextStream in(&genlang);
125 in.setCodec("UTF-8");
126 QString id, voice;
127 bool idfound = false;
128 bool voicefound=false;
129 while (!in.atEnd())
131 QString line = in.readLine();
132 if(line.contains("id:")) //ID found
134 id = line.remove("id:").remove('"').trimmed();
135 idfound = true;
137 else if(line.contains("voice:")) // voice found
139 voice = line.remove("voice:").remove('"').trimmed();
140 voicefound=true;
143 if(idfound && voicefound)
145 TalkGenerator::TalkEntry entry;
146 entry.toSpeak = voice;
147 entry.wavfilename = m_path + "/" + id + ".wav";
148 entry.talkfilename = m_path + "/" + id + ".mp3"; //voicefont wants them with .mp3 extension
149 entry.voiced = false;
150 entry.encoded = false;
151 if(id == "VOICE_PAUSE")
153 QFile::copy(":/builtin/VOICE_PAUSE.wav",m_path + "/VOICE_PAUSE.wav");
154 entry.wavfilename = m_path + "/VOICE_PAUSE.wav";
155 entry.voiced = true;
157 m_talkList.append(entry);
158 idfound=false;
159 voicefound=false;
162 genlang.close();
164 // check for empty list
165 if(m_talkList.size() == 0)
167 emit logItem(tr("The downloaded file was empty!"),LOGERROR);
168 emit done(true);
169 return;
172 // generate files
174 TalkGenerator generator(this);
175 connect(&generator,SIGNAL(done(bool)),this,SIGNAL(done(bool)));
176 connect(&generator,SIGNAL(logItem(QString,int)),this,SIGNAL(logItem(QString,int)));
177 connect(&generator,SIGNAL(logProgress(int,int)),this,SIGNAL(logProgress(int,int)));
178 connect(this,SIGNAL(aborted()),&generator,SLOT(abort()));
180 if(generator.process(&m_talkList) == TalkGenerator::eERROR)
182 cleanup();
183 emit logProgress(0,1);
184 emit done(true);
185 return;
189 //make voicefile
190 emit logItem(tr("Creating voicefiles..."),LOGINFO);
191 FILE* ids2 = fopen(filename.toLocal8Bit(), "r");
192 if (ids2 == NULL)
194 cleanup();
195 emit logItem(tr("Error opening downloaded file"),LOGERROR);
196 emit done(true);
197 return;
200 FILE* output = fopen(QString(m_mountpoint + "/.rockbox/langs/" + m_lang + ".voice").toLocal8Bit(), "wb");
201 if (output == NULL)
203 cleanup();
204 emit logItem(tr("Error opening output file"),LOGERROR);
205 emit done(true);
206 return;
209 voicefont(ids2,m_targetid,m_path.toLocal8Bit().data(), output);
211 //cleanup
212 cleanup();
214 // Add Voice file to the install log
215 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
216 installlog.beginGroup("selfcreated Voice");
217 installlog.setValue("/.rockbox/langs/" + m_lang + ".voice",QDate::currentDate().toString("yyyyMMdd"));
218 installlog.endGroup();
219 installlog.sync();
221 emit logProgress(1,1);
222 emit logItem(tr("successfully created."),LOGOK);
224 emit done(false);
227 //! \brief Cleans up Files potentially left in the temp dir
229 void VoiceFileCreator::cleanup()
231 emit logItem(tr("Cleaning up.."),LOGINFO);
233 for(int i=0; i < m_talkList.size(); i++)
235 if(QFile::exists(m_talkList[i].wavfilename))
236 QFile::remove(m_talkList[i].wavfilename);
237 if(QFile::exists(m_talkList[i].talkfilename))
238 QFile::remove(m_talkList[i].talkfilename);
240 QCoreApplication::processEvents();
242 emit logItem(tr("Finished"),LOGINFO);
244 return;