1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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_FLAC.h"
25 #include "Model_Settings.h"
30 ///////////////////////////////////////////////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////////////////
34 class FLACEncoderInfo
: public AbstractEncoderInfo
37 virtual bool isModeSupported(int mode
) const
41 case SettingsModel::VBRMode
:
44 case SettingsModel::ABRMode
:
45 case SettingsModel::CBRMode
:
49 throw "Bad RC mode specified!";
53 virtual int valueCount(int mode
) const
57 case SettingsModel::VBRMode
:
60 case SettingsModel::ABRMode
:
61 case SettingsModel::CBRMode
:
65 throw "Bad RC mode specified!";
69 virtual int valueAt(int mode
, int index
) const
73 case SettingsModel::VBRMode
:
74 return qBound(0, index
, 8);
76 case SettingsModel::ABRMode
:
77 case SettingsModel::CBRMode
:
81 throw "Bad RC mode specified!";
85 virtual int valueType(int mode
) const
89 case SettingsModel::VBRMode
:
90 return TYPE_COMPRESSION_LEVEL
;
92 case SettingsModel::ABRMode
:
93 case SettingsModel::CBRMode
:
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
)
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
))
155 bool bTimeout
= false;
156 bool bAborted
= false;
157 int prevProgress
= -1;
159 QRegExp
regExp("\\b(\\d+)% complete");
161 while(process
.state() != QProcess::NotRunning
)
167 emit
messageLogged("\nABORTED BY USER !!!");
170 process
.waitForReadyRead(m_processTimeoutInterval
);
171 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
174 qWarning("FLAC process timed out <-- killing!");
175 emit
messageLogged("\nPROCESS TIMEOUT !!!");
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)
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
)
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
)
218 QString
FLACEncoder::extension(void)
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)
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
};
248 const AbstractEncoderInfo
*FLACEncoder::getEncoderInfo(void)
250 return &g_flacEncoderInfo
;