1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Wenger
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 #include "rbsettings.h"
24 VoiceFileCreator::VoiceFileCreator(QObject
* parent
) :QObject(parent
)
26 m_wavtrimThreshold
=500;
29 void VoiceFileCreator::abort()
35 bool VoiceFileCreator::createVoiceFile()
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
);
52 emit
logItem(tr("could not find rockbox-info.txt"),LOGERROR
);
57 QString target
= info
.target();
58 QString features
= info
.features();
59 QString version
= info
.version();
60 m_targetid
= info
.targetID().toInt();
61 version
= version
.left(version
.indexOf("-")).remove("r");
63 //prepare download url
64 QUrl genlangUrl
= RbSettings::value(RbSettings::GenlangUrl
).toString()
65 +"?lang=" + m_lang
+ "&t=" + target
+ "&rev=" + version
+ "&f=" + features
;
67 qDebug() << "downloading " << genlangUrl
;
69 //download the correct genlang output
70 QTemporaryFile
*downloadFile
= new QTemporaryFile(this);
72 filename
= downloadFile
->fileName();
73 downloadFile
->close();
75 getter
= new HttpGet(this);
76 getter
->setFile(downloadFile
);
78 connect(getter
, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
79 connect(getter
, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(logProgress(int, int)));
80 connect(this, SIGNAL(aborted()), getter
, SLOT(abort()));
81 emit
logItem(tr("Downloading voice info.."),LOGINFO
);
82 getter
->getFile(genlangUrl
);
87 void VoiceFileCreator::downloadDone(bool error
)
89 qDebug() << "Voice creator::downloadDone, error:" << error
;
91 // update progress bar
92 emit
logProgress(1,1);
93 if(getter
->httpResponse() != 200 && !getter
->isCached()) {
94 emit
logItem(tr("Download error: received HTTP error %1.").arg(getter
->httpResponse()),LOGERROR
);
99 if(getter
->isCached())
100 emit
logItem(tr("Cached file used."), LOGINFO
);
103 emit
logItem(tr("Download error: %1").arg(getter
->errorString()),LOGERROR
);
108 emit
logItem(tr("Download finished."),LOGINFO
);
110 QCoreApplication::processEvents();
112 //open downloaded file
113 QFile
genlang(filename
);
114 if(!genlang
.open(QIODevice::ReadOnly
))
116 emit
logItem(tr("failed to open downloaded file"),LOGERROR
);
121 QCoreApplication::processEvents();
123 //read in downloaded file
124 emit
logItem(tr("Reading strings..."),LOGINFO
);
125 QTextStream
in(&genlang
);
126 in
.setCodec("UTF-8");
128 bool idfound
= false;
129 bool voicefound
=false;
132 QString line
= in
.readLine();
133 if(line
.contains("id:")) //ID found
135 id
= line
.remove("id:").remove('"').trimmed();
138 else if(line
.contains("voice:")) // voice found
140 voice
= line
.remove("voice:").remove('"').trimmed();
144 if(idfound
&& voicefound
)
146 TalkGenerator::TalkEntry entry
;
147 entry
.toSpeak
= voice
;
148 entry
.wavfilename
= m_path
+ "/" + id
+ ".wav";
149 entry
.talkfilename
= m_path
+ "/" + id
+ ".mp3"; //voicefont wants them with .mp3 extension
150 entry
.voiced
= false;
151 entry
.encoded
= false;
152 if(id
== "VOICE_PAUSE")
154 QFile::copy(":/builtin/VOICE_PAUSE.wav",m_path
+ "/VOICE_PAUSE.wav");
155 entry
.wavfilename
= m_path
+ "/VOICE_PAUSE.wav";
158 m_talkList
.append(entry
);
165 // check for empty list
166 if(m_talkList
.size() == 0)
168 emit
logItem(tr("The downloaded file was empty!"),LOGERROR
);
175 TalkGenerator
generator(this);
176 connect(&generator
,SIGNAL(done(bool)),this,SIGNAL(done(bool)));
177 connect(&generator
,SIGNAL(logItem(QString
,int)),this,SIGNAL(logItem(QString
,int)));
178 connect(&generator
,SIGNAL(logProgress(int,int)),this,SIGNAL(logProgress(int,int)));
179 connect(this,SIGNAL(aborted()),&generator
,SLOT(abort()));
181 if(generator
.process(&m_talkList
) == TalkGenerator::eERROR
)
184 emit
logProgress(0,1);
191 emit
logItem(tr("Creating voicefiles..."),LOGINFO
);
192 FILE* ids2
= fopen(filename
.toLocal8Bit(), "r");
196 emit
logItem(tr("Error opening downloaded file"),LOGERROR
);
201 FILE* output
= fopen(QString(m_mountpoint
+ "/.rockbox/langs/" + m_lang
+ ".voice").toLocal8Bit(), "wb");
205 emit
logItem(tr("Error opening output file"),LOGERROR
);
210 voicefont(ids2
,m_targetid
,m_path
.toLocal8Bit().data(), output
);
215 // Add Voice file to the install log
216 QSettings
installlog(m_mountpoint
+ "/.rockbox/rbutil.log", QSettings::IniFormat
, 0);
217 installlog
.beginGroup("selfcreated Voice");
218 installlog
.setValue("/.rockbox/langs/" + m_lang
+ ".voice",QDate::currentDate().toString("yyyyMMdd"));
219 installlog
.endGroup();
222 emit
logProgress(1,1);
223 emit
logItem(tr("successfully created."),LOGOK
);
228 //! \brief Cleans up Files potentially left in the temp dir
230 void VoiceFileCreator::cleanup()
232 emit
logItem(tr("Cleaning up.."),LOGINFO
);
234 for(int i
=0; i
< m_talkList
.size(); i
++)
236 if(QFile::exists(m_talkList
[i
].wavfilename
))
237 QFile::remove(m_talkList
[i
].wavfilename
);
238 if(QFile::exists(m_talkList
[i
].talkfilename
))
239 QFile::remove(m_talkList
[i
].talkfilename
);
241 QCoreApplication::processEvents();
243 emit
logItem(tr("Finished"),LOGINFO
);