Fixed a typo, thanks to Rub3n CT for reporting.
[LameXP.git] / src / Encoder_FLAC.cpp
blob22e82073fa82e41e4fbec0a806051c2eedbcd047
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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_FLAC.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <QProcess>
28 #include <QDir>
30 #define max(a,b) (((a) > (b)) ? (a) : (b))
31 #define min(a,b) (((a) < (b)) ? (a) : (b))
33 FLACEncoder::FLACEncoder(void)
35 m_binary(lamexp_lookup_tool("flac.exe"))
37 if(m_binary.isEmpty())
39 throw "Error initializing FLAC encoder. Tool 'flac.exe' is not registred!";
43 FLACEncoder::~FLACEncoder(void)
47 bool FLACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
49 QProcess process;
50 QStringList args;
52 args << QString("-%1").arg(QString::number(max(0, min(8, m_configBitrate))));
54 if(!metaInfo.fileName().isEmpty()) args << "-T" << QString("title=%1").arg(metaInfo.fileName());
55 if(!metaInfo.fileArtist().isEmpty()) args << "-T" << QString("artist=%1").arg(metaInfo.fileArtist());
56 if(!metaInfo.fileAlbum().isEmpty()) args << "-T" << QString("album=%1").arg(metaInfo.fileAlbum());
57 if(!metaInfo.fileGenre().isEmpty()) args << "-T" << QString("genre=%1").arg(metaInfo.fileGenre());
58 if(!metaInfo.fileComment().isEmpty()) args << "-T" << QString("comment=%1").arg(metaInfo.fileComment());
59 if(metaInfo.fileYear()) args << "-T" << QString("date=%1").arg(QString::number(metaInfo.fileYear()));
60 if(metaInfo.filePosition()) args << "-T" << QString("track=%1").arg(QString::number(metaInfo.filePosition()));
61 if(!metaInfo.fileCover().isEmpty()) args << QString("--picture=%1").arg(metaInfo.fileCover());
63 //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
65 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
67 args << "-f" << "-o" << QDir::toNativeSeparators(outputFile);
68 args << QDir::toNativeSeparators(sourceFile);
70 if(!startProcess(process, m_binary, args))
72 return false;
75 bool bTimeout = false;
76 bool bAborted = false;
78 QRegExp regExp("\\s(\\d+)% complete");
80 while(process.state() != QProcess::NotRunning)
82 if(*abortFlag)
84 process.kill();
85 bAborted = true;
86 emit messageLogged("\nABORTED BY USER !!!");
87 break;
89 process.waitForReadyRead(m_processTimeoutInterval);
90 if(!process.bytesAvailable() && process.state() == QProcess::Running)
92 process.kill();
93 qWarning("FLAC process timed out <-- killing!");
94 emit messageLogged("\nPROCESS TIMEOUT !!!");
95 bTimeout = true;
96 break;
98 while(process.bytesAvailable() > 0)
100 QByteArray line = process.readLine();
101 QString text = QString::fromUtf8(line.constData()).simplified();
102 if(regExp.lastIndexIn(text) >= 0)
104 bool ok = false;
105 int progress = regExp.cap(1).toInt(&ok);
106 if(ok) emit statusUpdated(progress);
108 else if(!text.isEmpty())
110 emit messageLogged(text);
115 process.waitForFinished();
116 if(process.state() != QProcess::NotRunning)
118 process.kill();
119 process.waitForFinished(-1);
122 emit statusUpdated(100);
123 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
125 if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
127 return false;
130 return true;
133 QString FLACEncoder::extension(void)
135 return "flac";
138 bool FLACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
140 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
142 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
144 return true;
148 return false;