1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
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"
26 #include "Model_Settings.h"
31 ///////////////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////////////
35 class MACEncoderInfo
: public AbstractEncoderInfo
37 virtual bool isModeSupported(int mode
) const
41 case SettingsModel::VBRMode
:
44 case SettingsModel::ABRMode
:
45 case SettingsModel::CBRMode
:
49 MUTILS_THROW("Bad RC mode specified!");
53 virtual int valueCount(int mode
) const
57 case SettingsModel::VBRMode
:
60 case SettingsModel::ABRMode
:
61 case SettingsModel::CBRMode
:
65 MUTILS_THROW("Bad RC mode specified!");
69 virtual int valueAt(int mode
, int index
) const
73 case SettingsModel::VBRMode
:
74 return qBound(0, index
+ 1, 8);
76 case SettingsModel::ABRMode
:
77 case SettingsModel::CBRMode
:
81 MUTILS_THROW("Bad RC mode specified!");
85 virtual int valueType(int mode
) const
89 case SettingsModel::VBRMode
:
90 return TYPE_COMPRESSION_LEVEL
;
92 case SettingsModel::ABRMode
:
93 case SettingsModel::CBRMode
:
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";
113 virtual bool isResamplingSupported(void) const
118 static const g_macEncoderInfo
;
120 ///////////////////////////////////////////////////////////////////////////////
121 // Encoder implementation
122 ///////////////////////////////////////////////////////////////////////////////
124 MACEncoder::MACEncoder(void)
126 m_binary_enc(lamexp_tools_lookup(L1S("mac.exe"))),
127 m_binary_tag(lamexp_tools_lookup(L1S("tag.exe")))
129 if(m_binary_enc
.isEmpty() || m_binary_tag
.isEmpty())
131 MUTILS_THROW("Error initializing MAC encoder. Tool 'mac.exe' or 'tag.exe' is not registred!");
135 MACEncoder::~MACEncoder(void)
139 bool MACEncoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const QString
&outputFile
, volatile bool *abortFlag
)
144 const QString baseName
= QFileInfo(outputFile
).fileName();
146 args
<< QDir::toNativeSeparators(sourceFile
);
147 args
<< QDir::toNativeSeparators(outputFile
);
149 switch(m_configRCMode
)
151 case SettingsModel::VBRMode
:
152 args
<< QString().sprintf("-c%d", (m_configBitrate
+ 1) * 1000);
155 MUTILS_THROW("Bad rate-control mode!");
159 if(!startProcess(process
, m_binary_enc
, args
))
164 bool bTimeout
= false;
165 bool bAborted
= false;
166 int prevProgress
= -1;
168 QRegExp
regExp(L1S("Progress: (\\d+).(\\d+)%"));
170 while(process
.state() != QProcess::NotRunning
)
176 emit
messageLogged(L1S("\nABORTED BY USER !!!"));
179 process
.waitForReadyRead(m_processTimeoutInterval
);
180 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
183 qWarning("MAC process timed out <-- killing!");
184 emit
messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
188 while(process
.bytesAvailable() > 0)
190 QByteArray line
= process
.readLine();
191 QString text
= QString::fromUtf8(line
.constData()).simplified();
192 if(regExp
.lastIndexIn(text
) >= 0)
195 int progress
= regExp
.cap(1).toInt(&ok
);
196 if(ok
&& (progress
> prevProgress
))
198 emit
statusUpdated(progress
);
199 prevProgress
= qMin(progress
+ 2, 99);
202 else if(!text
.isEmpty())
204 emit
messageLogged(text
);
209 process
.waitForFinished();
210 if(process
.state() != QProcess::NotRunning
)
213 process
.waitForFinished(-1);
216 emit
statusUpdated(100);
217 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
219 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
224 if(metaInfo
.empty(true))
229 emit
messageLogged(L1S("\n-------------------------------\n"));
232 args
<< L1S("APE2") << QDir::toNativeSeparators(outputFile
);
234 if(!metaInfo
.title().isEmpty()) args
<< QString("Title=%1").arg(cleanTag(metaInfo
.title()));
235 if(!metaInfo
.artist().isEmpty()) args
<< QString("Artist=%1").arg(cleanTag(metaInfo
.artist()));
236 if(!metaInfo
.album().isEmpty()) args
<< QString("Album=%1").arg(cleanTag(metaInfo
.album()));
237 if(!metaInfo
.genre().isEmpty()) args
<< QString("Genre=%1").arg(cleanTag(metaInfo
.genre()));
238 if(!metaInfo
.comment().isEmpty()) args
<< QString("Comment=%1").arg(cleanTag(metaInfo
.comment()));
239 if(metaInfo
.year()) args
<< QString("Year=%1").arg(QString::number(metaInfo
.year()));
240 if(metaInfo
.position()) args
<< QString("Track=%1").arg(QString::number(metaInfo
.position()));
242 //if(!metaInfo.cover().isEmpty()) args << QString("-add-cover:%1:%2").arg("front", metaInfo.cover());
244 if(!startProcess(process
, m_binary_tag
, args
))
251 while(process
.state() != QProcess::NotRunning
)
257 emit
messageLogged(L1S("\nABORTED BY USER !!!"));
260 process
.waitForReadyRead(m_processTimeoutInterval
);
261 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
264 qWarning("Tag process timed out <-- killing!");
265 emit
messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
269 while(process
.bytesAvailable() > 0)
271 QByteArray line
= process
.readLine();
272 QString text
= QString::fromUtf8(line
.constData()).simplified();
275 emit
messageLogged(text
);
280 process
.waitForFinished();
281 if(process
.state() != QProcess::NotRunning
)
284 process
.waitForFinished(-1);
287 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
289 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
297 bool MACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
299 if(containerType
.compare(L1S("Wave"), Qt::CaseInsensitive
) == 0)
301 if(formatType
.compare(L1S("PCM"), Qt::CaseInsensitive
) == 0)
310 const AbstractEncoderInfo
*MACEncoder::getEncoderInfo(void)
312 return &g_macEncoderInfo
;