OSDN Git Service

Set creation/modified time of the encoded file the same value as the original file...
[lamexp/LameXP.git] / src / Tool_WaveProperties.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Tool_WaveProperties.h"
24
25 //Internal
26 #include "Global.h"
27 #include "Model_AudioFile.h"
28
29 //MUtils
30 #include <MUtils/Exception.h>
31
32 //Qt
33 #include <QDir>
34 #include <QProcess>
35
36 WaveProperties::WaveProperties(void)
37 :
38         m_binary(lamexp_tools_lookup("sox.exe"))
39 {
40         if(m_binary.isEmpty())
41         {
42                 MUTILS_THROW("Error initializing MP3 encoder. Tool 'lame.exe' is not registred!");
43         }
44 }
45
46 WaveProperties::~WaveProperties(void)
47 {
48 }
49
50 bool WaveProperties::detect(const QString &sourceFile, AudioFileModel_TechInfo *info, volatile bool *abortFlag)
51 {
52         QProcess process;
53         QStringList args;
54
55         args << "--i" << QDir::toNativeSeparators(sourceFile);
56
57         if(!startProcess(process, m_binary, args))
58         {
59                 return false;
60         }
61
62         bool bTimeout = false;
63         bool bAborted = false;
64
65         int progress = 0;
66
67         QRegExp regExp_precision("Precision\\s*:\\s*(\\d+)-bit", Qt::CaseInsensitive);
68         QRegExp regExp_samplerate("Sample Rate\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
69         QRegExp regExp_duration("Duration\\s*:\\s*(\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d\\d)", Qt::CaseInsensitive);
70         QRegExp regExp_channels("Channels\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
71         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!
72
73         while(process.state() != QProcess::NotRunning)
74         {
75                 if(*abortFlag)
76                 {
77                         process.kill();
78                         bAborted = true;
79                         emit messageLogged("\nABORTED BY USER !!!");
80                         break;
81                 }
82                 process.waitForReadyRead(m_processTimeoutInterval);
83                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
84                 {
85                         process.kill();
86                         qWarning("SoX process timed out <-- killing!");
87                         emit messageLogged("\nPROCESS TIMEOUT !!!");
88                         bTimeout = true;
89                         break;
90                 }
91                 while(process.bytesAvailable() > 0)
92                 {
93                         QByteArray line = process.readLine();
94                         QString text = QString::fromUtf8(line.constData()).simplified();
95                         if(regExp_precision.lastIndexIn(text) >= 0)
96                         {
97                                 bool ok = false;
98                                 unsigned int tmp = regExp_precision.cap(1).toUInt(&ok);
99                                 if(ok) info->setAudioBitdepth(tmp);
100                                 emit statusUpdated(qMin(progress += 25, 100));
101                         }
102                         if(regExp_encoding.lastIndexIn(text) >= 0)
103                         {
104                                 bool ok = false;
105                                 unsigned int tmp = regExp_encoding.cap(1).toUInt(&ok);
106                                 if(ok) info->setAudioBitdepth((tmp == 32) ? AudioFileModel::BITDEPTH_IEEE_FLOAT32 : tmp);
107                                 emit statusUpdated(qMin(progress += 25, 100));
108                         }
109                         if(regExp_samplerate.lastIndexIn(text) >= 0)
110                         {
111                                 bool ok = false;
112                                 unsigned int tmp = regExp_samplerate.cap(1).toUInt(&ok);
113                                 if(ok) info->setAudioSamplerate(tmp);
114                                 emit statusUpdated(qMin(progress += 25, 100));
115                         }
116                         if(regExp_duration.lastIndexIn(text) >= 0)
117                         {
118                                 bool ok[4] = {false, false, false, false};
119                                 unsigned int tmp1 = regExp_duration.cap(1).toUInt(&ok[0]);
120                                 unsigned int tmp2 = regExp_duration.cap(2).toUInt(&ok[1]);
121                                 unsigned int tmp3 = regExp_duration.cap(3).toUInt(&ok[2]);
122                                 unsigned int tmp4 = regExp_duration.cap(4).toUInt(&ok[3]);
123                                 if(ok[0] && ok[1] && ok[2] && ok[3])
124                                 {
125                                         info->setDuration((tmp1 * 3600) + (tmp2 * 60) + tmp3 + qRound(static_cast<double>(tmp4) / 100.0));
126                                 }
127                                 emit statusUpdated(qMin(progress += 25, 100));
128                         }
129                         if(regExp_channels.lastIndexIn(text) >= 0)
130                         {
131                                 bool ok = false;
132                                 unsigned int tmp = regExp_channels.cap(1).toUInt(&ok);
133                                 if(ok) info->setAudioChannels(tmp);
134                                 emit statusUpdated(qMin(progress += 25, 100));
135                         }
136                         if(!text.isEmpty())
137                         {
138                                 emit messageLogged(text);
139                         }
140                 }
141         }
142
143         process.waitForFinished();
144         if(process.state() != QProcess::NotRunning)
145         {
146                 process.kill();
147                 process.waitForFinished(-1);
148         }
149
150         emit statusUpdated(100);
151         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
152
153         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
154         {
155                 return false;
156         }
157         
158         return true;
159 }