1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 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 #define max(a,b) (((a) > (b)) ? (a) : (b))
31 #define min(a,b) (((a) < (b)) ? (a) : (b))
32 #define IS_UNICODE(STR) (qstricmp(STR.toUtf8().constData(), QString::fromLocal8Bit(STR.toLocal8Bit()).toUtf8().constData()))
34 VorbisEncoder::VorbisEncoder(void)
36 m_binary_i386(lamexp_lookup_tool("oggenc2_i386.exe")),
37 m_binary_sse2(lamexp_lookup_tool("oggenc2_sse2.exe")),
38 m_binary_x64(lamexp_lookup_tool("oggenc2_x64.exe"))
40 if(m_binary_i386.isEmpty() || m_binary_sse2.isEmpty() || m_binary_x64.isEmpty())
42 throw "Error initializing Vorbis encoder. Tool 'oggenc2.exe' is not registred!";
46 VorbisEncoder::~VorbisEncoder(void)
50 bool VorbisEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
54 const QString baseName = QFileInfo(outputFile).fileName();
55 lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features();
56 const QString &binary = (cpuFeatures.x64 ? m_binary_x64 : ((cpuFeatures.intel && cpuFeatures.sse && cpuFeatures.sse2) ? m_binary_sse2 : m_binary_i386));
58 switch(m_configRCMode)
60 case SettingsModel::VBRMode:
61 args << "-q" << QString::number(max(-2, min(10, m_configBitrate)));
63 case SettingsModel::ABRMode:
64 args << "-b" << QString::number(max(32, min(500, (m_configBitrate * 8))));
67 throw "Bad rate-control mode!";
71 if(!metaInfo.fileName().isEmpty()) args << "-t" << metaInfo.fileName();
72 if(!metaInfo.fileArtist().isEmpty()) args << "-a" << metaInfo.fileArtist();
73 if(!metaInfo.fileAlbum().isEmpty()) args << "-l" << metaInfo.fileAlbum();
74 if(!metaInfo.fileGenre().isEmpty()) args << "-G" << metaInfo.fileGenre();
75 if(!metaInfo.fileComment().isEmpty()) args << "-c" << QString("comment=%1").arg(metaInfo.fileComment());
76 if(metaInfo.fileYear()) args << "-d" << QString::number(metaInfo.fileYear());
77 if(metaInfo.filePosition()) args << "-N" << QString::number(metaInfo.filePosition());
79 //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
81 args << "-o" << QDir::toNativeSeparators(outputFile);
82 args << QDir::toNativeSeparators(sourceFile);
84 if(!startProcess(process, binary, args))
89 bool bTimeout = false;
90 bool bAborted = false;
92 QRegExp regExp("\\[.*(\\d+)[.,](\\d+)%\\]");
94 while(process.state() != QProcess::NotRunning)
100 emit messageLogged("\nABORTED BY USER !!!");
103 process.waitForReadyRead();
104 if(!process.bytesAvailable() && process.state() == QProcess::Running)
107 qWarning("OggEnc process timed out <-- killing!");
111 while(process.bytesAvailable() > 0)
113 QByteArray line = process.readLine();
114 QString text = QString::fromUtf8(line.constData()).simplified();
115 if(regExp.lastIndexIn(text) >= 0)
118 int progress = regExp.cap(1).toInt(&ok);
119 if(ok) emit statusUpdated(progress);
121 else if(!text.isEmpty())
123 emit messageLogged(text);
128 process.waitForFinished();
129 if(process.state() != QProcess::NotRunning)
132 process.waitForFinished(-1);
135 emit statusUpdated(100);
136 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
138 if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
146 QString VorbisEncoder::extension(void)
151 bool VorbisEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
153 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
155 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
160 else if(containerType.compare("FLAC", Qt::CaseInsensitive) == 0)
162 if(formatType.compare("FLAC", Qt::CaseInsensitive) == 0)