Added support for Opus Audio Codec, based on Opus-Tools v0.1.3 (2012-07-10) by Xiph...
[LameXP.git] / src / Encoder_Opus.cpp
blob372d84e0a5ab3637d2d3205bd7de642d08537a9c
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
9 //
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"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <QProcess>
28 #include <QDir>
29 #include <QUUid>
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!";
41 OpusEncoder::~OpusEncoder(void)
45 bool OpusEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
47 QProcess process;
48 QStringList args;
50 args << "--music"; //TODO: Make other optimizations available!
52 switch(m_configRCMode)
54 case SettingsModel::VBRMode:
55 args << "--vbr";
56 break;
57 case SettingsModel::ABRMode:
58 args << "-cvbr";
59 break;
60 case SettingsModel::CBRMode:
61 args << "--hard-cbr";
62 break;
63 default:
64 throw "Bad rate-control mode!";
65 break;
68 args << QString("--bitrate") << QString::number(qMax(0, qMin(500, m_configBitrate * 8)));
70 if(!metaInfo.fileName().isEmpty()) args << "--title" << metaInfo.fileName();
71 if(!metaInfo.fileArtist().isEmpty()) args << "--artist" << metaInfo.fileArtist();
72 if(!metaInfo.fileAlbum().isEmpty()) args << "--comment" << QString("album=%1").arg(metaInfo.fileAlbum());
73 if(!metaInfo.fileGenre().isEmpty()) args << "--comment" << QString("genre=%1").arg(metaInfo.fileGenre());
74 if(!metaInfo.fileComment().isEmpty()) args << "--comment" << QString("comment=%1").arg(metaInfo.fileComment());
75 if(metaInfo.fileYear()) args << "--comment" << QString("date=%1").arg(QString::number(metaInfo.fileYear()));
76 if(metaInfo.filePosition()) args << "--comment" << QString("track=%1").arg(QString::number(metaInfo.filePosition()));
78 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
80 args << QDir::toNativeSeparators(sourceFile);
81 args << QDir::toNativeSeparators(outputFile);
83 if(!startProcess(process, m_binary, args))
85 return false;
88 bool bTimeout = false;
89 bool bAborted = false;
90 int prevProgress = -1;
92 QRegExp regExp("\\[(-|\\\\|/|\\|)\\]");
94 //The Opus encoder doesn't flus it's status updates :-[
95 emit statusUpdated(20 + (QUuid::createUuid().data1 % 60));
97 while(process.state() != QProcess::NotRunning)
99 if(*abortFlag)
101 process.kill();
102 bAborted = true;
103 emit messageLogged("\nABORTED BY USER !!!");
104 break;
106 process.waitForReadyRead(m_processTimeoutInterval);
107 if(!process.bytesAvailable() && process.state() == QProcess::Running)
109 process.kill();
110 qWarning("Opus process timed out <-- killing!");
111 emit messageLogged("\nPROCESS TIMEOUT !!!");
112 bTimeout = true;
113 break;
115 while(process.bytesAvailable() > 0)
117 QByteArray line = process.readLine();
118 QString text = QString::fromUtf8(line.constData()).simplified();
119 if(regExp.lastIndexIn(text) >= 0)
121 bool ok = false;
122 int progress = regExp.cap(1).toInt(&ok);
123 if(ok && (progress > prevProgress))
125 emit statusUpdated(progress);
126 prevProgress = qMin(progress + 2, 99);
129 else if(!text.isEmpty())
131 emit messageLogged(text);
136 process.waitForFinished();
137 if(process.state() != QProcess::NotRunning)
139 process.kill();
140 process.waitForFinished(-1);
143 emit statusUpdated(100);
144 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
146 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
148 return false;
151 return true;
154 QString OpusEncoder::extension(void)
156 return "opus";
159 bool OpusEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
161 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
163 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
165 return true;
169 return false;
172 const unsigned int *OpusEncoder::supportedChannelCount(void)
174 return NULL;
177 const unsigned int *OpusEncoder::supportedBitdepths(void)
179 return NULL;