Properly detect Windows 8, now that Qt supports it officially.
[LameXP.git] / src / Encoder_DCA.cpp
blob4902eda5a56c0b2faccd27d1fdc303da39a278dd
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.
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_DCA.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <QProcess>
28 #include <QDir>
29 #include <limits.h>
31 static int index2bitrate(const int index)
33 return (index < 8) ? ((index + 1) * 32) : ((index < 12) ? ((index - 3) * 64) : ((index < 24) ? (index - 7) * 128 : (index - 15) * 256));
36 ///////////////////////////////////////////////////////////////////////////////
37 // Encoder Info
38 ///////////////////////////////////////////////////////////////////////////////
40 class DCAEncoderInfo : public AbstractEncoderInfo
42 virtual bool isModeSupported(int mode) const
44 switch(mode)
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!";
58 virtual int valueCount(int mode) const
60 switch(mode)
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!";
74 virtual int valueAt(int mode, int index) const
76 switch(mode)
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!";
90 virtual int valueType(int mode) const
92 switch(mode)
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!";
106 virtual const char *description(void) const
108 static const char* s_description = "dcaenc-2 by Alexander E. Patrakov";
109 return s_description;
112 static const g_dcaEncoderInfo;
114 ///////////////////////////////////////////////////////////////////////////////
115 // Encoder implementation
116 ///////////////////////////////////////////////////////////////////////////////
118 DCAEncoder::DCAEncoder(void)
120 m_binary(lamexp_lookup_tool("dcaenc.exe"))
122 if(m_binary.isEmpty())
124 throw "Error initializing DCA encoder. Tool 'dcaenc.exe' is not registred!";
128 DCAEncoder::~DCAEncoder(void)
132 bool DCAEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
134 QProcess process;
135 QStringList args;
137 args << "-i" << QDir::toNativeSeparators(sourceFile);
138 args << "-o" << QDir::toNativeSeparators(outputFile);
139 args << "-b" << QString::number(qBound(32, index2bitrate(m_configBitrate), 4096));
141 if(!startProcess(process, m_binary, args))
143 return false;
146 bool bTimeout = false;
147 bool bAborted = false;
148 int prevProgress = -1;
150 QRegExp regExp("\\[(\\d+)\\.(\\d+)%\\]");
152 while(process.state() != QProcess::NotRunning)
154 if(*abortFlag)
156 process.kill();
157 bAborted = true;
158 emit messageLogged("\nABORTED BY USER !!!");
159 break;
161 process.waitForReadyRead(m_processTimeoutInterval);
162 if(!process.bytesAvailable() && process.state() == QProcess::Running)
164 process.kill();
165 qWarning("DCAENC process timed out <-- killing!");
166 emit messageLogged("\nPROCESS TIMEOUT !!!");
167 bTimeout = true;
168 break;
170 while(process.bytesAvailable() > 0)
172 QByteArray line = process.readLine();
173 QString text = QString::fromUtf8(line.constData()).simplified();
174 if(regExp.lastIndexIn(text) >= 0)
176 bool ok = false;
177 int progress = regExp.cap(1).toInt(&ok);
178 if(ok && (progress > prevProgress))
180 emit statusUpdated(progress);
181 prevProgress = qMin(progress + 2, 99);
184 else if(!text.isEmpty())
186 emit messageLogged(text);
191 process.waitForFinished();
192 if(process.state() != QProcess::NotRunning)
194 process.kill();
195 process.waitForFinished(-1);
198 emit statusUpdated(100);
199 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
201 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
203 return false;
206 return true;
209 QString DCAEncoder::extension(void)
211 return "dts";
214 bool DCAEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
216 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
218 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
220 return true;
224 return false;
227 const unsigned int *DCAEncoder::supportedChannelCount(void)
229 static const unsigned int supportedChannels[] = {1, 2, 4, 5, 6, NULL};
230 return supportedChannels;
233 const unsigned int *DCAEncoder::supportedSamplerates(void)
235 static const unsigned int supportedRates[] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, NULL};
236 return supportedRates;
239 const unsigned int *DCAEncoder::supportedBitdepths(void)
241 static const unsigned int supportedBPS[] = {16, 32, NULL};
242 return supportedBPS;
245 const AbstractEncoderInfo *DCAEncoder::getEncoderInfo(void)
247 return &g_dcaEncoderInfo;