1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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.
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.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Tool_WaveProperties.h"
27 #include "Model_AudioFile.h"
30 #include <MUtils/Exception.h>
36 WaveProperties::WaveProperties(void)
38 m_binary(lamexp_tools_lookup("sox.exe"))
40 if(m_binary
.isEmpty())
42 MUTILS_THROW("Error initializing MP3 encoder. Tool 'lame.exe' is not registred!");
46 WaveProperties::~WaveProperties(void)
50 bool WaveProperties::detect(const QString
&sourceFile
, AudioFileModel_TechInfo
*info
, volatile bool *abortFlag
)
55 args
<< "--i" << QDir::toNativeSeparators(sourceFile
);
57 if(!startProcess(process
, m_binary
, args
))
62 bool bTimeout
= false;
63 bool bAborted
= false;
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!
73 while(process
.state() != QProcess::NotRunning
)
79 emit
messageLogged("\nABORTED BY USER !!!");
82 process
.waitForReadyRead(m_processTimeoutInterval
);
83 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
86 qWarning("SoX process timed out <-- killing!");
87 emit
messageLogged("\nPROCESS TIMEOUT !!!");
91 while(process
.bytesAvailable() > 0)
93 QByteArray line
= process
.readLine();
94 QString text
= QString::fromUtf8(line
.constData()).simplified();
95 if(regExp_precision
.lastIndexIn(text
) >= 0)
98 unsigned int tmp
= regExp_precision
.cap(1).toUInt(&ok
);
99 if(ok
) info
->setAudioBitdepth(tmp
);
100 emit
statusUpdated(qMin(progress
+= 25, 100));
102 if(regExp_encoding
.lastIndexIn(text
) >= 0)
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));
109 if(regExp_samplerate
.lastIndexIn(text
) >= 0)
112 unsigned int tmp
= regExp_samplerate
.cap(1).toUInt(&ok
);
113 if(ok
) info
->setAudioSamplerate(tmp
);
114 emit
statusUpdated(qMin(progress
+= 25, 100));
116 if(regExp_duration
.lastIndexIn(text
) >= 0)
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])
125 info
->setDuration((tmp1
* 3600) + (tmp2
* 60) + tmp3
+ qRound(static_cast<double>(tmp4
) / 100.0));
127 emit
statusUpdated(qMin(progress
+= 25, 100));
129 if(regExp_channels
.lastIndexIn(text
) >= 0)
132 unsigned int tmp
= regExp_channels
.cap(1).toUInt(&ok
);
133 if(ok
) info
->setAudioChannels(tmp
);
134 emit
statusUpdated(qMin(progress
+= 25, 100));
138 emit
messageLogged(text
);
143 process
.waitForFinished();
144 if(process
.state() != QProcess::NotRunning
)
147 process
.waitForFinished(-1);
150 emit
statusUpdated(100);
151 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
153 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)