Bump version.
[LameXP.git] / src / Tool_WaveProperties.cpp
blobe4c4a1495c6b56fd5dbbd0d7a98813bd632c9cd3
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2021 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; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "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, QAtomicInt &abortFlag)
52 QProcess process;
53 QStringList args;
55 args << "--i" << QDir::toNativeSeparators(sourceFile);
57 if(!startProcess(process, m_binary, args))
59 return false;
62 int progress = 0;
64 QRegExp regExp_prc("Precision\\s*:\\s*(\\d+)-bit", Qt::CaseInsensitive);
65 QRegExp regExp_srt("Sample Rate\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
66 QRegExp regExp_drt("Duration\\s*:\\s*(\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d\\d)", Qt::CaseInsensitive);
67 QRegExp regExp_chl("Channels\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
68 QRegExp regExp_enc("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!
70 const result_t result = awaitProcess(process, abortFlag, [this, &info, &progress, &regExp_prc, &regExp_srt, &regExp_drt, &regExp_chl, &regExp_enc](const QString &text)
72 if (regExp_prc.lastIndexIn(text) >= 0)
74 quint32 tmp;
75 if (MUtils::regexp_parse_uint32(regExp_prc, tmp))
77 info->setAudioBitdepth(tmp);
78 emit statusUpdated(qMin(progress += 25, 100));
80 return true;
82 if (regExp_enc.lastIndexIn(text) >= 0)
84 quint32 tmp;
85 if (MUtils::regexp_parse_uint32(regExp_enc, tmp))
87 info->setAudioBitdepth((tmp == 32) ? AudioFileModel::BITDEPTH_IEEE_FLOAT32 : tmp);
88 emit statusUpdated(qMin(progress += 25, 100));
90 return true;
92 if (regExp_srt.lastIndexIn(text) >= 0)
94 quint32 tmp;
95 if (MUtils::regexp_parse_uint32(regExp_srt, tmp))
97 info->setAudioSamplerate(tmp);
98 emit statusUpdated(qMin(progress += 25, 100));
100 return true;
102 if (regExp_drt.lastIndexIn(text) >= 0)
104 quint32 tmp[4];
105 if (MUtils::regexp_parse_uint32(regExp_drt, tmp, 4))
107 info->setDuration((tmp[0] * 3600) + (tmp[1] * 60) + tmp[2] + qRound(static_cast<double>(tmp[3]) / 100.0));
108 emit statusUpdated(qMin(progress += 25, 100));
110 return true;
112 if (regExp_chl.lastIndexIn(text) >= 0)
114 quint32 tmp;
115 if (MUtils::regexp_parse_uint32(regExp_chl, tmp))
117 info->setAudioChannels(tmp);
118 emit statusUpdated(qMin(progress += 25, 100));
120 return true;
122 return false;
125 return (result == RESULT_SUCCESS);