OSDN Git Service

Updated Russian translation. Thanks to Иван Митин <bardak@inbox.ru>.
[lamexp/LameXP.git] / src / Encoder_AAC_QAAC.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Encoder_AAC_QAAC.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <math.h>
28 #include <QProcess>
29 #include <QDir>
30 #include <QCoreApplication>
31
32 QAACEncoder::QAACEncoder(void)
33 :
34         m_binary_enc(lamexp_lookup_tool("qaac.exe")),
35         m_binary_dll(lamexp_lookup_tool("libsoxrate.dll"))
36 {
37         if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
38         {
39                 throw "Error initializing QAAC. Tool 'qaac.exe' is not registred!";
40         }
41
42         m_configProfile = 0;
43 }
44
45 QAACEncoder::~QAACEncoder(void)
46 {
47 }
48
49 bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
50 {
51         QProcess process;
52         QStringList args;
53
54         process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
55
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);
59
60         if(m_configRCMode != SettingsModel::VBRMode)
61         {
62                 switch(m_configProfile)
63                 {
64                 case 2:
65                 case 3:
66                         args << "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
67                         break;
68                 }
69         }
70
71         switch(m_configRCMode)
72         {
73         case SettingsModel::CBRMode:
74                 args << "--cbr" << QString::number(qBound(32, m_configBitrate * 8, 500));
75                 break;
76         case SettingsModel::ABRMode:
77                 args << "--abr" << QString::number(qBound(32, m_configBitrate * 8, 500));
78                 break;
79         case SettingsModel::VBRMode:
80                 args << "--tvbr" << QString::number(qBound(0, qRound((static_cast<double>(m_configBitrate * 5) / 100.0) * 127.0), 127));
81                 break;
82         default:
83                 throw "Bad rate-control mode!";
84                 break;
85         }
86
87         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
88
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();
97
98         args << "-d" << ".";
99         args << "-o" << QDir::toNativeSeparators(outputFile);
100         args << QDir::toNativeSeparators(sourceFile);
101
102         if(!startProcess(process, m_binary_enc, args))
103         {
104                 return false;
105         }
106
107         bool bTimeout = false;
108         bool bAborted = false;
109         int prevProgress = -1;
110
111         QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]");
112
113         while(process.state() != QProcess::NotRunning)
114         {
115                 if(*abortFlag)
116                 {
117                         process.kill();
118                         bAborted = true;
119                         emit messageLogged("\nABORTED BY USER !!!");
120                         break;
121                 }
122                 process.waitForReadyRead(m_processTimeoutInterval);
123                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
124                 {
125                         process.kill();
126                         qWarning("QAAC process timed out <-- killing!");
127                         emit messageLogged("\nPROCESS TIMEOUT !!!");
128                         bTimeout = true;
129                         break;
130                 }
131                 while(process.bytesAvailable() > 0)
132                 {
133                         QByteArray line = process.readLine();
134                         QString text = QString::fromUtf8(line.constData()).simplified();
135                         if(regExp.lastIndexIn(text) >= 0)
136                         {
137                                 bool ok = false;
138                                 int progress = regExp.cap(1).toInt(&ok);
139                                 if(ok && (progress > prevProgress))
140                                 {
141                                         emit statusUpdated(progress);
142                                         prevProgress = qMin(progress + 2, 99);
143                                 }
144                         }
145                         else if(!text.isEmpty())
146                         {
147                                 emit messageLogged(text);
148                         }
149                 }
150         }
151
152         process.waitForFinished();
153         if(process.state() != QProcess::NotRunning)
154         {
155                 process.kill();
156                 process.waitForFinished(-1);
157         }
158         
159         emit statusUpdated(100);
160         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
161
162         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
163         {
164                 return false;
165         }
166
167         return true;
168 }
169
170 QString QAACEncoder::extension(void)
171 {
172         return "mp4";
173 }
174
175 bool QAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
176 {
177         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
178         {
179                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
180                 {
181                         return true;
182                 }
183         }
184
185         return false;
186 }
187
188
189 void QAACEncoder::setProfile(int profile)
190 {
191         m_configProfile = profile;
192 }