1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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_MP3.h"
26 #include "Model_Settings.h"
32 static const int g_lameAgorithmQualityLUT
[5] = {7, 5, 2, 0, INT_MAX
};
33 static const int g_mp3BitrateLUT
[15] = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1};
34 static const int g_lameVBRQualityLUT
[11] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, INT_MAX
};
37 QScopedPointer
<QRegExp
> MP3Encoder::m_regxLayer
, MP3Encoder::m_regxVersion
;
38 QMutex
MP3Encoder::m_regexMutex
;
40 ///////////////////////////////////////////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////////////////
44 class MP3EncoderInfo
: public AbstractEncoderInfo
46 virtual bool isModeSupported(int mode
) const
50 case SettingsModel::VBRMode
:
51 case SettingsModel::ABRMode
:
52 case SettingsModel::CBRMode
:
56 MUTILS_THROW("Bad RC mode specified!");
60 virtual int valueCount(int mode
) const
64 case SettingsModel::VBRMode
:
67 case SettingsModel::ABRMode
:
68 case SettingsModel::CBRMode
:
72 MUTILS_THROW("Bad RC mode specified!");
76 virtual int valueAt(int mode
, int index
) const
80 case SettingsModel::VBRMode
:
81 return g_lameVBRQualityLUT
[qBound(0, index
, 9)];
83 case SettingsModel::ABRMode
:
84 case SettingsModel::CBRMode
:
85 return g_mp3BitrateLUT
[qBound(0, index
, 13)];
88 MUTILS_THROW("Bad RC mode specified!");
92 virtual int valueType(int mode
) const
96 case SettingsModel::VBRMode
:
97 return TYPE_QUALITY_LEVEL_INT
;
99 case SettingsModel::ABRMode
:
100 return TYPE_APPROX_BITRATE
;
102 case SettingsModel::CBRMode
:
106 MUTILS_THROW("Bad RC mode specified!");
110 virtual const char *description(void) const
112 static const char* s_description
= "LAME MP3 Encoder";
113 return s_description
;
116 virtual const char *extension(void) const
118 static const char* s_extension
= "mp3";
122 virtual bool isResamplingSupported(void) const
127 static const g_mp3EncoderInfo
;
129 ///////////////////////////////////////////////////////////////////////////////
130 // Encoder implementation
131 ///////////////////////////////////////////////////////////////////////////////
133 MP3Encoder::MP3Encoder(void)
135 m_binary(lamexp_tools_lookup(L1S("lame.exe")))
137 if(m_binary
.isEmpty())
139 MUTILS_THROW("Error initializing MP3 encoder. Tool 'lame.exe' is not registred!");
142 m_algorithmQuality
= 2;
143 m_configBitrateMaximum
= 0;
144 m_configBitrateMinimum
= 0;
145 m_configSamplingRate
= 0;
146 m_configChannelMode
= 0;
149 MP3Encoder::~MP3Encoder(void)
153 bool MP3Encoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const unsigned int channels
, const QString
&outputFile
, QAtomicInt
&abortFlag
)
158 args
<< L1S("--nohist");
159 args
<< L1S("-q") << QString::number(g_lameAgorithmQualityLUT
[m_algorithmQuality
]);
161 switch(m_configRCMode
)
163 case SettingsModel::VBRMode
:
164 args
<< L1S("-V") << QString::number(g_lameVBRQualityLUT
[qBound(0, m_configBitrate
, 9)]);
166 case SettingsModel::ABRMode
:
167 args
<< L1S("--abr") << QString::number(g_mp3BitrateLUT
[qBound(0, m_configBitrate
, 13)]);
169 case SettingsModel::CBRMode
:
170 args
<< L1S("--cbr");
171 args
<< L1S("-b") << QString::number(g_mp3BitrateLUT
[qBound(0, m_configBitrate
, 13)]);
174 MUTILS_THROW("Bad rate-control mode!");
178 if((m_configBitrateMaximum
> 0) && (m_configBitrateMinimum
> 0) && (m_configBitrateMinimum
<= m_configBitrateMaximum
))
180 if(m_configRCMode
!= SettingsModel::CBRMode
)
182 args
<< L1S("-b") << QString::number(clipBitrate(m_configBitrateMinimum
));
183 args
<< L1S("-B") << QString::number(clipBitrate(m_configBitrateMaximum
));
187 if(m_configSamplingRate
> 0)
189 args
<< L1S("--resample") << QString::number(m_configSamplingRate
);
192 switch(m_configChannelMode
)
195 args
<< L1S("-m") << L1S("j");
198 args
<< L1S("-m") << L1S("f");
201 args
<< L1S("-m") << L1S("s");
204 args
<< L1S("-m") << L1S("d");
207 args
<< L1S("-m") << L1S("m");
211 bool bUseUCS2
= false;
213 if(isUnicode(metaInfo
.title())) bUseUCS2
= true;
214 if(isUnicode(metaInfo
.artist())) bUseUCS2
= true;
215 if(isUnicode(metaInfo
.album())) bUseUCS2
= true;
216 if(isUnicode(metaInfo
.genre())) bUseUCS2
= true;
217 if(isUnicode(metaInfo
.comment())) bUseUCS2
= true;
219 if (bUseUCS2
) args
<< L1S("--id3v2-ucs2"); //Must specify this BEFORE "--tt" and friends!
221 if(!metaInfo
.title().isEmpty()) args
<< L1S("--tt") << cleanTag(metaInfo
.title());
222 if(!metaInfo
.artist().isEmpty()) args
<< L1S("--ta") << cleanTag(metaInfo
.artist());
223 if(!metaInfo
.album().isEmpty()) args
<< L1S("--tl") << cleanTag( metaInfo
.album());
224 if(!metaInfo
.genre().isEmpty()) args
<< L1S("--tg") << cleanTag(metaInfo
.genre());
225 if(!metaInfo
.comment().isEmpty()) args
<< L1S("--tc") << cleanTag(metaInfo
.comment());
226 if(metaInfo
.year()) args
<< L1S("--ty") << QString::number(metaInfo
.year());
227 if(metaInfo
.position()) args
<< L1S("--tn") << QString::number(metaInfo
.position());
228 if(!metaInfo
.cover().isEmpty()) args
<< L1S("--ti") << QDir::toNativeSeparators(metaInfo
.cover());
230 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
232 args
<< QDir::toNativeSeparators(sourceFile
);
233 args
<< QDir::toNativeSeparators(outputFile
);
235 if(!startProcess(process
, m_binary
, args
))
240 bool bTimeout
= false;
241 bool bAborted
= false;
242 int prevProgress
= -1;
244 QRegExp
regExp(L1S("\\(.*(\\d+)%\\)\\|"));
246 while(process
.state() != QProcess::NotRunning
)
248 if (checkFlag(abortFlag
))
252 emit
messageLogged(L1S("\nABORTED BY USER !!!"));
255 process
.waitForReadyRead(m_processTimeoutInterval
);
256 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
259 qWarning("LAME process timed out <-- killing!");
260 emit
messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
264 while(process
.bytesAvailable() > 0)
266 QByteArray line
= process
.readLine();
267 QString text
= QString::fromUtf8(line
.constData()).simplified();
268 if(regExp
.lastIndexIn(text
) >= 0)
271 int progress
= regExp
.cap(1).toInt(&ok
);
272 if(ok
&& (progress
> prevProgress
))
274 emit
statusUpdated(progress
);
275 prevProgress
= qMin(progress
+ 2, 99);
278 else if(!text
.isEmpty())
280 emit
messageLogged(text
);
285 process
.waitForFinished();
286 if(process
.state() != QProcess::NotRunning
)
289 process
.waitForFinished(-1);
292 emit
statusUpdated(100);
293 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
295 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
303 bool MP3Encoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
305 if(containerType
.compare(L1S("Wave"), Qt::CaseInsensitive
) == 0)
307 if(formatType
.compare(L1S("PCM"), Qt::CaseInsensitive
) == 0)
313 static const QLatin1String
mpegAudio("MPEG Audio"), waveAudio("Wave"), pcmFormat("PCM");
314 if ((containerType
.compare(mpegAudio
, Qt::CaseInsensitive
) == 0) || (containerType
.compare(waveAudio
, Qt::CaseInsensitive
) == 0))
316 if (formatType
.compare(pcmFormat
, Qt::CaseInsensitive
) == 0)
320 else if (formatType
.compare(mpegAudio
, Qt::CaseInsensitive
) == 0)
322 QMutexLocker
lock(&m_regexMutex
);
323 if (m_regxLayer
.isNull())
325 m_regxLayer
.reset(new QRegExp(L1S("^Layer\\s+(1|2|3)\\b"), Qt::CaseInsensitive
));
327 if (m_regxLayer
->indexIn(formatProfile
) >= 0)
329 if (m_regxVersion
.isNull())
331 m_regxVersion
.reset(new QRegExp(L1S("^(Version\\s+)?(1|2|2\\.5)\\b"), Qt::CaseInsensitive
));
333 return (m_regxVersion
->indexIn(formatVersion
) >= 0);
341 const unsigned int *MP3Encoder::supportedChannelCount(void)
343 static const unsigned int supportedChannels
[] = {1, 2, NULL
};
344 return supportedChannels
;
347 void MP3Encoder::setAlgoQuality(int value
)
349 m_algorithmQuality
= qBound(0, value
, 3);
352 void MP3Encoder::setBitrateLimits(int minimumBitrate
, int maximumBitrate
)
354 m_configBitrateMinimum
= minimumBitrate
;
355 m_configBitrateMaximum
= maximumBitrate
;
358 void MP3Encoder::setChannelMode(int value
)
360 m_configChannelMode
= value
;
363 int MP3Encoder::clipBitrate(int bitrate
)
365 int targetBitrate
= qMin(qMax(bitrate
, 32), 320);
367 int minDiff
= INT_MAX
;
370 for(int i
= 0; g_mp3BitrateLUT
[i
] > 0; i
++)
372 int currentDiff
= abs(targetBitrate
- g_mp3BitrateLUT
[i
]);
373 if(currentDiff
< minDiff
)
375 minDiff
= currentDiff
;
382 return g_mp3BitrateLUT
[minIndx
];
385 return targetBitrate
;
388 const AbstractEncoderInfo
*MP3Encoder::getEncoderInfo(void)
390 return &g_mp3EncoderInfo
;