Adapt for latest MUtils changes.
[LameXP.git] / src / Tool_WaveProperties.cpp
bloba97766a9a4041f63abfb490a794d4cd906c34ec8
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 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.
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"
25 //Internal
26 #include "Global.h"
27 #include "Model_AudioFile.h"
29 //MUtils
30 #include <MUtils/Exception.h>
32 //Qt
33 #include <QDir>
34 #include <QProcess>
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)
52 QProcess process;
53 QStringList args;
55 args << "--i" << QDir::toNativeSeparators(sourceFile);
57 if(!startProcess(process, m_binary, args))
59 return false;
62 bool bTimeout = false;
63 bool bAborted = false;
65 int progress = 0;
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)
75 if(*abortFlag)
77 process.kill();
78 bAborted = true;
79 emit messageLogged("\nABORTED BY USER !!!");
80 break;
82 process.waitForReadyRead(m_processTimeoutInterval);
83 if(!process.bytesAvailable() && process.state() == QProcess::Running)
85 process.kill();
86 qWarning("SoX process timed out <-- killing!");
87 emit messageLogged("\nPROCESS TIMEOUT !!!");
88 bTimeout = true;
89 break;
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)
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));
102 if(regExp_encoding.lastIndexIn(text) >= 0)
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));
109 if(regExp_samplerate.lastIndexIn(text) >= 0)
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));
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)
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));
136 if(!text.isEmpty())
138 emit messageLogged(text);
143 process.waitForFinished();
144 if(process.state() != QProcess::NotRunning)
146 process.kill();
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)
155 return false;
158 return true;