Updated Simplified Chinese translation, thanks to <kidneybean@sohu.com>.
[LameXP.git] / src / Encoder_FLAC.cpp
blobeec735763fb43305ce12206fbe08e4855b31433b
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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 ///////////////////////////////////////////////////////////////////////////////
31 // Encoder Info
32 ///////////////////////////////////////////////////////////////////////////////
34 class FLACEncoderInfo : public AbstractEncoderInfo
36 public:
37 virtual bool isModeSupported(int mode) const
39 switch(mode)
41 case SettingsModel::VBRMode:
42 return true;
43 break;
44 case SettingsModel::ABRMode:
45 case SettingsModel::CBRMode:
46 return false;
47 break;
48 default:
49 throw "Bad RC mode specified!";
53 virtual int valueCount(int mode) const
55 switch(mode)
57 case SettingsModel::VBRMode:
58 return 9;
59 break;
60 case SettingsModel::ABRMode:
61 case SettingsModel::CBRMode:
62 return 0;
63 break;
64 default:
65 throw "Bad RC mode specified!";
69 virtual int valueAt(int mode, int index) const
71 switch(mode)
73 case SettingsModel::VBRMode:
74 return qBound(0, index, 8);
75 break;
76 case SettingsModel::ABRMode:
77 case SettingsModel::CBRMode:
78 return -1;
79 break;
80 default:
81 throw "Bad RC mode specified!";
85 virtual int valueType(int mode) const
87 switch(mode)
89 case SettingsModel::VBRMode:
90 return TYPE_COMPRESSION_LEVEL;
91 break;
92 case SettingsModel::ABRMode:
93 case SettingsModel::CBRMode:
94 return -1;
95 default:
96 throw "Bad RC mode specified!";
100 virtual const char *description(void) const
102 static const char* s_description = "Free Lossless Audio Codec (FLAC)";
103 return s_description;
106 static const g_flacEncoderInfo;
108 ///////////////////////////////////////////////////////////////////////////////
109 // Encoder implementation
110 ///////////////////////////////////////////////////////////////////////////////
112 FLACEncoder::FLACEncoder(void)
114 m_binary(lamexp_lookup_tool("flac.exe"))
116 if(m_binary.isEmpty())
118 throw "Error initializing FLAC encoder. Tool 'flac.exe' is not registred!";
122 FLACEncoder::~FLACEncoder(void)
126 bool FLACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
128 QProcess process;
129 QStringList args;
131 args << QString("-%1").arg(QString::number(qBound(0, m_configBitrate, 8)));
132 args << "--channel-map=none";
134 if(!metaInfo.fileName().isEmpty()) args << "-T" << QString("title=%1").arg(cleanTag(metaInfo.fileName()));
135 if(!metaInfo.fileArtist().isEmpty()) args << "-T" << QString("artist=%1").arg(cleanTag(metaInfo.fileArtist()));
136 if(!metaInfo.fileAlbum().isEmpty()) args << "-T" << QString("album=%1").arg(cleanTag(metaInfo.fileAlbum()));
137 if(!metaInfo.fileGenre().isEmpty()) args << "-T" << QString("genre=%1").arg(cleanTag(metaInfo.fileGenre()));
138 if(!metaInfo.fileComment().isEmpty()) args << "-T" << QString("comment=%1").arg(cleanTag(metaInfo.fileComment()));
139 if(metaInfo.fileYear()) args << "-T" << QString("date=%1").arg(QString::number(metaInfo.fileYear()));
140 if(metaInfo.filePosition()) args << "-T" << QString("track=%1").arg(QString::number(metaInfo.filePosition()));
141 if(!metaInfo.fileCover().isEmpty()) args << QString("--picture=%1").arg(metaInfo.fileCover());
143 //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
145 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
147 args << "-f" << "-o" << QDir::toNativeSeparators(outputFile);
148 args << QDir::toNativeSeparators(sourceFile);
150 if(!startProcess(process, m_binary, args))
152 return false;
155 bool bTimeout = false;
156 bool bAborted = false;
157 int prevProgress = -1;
159 QRegExp regExp("\\b(\\d+)% complete");
161 while(process.state() != QProcess::NotRunning)
163 if(*abortFlag)
165 process.kill();
166 bAborted = true;
167 emit messageLogged("\nABORTED BY USER !!!");
168 break;
170 process.waitForReadyRead(m_processTimeoutInterval);
171 if(!process.bytesAvailable() && process.state() == QProcess::Running)
173 process.kill();
174 qWarning("FLAC process timed out <-- killing!");
175 emit messageLogged("\nPROCESS TIMEOUT !!!");
176 bTimeout = true;
177 break;
179 while(process.bytesAvailable() > 0)
181 QByteArray line = process.readLine().replace('\b', char(0x20));
182 QString text = QString::fromUtf8(line.constData()).simplified();
183 if(regExp.lastIndexIn(text) >= 0)
185 bool ok = false;
186 int progress = regExp.cap(1).toInt(&ok);
187 if(ok && (progress > prevProgress))
189 emit statusUpdated(progress);
190 prevProgress = qMin(progress + 2, 99);
193 else if(!text.isEmpty())
195 emit messageLogged(text);
200 process.waitForFinished();
201 if(process.state() != QProcess::NotRunning)
203 process.kill();
204 process.waitForFinished(-1);
207 emit statusUpdated(100);
208 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
210 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
212 return false;
215 return true;
218 QString FLACEncoder::extension(void)
220 return "flac";
223 bool FLACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
225 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
227 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
229 return true;
233 return false;
236 const unsigned int *FLACEncoder::supportedChannelCount(void)
238 static const unsigned int supportedChannels[] = {2, 4, 5, 6, 7, 8, NULL};
239 return supportedChannels;
242 const unsigned int *FLACEncoder::supportedBitdepths(void)
244 static const unsigned int supportedBPS[] = {16, 24, NULL};
245 return supportedBPS;
248 const AbstractEncoderInfo *FLACEncoder::getEncoderInfo(void)
250 return &g_flacEncoderInfo;