diff options
Diffstat (limited to 'rbutil/rbutilqt/encoders.cpp')
-rw-r--r-- | rbutil/rbutilqt/encoders.cpp | 366 |
1 files changed, 366 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 | |||
24 | static QMap<QString,QString> encoderList; | ||
25 | static QMap<QString,EncBase*> encoderCache; | ||
26 | |||
27 | void initEncoderList() | ||
28 | { | ||
29 | encoderList["rbspeex"] = "Rockbox Speex Encoder"; | ||
30 | encoderList["lame"] = "Lame Mp3 Encoder"; | ||
31 | } | ||
32 | |||
33 | // function to get a specific encoder | ||
34 | EncBase* 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 | ||
60 | QStringList 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 | **********************************************************************/ | ||
76 | EncBase::EncBase(QWidget *parent): QDialog(parent) | ||
77 | { | ||
78 | |||
79 | } | ||
80 | |||
81 | /********************************************************************* | ||
82 | * GEneral Exe Encoder | ||
83 | **********************************************************************/ | ||
84 | EncExes::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 | |||
96 | bool 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 | |||
116 | bool 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 | |||
130 | void EncExes::reset() | ||
131 | { | ||
132 | ui.encoderpath->setText(""); | ||
133 | ui.encoderoptions->setText(""); | ||
134 | } | ||
135 | |||
136 | void 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 | |||
179 | void 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 | |||
194 | void EncExes::reject(void) | ||
195 | { | ||
196 | this->close(); | ||
197 | } | ||
198 | |||
199 | bool 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 | |||
211 | void 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 | **********************************************************************/ | ||
233 | EncRbSpeex::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 | |||
247 | bool 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 | |||
266 | bool 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 | |||
296 | void 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 | |||
304 | void 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 | |||
323 | void 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 | |||
341 | void EncRbSpeex::reject(void) | ||
342 | { | ||
343 | this->close(); | ||
344 | } | ||
345 | |||
346 | |||
347 | bool 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 | |||