Add some accelerator keys to the Actions menu.
[Rockbox.git] / rbutil / rbutilqt / encoders.cpp
blob1399ddff754578b3347a3749813316b1cebd7923
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id: encoders.cpp 15212 2007-10-19 21:49:07Z domonoky $
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 "encoders.h"
21 #include "browsedirtree.h"
23 #ifndef CONSOLE
24 #include "encodersgui.h"
25 #endif
27 static QMap<QString,QString> encoderList;
28 static QMap<QString,EncBase*> encoderCache;
30 void initEncoderList()
32 encoderList["rbspeex"] = "Rockbox Speex Encoder";
33 encoderList["lame"] = "Lame Mp3 Encoder";
36 // function to get a specific encoder
37 EncBase* getEncoder(QString encname)
39 // init list if its empty
40 if(encoderList.count() == 0) initEncoderList();
42 QString encoder = encoderList.key(encname);
44 // check cache
45 if(encoderCache.contains(encoder))
46 return encoderCache.value(encoder);
48 EncBase* enc;
49 if(encoder == "rbspeex")
51 enc = new EncRbSpeex();
52 encoderCache[encoder] = enc;
53 return enc;
55 else if(encoder == "lame")
57 enc = new EncExes(encoder);
58 encoderCache[encoder] = enc;
59 return enc;
61 else
62 return NULL;
65 // get the list of encoders, nice names
66 QStringList getEncoderList()
68 // init list if its empty
69 if(encoderList.count() == 0) initEncoderList();
71 QStringList encList;
72 QMapIterator<QString, QString> i(encoderList);
73 while (i.hasNext()) {
74 i.next();
75 encList << i.value();
78 return encList;
82 /*********************************************************************
83 * Encoder Base
84 **********************************************************************/
85 EncBase::EncBase(QWidget *parent): QDialog(parent)
90 /*********************************************************************
91 * GEneral Exe Encoder
92 **********************************************************************/
93 EncExes::EncExes(QString name,QWidget *parent) : EncBase(parent)
95 m_name = name;
97 m_TemplateMap["lame"] = "\"%exe\" %options \"%input\" \"%output\"";
100 bool EncExes::start()
102 m_EncExec = settings->encoderPath(m_name);
103 m_EncOpts = settings->encoderOptions(m_name);
105 m_EncTemplate = m_TemplateMap.value(m_name);
107 QFileInfo enc(m_EncExec);
108 if(enc.exists())
110 return true;
112 else
114 return false;
118 bool EncExes::encode(QString input,QString output)
120 //qDebug() << "encoding..";
121 QString execstring = m_EncTemplate;
123 execstring.replace("%exe",m_EncExec);
124 execstring.replace("%options",m_EncOpts);
125 execstring.replace("%input",input);
126 execstring.replace("%output",output);
127 qDebug() << execstring;
128 QProcess::execute(execstring);
129 return true;
134 void EncExes::showCfg()
136 EncExesGui gui;
137 gui.setCfg(settings);
138 gui.showCfg(m_name);
141 bool EncExes::configOk()
143 QString path = settings->encoderPath(m_name);
145 if (QFileInfo(path).exists())
146 return true;
148 return false;
153 /*********************************************************************
154 * RB SPEEX ENCODER
155 **********************************************************************/
156 EncRbSpeex::EncRbSpeex(QWidget *parent) : EncBase(parent)
159 defaultQuality = 8.f;
160 defaultVolume = 1.f;
161 defaultComplexity = 10;
162 defaultBand = false;
166 bool EncRbSpeex::start()
169 // try to get config from settings
170 quality = settings->encoderQuality("rbspeex");
171 complexity = settings->encoderComplexity("rbspeex");
172 volume = settings->encoderVolume("rbspeex");
173 narrowband = settings->encoderNarrowband("rbspeex");
176 return true;
179 bool EncRbSpeex::encode(QString input,QString output)
181 //qDebug() << "encoding
182 char errstr[512];
184 FILE *fin,*fout;
185 if ((fin = fopen(input.toUtf8(), "rb")) == NULL) {
186 qDebug() << "Error: could not open input file\n";
187 return false;
189 if ((fout = fopen(output.toUtf8(), "wb")) == NULL) {
190 qDebug() << "Error: could not open output file\n";
191 return false;
195 int ret = encode_file(fin, fout, quality, complexity, narrowband, volume,
196 errstr, sizeof(errstr));
197 fclose(fout);
198 fclose(fin);
200 if (!ret) {
201 /* Attempt to delete unfinished output */
202 qDebug() << "Error:" << errstr;
203 QFile(output).remove();
204 return false;
206 return true;
210 void EncRbSpeex::showCfg()
212 EncRbSpeexGui gui;
213 gui.setCfg(settings);
214 gui.showCfg(defaultQuality,defaultVolume,defaultComplexity,defaultBand);
217 bool EncRbSpeex::configOk()
219 bool result=true;
220 // check config
222 if(settings->encoderVolume("rbspeex") <= 0)
223 result =false;
225 if(settings->encoderQuality("rbspeex") <= 0)
226 result =false;
228 if(settings->encoderComplexity("rbspeex") <= 0)
229 result =false;
231 return result;