Simplified mpg123 progress computation.
[LameXP.git] / src / Decoder_MP3.cpp
blob9c3d9a8ad4396147ab9946555d9871f473302731
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");
74 QRegExp regExp("[_=>]\\s+(\\d+)\\+(\\d+)\\s+");
76 while(process.state() != QProcess::NotRunning)
78 if(checkFlag(abortFlag))
80 process.kill();
81 bAborted = true;
82 emit messageLogged("\nABORTED BY USER !!!");
83 break;
85 process.waitForReadyRead(m_processTimeoutInterval);
86 if(!process.bytesAvailable() && process.state() == QProcess::Running)
88 process.kill();
89 qWarning("mpg123 process timed out <-- killing!");
90 emit messageLogged("\nPROCESS TIMEOUT !!!");
91 bTimeout = true;
92 break;
94 while(process.bytesAvailable() > 0)
96 QByteArray line = process.readLine();
97 QString text = QString::fromUtf8(line.constData()).simplified();
98 if(regExp.lastIndexIn(text) >= 0)
100 quint32 values[2];
101 if (MUtils::regexp_parse_uint32(regExp, values, 2))
103 const quint32 total = values[0] + values[1];
104 if ((total >= 512U) && (values[0] >= 256U))
106 const int newProgress = qRound((static_cast<double>(values[0]) / static_cast<double>(total)) * 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 static 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())
150 m_regxLayer.reset(new QRegExp(L1S("^Layer\\s+(1|2|3)\\b"), Qt::CaseInsensitive));
152 if (m_regxLayer->indexIn(formatProfile) >= 0)
154 if (m_regxVersion.isNull())
156 m_regxVersion.reset(new QRegExp(L1S("^(Version\\s+)?(1|2|2\\.5)\\b"), Qt::CaseInsensitive));
158 return (m_regxVersion->indexIn(formatVersion) >= 0);
163 return false;
166 const AbstractDecoder::supportedType_t *MP3Decoder::supportedTypes(void)
168 static const char *exts[][3] =
170 { "mp3", "mpa", NULL },
171 { "mp2", "mpa", NULL },
172 { "mp1", "mpa", NULL }
175 static const supportedType_t s_supportedTypes[] =
177 { "MPEG Audio Layer III", exts[0] },
178 { "MPEG Audio Layer II", exts[1] },
179 { "MPEG Audio Layer I", exts[2] },
180 { NULL, NULL }
183 return s_supportedTypes;