OSDN Git Service

The information whether an encoder supports "native" resampling is provided via Abstr...
[lamexp/LameXP.git] / src / Encoder_MAC.cpp
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.
10 //
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.
15 //
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.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Encoder_MAC.h"
24
25 #include "Global.h"
26 #include "Model_Settings.h"
27
28 #include <QProcess>
29 #include <QDir>
30
31 ///////////////////////////////////////////////////////////////////////////////
32 // Encoder Info
33 ///////////////////////////////////////////////////////////////////////////////
34
35 class MACEncoderInfo : public AbstractEncoderInfo
36 {
37         virtual bool isModeSupported(int mode) const
38         {
39                 switch(mode)
40                 {
41                 case SettingsModel::VBRMode:
42                         return true;
43                         break;
44                 case SettingsModel::ABRMode:
45                 case SettingsModel::CBRMode:
46                         return false;
47                         break;
48                 default:
49                         MUTILS_THROW("Bad RC mode specified!");
50                 }
51         }
52
53         virtual int valueCount(int mode) const
54         {
55                 switch(mode)
56                 {
57                 case SettingsModel::VBRMode:
58                         return 5;
59                         break;
60                 case SettingsModel::ABRMode:
61                 case SettingsModel::CBRMode:
62                         return -1;
63                         break;
64                 default:
65                         MUTILS_THROW("Bad RC mode specified!");
66                 }
67         }
68
69         virtual int valueAt(int mode, int index) const
70         {
71                 switch(mode)
72                 {
73                 case SettingsModel::VBRMode:
74                         return qBound(0, index + 1, 8);
75                         break;
76                 case SettingsModel::ABRMode:
77                 case SettingsModel::CBRMode:
78                         return -1;
79                         break;
80                 default:
81                         MUTILS_THROW("Bad RC mode specified!");
82                 }
83         }
84
85         virtual int valueType(int mode) const
86         {
87                 switch(mode)
88                 {
89                 case SettingsModel::VBRMode:
90                         return TYPE_COMPRESSION_LEVEL;
91                         break;
92                 case SettingsModel::ABRMode:
93                 case SettingsModel::CBRMode:
94                         return -1;
95                         break;
96                 default:
97                         MUTILS_THROW("Bad RC mode specified!");
98                 }
99         }
100
101         virtual const char *description(void) const
102         {
103                 static const char* s_description = "Monkey's Audio (MAC)";
104                 return s_description;
105         }
106
107         virtual const char *extension(void) const
108         {
109                 static const char* s_extension = "ape";
110                 return s_extension;
111         }
112
113         virtual bool isResamplingSupported(void) const
114         {
115                 return false;
116         }
117 }
118 static const g_macEncoderInfo;
119
120 ///////////////////////////////////////////////////////////////////////////////
121 // Encoder implementation
122 ///////////////////////////////////////////////////////////////////////////////
123
124 MACEncoder::MACEncoder(void)
125 :
126         m_binary_enc(lamexp_tools_lookup("mac.exe")),
127         m_binary_tag(lamexp_tools_lookup("tag.exe"))
128 {
129         if(m_binary_enc.isEmpty() || m_binary_tag.isEmpty())
130         {
131                 MUTILS_THROW("Error initializing MAC encoder. Tool 'mac.exe' or 'tag.exe' is not registred!");
132         }
133 }
134
135 MACEncoder::~MACEncoder(void)
136 {
137 }
138
139 bool MACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
140 {
141         QProcess process;
142         QStringList args;
143         
144         const QString baseName = QFileInfo(outputFile).fileName();
145
146         args << QDir::toNativeSeparators(sourceFile);
147         args << QDir::toNativeSeparators(outputFile);
148
149         switch(m_configRCMode)
150         {
151         case SettingsModel::VBRMode:
152                 args << QString().sprintf("-c%d", (m_configBitrate + 1) * 1000);
153                 break;
154         default:
155                 MUTILS_THROW("Bad rate-control mode!");
156                 break;
157         }
158
159         if(!startProcess(process, m_binary_enc, args))
160         {
161                 return false;
162         }
163
164         bool bTimeout = false;
165         bool bAborted = false;
166         int prevProgress = -1;
167
168         QRegExp regExp("Progress: (\\d+).(\\d+)%");
169
170         while(process.state() != QProcess::NotRunning)
171         {
172                 if(*abortFlag)
173                 {
174                         process.kill();
175                         bAborted = true;
176                         emit messageLogged("\nABORTED BY USER !!!");
177                         break;
178                 }
179                 process.waitForReadyRead(m_processTimeoutInterval);
180                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
181                 {
182                         process.kill();
183                         qWarning("MAC process timed out <-- killing!");
184                         emit messageLogged("\nPROCESS TIMEOUT !!!");
185                         bTimeout = true;
186                         break;
187                 }
188                 while(process.bytesAvailable() > 0)
189                 {
190                         QByteArray line = process.readLine();
191                         QString text = QString::fromUtf8(line.constData()).simplified();
192                         if(regExp.lastIndexIn(text) >= 0)
193                         {
194                                 bool ok = false;
195                                 int progress = regExp.cap(1).toInt(&ok);
196                                 if(ok && (progress > prevProgress))
197                                 {
198                                         emit statusUpdated(progress);
199                                         prevProgress = qMin(progress + 2, 99);
200                                 }
201                         }
202                         else if(!text.isEmpty())
203                         {
204                                 emit messageLogged(text);
205                         }
206                 }
207         }
208
209         process.waitForFinished();
210         if(process.state() != QProcess::NotRunning)
211         {
212                 process.kill();
213                 process.waitForFinished(-1);
214         }
215         
216         emit statusUpdated(100);
217         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
218
219         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
220         {
221                 return false;
222         }
223
224         if(metaInfo.empty(true))
225         {
226                 return true;
227         }
228
229         emit messageLogged("\n-------------------------------\n");
230         
231         args.clear();
232         args << "APE2" << QDir::toNativeSeparators(outputFile);
233
234         if(!metaInfo.title().isEmpty())   args << QString("Title=%1").arg(cleanTag(metaInfo.title()));
235         if(!metaInfo.artist().isEmpty())  args << QString("Artist=%1").arg(cleanTag(metaInfo.artist()));
236         if(!metaInfo.album().isEmpty())   args << QString("Album=%1").arg(cleanTag(metaInfo.album()));
237         if(!metaInfo.genre().isEmpty())   args << QString("Genre=%1").arg(cleanTag(metaInfo.genre()));
238         if(!metaInfo.comment().isEmpty()) args << QString("Comment=%1").arg(cleanTag(metaInfo.comment()));
239         if(metaInfo.year())               args << QString("Year=%1").arg(QString::number(metaInfo.year()));
240         if(metaInfo.position())           args << QString("Track=%1").arg(QString::number(metaInfo.position()));
241         
242         //if(!metaInfo.cover().isEmpty()) args << QString("-add-cover:%1:%2").arg("front", metaInfo.cover());
243         
244         if(!startProcess(process, m_binary_tag, args))
245         {
246                 return false;
247         }
248
249         bTimeout = false;
250
251         while(process.state() != QProcess::NotRunning)
252         {
253                 if(*abortFlag)
254                 {
255                         process.kill();
256                         bAborted = true;
257                         emit messageLogged("\nABORTED BY USER !!!");
258                         break;
259                 }
260                 process.waitForReadyRead(m_processTimeoutInterval);
261                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
262                 {
263                         process.kill();
264                         qWarning("Tag process timed out <-- killing!");
265                         emit messageLogged("\nPROCESS TIMEOUT !!!");
266                         bTimeout = true;
267                         break;
268                 }
269                 while(process.bytesAvailable() > 0)
270                 {
271                         QByteArray line = process.readLine();
272                         QString text = QString::fromUtf8(line.constData()).simplified();
273                         if(!text.isEmpty())
274                         {
275                                 emit messageLogged(text);
276                         }
277                 }
278         }
279
280         process.waitForFinished();
281         if(process.state() != QProcess::NotRunning)
282         {
283                 process.kill();
284                 process.waitForFinished(-1);
285         }
286                 
287         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
288
289         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
290         {
291                 return false;
292         }
293
294         return true;
295 }
296
297 bool MACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
298 {
299         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
300         {
301                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
302                 {
303                         return true;
304                 }
305         }
306
307         return false;
308 }
309
310 const AbstractEncoderInfo *MACEncoder::getEncoderInfo(void)
311 {
312         return &g_macEncoderInfo;
313 }