Save the internal name for tts / encoder in the configuration file, not the displayed...
[Rockbox.git] / rbutil / rbutilqt / encoders.cpp
blobbb5474ce4d51f6c8197263d32b1848a49ebe1a8d
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;
31 // initialize list of encoders
32 void initEncodernamesList()
34 encoderList["rbspeex"] = "Rockbox Speex Encoder";
35 encoderList["lame"] = "Lame Mp3 Encoder";
39 // get nice name for a specific encoder
40 QString getEncoderName(QString encoder)
42 if(encoderList.isEmpty())
43 initEncodernamesList();
44 return encoderList.value(encoder);
48 // get a specific encoder object
49 EncBase* getEncoder(QString encoder)
51 // check cache
52 if(encoderCache.contains(encoder))
53 return encoderCache.value(encoder);
55 EncBase* enc;
56 if(encoder == "rbspeex")
58 enc = new EncRbSpeex();
59 encoderCache[encoder] = enc;
60 return enc;
62 else if(encoder == "lame")
64 enc = new EncExes(encoder);
65 encoderCache[encoder] = enc;
66 return enc;
68 else
69 return NULL;
73 QStringList getEncoderList()
75 if(encoderList.isEmpty())
76 initEncodernamesList();
77 return encoderList.keys();
81 /*********************************************************************
82 * Encoder Base
83 **********************************************************************/
84 EncBase::EncBase(QWidget *parent): QDialog(parent)
89 /*********************************************************************
90 * GEneral Exe Encoder
91 **********************************************************************/
92 EncExes::EncExes(QString name,QWidget *parent) : EncBase(parent)
94 m_name = name;
96 m_TemplateMap["lame"] = "\"%exe\" %options \"%input\" \"%output\"";
99 bool EncExes::start()
101 m_EncExec = settings->encoderPath(m_name);
102 m_EncOpts = settings->encoderOptions(m_name);
104 m_EncTemplate = m_TemplateMap.value(m_name);
106 QFileInfo enc(m_EncExec);
107 if(enc.exists())
109 return true;
111 else
113 return false;
117 bool EncExes::encode(QString input,QString output)
119 //qDebug() << "encoding..";
120 QString execstring = m_EncTemplate;
122 execstring.replace("%exe",m_EncExec);
123 execstring.replace("%options",m_EncOpts);
124 execstring.replace("%input",input);
125 execstring.replace("%output",output);
126 qDebug() << execstring;
127 QProcess::execute(execstring);
128 return true;
133 void EncExes::showCfg()
135 EncExesGui gui;
136 gui.setCfg(settings);
137 gui.showCfg(m_name);
140 bool EncExes::configOk()
142 QString path = settings->encoderPath(m_name);
144 if (QFileInfo(path).exists())
145 return true;
147 return false;
152 /*********************************************************************
153 * RB SPEEX ENCODER
154 **********************************************************************/
155 EncRbSpeex::EncRbSpeex(QWidget *parent) : EncBase(parent)
158 defaultQuality = 8.f;
159 defaultVolume = 1.f;
160 defaultComplexity = 10;
161 defaultBand = false;
165 bool EncRbSpeex::start()
168 // try to get config from settings
169 quality = settings->encoderQuality("rbspeex");
170 complexity = settings->encoderComplexity("rbspeex");
171 volume = settings->encoderVolume("rbspeex");
172 narrowband = settings->encoderNarrowband("rbspeex");
175 return true;
178 bool EncRbSpeex::encode(QString input,QString output)
180 //qDebug() << "encoding
181 char errstr[512];
183 FILE *fin,*fout;
184 if ((fin = fopen(input.toUtf8(), "rb")) == NULL) {
185 qDebug() << "Error: could not open input file\n";
186 return false;
188 if ((fout = fopen(output.toUtf8(), "wb")) == NULL) {
189 qDebug() << "Error: could not open output file\n";
190 return false;
194 int ret = encode_file(fin, fout, quality, complexity, narrowband, volume,
195 errstr, sizeof(errstr));
196 fclose(fout);
197 fclose(fin);
199 if (!ret) {
200 /* Attempt to delete unfinished output */
201 qDebug() << "Error:" << errstr;
202 QFile(output).remove();
203 return false;
205 return true;
209 void EncRbSpeex::showCfg()
211 EncRbSpeexGui gui;
212 gui.setCfg(settings);
213 gui.showCfg(defaultQuality,defaultVolume,defaultComplexity,defaultBand);
216 bool EncRbSpeex::configOk()
218 bool result=true;
219 // check config
221 if(settings->encoderVolume("rbspeex") <= 0)
222 result =false;
224 if(settings->encoderQuality("rbspeex") <= 0)
225 result =false;
227 if(settings->encoderComplexity("rbspeex") <= 0)
228 result =false;
230 return result;