1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
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.
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.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Encoder_Vorbis.h"
26 #include "Model_Settings.h"
31 ///////////////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////////////
35 class VorbisEncoderInfo
: public AbstractEncoderInfo
37 virtual bool isModeSupported(int mode
) const
41 case SettingsModel::VBRMode
:
42 case SettingsModel::ABRMode
:
45 case SettingsModel::CBRMode
:
49 MUTILS_THROW("Bad RC mode specified!");
53 virtual int valueCount(int mode
) const
57 case SettingsModel::VBRMode
:
60 case SettingsModel::ABRMode
:
61 case SettingsModel::CBRMode
:
65 MUTILS_THROW("Bad RC mode specified!");
69 virtual int valueAt(int mode
, int index
) const
73 case SettingsModel::VBRMode
:
74 return qBound(-2, index
- 2, 10);
76 case SettingsModel::ABRMode
:
77 case SettingsModel::CBRMode
:
78 return qBound(32, (index
+ 4) * 8, 500);
81 MUTILS_THROW("Bad RC mode specified!");
85 virtual int valueType(int mode
) const
89 case SettingsModel::VBRMode
:
90 return TYPE_QUALITY_LEVEL_INT
;
92 case SettingsModel::ABRMode
:
93 return TYPE_APPROX_BITRATE
;
95 case SettingsModel::CBRMode
:
99 MUTILS_THROW("Bad RC mode specified!");
103 virtual const char *description(void) const
105 static const char* s_description
= "OggEnc2 Vorbis Encoder (aoTuV)";
106 return s_description
;
109 static const g_vorbisEncoderInfo
;
111 ///////////////////////////////////////////////////////////////////////////////
112 // Encoder implementation
113 ///////////////////////////////////////////////////////////////////////////////
115 VorbisEncoder::VorbisEncoder(void)
117 m_binary(lamexp_tools_lookup("oggenc2.exe"))
119 if(m_binary
.isEmpty())
121 MUTILS_THROW("Error initializing Vorbis encoder. Tool 'oggenc2.exe' is not registred!");
124 m_configBitrateMaximum
= 0;
125 m_configBitrateMinimum
= 0;
126 m_configSamplingRate
= 0;
129 VorbisEncoder::~VorbisEncoder(void)
133 bool VorbisEncoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const QString
&outputFile
, volatile bool *abortFlag
)
137 const QString baseName
= QFileInfo(outputFile
).fileName();
139 switch(m_configRCMode
)
141 case SettingsModel::VBRMode
:
142 args
<< "-q" << QString::number(qBound(-2, m_configBitrate
- 2, 10));
144 case SettingsModel::ABRMode
:
145 args
<< "-b" << QString::number(qBound(32, (m_configBitrate
+ 4) * 8, 500));
148 MUTILS_THROW("Bad rate-control mode!");
152 if((m_configBitrateMaximum
> 0) && (m_configBitrateMinimum
> 0) && (m_configBitrateMinimum
<= m_configBitrateMaximum
))
154 args
<< "--min-bitrate" << QString::number(qBound(32, m_configBitrateMinimum
, 500));
155 args
<< "--max-bitrate" << QString::number(qBound(32, m_configBitrateMaximum
, 500));
158 if(m_configSamplingRate
> 0)
160 args
<< "--resample" << QString::number(m_configSamplingRate
) << "--converter" << QString::number(0);
163 if(!metaInfo
.title().isEmpty()) args
<< "-t" << cleanTag(metaInfo
.title());
164 if(!metaInfo
.artist().isEmpty()) args
<< "-a" << cleanTag(metaInfo
.artist());
165 if(!metaInfo
.album().isEmpty()) args
<< "-l" << cleanTag(metaInfo
.album());
166 if(!metaInfo
.genre().isEmpty()) args
<< "-G" << cleanTag(metaInfo
.genre());
167 if(!metaInfo
.comment().isEmpty()) args
<< "-c" << QString("comment=%1").arg(cleanTag(metaInfo
.comment()));
168 if(metaInfo
.year()) args
<< "-d" << QString::number(metaInfo
.year());
169 if(metaInfo
.position()) args
<< "-N" << QString::number(metaInfo
.position());
171 //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
173 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
175 args
<< "-o" << QDir::toNativeSeparators(outputFile
);
176 args
<< QDir::toNativeSeparators(sourceFile
);
178 if(!startProcess(process
, m_binary
, args
))
183 bool bTimeout
= false;
184 bool bAborted
= false;
185 int prevProgress
= -1;
187 QRegExp
regExp("\\[.*(\\d+)[.,](\\d+)%\\]");
189 while(process
.state() != QProcess::NotRunning
)
195 emit
messageLogged("\nABORTED BY USER !!!");
198 process
.waitForReadyRead(m_processTimeoutInterval
);
199 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
202 qWarning("OggEnc process timed out <-- killing!");
203 emit
messageLogged("\nPROCESS TIMEOUT !!!");
207 while(process
.bytesAvailable() > 0)
209 QByteArray line
= process
.readLine();
210 QString text
= QString::fromUtf8(line
.constData()).simplified();
211 if(regExp
.lastIndexIn(text
) >= 0)
214 int progress
= regExp
.cap(1).toInt(&ok
);
215 if(ok
&& (progress
> prevProgress
))
217 emit
statusUpdated(progress
);
218 prevProgress
= qMin(progress
+ 2, 99);
221 else if(!text
.isEmpty())
223 emit
messageLogged(text
);
228 process
.waitForFinished();
229 if(process
.state() != QProcess::NotRunning
)
232 process
.waitForFinished(-1);
235 emit
statusUpdated(100);
236 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
238 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
246 QString
VorbisEncoder::extension(void)
251 bool VorbisEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
253 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
255 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
260 else if(containerType
.compare("FLAC", Qt::CaseInsensitive
) == 0)
262 if(formatType
.compare("FLAC", Qt::CaseInsensitive
) == 0)
271 void VorbisEncoder::setBitrateLimits(int minimumBitrate
, int maximumBitrate
)
273 m_configBitrateMinimum
= minimumBitrate
;
274 m_configBitrateMaximum
= maximumBitrate
;
277 void VorbisEncoder::setSamplingRate(int value
)
279 m_configSamplingRate
= value
;
282 const AbstractEncoderInfo
*VorbisEncoder::getEncoderInfo(void)
284 return &g_vorbisEncoderInfo
;