1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
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"
29 #include <MUtils/Global.h>
30 #include <MUtils/Exception.h>
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
)
56 args
<< "-v" << "--utf8" << "-w" << QDir::toNativeSeparators(outputFile
);
57 args
<< QDir::toNativeSeparators(sourceFile
);
59 if(!startProcess(process
, m_binary
, args
))
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
)
76 emit
messageLogged("\nABORTED BY USER !!!");
79 process
.waitForReadyRead(m_processTimeoutInterval
);
80 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
83 qWarning("mpg123 process timed out <-- killing!");
84 emit
messageLogged("\nPROCESS TIMEOUT !!!");
88 while(process
.bytesAvailable() > 0)
90 QByteArray line
= process
.readLine();
91 QString text
= QString::fromUtf8(line
.constData()).simplified();
92 if(regExp
.lastIndexIn(text
) >= 0)
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
)
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
)
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)
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] },
171 return s_supportedTypes
;