OSDN Git Service

Updated Russian translation. Thanks to Иван Митин <bardak@inbox.ru>.
[lamexp/LameXP.git] / src / Tool_WaveProperties.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 "Tool_WaveProperties.h"
23
24 #include "Global.h"
25 #include "Model_AudioFile.h"
26
27 #include <QProcess>
28
29 WaveProperties::WaveProperties(void)
30 :
31         m_binary(lamexp_lookup_tool("sox.exe"))
32 {
33         if(m_binary.isEmpty())
34         {
35                 throw "Error initializing MP3 encoder. Tool 'lame.exe' is not registred!";
36         }
37 }
38
39 WaveProperties::~WaveProperties(void)
40 {
41 }
42
43 bool WaveProperties::detect(const QString &sourceFile, AudioFileModel *info, volatile bool *abortFlag)
44 {
45         QProcess process;
46         QStringList args;
47
48         args << "--i" << sourceFile;
49
50         if(!startProcess(process, m_binary, args))
51         {
52                 return false;
53         }
54
55         bool bTimeout = false;
56         bool bAborted = false;
57
58         int progress = 0;
59
60         QRegExp regExp_precision("Precision\\s*:\\s*(\\d+)-bit", Qt::CaseInsensitive);
61         QRegExp regExp_samplerate("Sample Rate\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
62         QRegExp regExp_duration("Duration\\s*:\\s*(\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d\\d)", Qt::CaseInsensitive);
63         QRegExp regExp_channels("Channels\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
64         QRegExp regExp_encoding("Sample Encoding\\s*:\\s*(\\d+)-bit\\s*Float", Qt::CaseInsensitive); //SoX returns a precision of 24-Bit for 32-Bit Float data, so we detect it this way!
65
66         while(process.state() != QProcess::NotRunning)
67         {
68                 if(*abortFlag)
69                 {
70                         process.kill();
71                         bAborted = true;
72                         emit messageLogged("\nABORTED BY USER !!!");
73                         break;
74                 }
75                 process.waitForReadyRead(m_processTimeoutInterval);
76                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
77                 {
78                         process.kill();
79                         qWarning("SoX process timed out <-- killing!");
80                         emit messageLogged("\nPROCESS TIMEOUT !!!");
81                         bTimeout = true;
82                         break;
83                 }
84                 while(process.bytesAvailable() > 0)
85                 {
86                         QByteArray line = process.readLine();
87                         QString text = QString::fromUtf8(line.constData()).simplified();
88                         if(regExp_precision.lastIndexIn(text) >= 0)
89                         {
90                                 bool ok = false;
91                                 unsigned int tmp = regExp_precision.cap(1).toUInt(&ok);
92                                 if(ok) info->setFormatAudioBitdepth(tmp);
93                                 emit statusUpdated(qMin(progress += 25, 100));
94                         }
95                         if(regExp_encoding.lastIndexIn(text) >= 0)
96                         {
97                                 bool ok = false;
98                                 unsigned int tmp = regExp_encoding.cap(1).toUInt(&ok);
99                                 if(ok) info->setFormatAudioBitdepth(tmp);
100                                 emit statusUpdated(qMin(progress += 25, 100));
101                         }
102                         if(regExp_samplerate.lastIndexIn(text) >= 0)
103                         {
104                                 bool ok = false;
105                                 unsigned int tmp = regExp_samplerate.cap(1).toUInt(&ok);
106                                 if(ok) info->setFormatAudioSamplerate(tmp);
107                                 emit statusUpdated(qMin(progress += 25, 100));
108                         }
109                         if(regExp_duration.lastIndexIn(text) >= 0)
110                         {
111                                 bool ok[4] = {false, false, false, false};
112                                 unsigned int tmp1 = regExp_duration.cap(1).toUInt(&ok[0]);
113                                 unsigned int tmp2 = regExp_duration.cap(2).toUInt(&ok[1]);
114                                 unsigned int tmp3 = regExp_duration.cap(3).toUInt(&ok[2]);
115                                 unsigned int tmp4 = regExp_duration.cap(4).toUInt(&ok[3]);
116                                 if(ok[0] && ok[1] && ok[2] && ok[3])
117                                 {
118                                         info->setFileDuration((tmp1 * 3600) + (tmp2 * 60) + tmp3 + qRound(static_cast<double>(tmp4) / 100.0));
119                                 }
120                                 emit statusUpdated(qMin(progress += 25, 100));
121                         }
122                         if(regExp_channels.lastIndexIn(text) >= 0)
123                         {
124                                 bool ok = false;
125                                 unsigned int tmp = regExp_channels.cap(1).toUInt(&ok);
126                                 if(ok) info->setFormatAudioChannels(tmp);
127                                 emit statusUpdated(qMin(progress += 25, 100));
128                         }
129                         if(!text.isEmpty())
130                         {
131                                 emit messageLogged(text);
132                         }
133                 }
134         }
135
136         process.waitForFinished();
137         if(process.state() != QProcess::NotRunning)
138         {
139                 process.kill();
140                 process.waitForFinished(-1);
141         }
142
143         emit statusUpdated(100);
144         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
145
146         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
147         {
148                 return false;
149         }
150         
151         return true;
152 }