Adapted MediaInfo parsing code for new MediaInfo XML output.
[LameXP.git] / src / Decoder_MP3.cpp
blobd293f0f23149710fb64a92374d19d64ed134fb40
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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 bool bTimeout = false;
70 bool bAborted = false;
71 int prevProgress = -1;
73 QRegExp regExp("\\b\\d+\\+\\d+\\s+(\\d+):(\\d+)\\.(\\d+)\\+(\\d+):(\\d+)\\.(\\d+)\\b");
75 while(process.state() != QProcess::NotRunning)
77 if(checkFlag(abortFlag))
79 process.kill();
80 bAborted = true;
81 emit messageLogged("\nABORTED BY USER !!!");
82 break;
84 process.waitForReadyRead(m_processTimeoutInterval);
85 if(!process.bytesAvailable() && process.state() == QProcess::Running)
87 process.kill();
88 qWarning("mpg123 process timed out <-- killing!");
89 emit messageLogged("\nPROCESS TIMEOUT !!!");
90 bTimeout = true;
91 break;
93 while(process.bytesAvailable() > 0)
95 QByteArray line = process.readLine();
96 QString text = QString::fromUtf8(line.constData()).simplified();
97 if(regExp.lastIndexIn(text) >= 0)
99 quint32 values[6];
100 if (MUtils::regexp_parse_uint32(regExp, values, 6))
102 const double timeDone = (60.0 * double(values[0])) + double(values[1]) + (double(values[2]) / 100.0);
103 const double timeLeft = (60.0 * double(values[3])) + double(values[4]) + (double(values[5]) / 100.0);
104 if ((timeDone >= 0.005) || (timeLeft >= 0.005))
106 const int newProgress = qRound((static_cast<double>(timeDone) / static_cast<double>(timeDone + timeLeft)) * 100.0);
107 if (newProgress > prevProgress)
109 emit statusUpdated(newProgress);
110 prevProgress = qMin(newProgress + 2, 99);
115 else if(!text.isEmpty())
117 emit messageLogged(text);
122 process.waitForFinished();
123 if(process.state() != QProcess::NotRunning)
125 process.kill();
126 process.waitForFinished(-1);
129 emit statusUpdated(100);
130 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
132 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
134 return false;
137 return true;
140 bool MP3Decoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
142 const QLatin1String mpegAudio("MPEG Audio"), waveAudio("Wave");
143 if((containerType.compare(mpegAudio, Qt::CaseInsensitive) == 0) || (containerType.compare(waveAudio, Qt::CaseInsensitive) == 0))
145 if(formatType.compare(mpegAudio, Qt::CaseInsensitive) == 0)
147 QMutexLocker lock(&m_regexMutex);
148 if (m_regxLayer.isNull() || m_regxVersion.isNull())
150 m_regxLayer.reset(new QRegExp("\\bLayer\\s+(1|2|3)\\b", Qt::CaseInsensitive));
151 m_regxVersion.reset(new QRegExp("\\bVersion\\s+(1|2|2\\.5)\\b", Qt::CaseInsensitive));
153 if (m_regxLayer->indexIn(formatProfile) >= 0)
155 return (m_regxVersion->indexIn(formatVersion) >= 0);
160 return false;
163 const AbstractDecoder::supportedType_t *MP3Decoder::supportedTypes(void)
165 static const char *exts[][3] =
167 { "mp3", "mpa", NULL },
168 { "mp2", "mpa", NULL },
169 { "mp1", "mpa", NULL }
172 static const supportedType_t s_supportedTypes[] =
174 { "MPEG Audio Layer III", exts[0] },
175 { "MPEG Audio Layer II", exts[1] },
176 { "MPEG Audio Layer I", exts[2] },
177 { NULL, NULL }
180 return s_supportedTypes;