Some code refactoring regarding the QWaitCondition/QMutex in FileAnalyzer_Task.
[LameXP.git] / src / Encoder_AC3.cpp
blobb6c92ab4750001233b0701769fa788ef9b5d9aed
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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 AC3Encoder::AC3Encoder(void)
32 m_binary(lamexp_lookup_tool("aften.exe"))
34 if(m_binary.isEmpty())
36 throw "Error initializing FLAC encoder. Tool 'aften.exe' is not registred!";
39 m_configAudioCodingMode = 0;
40 m_configDynamicRangeCompression = 5;
41 m_configExponentSearchSize = 8;
42 m_configFastBitAllocation = false;
45 AC3Encoder::~AC3Encoder(void)
49 bool AC3Encoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
51 QProcess process;
52 QStringList args;
54 switch(m_configRCMode)
56 case SettingsModel::VBRMode:
57 args << "-q" << QString::number(qMax(0, qMin(1023, m_configBitrate * 64)));
58 break;
59 case SettingsModel::CBRMode:
60 args << "-b" << QString::number(SettingsModel::ac3Bitrates[qMax(0, qMin(18, m_configBitrate))]);
61 break;
62 default:
63 throw "Bad rate-control mode!";
64 break;
67 if(m_configAudioCodingMode >= 1)
69 args << "-acmod" << QString::number(m_configAudioCodingMode - 1);
71 if(m_configDynamicRangeCompression != 5)
73 args << "-dynrng" << QString::number(m_configDynamicRangeCompression);
75 if(m_configExponentSearchSize != 8)
77 args << "-exps" << QString::number(m_configExponentSearchSize);
79 if(m_configFastBitAllocation)
81 args << "-fba" << QString::number(1);
84 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
86 args << QDir::toNativeSeparators(sourceFile);
87 args << QDir::toNativeSeparators(outputFile);
89 if(!startProcess(process, m_binary, args))
91 return false;
94 bool bTimeout = false;
95 bool bAborted = false;
96 int prevProgress = -1;
98 QRegExp regExp("progress:(\\s+)(\\d+)%(\\s+)\\|");
100 while(process.state() != QProcess::NotRunning)
102 if(*abortFlag)
104 process.kill();
105 bAborted = true;
106 emit messageLogged("\nABORTED BY USER !!!");
107 break;
109 process.waitForReadyRead(m_processTimeoutInterval);
110 if(!process.bytesAvailable() && process.state() == QProcess::Running)
112 process.kill();
113 qWarning("Aften process timed out <-- killing!");
114 emit messageLogged("\nPROCESS TIMEOUT !!!");
115 bTimeout = true;
116 break;
118 while(process.bytesAvailable() > 0)
120 QByteArray line = process.readLine();
121 QString text = QString::fromUtf8(line.constData()).simplified();
122 if(regExp.lastIndexIn(text) >= 0)
124 bool ok = false;
125 int progress = regExp.cap(2).toInt(&ok);
126 if(ok && (progress > prevProgress))
128 emit statusUpdated(progress);
129 prevProgress = qMin(progress + 2, 99);
132 else if(!text.isEmpty())
134 emit messageLogged(text);
139 process.waitForFinished();
140 if(process.state() != QProcess::NotRunning)
142 process.kill();
143 process.waitForFinished(-1);
146 emit statusUpdated(100);
147 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
149 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
151 return false;
154 return true;
157 void AC3Encoder::setAudioCodingMode(int value)
159 m_configAudioCodingMode = qMin(8, qMax(0, value));
162 void AC3Encoder::setDynamicRangeCompression(int value)
164 m_configDynamicRangeCompression = qMin(5, qMax(0, value));
167 void AC3Encoder::setExponentSearchSize(int value)
169 m_configExponentSearchSize = qMin(32, qMax(1, value));
172 void AC3Encoder::setFastBitAllocation(bool value)
174 m_configFastBitAllocation = value;
177 QString AC3Encoder::extension(void)
179 return "ac3";
182 const unsigned int *AC3Encoder::supportedChannelCount(void)
184 static const unsigned int supportedChannels[] = {1, 2, 3, 4, 5, 6, NULL};
185 return supportedChannels;
188 const unsigned int *AC3Encoder::supportedSamplerates(void)
190 static const unsigned int supportedRates[] = {48000, 44100, 32000, NULL};
191 return supportedRates;
194 bool AC3Encoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
196 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
198 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
200 return true;
204 return false;