OSDN Git Service

Fix compilation after previous changes to AudioFileModel in previous commit.
[lamexp/LameXP.git] / src / Encoder_DCA.cpp
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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Encoder_DCA.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <QProcess>
28 #include <QDir>
29 #include <limits.h>
30
31 static int index2bitrate(const int index)
32 {
33         return (index < 8) ? ((index + 1) * 32) : ((index < 12) ? ((index - 3) * 64) : ((index < 24) ? (index - 7) * 128 : (index - 15) * 256));
34 }
35
36 ///////////////////////////////////////////////////////////////////////////////
37 // Encoder Info
38 ///////////////////////////////////////////////////////////////////////////////
39
40 class DCAEncoderInfo : public AbstractEncoderInfo
41 {
42         virtual bool isModeSupported(int mode) const
43         {
44                 switch(mode)
45                 {
46                 case SettingsModel::VBRMode:
47                 case SettingsModel::ABRMode:
48                         return false;
49                         break;
50                 case SettingsModel::CBRMode:
51                         return true;
52                         break;
53                 default:
54                         throw "Bad RC mode specified!";
55                 }
56         }
57
58         virtual int valueCount(int mode) const
59         {
60                 switch(mode)
61                 {
62                 case SettingsModel::VBRMode:
63                 case SettingsModel::ABRMode:
64                         return 0;
65                         break;
66                 case SettingsModel::CBRMode:
67                         return 32;
68                         break;
69                 default:
70                         throw "Bad RC mode specified!";
71                 }
72         }
73
74         virtual int valueAt(int mode, int index) const
75         {
76                 switch(mode)
77                 {
78                 case SettingsModel::VBRMode:
79                 case SettingsModel::ABRMode:
80                         return 0;
81                         break;
82                 case SettingsModel::CBRMode:
83                         return qBound(32, index2bitrate(index), 4096);
84                         break;
85                 default:
86                         throw "Bad RC mode specified!";
87                 }
88         }
89
90         virtual int valueType(int mode) const
91         {
92                 switch(mode)
93                 {
94                 case SettingsModel::VBRMode:
95                         return TYPE_QUALITY_LEVEL_INT;
96                         break;
97                 case SettingsModel::ABRMode:
98                 case SettingsModel::CBRMode:
99                         return TYPE_BITRATE;
100                         break;
101                 default:
102                         throw "Bad RC mode specified!";
103                 }
104         }
105
106         virtual const char *description(void) const
107         {
108                 static const char* s_description = "dcaenc-2 by Alexander E. Patrakov";
109                 return s_description;
110         }
111 }
112 static const g_dcaEncoderInfo;
113
114 ///////////////////////////////////////////////////////////////////////////////
115 // Encoder implementation
116 ///////////////////////////////////////////////////////////////////////////////
117
118 DCAEncoder::DCAEncoder(void)
119 :
120         m_binary(lamexp_lookup_tool("dcaenc.exe"))
121 {
122         if(m_binary.isEmpty())
123         {
124                 throw "Error initializing DCA encoder. Tool 'dcaenc.exe' is not registred!";
125         }
126 }
127
128 DCAEncoder::~DCAEncoder(void)
129 {
130 }
131
132 bool DCAEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
133 {
134         QProcess process;
135         QStringList args;
136
137         args << "-i" << QDir::toNativeSeparators(sourceFile);
138         args << "-o" << QDir::toNativeSeparators(outputFile);
139         args << "-b" << QString::number(qBound(32, index2bitrate(m_configBitrate), 4096));
140
141         if(!startProcess(process, m_binary, args))
142         {
143                 return false;
144         }
145
146         bool bTimeout = false;
147         bool bAborted = false;
148         int prevProgress = -1;
149
150         QRegExp regExp("\\[(\\d+)\\.(\\d+)%\\]");
151
152         while(process.state() != QProcess::NotRunning)
153         {
154                 if(*abortFlag)
155                 {
156                         process.kill();
157                         bAborted = true;
158                         emit messageLogged("\nABORTED BY USER !!!");
159                         break;
160                 }
161                 process.waitForReadyRead(m_processTimeoutInterval);
162                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
163                 {
164                         process.kill();
165                         qWarning("DCAENC process timed out <-- killing!");
166                         emit messageLogged("\nPROCESS TIMEOUT !!!");
167                         bTimeout = true;
168                         break;
169                 }
170                 while(process.bytesAvailable() > 0)
171                 {
172                         QByteArray line = process.readLine();
173                         QString text = QString::fromUtf8(line.constData()).simplified();
174                         if(regExp.lastIndexIn(text) >= 0)
175                         {
176                                 bool ok = false;
177                                 int progress = regExp.cap(1).toInt(&ok);
178                                 if(ok && (progress > prevProgress))
179                                 {
180                                         emit statusUpdated(progress);
181                                         prevProgress = qMin(progress + 2, 99);
182                                 }
183                         }
184                         else if(!text.isEmpty())
185                         {
186                                 emit messageLogged(text);
187                         }
188                 }
189         }
190
191         process.waitForFinished();
192         if(process.state() != QProcess::NotRunning)
193         {
194                 process.kill();
195                 process.waitForFinished(-1);
196         }
197         
198         emit statusUpdated(100);
199         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
200
201         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
202         {
203                 return false;
204         }
205         
206         return true;
207 }
208
209 QString DCAEncoder::extension(void)
210 {
211         return "dts";
212 }
213
214 bool DCAEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
215 {
216         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
217         {
218                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
219                 {
220                         return true;
221                 }
222         }
223
224         return false;
225 }
226
227 const unsigned int *DCAEncoder::supportedChannelCount(void)
228 {
229         static const unsigned int supportedChannels[] = {1, 2, 4, 5, 6, NULL};
230         return supportedChannels;
231 }
232
233 const unsigned int *DCAEncoder::supportedSamplerates(void)
234 {
235         static const unsigned int supportedRates[] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, NULL};
236         return supportedRates;
237 }
238
239 const unsigned int *DCAEncoder::supportedBitdepths(void)
240 {
241         static const unsigned int supportedBPS[] = {16, 32, NULL};
242         return supportedBPS;
243 }
244
245 const AbstractEncoderInfo *DCAEncoder::getEncoderInfo(void)
246 {
247         return &g_dcaEncoderInfo;
248 }