Adapt for latest MUtils changes.
[LameXP.git] / src / Decoder_MP3.cpp
blob794e0d768cd314e5ae128cb19e172f0ed5e5c2d0
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 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>
37 MP3Decoder::MP3Decoder(void)
39 m_binary(lamexp_tools_lookup("mpg123.exe"))
41 if(m_binary.isEmpty())
43 MUTILS_THROW("Error initializing MPG123 decoder. Tool 'mpg123.exe' is not registred!");
47 MP3Decoder::~MP3Decoder(void)
51 bool MP3Decoder::decode(const QString &sourceFile, const QString &outputFile, volatile bool *abortFlag)
53 QProcess process;
54 QStringList args;
56 args << "-v" << "--utf8" << "-w" << QDir::toNativeSeparators(outputFile);
57 args << QDir::toNativeSeparators(sourceFile);
59 if(!startProcess(process, m_binary, args))
61 return false;
64 bool bTimeout = false;
65 bool bAborted = false;
66 int prevProgress = -1;
68 QRegExp regExp("\\b\\d+\\+\\d+\\s+(\\d+):(\\d+)\\.(\\d+)\\+(\\d+):(\\d+)\\.(\\d+)\\b");
70 while(process.state() != QProcess::NotRunning)
72 if(*abortFlag)
74 process.kill();
75 bAborted = true;
76 emit messageLogged("\nABORTED BY USER !!!");
77 break;
79 process.waitForReadyRead(m_processTimeoutInterval);
80 if(!process.bytesAvailable() && process.state() == QProcess::Running)
82 process.kill();
83 qWarning("mpg123 process timed out <-- killing!");
84 emit messageLogged("\nPROCESS TIMEOUT !!!");
85 bTimeout = true;
86 break;
88 while(process.bytesAvailable() > 0)
90 QByteArray line = process.readLine();
91 QString text = QString::fromUtf8(line.constData()).simplified();
92 if(regExp.lastIndexIn(text) >= 0)
94 quint32 values[6];
95 if (MUtils::regexp_parse_uint32(regExp, values, 6))
97 const double timeDone = (60.0 * double(values[0])) + double(values[1]) + (double(values[2]) / 100.0);
98 const double timeLeft = (60.0 * double(values[3])) + double(values[4]) + (double(values[5]) / 100.0);
99 if ((timeDone >= 0.005) || (timeLeft >= 0.005))
101 const int newProgress = qRound((static_cast<double>(timeDone) / static_cast<double>(timeDone + timeLeft)) * 100.0);
102 if (newProgress > prevProgress)
104 emit statusUpdated(newProgress);
105 prevProgress = qMin(newProgress + 2, 99);
110 else if(!text.isEmpty())
112 emit messageLogged(text);
117 process.waitForFinished();
118 if(process.state() != QProcess::NotRunning)
120 process.kill();
121 process.waitForFinished(-1);
124 emit statusUpdated(100);
125 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
127 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
129 return false;
132 return true;
135 bool MP3Decoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
137 if(containerType.compare("MPEG Audio", Qt::CaseInsensitive) == 0 || containerType.compare("Wave", Qt::CaseInsensitive) == 0)
139 if(formatType.compare("MPEG Audio", Qt::CaseInsensitive) == 0)
141 if(formatProfile.compare("Layer 3", Qt::CaseInsensitive) == 0 || formatProfile.compare("Layer 2", Qt::CaseInsensitive) == 0)
143 if(formatVersion.compare("Version 1", Qt::CaseInsensitive) == 0 || formatVersion.compare("Version 2", Qt::CaseInsensitive) == 0)
145 return true;
151 return false;
154 const AbstractDecoder::supportedType_t *MP3Decoder::supportedTypes(void)
156 static const char *exts[][3] =
158 { "mp3", "mpa", NULL },
159 { "mp2", "mpa", NULL },
160 { "mp1", "mpa", NULL }
163 static const supportedType_t s_supportedTypes[] =
165 { "MPEG Audio Layer III", exts[0] },
166 { "MPEG Audio Layer II", exts[1] },
167 { "MPEG Audio Layer I", exts[2] },
168 { NULL, NULL }
171 return s_supportedTypes;