OSDN Git Service

Set creation/modified time of the encoded file the same value as the original file...
[lamexp/LameXP.git] / src / Encoder_Vorbis.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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_Vorbis.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 VorbisEncoderInfo : public AbstractEncoderInfo
36 {
37         virtual bool isModeSupported(int mode) const
38         {
39                 switch(mode)
40                 {
41                 case SettingsModel::VBRMode:
42                 case SettingsModel::ABRMode:
43                         return true;
44                         break;
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 12;
59                         break;
60                 case SettingsModel::ABRMode:
61                 case SettingsModel::CBRMode:
62                         return 60;
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(-2, index - 2, 10);
75                         break;
76                 case SettingsModel::ABRMode:
77                 case SettingsModel::CBRMode:
78                         return qBound(32, (index + 4) * 8, 500);
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_QUALITY_LEVEL_INT;
91                         break;
92                 case SettingsModel::ABRMode:
93                         return TYPE_APPROX_BITRATE;
94                         break;
95                 case SettingsModel::CBRMode:
96                         return TYPE_BITRATE;
97                         break;
98                 default:
99                         MUTILS_THROW("Bad RC mode specified!");
100                 }
101         }
102
103         virtual const char *description(void) const
104         {
105                 static const char* s_description = "OggEnc2 Vorbis Encoder (aoTuV)";
106                 return s_description;
107         }
108
109         virtual const char *extension(void) const
110         {
111                 static const char* s_extension = "ogg";
112                 return s_extension;
113         }
114 }
115 static const g_vorbisEncoderInfo;
116
117 ///////////////////////////////////////////////////////////////////////////////
118 // Encoder implementation
119 ///////////////////////////////////////////////////////////////////////////////
120
121 VorbisEncoder::VorbisEncoder(void)
122 :
123         m_binary(lamexp_tools_lookup("oggenc2.exe"))
124 {
125         if(m_binary.isEmpty())
126         {
127                 MUTILS_THROW("Error initializing Vorbis encoder. Tool 'oggenc2.exe' is not registred!");
128         }
129
130         m_configBitrateMaximum = 0;
131         m_configBitrateMinimum = 0;
132         m_configSamplingRate = 0;
133 }
134
135 VorbisEncoder::~VorbisEncoder(void)
136 {
137 }
138
139 bool VorbisEncoder::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         const QString baseName = QFileInfo(outputFile).fileName();
144
145         switch(m_configRCMode)
146         {
147         case SettingsModel::VBRMode:
148                 args << "-q" << QString::number(qBound(-2, m_configBitrate - 2, 10));
149                 break;
150         case SettingsModel::ABRMode:
151                 args << "-b" << QString::number(qBound(32, (m_configBitrate + 4) * 8, 500));
152                 break;
153         default:
154                 MUTILS_THROW("Bad rate-control mode!");
155                 break;
156         }
157
158         if((m_configBitrateMaximum > 0) && (m_configBitrateMinimum > 0) && (m_configBitrateMinimum <= m_configBitrateMaximum))
159         {
160                 args << "--min-bitrate" << QString::number(qBound(32, m_configBitrateMinimum, 500));
161                 args << "--max-bitrate" << QString::number(qBound(32, m_configBitrateMaximum, 500));
162         }
163
164         if(m_configSamplingRate > 0)
165         {
166                 args << "--resample" << QString::number(m_configSamplingRate) << "--converter" << QString::number(0);
167         }
168
169         if(!metaInfo.title().isEmpty()) args << "-t" << cleanTag(metaInfo.title());
170         if(!metaInfo.artist().isEmpty()) args << "-a" << cleanTag(metaInfo.artist());
171         if(!metaInfo.album().isEmpty()) args << "-l" << cleanTag(metaInfo.album());
172         if(!metaInfo.genre().isEmpty()) args << "-G" << cleanTag(metaInfo.genre());
173         if(!metaInfo.comment().isEmpty()) args << "-c" << QString("comment=%1").arg(cleanTag(metaInfo.comment()));
174         if(metaInfo.year()) args << "-d" << QString::number(metaInfo.year());
175         if(metaInfo.position()) args << "-N" << QString::number(metaInfo.position());
176         
177         //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
178
179         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
180
181         args << "-o" << QDir::toNativeSeparators(outputFile);
182         args << QDir::toNativeSeparators(sourceFile);
183
184         if(!startProcess(process, m_binary, args))
185         {
186                 return false;
187         }
188
189         bool bTimeout = false;
190         bool bAborted = false;
191         int prevProgress = -1;
192
193         QRegExp regExp("\\[.*(\\d+)[.,](\\d+)%\\]");
194
195         while(process.state() != QProcess::NotRunning)
196         {
197                 if(*abortFlag)
198                 {
199                         process.kill();
200                         bAborted = true;
201                         emit messageLogged("\nABORTED BY USER !!!");
202                         break;
203                 }
204                 process.waitForReadyRead(m_processTimeoutInterval);
205                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
206                 {
207                         process.kill();
208                         qWarning("OggEnc process timed out <-- killing!");
209                         emit messageLogged("\nPROCESS TIMEOUT !!!");
210                         bTimeout = true;
211                         break;
212                 }
213                 while(process.bytesAvailable() > 0)
214                 {
215                         QByteArray line = process.readLine();
216                         QString text = QString::fromUtf8(line.constData()).simplified();
217                         if(regExp.lastIndexIn(text) >= 0)
218                         {
219                                 bool ok = false;
220                                 int progress = regExp.cap(1).toInt(&ok);
221                                 if(ok && (progress > prevProgress))
222                                 {
223                                         emit statusUpdated(progress);
224                                         prevProgress = qMin(progress + 2, 99);
225                                 }
226                         }
227                         else if(!text.isEmpty())
228                         {
229                                 emit messageLogged(text);
230                         }
231                 }
232         }
233
234         process.waitForFinished();
235         if(process.state() != QProcess::NotRunning)
236         {
237                 process.kill();
238                 process.waitForFinished(-1);
239         }
240         
241         emit statusUpdated(100);
242         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
243
244         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
245         {
246                 return false;
247         }
248         
249         return true;
250 }
251
252 bool VorbisEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
253 {
254         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
255         {
256                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
257                 {
258                         return true;
259                 }
260         }
261         else if(containerType.compare("FLAC", Qt::CaseInsensitive) == 0)
262         {
263                 if(formatType.compare("FLAC", Qt::CaseInsensitive) == 0)
264                 {
265                         return true;
266                 }
267         }
268
269         return false;
270 }
271
272 void VorbisEncoder::setBitrateLimits(int minimumBitrate, int maximumBitrate)
273 {
274         m_configBitrateMinimum = minimumBitrate;
275         m_configBitrateMaximum = maximumBitrate;
276 }
277
278 void VorbisEncoder::setSamplingRate(int value)
279 {
280         m_configSamplingRate = value;
281 }
282
283 const AbstractEncoderInfo *VorbisEncoder::getEncoderInfo(void)
284 {
285         return &g_vorbisEncoderInfo;
286 }