Happy new year 2016!
[LameXP.git] / src / Encoder_MAC.cpp
blob05018cc3e5e4ea4a49333ccbfa8ec3f49fef0989
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Encoder_MAC.h"
25 #include "Global.h"
26 #include "Model_Settings.h"
28 #include <QProcess>
29 #include <QDir>
31 ///////////////////////////////////////////////////////////////////////////////
32 // Encoder Info
33 ///////////////////////////////////////////////////////////////////////////////
35 class MACEncoderInfo : public AbstractEncoderInfo
37 virtual bool isModeSupported(int mode) const
39 switch(mode)
41 case SettingsModel::VBRMode:
42 return true;
43 break;
44 case SettingsModel::ABRMode:
45 case SettingsModel::CBRMode:
46 return false;
47 break;
48 default:
49 MUTILS_THROW("Bad RC mode specified!");
53 virtual int valueCount(int mode) const
55 switch(mode)
57 case SettingsModel::VBRMode:
58 return 5;
59 break;
60 case SettingsModel::ABRMode:
61 case SettingsModel::CBRMode:
62 return -1;
63 break;
64 default:
65 MUTILS_THROW("Bad RC mode specified!");
69 virtual int valueAt(int mode, int index) const
71 switch(mode)
73 case SettingsModel::VBRMode:
74 return qBound(0, index + 1, 8);
75 break;
76 case SettingsModel::ABRMode:
77 case SettingsModel::CBRMode:
78 return -1;
79 break;
80 default:
81 MUTILS_THROW("Bad RC mode specified!");
85 virtual int valueType(int mode) const
87 switch(mode)
89 case SettingsModel::VBRMode:
90 return TYPE_COMPRESSION_LEVEL;
91 break;
92 case SettingsModel::ABRMode:
93 case SettingsModel::CBRMode:
94 return -1;
95 break;
96 default:
97 MUTILS_THROW("Bad RC mode specified!");
101 virtual const char *description(void) const
103 static const char* s_description = "Monkey's Audio (MAC)";
104 return s_description;
107 virtual const char *extension(void) const
109 static const char* s_extension = "ape";
110 return s_extension;
113 static const g_macEncoderInfo;
115 ///////////////////////////////////////////////////////////////////////////////
116 // Encoder implementation
117 ///////////////////////////////////////////////////////////////////////////////
119 MACEncoder::MACEncoder(void)
121 m_binary_enc(lamexp_tools_lookup("mac.exe")),
122 m_binary_tag(lamexp_tools_lookup("tag.exe"))
124 if(m_binary_enc.isEmpty() || m_binary_tag.isEmpty())
126 MUTILS_THROW("Error initializing MAC encoder. Tool 'mac.exe' or 'tag.exe' is not registred!");
130 MACEncoder::~MACEncoder(void)
134 bool MACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
136 QProcess process;
137 QStringList args;
139 const QString baseName = QFileInfo(outputFile).fileName();
141 args << QDir::toNativeSeparators(sourceFile);
142 args << QDir::toNativeSeparators(outputFile);
144 switch(m_configRCMode)
146 case SettingsModel::VBRMode:
147 args << QString().sprintf("-c%d", (m_configBitrate + 1) * 1000);
148 break;
149 default:
150 MUTILS_THROW("Bad rate-control mode!");
151 break;
154 if(!startProcess(process, m_binary_enc, args))
156 return false;
159 bool bTimeout = false;
160 bool bAborted = false;
161 int prevProgress = -1;
163 QRegExp regExp("Progress: (\\d+).(\\d+)%");
165 while(process.state() != QProcess::NotRunning)
167 if(*abortFlag)
169 process.kill();
170 bAborted = true;
171 emit messageLogged("\nABORTED BY USER !!!");
172 break;
174 process.waitForReadyRead(m_processTimeoutInterval);
175 if(!process.bytesAvailable() && process.state() == QProcess::Running)
177 process.kill();
178 qWarning("MAC process timed out <-- killing!");
179 emit messageLogged("\nPROCESS TIMEOUT !!!");
180 bTimeout = true;
181 break;
183 while(process.bytesAvailable() > 0)
185 QByteArray line = process.readLine();
186 QString text = QString::fromUtf8(line.constData()).simplified();
187 if(regExp.lastIndexIn(text) >= 0)
189 bool ok = false;
190 int progress = regExp.cap(1).toInt(&ok);
191 if(ok && (progress > prevProgress))
193 emit statusUpdated(progress);
194 prevProgress = qMin(progress + 2, 99);
197 else if(!text.isEmpty())
199 emit messageLogged(text);
204 process.waitForFinished();
205 if(process.state() != QProcess::NotRunning)
207 process.kill();
208 process.waitForFinished(-1);
211 emit statusUpdated(100);
212 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
214 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
216 return false;
219 if(metaInfo.empty(true))
221 return true;
224 emit messageLogged("\n-------------------------------\n");
226 args.clear();
227 args << "APE2" << QDir::toNativeSeparators(outputFile);
229 if(!metaInfo.title().isEmpty()) args << QString("Title=%1").arg(cleanTag(metaInfo.title()));
230 if(!metaInfo.artist().isEmpty()) args << QString("Artist=%1").arg(cleanTag(metaInfo.artist()));
231 if(!metaInfo.album().isEmpty()) args << QString("Album=%1").arg(cleanTag(metaInfo.album()));
232 if(!metaInfo.genre().isEmpty()) args << QString("Genre=%1").arg(cleanTag(metaInfo.genre()));
233 if(!metaInfo.comment().isEmpty()) args << QString("Comment=%1").arg(cleanTag(metaInfo.comment()));
234 if(metaInfo.year()) args << QString("Year=%1").arg(QString::number(metaInfo.year()));
235 if(metaInfo.position()) args << QString("Track=%1").arg(QString::number(metaInfo.position()));
237 //if(!metaInfo.cover().isEmpty()) args << QString("-add-cover:%1:%2").arg("front", metaInfo.cover());
239 if(!startProcess(process, m_binary_tag, args))
241 return false;
244 bTimeout = false;
246 while(process.state() != QProcess::NotRunning)
248 if(*abortFlag)
250 process.kill();
251 bAborted = true;
252 emit messageLogged("\nABORTED BY USER !!!");
253 break;
255 process.waitForReadyRead(m_processTimeoutInterval);
256 if(!process.bytesAvailable() && process.state() == QProcess::Running)
258 process.kill();
259 qWarning("Tag process timed out <-- killing!");
260 emit messageLogged("\nPROCESS TIMEOUT !!!");
261 bTimeout = true;
262 break;
264 while(process.bytesAvailable() > 0)
266 QByteArray line = process.readLine();
267 QString text = QString::fromUtf8(line.constData()).simplified();
268 if(!text.isEmpty())
270 emit messageLogged(text);
275 process.waitForFinished();
276 if(process.state() != QProcess::NotRunning)
278 process.kill();
279 process.waitForFinished(-1);
282 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
284 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
286 return false;
289 return true;
292 bool MACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
294 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
296 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
298 return true;
302 return false;
305 const AbstractEncoderInfo *MACEncoder::getEncoderInfo(void)
307 return &g_macEncoderInfo;