OSDN Git Service

Updated Monkey's Audio binary to v4.11 (2013-01-20), including STDERR flush fix.
[lamexp/LameXP.git] / src / Encoder_AC3.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_AC3.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <QProcess>
28 #include <QDir>
29
30 AC3Encoder::AC3Encoder(void)
31 :
32         m_binary(lamexp_lookup_tool("aften.exe"))
33 {
34         if(m_binary.isEmpty())
35         {
36                 throw "Error initializing FLAC encoder. Tool 'aften.exe' is not registred!";
37         }
38
39         m_configAudioCodingMode = 0;
40         m_configDynamicRangeCompression = 5;
41         m_configExponentSearchSize = 8;
42         m_configFastBitAllocation = false;
43 }
44
45 AC3Encoder::~AC3Encoder(void)
46 {
47 }
48
49 bool AC3Encoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
50 {
51         QProcess process;
52         QStringList args;
53
54         switch(m_configRCMode)
55         {
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;
65         }
66
67         if(m_configAudioCodingMode >= 1)
68         {
69                 args << "-acmod" << QString::number(m_configAudioCodingMode - 1);
70         }
71         if(m_configDynamicRangeCompression != 5)
72         {
73                 args << "-dynrng" << QString::number(m_configDynamicRangeCompression);
74         }
75         if(m_configExponentSearchSize != 8)
76         {
77                 args << "-exps" << QString::number(m_configExponentSearchSize);
78         }
79         if(m_configFastBitAllocation)
80         {
81                 args << "-fba" << QString::number(1);
82         }
83
84         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
85
86         args << QDir::toNativeSeparators(sourceFile);
87         args << QDir::toNativeSeparators(outputFile);
88
89         if(!startProcess(process, m_binary, args))
90         {
91                 return false;
92         }
93
94         bool bTimeout = false;
95         bool bAborted = false;
96         int prevProgress = -1;
97
98         QRegExp regExp("progress:(\\s+)(\\d+)%(\\s+)\\|");
99
100         while(process.state() != QProcess::NotRunning)
101         {
102                 if(*abortFlag)
103                 {
104                         process.kill();
105                         bAborted = true;
106                         emit messageLogged("\nABORTED BY USER !!!");
107                         break;
108                 }
109                 process.waitForReadyRead(m_processTimeoutInterval);
110                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
111                 {
112                         process.kill();
113                         qWarning("Aften process timed out <-- killing!");
114                         emit messageLogged("\nPROCESS TIMEOUT !!!");
115                         bTimeout = true;
116                         break;
117                 }
118                 while(process.bytesAvailable() > 0)
119                 {
120                         QByteArray line = process.readLine();
121                         QString text = QString::fromUtf8(line.constData()).simplified();
122                         if(regExp.lastIndexIn(text) >= 0)
123                         {
124                                 bool ok = false;
125                                 int progress = regExp.cap(2).toInt(&ok);
126                                 if(ok && (progress > prevProgress))
127                                 {
128                                         emit statusUpdated(progress);
129                                         prevProgress = qMin(progress + 2, 99);
130                                 }
131                         }
132                         else if(!text.isEmpty())
133                         {
134                                 emit messageLogged(text);
135                         }
136                 }
137         }
138
139         process.waitForFinished();
140         if(process.state() != QProcess::NotRunning)
141         {
142                 process.kill();
143                 process.waitForFinished(-1);
144         }
145         
146         emit statusUpdated(100);
147         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
148
149         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
150         {
151                 return false;
152         }
153         
154         return true;
155 }
156
157 void AC3Encoder::setAudioCodingMode(int value)
158 {
159         m_configAudioCodingMode = qMin(8, qMax(0, value));
160 }
161
162 void AC3Encoder::setDynamicRangeCompression(int value)
163 {
164         m_configDynamicRangeCompression = qMin(5, qMax(0, value));
165 }
166
167 void AC3Encoder::setExponentSearchSize(int value)
168 {
169         m_configExponentSearchSize = qMin(32, qMax(1, value));
170 }
171
172 void AC3Encoder::setFastBitAllocation(bool value)
173 {
174         m_configFastBitAllocation = value;
175 }
176
177 QString AC3Encoder::extension(void)
178 {
179         return "ac3";
180 }
181
182 const unsigned int *AC3Encoder::supportedChannelCount(void)
183 {
184         static const unsigned int supportedChannels[] = {1, 2, 3, 4, 5, 6, NULL};
185         return supportedChannels;
186 }
187
188 const unsigned int *AC3Encoder::supportedSamplerates(void)
189 {
190         static const unsigned int supportedRates[] = {48000, 44100, 32000, NULL};
191         return supportedRates;
192 }
193
194 bool AC3Encoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
195 {
196         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
197         {
198                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
199                 {
200                         return true;
201                 }
202         }
203
204         return false;
205 }