Adapt for latest MUtils changes.
[LameXP.git] / src / Decoder_ALAC.cpp
blob43099a75727434a92cfb300e292cf9198756b46a
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_ALAC.h"
25 //Internal
26 #include "Global.h"
28 //MUtils
29 #include <MUtils/Exception.h>
31 //Qt
32 #include <QDir>
33 #include <QProcess>
34 #include <QRegExp>
35 #include <QUuid>
37 ALACDecoder::ALACDecoder(void)
39 m_binary(lamexp_tools_lookup("refalac.exe"))
41 if(m_binary.isEmpty())
43 MUTILS_THROW("Error initializing ALAC decoder. Tool 'refalac.exe' is not registred!");
47 ALACDecoder::~ALACDecoder(void)
51 bool ALACDecoder::decode(const QString &sourceFile, const QString &outputFile, volatile bool *abortFlag)
53 QProcess process;
54 QStringList args;
56 args << "--decode";
57 args << "-o" << QDir::toNativeSeparators(outputFile);
58 args << QDir::toNativeSeparators(sourceFile);
60 if(!startProcess(process, m_binary, args))
62 return false;
65 bool bTimeout = false;
66 bool bAborted = false;
67 int prevProgress = -1;
69 //The ALAC Decoder doesn't actually send any status updates :-[
70 //emit statusUpdated(20 + (QUuid::createUuid().data1 % 60));
71 QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]");
73 while(process.state() != QProcess::NotRunning)
75 if(*abortFlag)
77 process.kill();
78 bAborted = true;
79 emit messageLogged("\nABORTED BY USER !!!");
80 break;
82 process.waitForReadyRead(m_processTimeoutInterval);
83 if(!process.bytesAvailable() && process.state() == QProcess::Running)
85 process.kill();
86 qWarning("ALAC process timed out <-- killing!");
87 emit messageLogged("\nPROCESS TIMEOUT !!!");
88 bTimeout = true;
89 break;
91 while(process.bytesAvailable() > 0)
93 QByteArray line = process.readLine();
94 QString text = QString::fromUtf8(line.constData()).simplified();
95 if(regExp.lastIndexIn(text) >= 0)
97 bool ok[2] = {false, false};
98 int intVal[2] = {0, 0};
99 intVal[0] = regExp.cap(1).toInt(&ok[0]);
100 intVal[1] = regExp.cap(2).toInt(&ok[1]);
101 if(ok[0] && ok[1])
103 int progress = qRound(static_cast<double>(intVal[0]) + (static_cast<double>(intVal[1]) / 10.0));
104 if(progress > prevProgress)
106 emit statusUpdated(progress);
107 prevProgress = qMin(progress + 2, 99);
111 else if(!text.isEmpty())
113 emit messageLogged(text);
118 process.waitForFinished();
119 if(process.state() != QProcess::NotRunning)
121 process.kill();
122 process.waitForFinished(-1);
125 emit statusUpdated(100);
126 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
128 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0)
130 return false;
133 return true;
136 bool ALACDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
138 if(containerType.compare("MPEG-4", Qt::CaseInsensitive) == 0)
140 if(formatType.compare("ALAC", Qt::CaseInsensitive) == 0)
142 return true;
146 return false;
149 const AbstractDecoder::supportedType_t *ALACDecoder::supportedTypes(void)
151 static const char *exts[] =
153 "mp4", "m4a", NULL
156 static const supportedType_t s_supportedTypes[] =
158 { "Apple Lossless", exts },
159 { NULL, NULL }
162 return s_supportedTypes;