Happy new year 2019!
[LameXP.git] / src / Decoder_MP3.cpp
blobc82d2337d781239e10790a458241494a2b867608
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2019 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 QMutex MP3Decoder::m_regexMutex;
40 MUtils::Lazy<QRegExp> MP3Decoder::m_regxLayer([]
42 return new QRegExp(L1S("^Layer\\s+(1|2|3)\\b"), Qt::CaseInsensitive);
43 });
44 MUtils::Lazy<QRegExp> MP3Decoder::m_regxVersion([]
46 return new QRegExp(L1S("^(Version\\s+)?(1|2|2\\.5)\\b"), Qt::CaseInsensitive);
47 });
49 MP3Decoder::MP3Decoder(void)
51 m_binary(lamexp_tools_lookup("mpg123.exe"))
53 if (m_binary.isEmpty())
55 MUTILS_THROW("Error initializing MPG123 decoder. Tool 'mpg123.exe' is not registred!");
59 MP3Decoder::~MP3Decoder(void)
63 bool MP3Decoder::decode(const QString &sourceFile, const QString &outputFile, QAtomicInt &abortFlag)
65 QProcess process;
66 QStringList args;
68 args << "-v" << "--utf8" << "-w" << QDir::toNativeSeparators(outputFile);
69 args << QDir::toNativeSeparators(sourceFile);
71 if (!startProcess(process, m_binary, args))
73 return false;
76 int prevProgress = -1;
77 QRegExp regExp("[_=>]\\s+(\\d+)\\+(\\d+)\\s+");
79 const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &regExp](const QString &text)
81 if (regExp.lastIndexIn(text) >= 0)
83 quint32 values[2];
84 if (MUtils::regexp_parse_uint32(regExp, values, 2))
86 const quint32 total = values[0] + values[1];
87 if ((total >= 512U) && (values[0] >= 256U))
89 const int newProgress = qRound((static_cast<double>(values[0]) / static_cast<double>(total)) * 100.0);
90 if (newProgress > prevProgress)
92 emit statusUpdated(newProgress);
93 prevProgress = NEXT_PROGRESS(newProgress);
97 return true;
99 return false;
102 return (result == RESULT_SUCCESS);
105 bool MP3Decoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
107 static const QLatin1String mpegAudio("MPEG Audio"), waveAudio("Wave");
108 if((containerType.compare(mpegAudio, Qt::CaseInsensitive) == 0) || (containerType.compare(waveAudio, Qt::CaseInsensitive) == 0))
110 if(formatType.compare(mpegAudio, Qt::CaseInsensitive) == 0)
112 QMutexLocker lock(&m_regexMutex);
113 if (m_regxLayer->indexIn(formatProfile) >= 0)
115 return (m_regxVersion->indexIn(formatVersion) >= 0);
120 return false;
123 const AbstractDecoder::supportedType_t *MP3Decoder::supportedTypes(void)
125 static const char *exts[][3] =
127 { "mp3", "mpa", NULL },
128 { "mp2", "mpa", NULL },
129 { "mp1", "mpa", NULL }
132 static const supportedType_t s_supportedTypes[] =
134 { "MPEG Audio Layer III", exts[0] },
135 { "MPEG Audio Layer II", exts[1] },
136 { "MPEG Audio Layer I", exts[2] },
137 { NULL, NULL }
140 return s_supportedTypes;