Changed detection of QAAC for the new fully-static build.
[LameXP.git] / src / Encoder_AAC_QAAC.cpp
blob754327bf7e402cec6812c6ec029fe78f832ec0d2
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Encoder_AAC_QAAC.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <math.h>
28 #include <QProcess>
29 #include <QDir>
30 #include <QCoreApplication>
32 QAACEncoder::QAACEncoder(void)
34 m_binary_enc(lamexp_lookup_tool("qaac.exe"))
36 if(m_binary_enc.isEmpty())
38 throw "Error initializing QAAC. Tool 'qaac.exe' is not registred!";
41 m_configProfile = 0;
44 QAACEncoder::~QAACEncoder(void)
48 bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
50 QProcess process;
51 QStringList args;
53 process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
55 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
56 env.insert("PATH", QString("%1;%2").arg(QDir::toNativeSeparators(QDir(QCoreApplication::applicationDirPath()).canonicalPath()), QDir::toNativeSeparators(lamexp_temp_folder2())));
57 env.insert("TEMP", QDir::toNativeSeparators(lamexp_temp_folder2()));
58 env.insert("TMP", QDir::toNativeSeparators(lamexp_temp_folder2()));
59 process.setProcessEnvironment(env);
61 if(m_configRCMode != SettingsModel::VBRMode)
63 switch(m_configProfile)
65 case 2:
66 case 3:
67 args << "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
68 break;
72 switch(m_configRCMode)
74 case SettingsModel::CBRMode:
75 args << "--cbr" << QString::number(qBound(32, m_configBitrate * 8, 500));
76 break;
77 case SettingsModel::ABRMode:
78 args << "--abr" << QString::number(qBound(32, m_configBitrate * 8, 500));
79 break;
80 case SettingsModel::VBRMode:
81 args << "--tvbr" << QString::number(qBound(0, qRound((static_cast<double>(m_configBitrate * 5) / 100.0) * 127.0), 127));
82 break;
83 default:
84 throw "Bad rate-control mode!";
85 break;
88 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
90 if(!metaInfo.fileName().isEmpty()) args << "--title" << metaInfo.fileName();
91 if(!metaInfo.fileArtist().isEmpty()) args << "--artist" << metaInfo.fileArtist();
92 if(!metaInfo.fileAlbum().isEmpty()) args << "--album" << metaInfo.fileAlbum();
93 if(!metaInfo.fileGenre().isEmpty()) args << "--genre" << metaInfo.fileGenre();
94 if(!metaInfo.fileComment().isEmpty()) args << "--comment" << metaInfo.fileComment();
95 if(metaInfo.fileYear()) args << "--date" << QString::number(metaInfo.fileYear());
96 if(metaInfo.filePosition()) args << "--track" << QString::number(metaInfo.filePosition());
97 if(!metaInfo.fileCover().isEmpty()) args << "--artwork" << metaInfo.fileCover();
99 args << "-d" << ".";
100 args << "-o" << QDir::toNativeSeparators(outputFile);
101 args << QDir::toNativeSeparators(sourceFile);
103 if(!startProcess(process, m_binary_enc, args))
105 return false;
108 bool bTimeout = false;
109 bool bAborted = false;
110 int prevProgress = -1;
112 QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]");
114 while(process.state() != QProcess::NotRunning)
116 if(*abortFlag)
118 process.kill();
119 bAborted = true;
120 emit messageLogged("\nABORTED BY USER !!!");
121 break;
123 process.waitForReadyRead(m_processTimeoutInterval);
124 if(!process.bytesAvailable() && process.state() == QProcess::Running)
126 process.kill();
127 qWarning("QAAC process timed out <-- killing!");
128 emit messageLogged("\nPROCESS TIMEOUT !!!");
129 bTimeout = true;
130 break;
132 while(process.bytesAvailable() > 0)
134 QByteArray line = process.readLine();
135 QString text = QString::fromUtf8(line.constData()).simplified();
136 if(regExp.lastIndexIn(text) >= 0)
138 bool ok = false;
139 int progress = regExp.cap(1).toInt(&ok);
140 if(ok && (progress > prevProgress))
142 emit statusUpdated(progress);
143 prevProgress = qMin(progress + 2, 99);
146 else if(!text.isEmpty())
148 emit messageLogged(text);
153 process.waitForFinished();
154 if(process.state() != QProcess::NotRunning)
156 process.kill();
157 process.waitForFinished(-1);
160 emit statusUpdated(100);
161 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
163 if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
165 return false;
168 return true;
171 QString QAACEncoder::extension(void)
173 return "mp4";
176 bool QAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
178 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
180 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
182 return true;
186 return false;
190 void QAACEncoder::setProfile(int profile)
192 m_configProfile = profile;