OSDN Git Service

Updated Russian translation. Thanks to Иван Митин <bardak@inbox.ru>.
[lamexp/LameXP.git] / src / Encoder_DCA.cpp
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.
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 DCAEncoder::DCAEncoder(void)
32 :
33         m_binary(lamexp_lookup_tool("dcaenc.exe"))
34 {
35         if(m_binary.isEmpty())
36         {
37                 throw "Error initializing DCA encoder. Tool 'dcaenc.exe' is not registred!";
38         }
39 }
40
41 DCAEncoder::~DCAEncoder(void)
42 {
43 }
44
45 bool DCAEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
46 {
47         QProcess process;
48         QStringList args;
49
50         int bitrate = qBound(32, m_configBitrate * 32, 6144);
51
52         args << "-i" << QDir::toNativeSeparators(sourceFile);
53         args << "-o" << QDir::toNativeSeparators(outputFile);
54         args << "-b" << QString::number(bitrate);
55
56         if(!startProcess(process, m_binary, args))
57         {
58                 return false;
59         }
60
61         bool bTimeout = false;
62         bool bAborted = false;
63         int prevProgress = -1;
64
65         QRegExp regExp("\\[(\\d+)\\.(\\d+)%\\]");
66
67         while(process.state() != QProcess::NotRunning)
68         {
69                 if(*abortFlag)
70                 {
71                         process.kill();
72                         bAborted = true;
73                         emit messageLogged("\nABORTED BY USER !!!");
74                         break;
75                 }
76                 process.waitForReadyRead(m_processTimeoutInterval);
77                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
78                 {
79                         process.kill();
80                         qWarning("DCAENC process timed out <-- killing!");
81                         emit messageLogged("\nPROCESS TIMEOUT !!!");
82                         bTimeout = true;
83                         break;
84                 }
85                 while(process.bytesAvailable() > 0)
86                 {
87                         QByteArray line = process.readLine();
88                         QString text = QString::fromUtf8(line.constData()).simplified();
89                         if(regExp.lastIndexIn(text) >= 0)
90                         {
91                                 bool ok = false;
92                                 int progress = regExp.cap(1).toInt(&ok);
93                                 if(ok && (progress > prevProgress))
94                                 {
95                                         emit statusUpdated(progress);
96                                         prevProgress = qMin(progress + 2, 99);
97                                 }
98                         }
99                         else if(!text.isEmpty())
100                         {
101                                 emit messageLogged(text);
102                         }
103                 }
104         }
105
106         process.waitForFinished();
107         if(process.state() != QProcess::NotRunning)
108         {
109                 process.kill();
110                 process.waitForFinished(-1);
111         }
112         
113         emit statusUpdated(100);
114         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
115
116         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
117         {
118                 return false;
119         }
120         
121         return true;
122 }
123
124 QString DCAEncoder::extension(void)
125 {
126         return "dts";
127 }
128
129 bool DCAEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
130 {
131         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
132         {
133                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
134                 {
135                         return true;
136                 }
137         }
138
139         return false;
140 }
141
142 const unsigned int *DCAEncoder::supportedChannelCount(void)
143 {
144         static const unsigned int supportedChannels[] = {1, 2, 4, 5, 6, NULL};
145         return supportedChannels;
146 }
147
148 const unsigned int *DCAEncoder::supportedSamplerates(void)
149 {
150         static const unsigned int supportedRates[] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, NULL};
151         return supportedRates;
152 }
153
154 const unsigned int *DCAEncoder::supportedBitdepths(void)
155 {
156         static const unsigned int supportedBPS[] = {16, 32, NULL};
157         return supportedBPS;
158 }