1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Encoder_FLAC.h"
26 #include "Model_Settings.h"
31 ///////////////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////////////
35 class FLACEncoderInfo
: public AbstractEncoderInfo
38 virtual bool isModeSupported(int mode
) const
42 case SettingsModel::VBRMode
:
45 case SettingsModel::ABRMode
:
46 case SettingsModel::CBRMode
:
50 MUTILS_THROW("Bad RC mode specified!");
54 virtual int valueCount(int mode
) const
58 case SettingsModel::VBRMode
:
61 case SettingsModel::ABRMode
:
62 case SettingsModel::CBRMode
:
66 MUTILS_THROW("Bad RC mode specified!");
70 virtual int valueAt(int mode
, int index
) const
74 case SettingsModel::VBRMode
:
75 return qBound(0, index
, 8);
77 case SettingsModel::ABRMode
:
78 case SettingsModel::CBRMode
:
82 MUTILS_THROW("Bad RC mode specified!");
86 virtual int valueType(int mode
) const
90 case SettingsModel::VBRMode
:
91 return TYPE_COMPRESSION_LEVEL
;
93 case SettingsModel::ABRMode
:
94 case SettingsModel::CBRMode
:
97 MUTILS_THROW("Bad RC mode specified!");
101 virtual const char *description(void) const
103 static const char* s_description
= "Free Lossless Audio Codec (FLAC)";
104 return s_description
;
107 virtual const char *extension(void) const
109 static const char* s_extension
= "flac";
113 virtual bool isResamplingSupported(void) const
118 static const g_flacEncoderInfo
;
120 ///////////////////////////////////////////////////////////////////////////////
121 // Encoder implementation
122 ///////////////////////////////////////////////////////////////////////////////
124 FLACEncoder::FLACEncoder(void)
126 m_binary(lamexp_tools_lookup(L1S("flac.exe")))
128 if(m_binary
.isEmpty())
130 MUTILS_THROW("Error initializing FLAC encoder. Tool 'flac.exe' is not registred!");
134 FLACEncoder::~FLACEncoder(void)
138 bool FLACEncoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const unsigned int channels
, const QString
&outputFile
, QAtomicInt
&abortFlag
)
143 args
<< QString("-%1").arg(QString::number(qBound(0, m_configBitrate
, 8)));
144 args
<< L1S("--channel-map=none");
146 if(!metaInfo
.title().isEmpty()) args
<< L1S("-T") << QString("title=%1").arg(cleanTag(metaInfo
.title()));
147 if(!metaInfo
.artist().isEmpty()) args
<< L1S("-T") << QString("artist=%1").arg(cleanTag(metaInfo
.artist()));
148 if(!metaInfo
.album().isEmpty()) args
<< L1S("-T") << QString("album=%1").arg(cleanTag(metaInfo
.album()));
149 if(!metaInfo
.genre().isEmpty()) args
<< L1S("-T") << QString("genre=%1").arg(cleanTag(metaInfo
.genre()));
150 if(!metaInfo
.comment().isEmpty()) args
<< L1S("-T") << QString("comment=%1").arg(cleanTag(metaInfo
.comment()));
151 if(metaInfo
.year()) args
<< L1S("-T") << QString("date=%1").arg(QString::number(metaInfo
.year()));
152 if(metaInfo
.position()) args
<< L1S("-T") << QString("track=%1").arg(QString::number(metaInfo
.position()));
153 if(!metaInfo
.cover().isEmpty()) args
<< QString("--picture=%1").arg(metaInfo
.cover());
155 //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
157 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
159 args
<< L1S("-f") << L1S("-o") << QDir::toNativeSeparators(outputFile
);
160 args
<< QDir::toNativeSeparators(sourceFile
);
162 if(!startProcess(process
, m_binary
, args
))
167 bool bTimeout
= false;
168 bool bAborted
= false;
169 int prevProgress
= -1;
171 QRegExp
regExp(L1S("\\b(\\d+)% complete"));
173 while(process
.state() != QProcess::NotRunning
)
175 if(checkFlag(abortFlag
))
179 emit
messageLogged(L1S("\nABORTED BY USER !!!"));
182 process
.waitForReadyRead(m_processTimeoutInterval
);
183 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
186 qWarning("FLAC process timed out <-- killing!");
187 emit
messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
191 while(process
.bytesAvailable() > 0)
193 QByteArray line
= process
.readLine().replace('\b', char(0x20));
194 QString text
= QString::fromUtf8(line
.constData()).simplified();
195 if(regExp
.lastIndexIn(text
) >= 0)
198 int progress
= regExp
.cap(1).toInt(&ok
);
199 if(ok
&& (progress
> prevProgress
))
201 emit
statusUpdated(progress
);
202 prevProgress
= qMin(progress
+ 2, 99);
205 else if(!text
.isEmpty())
207 emit
messageLogged(text
);
212 process
.waitForFinished();
213 if(process
.state() != QProcess::NotRunning
)
216 process
.waitForFinished(-1);
219 emit
statusUpdated(100);
220 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
222 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
230 bool FLACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
232 if(containerType
.compare(L1S("Wave"), Qt::CaseInsensitive
) == 0)
234 if(formatType
.compare(L1S("PCM"), Qt::CaseInsensitive
) == 0)
243 const unsigned int *FLACEncoder::supportedChannelCount(void)
245 static const unsigned int supportedChannels
[] = {2, 4, 5, 6, 7, 8, NULL
};
246 return supportedChannels
;
249 const unsigned int *FLACEncoder::supportedBitdepths(void)
251 static const unsigned int supportedBPS
[] = {16, 24, NULL
};
255 const AbstractEncoderInfo
*FLACEncoder::getEncoderInfo(void)
257 return &g_flacEncoderInfo
;