OSDN Git Service

Increase process timeout interval to 180 seconds. Should avoid Timeout errors on...
[lamexp/LameXP.git] / src / Encoder_MP3.cpp
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.
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_MP3.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <QProcess>
28 #include <QDir>
29 #include <limits.h>
30
31 static const int g_lameAgorithmQualityLUT[] = {9, 7, 5, 2, 0, INT_MAX};
32
33 MP3Encoder::MP3Encoder(void)
34 :
35         m_binary(lamexp_lookup_tool("lame.exe"))
36 {
37         if(m_binary.isEmpty())
38         {
39                 throw "Error initializing MP3 encoder. Tool 'lame.exe' is not registred!";
40         }
41         
42         m_algorithmQuality = 3;
43         m_configBitrateMaximum = 0;
44         m_configBitrateMinimum = 0;
45         m_configSamplingRate = 0;
46         m_configChannelMode = 0;
47 }
48
49 MP3Encoder::~MP3Encoder(void)
50 {
51 }
52
53 bool MP3Encoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
54 {
55         QProcess process;
56         QStringList args;
57
58         args << "--nohist";
59         args << "-q" << QString::number(g_lameAgorithmQualityLUT[m_algorithmQuality]);
60                 
61         switch(m_configRCMode)
62         {
63         case SettingsModel::VBRMode:
64                 args << "-V" << QString::number(9 - min(9, m_configBitrate));
65                 break;
66         case SettingsModel::ABRMode:
67                 args << "--abr" << QString::number(SettingsModel::mp3Bitrates[max(0, min(13, m_configBitrate))]);
68                 break;
69         case SettingsModel::CBRMode:
70                 args << "--cbr";
71                 args << "-b" << QString::number(SettingsModel::mp3Bitrates[max(0, min(13, m_configBitrate))]);
72                 break;
73         default:
74                 throw "Bad rate-control mode!";
75                 break;
76         }
77
78         if((m_configBitrateMaximum > 0) && (m_configBitrateMinimum > 0) && (m_configBitrateMinimum <= m_configBitrateMaximum))
79         {
80                 if(m_configRCMode != SettingsModel::CBRMode)
81                 {
82                         args << "-b" << QString::number(clipBitrate(m_configBitrateMinimum));
83                         args << "-B" << QString::number(clipBitrate(m_configBitrateMaximum));
84                 }
85         }
86
87         if(m_configSamplingRate > 0)
88         {
89                 args << "--resample" << QString::number(m_configSamplingRate);
90         }
91
92         switch(m_configChannelMode)
93         {
94         case 1:
95                 args << "-m" << "j";
96                 break;
97         case 2:
98                 args << "-m" << "f";
99                 break;
100         case 3:
101                 args << "-m" << "s";
102                 break;
103         case 4:
104                 args << "-m" << "d";
105                 break;
106         case 5:
107                 args << "-m" << "m";
108                 break;
109         }
110
111         bool bUseUCS2 = false;
112
113         if(!metaInfo.fileName().isEmpty() && isUnicode(metaInfo.fileName())) bUseUCS2 = true;
114         if(!metaInfo.fileArtist().isEmpty() && isUnicode(metaInfo.fileArtist())) bUseUCS2 = true;
115         if(!metaInfo.fileAlbum().isEmpty() && isUnicode(metaInfo.fileAlbum())) bUseUCS2 = true;
116         if(!metaInfo.fileGenre().isEmpty() && isUnicode(metaInfo.fileGenre())) bUseUCS2 = true;
117         if(!metaInfo.fileComment().isEmpty() && isUnicode(metaInfo.fileComment())) bUseUCS2 = true;
118
119         if(bUseUCS2) args << "--id3v2-ucs2"; //Must specify this BEFORE "--tt" and friends!
120
121         if(!metaInfo.fileName().isEmpty()) args << "--tt" << metaInfo.fileName();
122         if(!metaInfo.fileArtist().isEmpty()) args << "--ta" << metaInfo.fileArtist();
123         if(!metaInfo.fileAlbum().isEmpty()) args << "--tl" << metaInfo.fileAlbum();
124         if(!metaInfo.fileGenre().isEmpty()) args << "--tg" << metaInfo.fileGenre();
125         if(!metaInfo.fileComment().isEmpty()) args << "--tc" << metaInfo.fileComment();
126         if(metaInfo.fileYear()) args << "--ty" << QString::number(metaInfo.fileYear());
127         if(metaInfo.filePosition()) args << "--tn" << QString::number(metaInfo.filePosition());
128         if(!metaInfo.fileCover().isEmpty()) args << "--ti" << QDir::toNativeSeparators(metaInfo.fileCover());
129
130         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
131
132         args << QDir::toNativeSeparators(sourceFile);
133         args << QDir::toNativeSeparators(outputFile);
134
135         if(!startProcess(process, m_binary, args))
136         {
137                 return false;
138         }
139
140         bool bTimeout = false;
141         bool bAborted = false;
142
143         QRegExp regExp("\\(.*(\\d+)%\\)\\|");
144
145         while(process.state() != QProcess::NotRunning)
146         {
147                 if(*abortFlag)
148                 {
149                         process.kill();
150                         bAborted = true;
151                         emit messageLogged("\nABORTED BY USER !!!");
152                         break;
153                 }
154                 process.waitForReadyRead(m_processTimeoutInterval);
155                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
156                 {
157                         process.kill();
158                         qWarning("LAME process timed out <-- killing!");
159                         emit messageLogged("\nPROCESS TIMEOUT !!!");
160                         bTimeout = true;
161                         break;
162                 }
163                 while(process.bytesAvailable() > 0)
164                 {
165                         QByteArray line = process.readLine();
166                         QString text = QString::fromUtf8(line.constData()).simplified();
167                         if(regExp.lastIndexIn(text) >= 0)
168                         {
169                                 bool ok = false;
170                                 int progress = regExp.cap(1).toInt(&ok);
171                                 if(ok) emit statusUpdated(progress);
172                         }
173                         else if(!text.isEmpty())
174                         {
175                                 emit messageLogged(text);
176                         }
177                 }
178         }
179
180         process.waitForFinished();
181         if(process.state() != QProcess::NotRunning)
182         {
183                 process.kill();
184                 process.waitForFinished(-1);
185         }
186         
187         emit statusUpdated(100);
188         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
189
190         if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
191         {
192                 return false;
193         }
194         
195         return true;
196 }
197
198 QString MP3Encoder::extension(void)
199 {
200         return "mp3";
201 }
202
203 bool MP3Encoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
204 {
205         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
206         {
207                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
208                 {
209                         return true;
210                 }
211         }
212         else if(containerType.compare("MPEG Audio", Qt::CaseInsensitive) == 0)
213         {
214                 if(formatType.compare("MPEG Audio", Qt::CaseInsensitive) == 0)
215                 {
216                         if(formatProfile.compare("Layer 3", Qt::CaseInsensitive) == 0 || formatProfile.compare("Layer 2", Qt::CaseInsensitive) == 0)
217                         {
218                                 if(formatVersion.compare("Version 1", Qt::CaseInsensitive) == 0 || formatVersion.compare("Version 2", Qt::CaseInsensitive) == 0)
219                                 {
220                                         return true;
221                                 }
222                         }
223                 }
224         }
225
226         return false;
227 }
228
229 bool MP3Encoder::requiresDownmix(void)
230 {
231         return true;
232 }
233
234 void MP3Encoder::setAlgoQuality(int value)
235 {
236         m_algorithmQuality = value;
237 }
238
239 void MP3Encoder::setBitrateLimits(int minimumBitrate, int maximumBitrate)
240 {
241         m_configBitrateMinimum = minimumBitrate;
242         m_configBitrateMaximum = maximumBitrate;
243 }
244
245 void MP3Encoder::setSamplingRate(int value)
246 {
247         m_configSamplingRate = value;
248 }
249
250 void MP3Encoder::setChannelMode(int value)
251 {
252         m_configChannelMode = value;
253 }
254
255 int MP3Encoder::clipBitrate(int bitrate)
256 {
257         int targetBitrate = min(max(bitrate, 32), 320);
258         
259         int minDiff = INT_MAX;
260         int minIndx = -1;
261
262         for(int i = 0; SettingsModel::mp3Bitrates[i] > 0; i++)
263         {
264                 int currentDiff = abs(targetBitrate - SettingsModel::mp3Bitrates[i]);
265                 if(currentDiff < minDiff)
266                 {
267                         minDiff = currentDiff;
268                         minIndx = i;
269                 }
270         }
271
272         if(minIndx >= 0)
273         {
274                 return SettingsModel::mp3Bitrates[minIndx];
275         }
276
277         return targetBitrate;
278 }