Make sure to include audiohw.h in settings.h or the definition of struct user_setting...
[kugel-rb.git] / rbutil / rbutilqt / base / voicefile.cpp
blob208cd1eeb8ec4c4fd2757389294897b9dca58926
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 "rockboxinfo.h"
23 #include "rbsettings.h"
24 #include "systeminfo.h"
26 VoiceFileCreator::VoiceFileCreator(QObject* parent) :QObject(parent)
28 m_wavtrimThreshold=500;
31 void VoiceFileCreator::abort()
33 m_abort = true;
34 emit aborted();
37 bool VoiceFileCreator::createVoiceFile()
39 m_talkList.clear();
40 m_abort = false;
41 emit logItem(tr("Starting Voicefile generation"),LOGINFO);
43 // test if tempdir exists
44 if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
46 QDir(QDir::tempPath()).mkdir("rbvoice");
48 m_path = QDir::tempPath() + "/rbvoice/";
50 // read rockbox-info.txt
51 RockboxInfo info(m_mountpoint);
52 if(!info.success())
54 emit logItem(tr("could not find rockbox-info.txt"),LOGERROR);
55 emit done(true);
56 return false;
59 QString target = info.target();
60 QString features = info.features();
61 QString version = info.version();
62 m_targetid = info.targetID().toInt();
63 version = version.left(version.indexOf("-")).remove("r");
65 //prepare download url
66 QUrl genlangUrl = SystemInfo::value(SystemInfo::GenlangUrl).toString()
67 +"?lang=" + m_lang + "&t=" + target + "&rev=" + version + "&f=" + features;
69 qDebug() << "downloading " << genlangUrl;
71 //download the correct genlang output
72 QTemporaryFile *downloadFile = new QTemporaryFile(this);
73 downloadFile->open();
74 filename = downloadFile->fileName();
75 downloadFile->close();
76 // get the real file.
77 getter = new HttpGet(this);
78 getter->setFile(downloadFile);
80 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
81 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(logProgress(int, int)));
82 connect(this, SIGNAL(aborted()), getter, SLOT(abort()));
83 emit logItem(tr("Downloading voice info..."),LOGINFO);
84 getter->getFile(genlangUrl);
85 return true;
89 void VoiceFileCreator::downloadDone(bool error)
91 qDebug() << "Voice creator::downloadDone, error:" << error;
93 // update progress bar
94 emit logProgress(1,1);
95 if(getter->httpResponse() != 200 && !getter->isCached()) {
96 emit logItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
97 emit done(true);
98 return;
101 if(getter->isCached())
102 emit logItem(tr("Cached file used."), LOGINFO);
103 if(error)
105 emit logItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
106 emit done(true);
107 return;
109 else
110 emit logItem(tr("Download finished."),LOGINFO);
112 QCoreApplication::processEvents();
114 //open downloaded file
115 QFile genlang(filename);
116 if(!genlang.open(QIODevice::ReadOnly))
118 emit logItem(tr("failed to open downloaded file"),LOGERROR);
119 emit done(true);
120 return;
123 QCoreApplication::processEvents();
125 //read in downloaded file
126 emit logItem(tr("Reading strings..."),LOGINFO);
127 QTextStream in(&genlang);
128 in.setCodec("UTF-8");
129 QString id, voice;
130 bool idfound = false;
131 bool voicefound=false;
132 while (!in.atEnd())
134 QString line = in.readLine();
135 if(line.contains("id:")) //ID found
137 id = line.remove("id:").remove('"').trimmed();
138 idfound = true;
140 else if(line.contains("voice:")) // voice found
142 voice = line.remove("voice:").remove('"').trimmed();
143 voicefound=true;
146 if(idfound && voicefound)
148 TalkGenerator::TalkEntry entry;
149 entry.toSpeak = voice;
150 entry.wavfilename = m_path + "/" + id + ".wav";
151 entry.talkfilename = m_path + "/" + id + ".mp3"; //voicefont wants them with .mp3 extension
152 entry.voiced = false;
153 entry.encoded = false;
154 if(id == "VOICE_PAUSE")
156 QFile::copy(":/builtin/VOICE_PAUSE.wav",m_path + "/VOICE_PAUSE.wav");
157 entry.wavfilename = m_path + "/VOICE_PAUSE.wav";
158 entry.voiced = true;
160 m_talkList.append(entry);
161 idfound=false;
162 voicefound=false;
165 genlang.close();
167 // check for empty list
168 if(m_talkList.size() == 0)
170 emit logItem(tr("The downloaded file was empty!"),LOGERROR);
171 emit done(true);
172 return;
175 // generate files
177 TalkGenerator generator(this);
178 connect(&generator,SIGNAL(done(bool)),this,SIGNAL(done(bool)));
179 connect(&generator,SIGNAL(logItem(QString,int)),this,SIGNAL(logItem(QString,int)));
180 connect(&generator,SIGNAL(logProgress(int,int)),this,SIGNAL(logProgress(int,int)));
181 connect(this,SIGNAL(aborted()),&generator,SLOT(abort()));
183 if(generator.process(&m_talkList) == TalkGenerator::eERROR)
185 cleanup();
186 emit logProgress(0,1);
187 emit done(true);
188 return;
192 //make voicefile
193 emit logItem(tr("Creating voicefiles..."),LOGINFO);
194 FILE* ids2 = fopen(filename.toLocal8Bit(), "r");
195 if (ids2 == NULL)
197 cleanup();
198 emit logItem(tr("Error opening downloaded file"),LOGERROR);
199 emit done(true);
200 return;
203 FILE* output = fopen(QString(m_mountpoint + "/.rockbox/langs/" + m_lang
204 + ".voice").toLocal8Bit(), "wb");
205 if (output == NULL)
207 cleanup();
208 fclose(ids2);
209 emit logItem(tr("Error opening output file"),LOGERROR);
210 emit done(true);
211 return;
214 voicefont(ids2,m_targetid,m_path.toLocal8Bit().data(), output);
215 // ids2 and output are closed by voicefont().
217 //cleanup
218 cleanup();
220 // Add Voice file to the install log
221 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
222 installlog.beginGroup("selfcreated Voice");
223 installlog.setValue("/.rockbox/langs/" + m_lang + ".voice",
224 QDate::currentDate().toString("yyyyMMdd"));
225 installlog.endGroup();
226 installlog.sync();
228 emit logProgress(1,1);
229 emit logItem(tr("successfully created."),LOGOK);
231 emit done(false);
234 //! \brief Cleans up Files potentially left in the temp dir
236 void VoiceFileCreator::cleanup()
238 emit logItem(tr("Cleaning up..."),LOGINFO);
240 for(int i=0; i < m_talkList.size(); i++)
242 if(QFile::exists(m_talkList[i].wavfilename))
243 QFile::remove(m_talkList[i].wavfilename);
244 if(QFile::exists(m_talkList[i].talkfilename))
245 QFile::remove(m_talkList[i].talkfilename);
247 QCoreApplication::processEvents();
249 emit logItem(tr("Finished"),LOGINFO);
251 return;