Happy new year 2018!
[LameXP.git] / src / Decoder_MP3.cpp
blob073c3318d0dbb2363d5fa1ae74abee9effe566da
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2018 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 "Decoder_MP3.h"
25 //Internal
26 #include "Global.h"
28 //MUtils
29 #include <MUtils/Global.h>
30 #include <MUtils/Exception.h>
32 //Qt
33 #include <QDir>
34 #include <QProcess>
35 #include <QRegExp>
36 #include <QMutexLocker>
38 //Static
39 QScopedPointer<QRegExp> MP3Decoder::m_regxLayer, MP3Decoder::m_regxVersion;
40 QMutex MP3Decoder::m_regexMutex;
42 MP3Decoder::MP3Decoder(void)
44 m_binary(lamexp_tools_lookup("mpg123.exe"))
46 if (m_binary.isEmpty())
48 MUTILS_THROW("Error initializing MPG123 decoder. Tool 'mpg123.exe' is not registred!");
52 MP3Decoder::~MP3Decoder(void)
56 bool MP3Decoder::decode(const QString &sourceFile, const QString &outputFile, QAtomicInt &abortFlag)
58 QProcess process;
59 QStringList args;
61 args << "-v" << "--utf8" << "-w" << QDir::toNativeSeparators(outputFile);
62 args << QDir::toNativeSeparators(sourceFile);
64 if (!startProcess(process, m_binary, args))
66 return false;
69 int prevProgress = -1;
70 QRegExp regExp("[_=>]\\s+(\\d+)\\+(\\d+)\\s+");
72 const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &regExp](const QString &text)
74 if (regExp.lastIndexIn(text) >= 0)
76 quint32 values[2];
77 if (MUtils::regexp_parse_uint32(regExp, values, 2))
79 const quint32 total = values[0] + values[1];
80 if ((total >= 512U) && (values[0] >= 256U))
82 const int newProgress = qRound((static_cast<double>(values[0]) / static_cast<double>(total)) * 100.0);
83 if (newProgress > prevProgress)
85 emit statusUpdated(newProgress);
86 prevProgress = NEXT_PROGRESS(newProgress);
90 return true;
92 return false;
93 });
95 return (result == RESULT_SUCCESS);
98 bool MP3Decoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
100 static const QLatin1String mpegAudio("MPEG Audio"), waveAudio("Wave");
101 if((containerType.compare(mpegAudio, Qt::CaseInsensitive) == 0) || (containerType.compare(waveAudio, Qt::CaseInsensitive) == 0))
103 if(formatType.compare(mpegAudio, Qt::CaseInsensitive) == 0)
105 QMutexLocker lock(&m_regexMutex);
106 if (m_regxLayer.isNull())
108 m_regxLayer.reset(new QRegExp(L1S("^Layer\\s+(1|2|3)\\b"), Qt::CaseInsensitive));
110 if (m_regxLayer->indexIn(formatProfile) >= 0)
112 if (m_regxVersion.isNull())
114 m_regxVersion.reset(new QRegExp(L1S("^(Version\\s+)?(1|2|2\\.5)\\b"), Qt::CaseInsensitive));
116 return (m_regxVersion->indexIn(formatVersion) >= 0);
121 return false;
124 const AbstractDecoder::supportedType_t *MP3Decoder::supportedTypes(void)
126 static const char *exts[][3] =
128 { "mp3", "mpa", NULL },
129 { "mp2", "mpa", NULL },
130 { "mp1", "mpa", NULL }
133 static const supportedType_t s_supportedTypes[] =
135 { "MPEG Audio Layer III", exts[0] },
136 { "MPEG Audio Layer II", exts[1] },
137 { "MPEG Audio Layer I", exts[2] },
138 { NULL, NULL }
141 return s_supportedTypes;