1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
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"
25 #include "Model_Settings.h"
30 #include <QCoreApplication>
32 QAACEncoder::QAACEncoder(void)
34 m_binary_enc(lamexp_lookup_tool("qaac.exe")),
35 m_binary_dll(lamexp_lookup_tool("libsoxrate.dll"))
37 if(m_binary_enc
.isEmpty() || m_binary_dll
.isEmpty())
39 throw "Error initializing QAAC. Tool 'qaac.exe' is not registred!";
45 QAACEncoder::~QAACEncoder(void)
49 bool QAACEncoder::encode(const QString
&sourceFile
, const AudioFileModel
&metaInfo
, const QString
&outputFile
, volatile bool *abortFlag
)
54 process
.setWorkingDirectory(QFileInfo(outputFile
).canonicalPath());
56 QProcessEnvironment env
= QProcessEnvironment::systemEnvironment();
57 env
.insert("PATH", QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), lamexp_temp_folder2())));
58 process
.setProcessEnvironment(env
);
60 if(m_configRCMode
!= SettingsModel::VBRMode
)
62 switch(m_configProfile
)
66 args
<< "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
71 switch(m_configRCMode
)
73 case SettingsModel::CBRMode
:
74 args
<< "--cbr" << QString::number(qBound(32, m_configBitrate
* 8, 500));
76 case SettingsModel::ABRMode
:
77 args
<< "--abr" << QString::number(qBound(32, m_configBitrate
* 8, 500));
79 case SettingsModel::VBRMode
:
80 args
<< "--tvbr" << QString::number(qBound(0, qRound((static_cast<double>(m_configBitrate
* 5) / 100.0) * 127.0), 127));
83 throw "Bad rate-control mode!";
87 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
89 if(!metaInfo
.fileName().isEmpty()) args
<< "--title" << metaInfo
.fileName();
90 if(!metaInfo
.fileArtist().isEmpty()) args
<< "--artist" << metaInfo
.fileArtist();
91 if(!metaInfo
.fileAlbum().isEmpty()) args
<< "--album" << metaInfo
.fileAlbum();
92 if(!metaInfo
.fileGenre().isEmpty()) args
<< "--genre" << metaInfo
.fileGenre();
93 if(!metaInfo
.fileComment().isEmpty()) args
<< "--comment" << metaInfo
.fileComment();
94 if(metaInfo
.fileYear()) args
<< "--date" << QString::number(metaInfo
.fileYear());
95 if(metaInfo
.filePosition()) args
<< "--track" << QString::number(metaInfo
.filePosition());
96 if(!metaInfo
.fileCover().isEmpty()) args
<< "--artwork" << metaInfo
.fileCover();
99 args
<< "-o" << QDir::toNativeSeparators(outputFile
);
100 args
<< QDir::toNativeSeparators(sourceFile
);
102 if(!startProcess(process
, m_binary_enc
, args
))
107 bool bTimeout
= false;
108 bool bAborted
= false;
109 int prevProgress
= -1;
111 QRegExp
regExp("\\[(\\d+)\\.(\\d)%\\]");
113 while(process
.state() != QProcess::NotRunning
)
119 emit
messageLogged("\nABORTED BY USER !!!");
122 process
.waitForReadyRead(m_processTimeoutInterval
);
123 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
126 qWarning("QAAC process timed out <-- killing!");
127 emit
messageLogged("\nPROCESS TIMEOUT !!!");
131 while(process
.bytesAvailable() > 0)
133 QByteArray line
= process
.readLine();
134 QString text
= QString::fromUtf8(line
.constData()).simplified();
135 if(regExp
.lastIndexIn(text
) >= 0)
138 int progress
= regExp
.cap(1).toInt(&ok
);
139 if(ok
&& (progress
> prevProgress
))
141 emit
statusUpdated(progress
);
142 prevProgress
= qMin(progress
+ 2, 99);
145 else if(!text
.isEmpty())
147 emit
messageLogged(text
);
152 process
.waitForFinished();
153 if(process
.state() != QProcess::NotRunning
)
156 process
.waitForFinished(-1);
159 emit
statusUpdated(100);
160 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
162 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
170 QString
QAACEncoder::extension(void)
175 bool QAACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
177 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
179 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
189 void QAACEncoder::setProfile(int profile
)
191 m_configProfile
= profile
;