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_MP3.h"
25 #include "Model_Settings.h"
30 #define IS_UNICODE(STR) (qstricmp(STR.toUtf8().constData(), QString::fromLocal8Bit(STR.toLocal8Bit()).toUtf8().constData()))
32 MP3Encoder::MP3Encoder(void)
34 m_binary(lamexp_lookup_tool("lame.exe"))
36 if(m_binary.isEmpty())
38 throw "Error initializing MP3 encoder. Tool 'lame.exe' is not registred!";
42 MP3Encoder::~MP3Encoder(void)
46 bool MP3Encoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
54 switch(m_configRCMode)
56 case SettingsModel::VBRMode:
57 args << "-V" << QString::number(9 - min(9, m_configBitrate));
59 case SettingsModel::ABRMode:
60 args << "--abr" << QString::number(SettingsModel::mp3Bitrates[min(13, m_configBitrate)]);
62 case SettingsModel::CBRMode:
64 args << "-b" << QString::number(SettingsModel::mp3Bitrates[min(13, m_configBitrate)]);
67 throw "Bad rate-control mode!";
71 if(!metaInfo.fileName().isEmpty()) args << (IS_UNICODE(metaInfo.fileName()) ? "--uTitle" : "--lTitle") << metaInfo.fileName();
72 if(!metaInfo.fileArtist().isEmpty()) args << (IS_UNICODE(metaInfo.fileArtist()) ? "--uArtist" : "--lArtist") << metaInfo.fileArtist();
73 if(!metaInfo.fileAlbum().isEmpty()) args << (IS_UNICODE(metaInfo.fileAlbum()) ? "--uAlbum" : "--lAlbum") << metaInfo.fileAlbum();
74 if(!metaInfo.fileGenre().isEmpty()) args << (IS_UNICODE(metaInfo.fileGenre()) ? "--uGenre" : "--lGenre") << metaInfo.fileGenre();
75 if(!metaInfo.fileComment().isEmpty()) args << (IS_UNICODE(metaInfo.fileComment()) ? "--uComment" : "--lComment") << metaInfo.fileComment();
76 if(metaInfo.fileYear()) args << "--ty" << QString::number(metaInfo.fileYear());
77 if(metaInfo.filePosition()) args << "--tn" << 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 << QDir::toNativeSeparators(sourceFile);
82 args << QDir::toNativeSeparators(outputFile);
84 if(!startProcess(process, m_binary, args))
89 bool bTimeout = false;
90 bool bAborted = false;
92 QRegExp regExp("\\(.*(\\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("LAME 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 messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
137 if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
145 QString MP3Encoder::extension(void)
150 bool MP3Encoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
152 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
154 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
159 else if(containerType.compare("MPEG Audio", Qt::CaseInsensitive) == 0)
161 if(formatType.compare("MPEG Audio", Qt::CaseInsensitive) == 0)
163 if(formatProfile.compare("Layer 3", Qt::CaseInsensitive) == 0 || formatProfile.compare("Layer 2", Qt::CaseInsensitive) == 0)
165 if(formatVersion.compare("Version 1", Qt::CaseInsensitive) == 0 || formatVersion.compare("Version 2", Qt::CaseInsensitive) == 0)