diff options
Diffstat (limited to 'rbutil/rbutilqt/base/encoders.cpp')
-rw-r--r-- | rbutil/rbutilqt/base/encoders.cpp | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/base/encoders.cpp b/rbutil/rbutilqt/base/encoders.cpp new file mode 100644 index 0000000000..6ff1185c82 --- /dev/null +++ b/rbutil/rbutilqt/base/encoders.cpp | |||
@@ -0,0 +1,233 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * Copyright (C) 2007 by Dominik Wenger | ||
10 | * $Id$ | ||
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 "utils.h" | ||
22 | |||
23 | /********************************************************************* | ||
24 | * Encoder Base | ||
25 | **********************************************************************/ | ||
26 | QMap<QString,QString> EncBase::encoderList; | ||
27 | |||
28 | EncBase::EncBase(QObject *parent): EncTtsSettingInterface(parent) | ||
29 | { | ||
30 | |||
31 | } | ||
32 | |||
33 | // initialize list of encoders | ||
34 | void EncBase::initEncodernamesList() | ||
35 | { | ||
36 | encoderList["rbspeex"] = "Rockbox Speex Encoder"; | ||
37 | encoderList["lame"] = "Lame Mp3 Encoder"; | ||
38 | } | ||
39 | |||
40 | |||
41 | // get nice name for a specific encoder | ||
42 | QString EncBase::getEncoderName(QString encoder) | ||
43 | { | ||
44 | if(encoderList.isEmpty()) | ||
45 | initEncodernamesList(); | ||
46 | return encoderList.value(encoder); | ||
47 | } | ||
48 | |||
49 | |||
50 | // get a specific encoder object | ||
51 | EncBase* EncBase::getEncoder(QObject* parent,QString encoder) | ||
52 | { | ||
53 | EncBase* enc; | ||
54 | if(encoder == "lame") | ||
55 | { | ||
56 | enc = new EncExes(encoder,parent); | ||
57 | return enc; | ||
58 | } | ||
59 | else // rbspeex is default | ||
60 | { | ||
61 | enc = new EncRbSpeex(parent); | ||
62 | return enc; | ||
63 | } | ||
64 | } | ||
65 | |||
66 | |||
67 | QStringList EncBase::getEncoderList() | ||
68 | { | ||
69 | if(encoderList.isEmpty()) | ||
70 | initEncodernamesList(); | ||
71 | return encoderList.keys(); | ||
72 | } | ||
73 | |||
74 | |||
75 | /********************************************************************* | ||
76 | * GEneral Exe Encoder | ||
77 | **********************************************************************/ | ||
78 | EncExes::EncExes(QString name,QObject *parent) : EncBase(parent) | ||
79 | { | ||
80 | m_name = name; | ||
81 | |||
82 | m_TemplateMap["lame"] = "\"%exe\" %options \"%input\" \"%output\""; | ||
83 | |||
84 | } | ||
85 | |||
86 | |||
87 | |||
88 | void EncExes::generateSettings() | ||
89 | { | ||
90 | QString exepath =settings->subValue(m_name,RbSettings::EncoderPath).toString(); | ||
91 | if(exepath == "") exepath = findExecutable(m_name); | ||
92 | |||
93 | insertSetting(eEXEPATH,new EncTtsSetting(this,EncTtsSetting::eSTRING,"Path to Encoder:",exepath,EncTtsSetting::eBROWSEBTN)); | ||
94 | insertSetting(eEXEOPTIONS,new EncTtsSetting(this,EncTtsSetting::eSTRING,"Encoder options:",settings->subValue(m_name,RbSettings::EncoderOptions))); | ||
95 | } | ||
96 | |||
97 | void EncExes::saveSettings() | ||
98 | { | ||
99 | settings->setSubValue(m_name,RbSettings::EncoderPath,getSetting(eEXEPATH)->current().toString()); | ||
100 | settings->setSubValue(m_name,RbSettings::EncoderOptions,getSetting(eEXEOPTIONS)->current().toString()); | ||
101 | settings->sync(); | ||
102 | } | ||
103 | |||
104 | bool EncExes::start() | ||
105 | { | ||
106 | m_EncExec = settings->subValue(m_name, RbSettings::EncoderPath).toString(); | ||
107 | m_EncOpts = settings->subValue(m_name, RbSettings::EncoderOptions).toString(); | ||
108 | |||
109 | m_EncTemplate = m_TemplateMap.value(m_name); | ||
110 | |||
111 | QFileInfo enc(m_EncExec); | ||
112 | if(enc.exists()) | ||
113 | { | ||
114 | return true; | ||
115 | } | ||
116 | else | ||
117 | { | ||
118 | return false; | ||
119 | } | ||
120 | } | ||
121 | |||
122 | bool EncExes::encode(QString input,QString output) | ||
123 | { | ||
124 | //qDebug() << "encoding.."; | ||
125 | QString execstring = m_EncTemplate; | ||
126 | |||
127 | execstring.replace("%exe",m_EncExec); | ||
128 | execstring.replace("%options",m_EncOpts); | ||
129 | execstring.replace("%input",input); | ||
130 | execstring.replace("%output",output); | ||
131 | qDebug() << execstring; | ||
132 | QProcess::execute(execstring); | ||
133 | return true; | ||
134 | } | ||
135 | |||
136 | |||
137 | bool EncExes::configOk() | ||
138 | { | ||
139 | QString path = settings->subValue(m_name, RbSettings::EncoderPath).toString(); | ||
140 | |||
141 | if (QFileInfo(path).exists()) | ||
142 | return true; | ||
143 | |||
144 | return false; | ||
145 | } | ||
146 | |||
147 | /********************************************************************* | ||
148 | * RB SPEEX ENCODER | ||
149 | **********************************************************************/ | ||
150 | EncRbSpeex::EncRbSpeex(QObject *parent) : EncBase(parent) | ||
151 | { | ||
152 | |||
153 | } | ||
154 | |||
155 | void EncRbSpeex::generateSettings() | ||
156 | { | ||
157 | insertSetting(eVOLUME,new EncTtsSetting(this,EncTtsSetting::eDOUBLE,"Volume:",settings->subValue("rbspeex",RbSettings::EncoderVolume),1.0,10.0)); | ||
158 | insertSetting(eQUALITY,new EncTtsSetting(this,EncTtsSetting::eDOUBLE,"Quality:",settings->subValue("rbspeex",RbSettings::EncoderQuality),0,10.0)); | ||
159 | insertSetting(eCOMPLEXITY,new EncTtsSetting(this,EncTtsSetting::eINT,"Complexity:",settings->subValue("rbspeex",RbSettings::EncoderComplexity),0,10)); | ||
160 | insertSetting(eNARROWBAND,new EncTtsSetting(this,EncTtsSetting::eBOOL,"Use Narrowband:",settings->subValue("rbspeex",RbSettings::EncoderNarrowBand))); | ||
161 | } | ||
162 | |||
163 | void EncRbSpeex::saveSettings() | ||
164 | { | ||
165 | //save settings in user config | ||
166 | settings->setSubValue("rbspeex",RbSettings::EncoderVolume,getSetting(eVOLUME)->current().toDouble()); | ||
167 | settings->setSubValue("rbspeex",RbSettings::EncoderQuality,getSetting(eQUALITY)->current().toDouble()); | ||
168 | settings->setSubValue("rbspeex",RbSettings::EncoderComplexity,getSetting(eCOMPLEXITY)->current().toInt()); | ||
169 | settings->setSubValue("rbspeex",RbSettings::EncoderNarrowBand,getSetting(eNARROWBAND)->current().toBool()); | ||
170 | |||
171 | settings->sync(); | ||
172 | } | ||
173 | |||
174 | bool EncRbSpeex::start() | ||
175 | { | ||
176 | |||
177 | // try to get config from settings | ||
178 | quality = settings->subValue("rbspeex", RbSettings::EncoderQuality).toDouble(); | ||
179 | complexity = settings->subValue("rbspeex", RbSettings::EncoderComplexity).toInt(); | ||
180 | volume = settings->subValue("rbspeex", RbSettings::EncoderVolume).toDouble(); | ||
181 | narrowband = settings->subValue("rbspeex", RbSettings::EncoderNarrowBand).toBool(); | ||
182 | |||
183 | |||
184 | return true; | ||
185 | } | ||
186 | |||
187 | bool EncRbSpeex::encode(QString input,QString output) | ||
188 | { | ||
189 | qDebug() << "encoding " << input << " to "<< output; | ||
190 | char errstr[512]; | ||
191 | |||
192 | FILE *fin,*fout; | ||
193 | if ((fin = fopen(input.toLocal8Bit(), "rb")) == NULL) { | ||
194 | qDebug() << "Error: could not open input file\n"; | ||
195 | return false; | ||
196 | } | ||
197 | if ((fout = fopen(output.toLocal8Bit(), "wb")) == NULL) { | ||
198 | qDebug() << "Error: could not open output file\n"; | ||
199 | return false; | ||
200 | } | ||
201 | |||
202 | |||
203 | int ret = encode_file(fin, fout, quality, complexity, narrowband, volume, | ||
204 | errstr, sizeof(errstr)); | ||
205 | fclose(fout); | ||
206 | fclose(fin); | ||
207 | |||
208 | if (!ret) { | ||
209 | /* Attempt to delete unfinished output */ | ||
210 | qDebug() << "Error:" << errstr; | ||
211 | QFile(output).remove(); | ||
212 | return false; | ||
213 | } | ||
214 | return true; | ||
215 | } | ||
216 | |||
217 | bool EncRbSpeex::configOk() | ||
218 | { | ||
219 | bool result=true; | ||
220 | // check config | ||
221 | |||
222 | if(settings->subValue("rbspeex", RbSettings::EncoderVolume).toDouble() <= 0) | ||
223 | result =false; | ||
224 | |||
225 | if(settings->subValue("rbspeex", RbSettings::EncoderQuality).toDouble() <= 0) | ||
226 | result =false; | ||
227 | |||
228 | if(settings->subValue("rbspeex", RbSettings::EncoderComplexity).toInt() <= 0) | ||
229 | result =false; | ||
230 | |||
231 | return result; | ||
232 | } | ||
233 | |||