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_Opus.h"
26 #include "Model_Settings.h"
32 ///////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
36 class OpusEncoderInfo
: public AbstractEncoderInfo
38 virtual bool isModeSupported(int mode
) const
42 case SettingsModel::VBRMode
:
43 case SettingsModel::ABRMode
:
44 case SettingsModel::CBRMode
:
48 MUTILS_THROW("Bad RC mode specified!");
52 virtual int valueCount(int mode
) const
56 case SettingsModel::VBRMode
:
57 case SettingsModel::ABRMode
:
58 case SettingsModel::CBRMode
:
62 MUTILS_THROW("Bad RC mode specified!");
66 virtual int valueAt(int mode
, int index
) const
70 case SettingsModel::VBRMode
:
71 case SettingsModel::ABRMode
:
72 case SettingsModel::CBRMode
:
73 return qBound(8, (index
+ 1) * 8, 256);
76 MUTILS_THROW("Bad RC mode specified!");
80 virtual int valueType(int mode
) const
84 case SettingsModel::VBRMode
:
85 case SettingsModel::ABRMode
:
86 return TYPE_APPROX_BITRATE
;
88 case SettingsModel::CBRMode
:
92 MUTILS_THROW("Bad RC mode specified!");
96 virtual const char *description(void) const
98 static const char* s_description
= "Opus-Tools OpusEnc (libopus)";
102 static const g_opusEncoderInfo
;
104 ///////////////////////////////////////////////////////////////////////////////
105 // Encoder implementation
106 ///////////////////////////////////////////////////////////////////////////////
108 OpusEncoder::OpusEncoder(void)
110 m_binary(lamexp_tools_lookup("opusenc.exe"))
112 if(m_binary
.isEmpty())
114 MUTILS_THROW("Error initializing Opus encoder. Tool 'opusenc.exe' is not registred!");
117 m_configOptimizeFor
= 0;
118 m_configEncodeComplexity
= 10;
119 m_configFrameSize
= 3;
122 OpusEncoder::~OpusEncoder(void)
126 bool OpusEncoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const QString
&outputFile
, volatile bool *abortFlag
)
131 switch(m_configRCMode
)
133 case SettingsModel::VBRMode
:
136 case SettingsModel::ABRMode
:
139 case SettingsModel::CBRMode
:
140 args
<< "--hard-cbr";
143 MUTILS_THROW("Bad rate-control mode!");
147 args
<< "--comp" << QString::number(m_configEncodeComplexity
);
149 switch(m_configFrameSize
)
152 args
<< "--framesize" << "2.5";
155 args
<< "--framesize" << "5";
158 args
<< "--framesize" << "10";
161 args
<< "--framesize" << "20";
164 args
<< "--framesize" << "40";
167 args
<< "--framesize" << "60";
171 args
<< QString("--bitrate") << QString::number(qBound(8, (m_configBitrate
+ 1) * 8, 256));
173 if(!metaInfo
.title().isEmpty()) args
<< "--title" << cleanTag(metaInfo
.title());
174 if(!metaInfo
.artist().isEmpty()) args
<< "--artist" << cleanTag(metaInfo
.artist());
175 if(!metaInfo
.album().isEmpty()) args
<< "--album" << cleanTag(metaInfo
.album());
176 if(!metaInfo
.genre().isEmpty()) args
<< "--genre" << cleanTag(metaInfo
.genre());
177 if(metaInfo
.year()) args
<< "--date" << QString::number(metaInfo
.year());
178 if(metaInfo
.position()) args
<< "--comment" << QString("tracknumber=%1").arg(QString::number(metaInfo
.position()));
179 if(!metaInfo
.comment().isEmpty()) args
<< "--comment" << QString("comment=%1").arg(cleanTag(metaInfo
.comment()));
181 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
183 args
<< QDir::toNativeSeparators(sourceFile
);
184 args
<< QDir::toNativeSeparators(outputFile
);
186 if(!startProcess(process
, m_binary
, args
))
191 bool bTimeout
= false;
192 bool bAborted
= false;
193 int prevProgress
= -1;
195 QRegExp
regExp("\\((\\d+)%\\)");
197 while(process
.state() != QProcess::NotRunning
)
203 emit
messageLogged("\nABORTED BY USER !!!");
206 process
.waitForReadyRead(m_processTimeoutInterval
);
207 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
210 qWarning("Opus process timed out <-- killing!");
211 emit
messageLogged("\nPROCESS TIMEOUT !!!");
215 while(process
.bytesAvailable() > 0)
217 QByteArray line
= process
.readLine();
218 QString text
= QString::fromUtf8(line
.constData()).simplified();
219 if(regExp
.lastIndexIn(text
) >= 0)
222 int progress
= regExp
.cap(1).toInt(&ok
);
223 if(ok
&& (progress
> prevProgress
))
225 emit
statusUpdated(progress
);
226 prevProgress
= qMin(progress
+ 2, 99);
229 else if(!text
.isEmpty())
231 emit
messageLogged(text
);
236 process
.waitForFinished();
237 if(process
.state() != QProcess::NotRunning
)
240 process
.waitForFinished(-1);
243 emit
statusUpdated(100);
244 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
246 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
254 void OpusEncoder::setOptimizeFor(int optimizeFor
)
256 m_configOptimizeFor
= qBound(0, optimizeFor
, 2);
259 void OpusEncoder::setEncodeComplexity(int complexity
)
261 m_configEncodeComplexity
= qBound(0, complexity
, 10);
264 void OpusEncoder::setFrameSize(int frameSize
)
266 m_configFrameSize
= qBound(0, frameSize
, 5);
269 QString
OpusEncoder::extension(void)
274 bool OpusEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
276 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
278 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
287 const unsigned int *OpusEncoder::supportedChannelCount(void)
292 const unsigned int *OpusEncoder::supportedBitdepths(void)
294 static const unsigned int supportedBPS
[] = {8, 16, 24, AudioFileModel::BITDEPTH_IEEE_FLOAT32
, NULL
};
298 const bool OpusEncoder::needsTimingInfo(void)
303 const AbstractEncoderInfo
*OpusEncoder::getEncoderInfo(void)
305 return &g_opusEncoderInfo
;