summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rbutil/rbutilqt/encoders.cpp366
-rw-r--r--rbutil/rbutilqt/encoders.h125
2 files changed, 491 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/encoders.cpp b/rbutil/rbutilqt/encoders.cpp
new file mode 100644
index 0000000000..3eeea02300
--- /dev/null
+++ b/rbutil/rbutilqt/encoders.cpp
@@ -0,0 +1,366 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id: talkfile.cpp 15212 2007-10-19 21:49:07Z domonoky $
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include "encoders.h"
21#include "browsedirtree.h"
22
23
24static QMap<QString,QString> encoderList;
25static QMap<QString,EncBase*> encoderCache;
26
27void initEncoderList()
28{
29 encoderList["rbspeex"] = "Rockbox Speex Encoder";
30 encoderList["lame"] = "Lame Mp3 Encoder";
31}
32
33// function to get a specific encoder
34EncBase* getEncoder(QString encname)
35{
36 QString encoder = encoderList.key(encname);
37
38 // check cache
39 if(encoderCache.contains(encoder))
40 return encoderCache.value(encoder);
41
42 EncBase* enc;
43 if(encoder == "rbspeex")
44 {
45 enc = new EncRbSpeex();
46 encoderCache[encoder] = enc;
47 return enc;
48 }
49 else if(encoder == "lame")
50 {
51 enc = new EncExes(encoder);
52 encoderCache[encoder] = enc;
53 return enc;
54 }
55 else
56 return NULL;
57}
58
59// get the list of encoders, nice names
60QStringList getEncoderList()
61{
62 QStringList encList;
63 QMapIterator<QString, QString> i(encoderList);
64 while (i.hasNext()) {
65 i.next();
66 encList << i.value();
67 }
68
69 return encList;
70}
71
72
73/*********************************************************************
74* Encoder Base
75**********************************************************************/
76EncBase::EncBase(QWidget *parent): QDialog(parent)
77{
78
79}
80
81/*********************************************************************
82* GEneral Exe Encoder
83**********************************************************************/
84EncExes::EncExes(QString name,QWidget *parent) : EncBase(parent)
85{
86 m_name = name;
87
88 m_TemplateMap["lame"] = "\"%exe\" %options \"%input\" \"%output\"";
89
90 ui.setupUi(this);
91 this->hide();
92 connect(ui.reset,SIGNAL(clicked()),this,SLOT(reset()));
93 connect(ui.browse,SIGNAL(clicked()),this,SLOT(browse()));
94}
95
96bool EncExes::start()
97{
98 userSettings->beginGroup(m_name);
99 m_EncExec = userSettings->value("encoderpath","").toString();
100 m_EncOpts = userSettings->value("encoderoptions","").toString();
101 userSettings->endGroup();
102
103 m_EncTemplate = m_TemplateMap.value(m_name);
104
105 QFileInfo enc(m_EncExec);
106 if(enc.exists())
107 {
108 return true;
109 }
110 else
111 {
112 return false;
113 }
114}
115
116bool EncExes::encode(QString input,QString output)
117{
118 //qDebug() << "encoding..";
119 QString execstring = m_EncTemplate;
120
121 execstring.replace("%exe",m_EncExec);
122 execstring.replace("%options",m_EncOpts);
123 execstring.replace("%input",input);
124 execstring.replace("%output",output);
125 qDebug() << execstring;
126 QProcess::execute(execstring);
127 return true;
128}
129
130void EncExes::reset()
131{
132 ui.encoderpath->setText("");
133 ui.encoderoptions->setText("");
134}
135
136void EncExes::showCfg()
137{
138 // try to get config from settings
139 userSettings->beginGroup(m_name);
140 QString exepath =userSettings->value("encoderpath","").toString();
141 ui.encoderoptions->setText(userSettings->value("encoderoptions","").toString());
142 userSettings->endGroup();
143
144 if(exepath == "")
145 {
146
147 // try to autodetect encoder
148#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
149 QStringList path = QString(getenv("PATH")).split(":", QString::SkipEmptyParts);
150#elif defined(Q_OS_WIN)
151 QStringList path = QString(getenv("PATH")).split(";", QString::SkipEmptyParts);
152#endif
153 qDebug() << path;
154
155 for(int i = 0; i < path.size(); i++)
156 {
157 QString executable = QDir::fromNativeSeparators(path.at(i)) + "/" + m_name;
158#if defined(Q_OS_WIN)
159 executable += ".exe";
160 QStringList ex = executable.split("\"", QString::SkipEmptyParts);
161 executable = ex.join("");
162#endif
163 if(QFileInfo(executable).isExecutable())
164 {
165 qDebug() << "found:" << executable;
166 exepath = QDir::toNativeSeparators(executable);
167 break;
168 }
169 }
170 }
171
172 ui.encoderpath->setText(exepath);
173
174 //show dialog
175 this->exec();
176
177}
178
179void EncExes::accept(void)
180{
181 if(userSettings != NULL)
182 {
183 //save settings in user config
184 userSettings->beginGroup(m_name);
185 userSettings->setValue("encoderpath",ui.encoderpath->text());
186 userSettings->setValue("encoderoptions",ui.encoderoptions->text());
187 userSettings->endGroup();
188 // sync settings
189 userSettings->sync();
190 }
191 this->close();
192}
193
194void EncExes::reject(void)
195{
196 this->close();
197}
198
199bool EncExes::configOk()
200{
201 userSettings->beginGroup(m_name);
202 QString path = userSettings->value("encoderpath","").toString();
203 userSettings->endGroup();
204
205 if (QFileInfo(path).exists())
206 return true;
207
208 return false;
209}
210
211void EncExes::browse()
212{
213 BrowseDirtree browser(this);
214 browser.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
215
216 if(QFileInfo(ui.encoderpath->text()).isDir())
217 {
218 browser.setDir(ui.encoderpath->text());
219 }
220 if(browser.exec() == QDialog::Accepted)
221 {
222 qDebug() << browser.getSelected();
223 QString exe = browser.getSelected();
224 if(!QFileInfo(exe).isExecutable())
225 return;
226 ui.encoderpath->setText(exe);
227 }
228}
229
230/*********************************************************************
231* RB SPEEX ENCODER
232**********************************************************************/
233EncRbSpeex::EncRbSpeex(QWidget *parent) : EncBase(parent)
234{
235 ui.setupUi(this);
236 this->hide();
237 connect(ui.reset,SIGNAL(clicked()),this,SLOT(reset()));
238
239 defaultQuality = 8.f;
240 defaultVolume = 1.f;
241 defaultComplexity =3;
242 defaultBand = false;
243
244}
245
246
247bool EncRbSpeex::start()
248{
249 // no user config
250 if(userSettings == NULL)
251 {
252 return false;
253 }
254 // try to get config from settings
255 userSettings->beginGroup("rbspeex");
256 quality = userSettings->value("quality",defaultQuality).toDouble();
257 complexity = userSettings->value("complexity",defaultComplexity).toInt();
258 volume =userSettings->value("volume",defaultVolume).toDouble();
259 narrowband = userSettings->value("narrowband",false).toBool();
260
261 userSettings->endGroup();
262
263 return true;
264}
265
266bool EncRbSpeex::encode(QString input,QString output)
267{
268 //qDebug() << "encoding
269 char errstr[512];
270
271 FILE *fin,*fout;
272 if ((fin = fopen(input.toUtf8(), "rb")) == NULL) {
273 qDebug() << "Error: could not open input file\n";
274 return false;
275 }
276 if ((fout = fopen(output.toUtf8(), "wb")) == NULL) {
277 qDebug() << "Error: could not open output file\n";
278 return false;
279 }
280
281
282 int ret = encode_file(fin, fout, quality, complexity, narrowband, volume,
283 errstr, sizeof(errstr));
284 fclose(fout);
285 fclose(fin);
286
287 if (!ret) {
288 /* Attempt to delete unfinished output */
289 qDebug() << "Error:" << errstr;
290 QFile(output).remove();
291 return false;
292 }
293 return true;
294}
295
296void EncRbSpeex::reset()
297{
298 ui.volume->setValue(defaultVolume);
299 ui.quality->setValue(defaultQuality);
300 ui.complexity->setValue(defaultComplexity);
301 ui.narrowband->setChecked(Qt::Unchecked);
302}
303
304void EncRbSpeex::showCfg()
305{
306 //fill in the usersettings
307 userSettings->beginGroup("rbspeex");
308 ui.volume->setValue(userSettings->value("volume",defaultVolume).toDouble());
309 ui.quality->setValue(userSettings->value("quality",defaultQuality).toDouble());
310 ui.complexity->setValue(userSettings->value("complexity",defaultComplexity).toInt());
311
312 if(userSettings->value("narrowband","False").toString() == "True")
313 ui.narrowband->setCheckState(Qt::Checked);
314 else
315 ui.narrowband->setCheckState(Qt::Unchecked);
316
317 userSettings->endGroup();
318
319 //show dialog
320 this->exec();
321}
322
323void EncRbSpeex::accept(void)
324{
325 if(userSettings != NULL)
326 {
327 //save settings in user config
328 userSettings->beginGroup("rbspeex");
329 userSettings->setValue("volume",ui.volume->value());
330 userSettings->setValue("quality",ui.quality->value());
331 userSettings->setValue("complexity",ui.complexity->value());
332 userSettings->setValue("narrowband",ui.narrowband->isChecked() ? true : false);
333
334 userSettings->endGroup();
335 // sync settings
336 userSettings->sync();
337 }
338 this->close();
339}
340
341void EncRbSpeex::reject(void)
342{
343 this->close();
344}
345
346
347bool EncRbSpeex::configOk()
348{
349 bool result=true;
350 // check config
351 userSettings->beginGroup("rbspeex");
352
353 if(userSettings->value("volume","null").toDouble() <= 0)
354 result =false;
355
356 if(userSettings->value("quality","null").toDouble() <= 0)
357 result =false;
358
359 if(userSettings->value("complexity","null").toInt() <= 0)
360 result =false;
361
362 userSettings->endGroup();
363
364 return result;
365}
366
diff --git a/rbutil/rbutilqt/encoders.h b/rbutil/rbutilqt/encoders.h
new file mode 100644
index 0000000000..f69c3ba611
--- /dev/null
+++ b/rbutil/rbutilqt/encoders.h
@@ -0,0 +1,125 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id: talkfile.h 15212 2007-10-19 21:49:07Z domonoky $
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#ifndef ENCODERS_H
21#define ENCODERS_H
22
23#include "ui_rbspeexcfgfrm.h"
24#include "ui_encexescfgfrm.h"
25#include <QtGui>
26
27extern "C"
28{
29 #include "rbspeex.h"
30}
31
32class EncBase;
33
34//inits the encoder List
35void initEncoderList();
36// function to get a specific encoder
37EncBase* getEncoder(QString encname);
38// get the list of encoders, nice names
39QStringList getEncoderList();
40
41
42
43class EncBase : public QDialog
44{
45 Q_OBJECT
46public:
47 EncBase(QWidget *parent );
48 virtual ~EncBase(){}
49 virtual bool encode(QString input,QString output) = 0;
50 virtual bool start() = 0;
51 virtual bool stop() = 0;
52 virtual void showCfg() = 0;
53 virtual bool configOk()=0;
54
55 void setUserCfg(QSettings *uSettings){userSettings = uSettings;}
56
57public slots:
58 virtual void accept(void)=0;
59 virtual void reject(void)=0;
60 virtual void reset(void)=0;
61
62protected:
63
64 QSettings *userSettings;
65};
66
67
68
69class EncExes : public EncBase
70{
71 Q_OBJECT
72public:
73 EncExes(QString name,QWidget *parent = NULL);
74 virtual bool encode(QString input,QString output);
75 virtual bool start();
76 virtual bool stop() {return true;}
77 virtual void showCfg();
78 virtual bool configOk();
79
80public slots:
81 virtual void accept(void);
82 virtual void reject(void);
83 virtual void reset(void);
84 void browse(void);
85
86private:
87 Ui::EncExesCfgFrm ui;
88 QString m_name;
89 QString m_EncExec;
90 QString m_EncOpts;
91 QMap<QString,QString> m_TemplateMap;
92 QString m_EncTemplate;
93};
94
95class EncRbSpeex : public EncBase
96{
97 Q_OBJECT
98public:
99 EncRbSpeex(QWidget *parent = NULL);
100 virtual bool encode(QString input,QString output);
101 virtual bool start();
102 virtual bool stop() {return true;}
103 virtual void showCfg();
104 virtual bool configOk();
105
106public slots:
107 virtual void accept(void);
108 virtual void reject(void);
109 virtual void reset(void);
110
111private:
112 Ui::RbSpeexCfgFrm ui;
113 float quality;
114 float volume;
115 int complexity;
116 bool narrowband;
117
118 float defaultQuality;
119 float defaultVolume;
120 int defaultComplexity;
121 bool defaultBand;
122};
123
124
125#endif