1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Encoder_Vorbis.h"
25 #include "Model_Settings.h"
30 VorbisEncoder::VorbisEncoder(void)
32 m_binary(lamexp_lookup_tool("oggenc2.exe"))
34 if(m_binary
.isEmpty())
36 throw "Error initializing Vorbis encoder. Tool 'oggenc2.exe' is not registred!";
39 m_configBitrateMaximum
= 0;
40 m_configBitrateMinimum
= 0;
41 m_configSamplingRate
= 0;
44 VorbisEncoder::~VorbisEncoder(void)
48 bool VorbisEncoder::encode(const QString
&sourceFile
, const AudioFileModel
&metaInfo
, const QString
&outputFile
, volatile bool *abortFlag
)
52 const QString baseName
= QFileInfo(outputFile
).fileName();
54 switch(m_configRCMode
)
56 case SettingsModel::VBRMode
:
57 args
<< "-q" << QString::number(qMax(-2, qMin(10, m_configBitrate
)));
59 case SettingsModel::ABRMode
:
60 args
<< "-b" << QString::number(qMax(32, qMin(500, (m_configBitrate
* 8))));
63 throw "Bad rate-control mode!";
67 if((m_configBitrateMaximum
> 0) && (m_configBitrateMinimum
> 0) && (m_configBitrateMinimum
<= m_configBitrateMaximum
))
69 args
<< "--min-bitrate" << QString::number(qMin(qMax(m_configBitrateMinimum
, 32), 500));
70 args
<< "--max-bitrate" << QString::number(qMin(qMax(m_configBitrateMaximum
, 32), 500));
73 if(m_configSamplingRate
> 0)
75 args
<< "--resample" << QString::number(m_configSamplingRate
) << "--converter" << QString::number(0);
78 if(!metaInfo
.fileName().isEmpty()) args
<< "-t" << metaInfo
.fileName();
79 if(!metaInfo
.fileArtist().isEmpty()) args
<< "-a" << metaInfo
.fileArtist();
80 if(!metaInfo
.fileAlbum().isEmpty()) args
<< "-l" << metaInfo
.fileAlbum();
81 if(!metaInfo
.fileGenre().isEmpty()) args
<< "-G" << metaInfo
.fileGenre();
82 if(!metaInfo
.fileComment().isEmpty()) args
<< "-c" << QString("comment=%1").arg(metaInfo
.fileComment());
83 if(metaInfo
.fileYear()) args
<< "-d" << QString::number(metaInfo
.fileYear());
84 if(metaInfo
.filePosition()) args
<< "-N" << QString::number(metaInfo
.filePosition());
86 //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
88 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
90 args
<< "-o" << QDir::toNativeSeparators(outputFile
);
91 args
<< QDir::toNativeSeparators(sourceFile
);
93 if(!startProcess(process
, m_binary
, args
))
98 bool bTimeout
= false;
99 bool bAborted
= false;
100 int prevProgress
= -1;
102 QRegExp
regExp("\\[.*(\\d+)[.,](\\d+)%\\]");
104 while(process
.state() != QProcess::NotRunning
)
110 emit
messageLogged("\nABORTED BY USER !!!");
113 process
.waitForReadyRead(m_processTimeoutInterval
);
114 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
117 qWarning("OggEnc process timed out <-- killing!");
118 emit
messageLogged("\nPROCESS TIMEOUT !!!");
122 while(process
.bytesAvailable() > 0)
124 QByteArray line
= process
.readLine();
125 QString text
= QString::fromUtf8(line
.constData()).simplified();
126 if(regExp
.lastIndexIn(text
) >= 0)
129 int progress
= regExp
.cap(1).toInt(&ok
);
130 if(ok
&& (progress
> prevProgress
))
132 emit
statusUpdated(progress
);
133 prevProgress
= qMin(progress
+ 2, 99);
136 else if(!text
.isEmpty())
138 emit
messageLogged(text
);
143 process
.waitForFinished();
144 if(process
.state() != QProcess::NotRunning
)
147 process
.waitForFinished(-1);
150 emit
statusUpdated(100);
151 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
153 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
161 QString
VorbisEncoder::extension(void)
166 bool VorbisEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
168 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
170 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
175 else if(containerType
.compare("FLAC", Qt::CaseInsensitive
) == 0)
177 if(formatType
.compare("FLAC", Qt::CaseInsensitive
) == 0)
186 void VorbisEncoder::setBitrateLimits(int minimumBitrate
, int maximumBitrate
)
188 m_configBitrateMinimum
= minimumBitrate
;
189 m_configBitrateMaximum
= maximumBitrate
;
192 void VorbisEncoder::setSamplingRate(int value
)
194 m_configSamplingRate
= value
;