OSDN Git Service

Update MediaInfo binaries to v0.7.53 (2012-01-24), compiled with ICL 12.1.6 and MSVC...
[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
65         while(process.state() != QProcess::NotRunning)
66         {
67                 if(*abortFlag)
68                 {
69                         process.kill();
70                         bAborted = true;
71                         emit messageLogged("\nABORTED BY USER !!!");
72                         break;
73                 }
74                 process.waitForReadyRead(m_processTimeoutInterval);
75                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
76                 {
77                         process.kill();
78                         qWarning("SoX process timed out <-- killing!");
79                         emit messageLogged("\nPROCESS TIMEOUT !!!");
80                         bTimeout = true;
81                         break;
82                 }
83                 while(process.bytesAvailable() > 0)
84                 {
85                         QByteArray line = process.readLine();
86                         QString text = QString::fromUtf8(line.constData()).simplified();
87                         if(regExp_precision.lastIndexIn(text) >= 0)
88                         {
89                                 bool ok = false;
90                                 unsigned int tmp = regExp_precision.cap(1).toUInt(&ok);
91                                 if(ok) info->setFormatAudioBitdepth(tmp);
92                                 emit statusUpdated(qMin(progress += 25, 100));
93                         }
94                         if(regExp_samplerate.lastIndexIn(text) >= 0)
95                         {
96                                 bool ok = false;
97                                 unsigned int tmp = regExp_samplerate.cap(1).toUInt(&ok);
98                                 if(ok) info->setFormatAudioSamplerate(tmp);
99                                 emit statusUpdated(qMin(progress += 25, 100));
100                         }
101                         if(regExp_duration.lastIndexIn(text) >= 0)
102                         {
103                                 bool ok[4] = {false, false, false, false};
104                                 unsigned int tmp1 = regExp_duration.cap(1).toUInt(&ok[0]);
105                                 unsigned int tmp2 = regExp_duration.cap(2).toUInt(&ok[1]);
106                                 unsigned int tmp3 = regExp_duration.cap(3).toUInt(&ok[2]);
107                                 unsigned int tmp4 = regExp_duration.cap(4).toUInt(&ok[3]);
108                                 if(ok[0] && ok[1] && ok[2] && ok[3])
109                                 {
110                                         info->setFileDuration((tmp1 * 3600) + (tmp2 * 60) + tmp3 + qRound(static_cast<double>(tmp4) / 100.0));
111                                 }
112                                 emit statusUpdated(qMin(progress += 25, 100));
113                         }
114                         if(regExp_channels.lastIndexIn(text) >= 0)
115                         {
116                                 bool ok = false;
117                                 unsigned int tmp = regExp_channels.cap(1).toUInt(&ok);
118                                 if(ok) info->setFormatAudioChannels(tmp);
119                                 emit statusUpdated(qMin(progress += 25, 100));
120                         }
121                         if(!text.isEmpty())
122                         {
123                                 emit messageLogged(text);
124                         }
125                 }
126         }
127
128         process.waitForFinished();
129         if(process.state() != QProcess::NotRunning)
130         {
131                 process.kill();
132                 process.waitForFinished(-1);
133         }
134
135         emit statusUpdated(100);
136         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
137
138         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
139         {
140                 return false;
141         }
142         
143         return true;
144 }