codec.link must be created with -DCODEC
[kugel-rb.git] / rbutil / rbutilqt / encoders.cpp
blobdcd0e13c6d4a4d261f8f78915ff50567e907db8a
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 "encoders.h"
22 #ifndef CONSOLE
23 #include "encodersgui.h"
24 #include "browsedirtree.h"
25 #else
26 #include "encodersguicli.h"
27 #endif
30 QMap<QString,QString> EncBase::encoderList;
31 QMap<QString,EncBase*> EncBase::encoderCache;
34 // initialize list of encoders
35 void EncBase::initEncodernamesList()
37 encoderList["rbspeex"] = "Rockbox Speex Encoder";
38 encoderList["lame"] = "Lame Mp3 Encoder";
42 // get nice name for a specific encoder
43 QString EncBase::getEncoderName(QString encoder)
45 if(encoderList.isEmpty())
46 initEncodernamesList();
47 return encoderList.value(encoder);
51 // get a specific encoder object
52 EncBase* EncBase::getEncoder(QString encoder)
54 // check cache
55 if(encoderCache.contains(encoder))
56 return encoderCache.value(encoder);
58 EncBase* enc;
59 if(encoder == "lame")
61 enc = new EncExes(encoder);
62 encoderCache[encoder] = enc;
63 return enc;
65 else // rbspeex is default
67 enc = new EncRbSpeex();
68 encoderCache[encoder] = enc;
69 return enc;
74 QStringList EncBase::getEncoderList()
76 if(encoderList.isEmpty())
77 initEncodernamesList();
78 return encoderList.keys();
82 /*********************************************************************
83 * Encoder Base
84 **********************************************************************/
85 EncBase::EncBase(QObject *parent): QObject(parent)
90 /*********************************************************************
91 * GEneral Exe Encoder
92 **********************************************************************/
93 EncExes::EncExes(QString name,QObject *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 #ifndef CONSOLE
137 EncExesGui gui;
138 #else
139 EncExesGuiCli gui;
140 #endif
141 gui.setCfg(settings);
142 gui.showCfg(m_name);
145 bool EncExes::configOk()
147 QString path = settings->encoderPath(m_name);
149 if (QFileInfo(path).exists())
150 return true;
152 return false;
157 /*********************************************************************
158 * RB SPEEX ENCODER
159 **********************************************************************/
160 EncRbSpeex::EncRbSpeex(QObject *parent) : EncBase(parent)
163 defaultQuality = 8.f;
164 defaultVolume = 1.f;
165 defaultComplexity = 10;
166 defaultBand = false;
170 bool EncRbSpeex::start()
173 // try to get config from settings
174 quality = settings->encoderQuality("rbspeex");
175 complexity = settings->encoderComplexity("rbspeex");
176 volume = settings->encoderVolume("rbspeex");
177 narrowband = settings->encoderNarrowband("rbspeex");
180 return true;
183 bool EncRbSpeex::encode(QString input,QString output)
185 qDebug() << "encoding " << input << " to "<< output;
186 char errstr[512];
188 FILE *fin,*fout;
189 if ((fin = fopen(input.toLocal8Bit(), "rb")) == NULL) {
190 qDebug() << "Error: could not open input file\n";
191 return false;
193 if ((fout = fopen(output.toLocal8Bit(), "wb")) == NULL) {
194 qDebug() << "Error: could not open output file\n";
195 return false;
199 int ret = encode_file(fin, fout, quality, complexity, narrowband, volume,
200 errstr, sizeof(errstr));
201 fclose(fout);
202 fclose(fin);
204 if (!ret) {
205 /* Attempt to delete unfinished output */
206 qDebug() << "Error:" << errstr;
207 QFile(output).remove();
208 return false;
210 return true;
214 void EncRbSpeex::showCfg()
216 #ifndef CONSOLE
217 EncRbSpeexGui gui;
218 #else
219 EncRbSpeexGuiCli gui;
220 #endif
221 gui.setCfg(settings);
222 gui.showCfg(defaultQuality,defaultVolume,defaultComplexity,defaultBand);
225 bool EncRbSpeex::configOk()
227 bool result=true;
228 // check config
230 if(settings->encoderVolume("rbspeex") <= 0)
231 result =false;
233 if(settings->encoderQuality("rbspeex") <= 0)
234 result =false;
236 if(settings->encoderComplexity("rbspeex") <= 0)
237 result =false;
239 return result;