OSDN Git Service

Bump version.
[lamexp/LameXP.git] / src / Encoder_FLAC.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2020 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; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "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         virtual const char *extension(void) const
108         {
109                 static const char* s_extension = "flac";
110                 return s_extension;
111         }
112
113         virtual bool isResamplingSupported(void) const
114         {
115                 return false;
116         }
117 }
118 static const g_flacEncoderInfo;
119
120 ///////////////////////////////////////////////////////////////////////////////
121 // Encoder implementation
122 ///////////////////////////////////////////////////////////////////////////////
123
124 FLACEncoder::FLACEncoder(void)
125 :
126         m_binary(lamexp_tools_lookup(L1S("flac.exe")))
127 {
128         if(m_binary.isEmpty())
129         {
130                 MUTILS_THROW("Error initializing FLAC encoder. Tool 'flac.exe' is not registred!");
131         }
132 }
133
134 FLACEncoder::~FLACEncoder(void)
135 {
136 }
137
138 bool FLACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const unsigned int channels, const QString &outputFile, QAtomicInt &abortFlag)
139 {
140         QProcess process;
141         QStringList args;
142
143         args << QString("-%1").arg(QString::number(qBound(0, m_configBitrate, 8)));
144         args << L1S("--channel-map=none");
145
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());
154
155         //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
156
157         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
158
159         args << L1S("-f") << L1S("-o") << QDir::toNativeSeparators(outputFile);
160         args << QDir::toNativeSeparators(sourceFile);
161
162         if(!startProcess(process, m_binary, args))
163         {
164                 return false;
165         }
166
167         int prevProgress = -1;
168         QRegExp regExp(L1S("\\b(\\d+)% complete"));
169
170         const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &regExp](const QString &text)
171         {
172                 if (regExp.lastIndexIn(text) >= 0)
173                 {
174                         qint32 newProgress;
175                         if (MUtils::regexp_parse_int32(regExp, newProgress))
176                         {
177                                 if (newProgress > prevProgress)
178                                 {
179                                         emit statusUpdated(newProgress);
180                                         prevProgress = NEXT_PROGRESS(newProgress);
181                                 }
182                         }
183                         return true;
184                 }
185                 return false;
186         });
187
188         return (result == RESULT_SUCCESS);
189 }
190
191 bool FLACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
192 {
193         if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
194         {
195                 if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
196                 {
197                         return true;
198                 }
199         }
200
201         return false;
202 }
203
204 const unsigned int *FLACEncoder::supportedChannelCount(void)
205 {
206         static const unsigned int supportedChannels[] = {2, 4, 5, 6, 7, 8, NULL};
207         return supportedChannels;
208 }
209
210 const unsigned int *FLACEncoder::supportedBitdepths(void)
211 {
212         static const unsigned int supportedBPS[] = {16, 24, NULL};
213         return supportedBPS;
214 }
215
216 const AbstractEncoderInfo *FLACEncoder::getEncoderInfo(void)
217 {
218         return &g_flacEncoderInfo;
219 }