Added an option to hibernate the computer ("suspend to disk") instead of shutting...
[LameXP.git] / src / Encoder_AC3.cpp
blob2b62e5714b439db2db19886634c5348d0e5cc8a7
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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_AC3.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <QProcess>
28 #include <QDir>
30 #define max(a,b) (((a) > (b)) ? (a) : (b))
31 #define min(a,b) (((a) < (b)) ? (a) : (b))
33 AC3Encoder::AC3Encoder(void)
35 m_binary(lamexp_lookup_tool("aften.exe"))
37 if(m_binary.isEmpty())
39 throw "Error initializing FLAC encoder. Tool 'aften.exe' is not registred!";
42 m_configAudioCodingMode = 0;
43 m_configDynamicRangeCompression = 5;
44 m_configExponentSearchSize = 8;
45 m_configFastBitAllocation = false;
48 AC3Encoder::~AC3Encoder(void)
52 bool AC3Encoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
54 QProcess process;
55 QStringList args;
57 switch(m_configRCMode)
59 case SettingsModel::VBRMode:
60 args << "-q" << QString::number(max(0, min(1023, m_configBitrate * 64)));
61 break;
62 case SettingsModel::CBRMode:
63 args << "-b" << QString::number(SettingsModel::ac3Bitrates[max(0, min(18, m_configBitrate))]);
64 break;
65 default:
66 throw "Bad rate-control mode!";
67 break;
70 if(m_configAudioCodingMode >= 1)
72 args << "-acmod" << QString::number(m_configAudioCodingMode - 1);
74 if(m_configDynamicRangeCompression != 5)
76 args << "-dynrng" << QString::number(m_configDynamicRangeCompression);
78 if(m_configExponentSearchSize != 8)
80 args << "-exps" << QString::number(m_configExponentSearchSize);
82 if(m_configFastBitAllocation)
84 args << "-fba" << QString::number(1);
87 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
89 args << QDir::toNativeSeparators(sourceFile);
90 args << QDir::toNativeSeparators(outputFile);
92 if(!startProcess(process, m_binary, args))
94 return false;
97 bool bTimeout = false;
98 bool bAborted = false;
100 QRegExp regExp("progress:(\\s+)(\\d+)%(\\s+)\\|");
102 while(process.state() != QProcess::NotRunning)
104 if(*abortFlag)
106 process.kill();
107 bAborted = true;
108 emit messageLogged("\nABORTED BY USER !!!");
109 break;
111 process.waitForReadyRead(m_processTimeoutInterval);
112 if(!process.bytesAvailable() && process.state() == QProcess::Running)
114 process.kill();
115 qWarning("Aften process timed out <-- killing!");
116 emit messageLogged("\nPROCESS TIMEOUT !!!");
117 bTimeout = true;
118 break;
120 while(process.bytesAvailable() > 0)
122 QByteArray line = process.readLine();
123 QString text = QString::fromUtf8(line.constData()).simplified();
124 if(regExp.lastIndexIn(text) >= 0)
126 bool ok = false;
127 int progress = regExp.cap(2).toInt(&ok);
128 if(ok) emit statusUpdated(progress);
130 else if(!text.isEmpty())
132 emit messageLogged(text);
137 process.waitForFinished();
138 if(process.state() != QProcess::NotRunning)
140 process.kill();
141 process.waitForFinished(-1);
144 emit statusUpdated(100);
145 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
147 if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
149 return false;
152 return true;
155 void AC3Encoder::setAudioCodingMode(int value)
157 m_configAudioCodingMode = min(8, max(0, value));
160 void AC3Encoder::setDynamicRangeCompression(int value)
162 m_configDynamicRangeCompression = min(5, max(0, value));
165 void AC3Encoder::setExponentSearchSize(int value)
167 m_configExponentSearchSize = min(32, max(1, value));
170 void AC3Encoder::setFastBitAllocation(bool value)
172 m_configFastBitAllocation = value;
175 QString AC3Encoder::extension(void)
177 return "ac3";
180 const unsigned int *AC3Encoder::requiresDownsample(void)
182 static const unsigned int supportedRates[] = {48000, 44100, 32000, NULL};
183 return supportedRates;
186 bool AC3Encoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
188 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
190 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
192 return true;
196 return false;