OSDN Git Service

Updated changelog.
[lamexp/LameXP.git] / src / Tool_WaveProperties.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2023 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!
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, QAtomicInt &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         int progress = 0;
63
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!
69
70         const result_t result = awaitProcess(process, abortFlag, [this, &info, &progress, &regExp_prc, &regExp_srt, &regExp_drt, &regExp_chl, &regExp_enc](const QString &text)
71         {
72                 if (regExp_prc.lastIndexIn(text) >= 0)
73                 {
74                         quint32 tmp;
75                         if (MUtils::regexp_parse_uint32(regExp_prc, tmp))
76                         {
77                                 info->setAudioBitdepth(tmp);
78                                 emit statusUpdated(qMin(progress += 25, 100));
79                         }
80                         return true;
81                 }
82                 if (regExp_enc.lastIndexIn(text) >= 0)
83                 {
84                         quint32 tmp;
85                         if (MUtils::regexp_parse_uint32(regExp_enc, tmp))
86                         {
87                                 info->setAudioBitdepth((tmp == 32) ? AudioFileModel::BITDEPTH_IEEE_FLOAT32 : tmp);
88                                 emit statusUpdated(qMin(progress += 25, 100));
89                         }
90                         return true;
91                 }
92                 if (regExp_srt.lastIndexIn(text) >= 0)
93                 {
94                         quint32 tmp;
95                         if (MUtils::regexp_parse_uint32(regExp_srt, tmp))
96                         {
97                                 info->setAudioSamplerate(tmp);
98                                 emit statusUpdated(qMin(progress += 25, 100));
99                         }
100                         return true;
101                 }
102                 if (regExp_drt.lastIndexIn(text) >= 0)
103                 {
104                         quint32 tmp[4];
105                         if (MUtils::regexp_parse_uint32(regExp_drt, tmp, 4))
106                         {
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));
109                         }
110                         return true;
111                 }
112                 if (regExp_chl.lastIndexIn(text) >= 0)
113                 {
114                         quint32 tmp;
115                         if (MUtils::regexp_parse_uint32(regExp_chl, tmp))
116                         {
117                                 info->setAudioChannels(tmp);
118                                 emit statusUpdated(qMin(progress += 25, 100));
119                         }
120                         return true;
121                 }
122                 return false;
123         });
124         
125         return (result == RESULT_SUCCESS);
126 }