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_Opus.h"
25 #include "Model_Settings.h"
31 OpusEncoder::OpusEncoder(void)
33 m_binary(lamexp_lookup_tool("opusenc.exe"))
35 if(m_binary
.isEmpty())
37 throw "Error initializing Opus encoder. Tool 'opusenc.exe' is not registred!";
40 m_configOptimizeFor
= 0;
41 m_configEncodeComplexity
= 10;
42 m_configFrameSize
= 3;
45 OpusEncoder::~OpusEncoder(void)
49 bool OpusEncoder::encode(const QString
&sourceFile
, const AudioFileModel
&metaInfo
, const QString
&outputFile
, volatile bool *abortFlag
)
51 const unsigned int fileDuration
= metaInfo
.fileDuration();
56 switch(m_configRCMode
)
58 case SettingsModel::VBRMode
:
61 case SettingsModel::ABRMode
:
64 case SettingsModel::CBRMode
:
68 throw "Bad rate-control mode!";
72 //switch(m_configOptimizeFor)
78 // args << "--speech";
82 args
<< "--comp" << QString::number(m_configEncodeComplexity
);
84 switch(m_configFrameSize
)
87 args
<< "--framesize" << "2.5";
90 args
<< "--framesize" << "5";
93 args
<< "--framesize" << "10";
96 args
<< "--framesize" << "20";
99 args
<< "--framesize" << "40";
102 args
<< "--framesize" << "60";
106 args
<< QString("--bitrate") << QString::number(qMax(0, qMin(500, m_configBitrate
* 8)));
108 if(!metaInfo
.fileName().isEmpty()) args
<< "--title" << cleanTag(metaInfo
.fileName());
109 if(!metaInfo
.fileArtist().isEmpty()) args
<< "--artist" << cleanTag(metaInfo
.fileArtist());
110 if(!metaInfo
.fileAlbum().isEmpty()) args
<< "--comment" << QString("album=%1").arg(cleanTag(metaInfo
.fileAlbum()));
111 if(!metaInfo
.fileGenre().isEmpty()) args
<< "--comment" << QString("genre=%1").arg(cleanTag(metaInfo
.fileGenre()));
112 if(!metaInfo
.fileComment().isEmpty()) args
<< "--comment" << QString("comment=%1").arg(cleanTag(metaInfo
.fileComment()));
113 if(metaInfo
.fileYear()) args
<< "--comment" << QString("date=%1").arg(QString::number(metaInfo
.fileYear()));
114 if(metaInfo
.filePosition()) args
<< "--comment" << QString("track=%1").arg(QString::number(metaInfo
.filePosition()));
116 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
118 args
<< QDir::toNativeSeparators(sourceFile
);
119 args
<< QDir::toNativeSeparators(outputFile
);
121 if(!startProcess(process
, m_binary
, args
))
126 bool bTimeout
= false;
127 bool bAborted
= false;
128 int prevProgress
= -1;
130 QRegExp
regExp("\\((\\d+)%\\)");
132 while(process
.state() != QProcess::NotRunning
)
138 emit
messageLogged("\nABORTED BY USER !!!");
141 process
.waitForReadyRead(m_processTimeoutInterval
);
142 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
145 qWarning("Opus process timed out <-- killing!");
146 emit
messageLogged("\nPROCESS TIMEOUT !!!");
150 while(process
.bytesAvailable() > 0)
152 QByteArray line
= process
.readLine();
153 QString text
= QString::fromUtf8(line
.constData()).simplified();
154 if(regExp
.lastIndexIn(text
) >= 0)
157 int progress
= regExp
.cap(1).toInt(&ok
);
158 if(ok
&& (progress
> prevProgress
))
160 emit
statusUpdated(progress
);
161 prevProgress
= qMin(progress
+ 2, 99);
164 else if(!text
.isEmpty())
166 emit
messageLogged(text
);
171 process
.waitForFinished();
172 if(process
.state() != QProcess::NotRunning
)
175 process
.waitForFinished(-1);
178 emit
statusUpdated(100);
179 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
181 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
189 void OpusEncoder::setOptimizeFor(int optimizeFor
)
191 m_configOptimizeFor
= qBound(0, optimizeFor
, 2);
194 void OpusEncoder::setEncodeComplexity(int complexity
)
196 m_configEncodeComplexity
= qBound(0, complexity
, 10);
199 void OpusEncoder::setFrameSize(int frameSize
)
201 m_configFrameSize
= qBound(0, frameSize
, 5);
204 QString
OpusEncoder::extension(void)
209 bool OpusEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
211 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
213 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
222 const unsigned int *OpusEncoder::supportedChannelCount(void)
227 const unsigned int *OpusEncoder::supportedBitdepths(void)
229 static const unsigned int supportedBPS
[] = {8, 16, 24, AudioFileModel::BITDEPTH_IEEE_FLOAT32
, NULL
};
233 const bool OpusEncoder::needsTimingInfo(void)