OSDN Git Service

ff183d3c78d71df13ce348e37647eb9cde1bd995
[lamexp/LameXP.git] / src / Encoder_FLAC.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
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.
15 //
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.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Encoder_FLAC.h"
24
25 #include "Global.h"
26 #include "Model_Settings.h"
27
28 #include <QProcess>
29 #include <QDir>
30
31 ///////////////////////////////////////////////////////////////////////////////
32 // Encoder Info
33 ///////////////////////////////////////////////////////////////////////////////
34
35 class FLACEncoderInfo : public AbstractEncoderInfo
36 {
37 public:
38         virtual bool isModeSupported(int mode) const
39         {
40                 switch(mode)
41                 {
42                 case SettingsModel::VBRMode:
43                         return true;
44                         break;
45                 case SettingsModel::ABRMode:
46                 case SettingsModel::CBRMode:
47                         return false;
48                         break;
49                 default:
50                         MUTILS_THROW("Bad RC mode specified!");
51                 }
52         }
53
54         virtual int valueCount(int mode) const
55         {
56                 switch(mode)
57                 {
58                 case SettingsModel::VBRMode:
59                         return 9;
60                         break;
61                 case SettingsModel::ABRMode:
62                 case SettingsModel::CBRMode:
63                         return 0;
64                         break;
65                 default:
66                         MUTILS_THROW("Bad RC mode specified!");
67                 }
68         }
69
70         virtual int valueAt(int mode, int index) const
71         {
72                 switch(mode)
73                 {
74                 case SettingsModel::VBRMode:
75                         return qBound(0, index, 8);
76                         break;
77                 case SettingsModel::ABRMode:
78                 case SettingsModel::CBRMode:
79                         return -1;
80                         break;
81                 default:
82                         MUTILS_THROW("Bad RC mode specified!");
83                 }
84         }
85
86         virtual int valueType(int mode) const
87         {
88                 switch(mode)
89                 {
90                 case SettingsModel::VBRMode:
91                         return TYPE_COMPRESSION_LEVEL;
92                         break;
93                 case SettingsModel::ABRMode:
94                 case SettingsModel::CBRMode:
95                         return -1;
96                 default:
97                         MUTILS_THROW("Bad RC mode specified!");
98                 }
99         }
100
101         virtual const char *description(void) const
102         {
103                 static const char* s_description = "Free Lossless Audio Codec (FLAC)";
104                 return s_description;
105         }
106 }
107 static const g_flacEncoderInfo;
108
109 ///////////////////////////////////////////////////////////////////////////////
110 // Encoder implementation
111 ///////////////////////////////////////////////////////////////////////////////
112
113 FLACEncoder::FLACEncoder(void)
114 :
115         m_binary(lamexp_tools_lookup("flac.exe"))
116 {
117         if(m_binary.isEmpty())
118         {
119                 MUTILS_THROW("Error initializing FLAC encoder. Tool 'flac.exe' is not registred!");
120         }
121 }
122
123 FLACEncoder::~FLACEncoder(void)
124 {
125 }
126
127 bool FLACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
128 {
129         QProcess process;
130         QStringList args;
131
132         args << QString("-%1").arg(QString::number(qBound(0, m_configBitrate, 8)));
133         args << "--channel-map=none";
134
135         if(!metaInfo.title().isEmpty()) args << "-T" << QString("title=%1").arg(cleanTag(metaInfo.title()));
136         if(!metaInfo.artist().isEmpty()) args << "-T" << QString("artist=%1").arg(cleanTag(metaInfo.artist()));
137         if(!metaInfo.album().isEmpty()) args << "-T" << QString("album=%1").arg(cleanTag(metaInfo.album()));
138         if(!metaInfo.genre().isEmpty()) args << "-T" << QString("genre=%1").arg(cleanTag(metaInfo.genre()));
139         if(!metaInfo.comment().isEmpty()) args << "-T" << QString("comment=%1").arg(cleanTag(metaInfo.comment()));
140         if(metaInfo.year()) args << "-T" << QString("date=%1").arg(QString::number(metaInfo.year()));
141         if(metaInfo.position()) args << "-T" << QString("track=%1").arg(QString::number(metaInfo.position()));
142         if(!metaInfo.cover().isEmpty()) args << QString("--picture=%1").arg(metaInfo.cover());
143
144         //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
145
146         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
147
148         args << "-f" << "-o" << QDir::toNativeSeparators(outputFile);
149         args << QDir::toNativeSeparators(sourceFile);
150
151         if(!startProcess(process, m_binary, args))
152         {
153                 return false;
154         }
155
156         bool bTimeout = false;
157         bool bAborted = false;
158         int prevProgress = -1;
159
160         QRegExp regExp("\\b(\\d+)% complete");
161
162         while(process.state() != QProcess::NotRunning)
163         {
164                 if(*abortFlag)
165                 {
166                         process.kill();
167                         bAborted = true;
168                         emit messageLogged("\nABORTED BY USER !!!");
169                         break;
170                 }
171                 process.waitForReadyRead(m_processTimeoutInterval);
172                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
173                 {
174                         process.kill();
175                         qWarning("FLAC process timed out <-- killing!");
176                         emit messageLogged("\nPROCESS TIMEOUT !!!");
177                         bTimeout = true;
178                         break;
179                 }
180                 while(process.bytesAvailable() > 0)
181                 {
182                         QByteArray line = process.readLine().replace('\b', char(0x20));
183                         QString text = QString::fromUtf8(line.constData()).simplified();
184                         if(regExp.lastIndexIn(text) >= 0)
185                         {
186                                 bool ok = false;
187                                 int progress = regExp.cap(1).toInt(&ok);
188                                 if(ok && (progress > prevProgress))
189                                 {
190                                         emit statusUpdated(progress);
191                                         prevProgress = qMin(progress + 2, 99);
192                                 }
193                         }
194                         else if(!text.isEmpty())
195                         {
196                                 emit messageLogged(text);
197                         }
198                 }
199         }
200
201         process.waitForFinished();
202         if(process.state() != QProcess::NotRunning)
203         {
204                 process.kill();
205                 process.waitForFinished(-1);
206         }
207         
208         emit statusUpdated(100);
209         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
210
211         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
212         {
213                 return false;
214         }
215         
216         return true;
217 }
218
219 QString FLACEncoder::extension(void)
220 {
221         return "flac";
222 }
223
224 bool FLACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
225 {
226         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
227         {
228                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
229                 {
230                         return true;
231                 }
232         }
233
234         return false;
235 }
236
237 const unsigned int *FLACEncoder::supportedChannelCount(void)
238 {
239         static const unsigned int supportedChannels[] = {2, 4, 5, 6, 7, 8, NULL};
240         return supportedChannels;
241 }
242
243 const unsigned int *FLACEncoder::supportedBitdepths(void)
244 {
245         static const unsigned int supportedBPS[] = {16, 24, NULL};
246         return supportedBPS;
247 }
248
249 const AbstractEncoderInfo *FLACEncoder::getEncoderInfo(void)
250 {
251         return &g_flacEncoderInfo;
252 }