OSDN Git Service

Updated Monkey's Audio binaries to v4.73 (2019-05-15), compiled with ICL 19.0 and...
[lamexp/LameXP.git] / src / Filter_Normalize.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2019 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 "Filter_Normalize.h"
24
25 //Internal
26 #include "Global.h"
27
28 //MUtils
29 #include <MUtils/Global.h>
30 #include <MUtils/Exception.h>
31
32 //Qt
33 #include <QDir>
34 #include <QProcess>
35 #include <QRegExp>
36
37 static double dbToLinear(const double &value)
38 {
39         return pow(10.0, value / 20.0);
40 }
41
42 NormalizeFilter::NormalizeFilter(const int &peakVolume, const bool &dnyAudNorm, const bool &channelsCoupled, const int &filterSize)
43 :
44         m_binary(lamexp_tools_lookup("sox.exe")),
45         m_useDynAudNorm(dnyAudNorm),
46         m_peakVolume(qMin(-50, qMax(-3200, peakVolume))),
47         m_channelsCoupled(channelsCoupled),
48         m_filterLength(qBound(3, filterSize + (1 - (filterSize % 2)), 301))
49 {
50         if(m_binary.isEmpty())
51         {
52                 MUTILS_THROW("Error initializing SoX filter. Tool 'sox.exe' is not registred!");
53         }
54 }
55
56 NormalizeFilter::~NormalizeFilter(void)
57 {
58 }
59
60 AbstractFilter::FilterResult NormalizeFilter::apply(const QString &sourceFile, const QString &outputFile, AudioFileModel_TechInfo *const formatInfo, QAtomicInt &abortFlag)
61 {
62         QProcess process;
63         QStringList args;
64
65         args << "-V3" << "-S";
66         args << "--temp" << ".";
67         args << QDir::toNativeSeparators(sourceFile);
68         args << QDir::toNativeSeparators(outputFile);
69
70         if(!m_useDynAudNorm)
71         {
72                 args << "gain";
73                 args << (m_channelsCoupled ? "-n" : "-nb");
74                 args << QString().sprintf("%.2f", static_cast<double>(m_peakVolume) / 100.0);
75         }
76         else
77         {
78                 args << "dynaudnorm";
79                 args << "-p" << QString().sprintf("%.2f", qBound(0.1, dbToLinear(static_cast<double>(m_peakVolume) / 100.0), 1.0));
80                 args << "-g" << QString().sprintf("%d", m_filterLength);
81                 if(!m_channelsCoupled)
82                 {
83                         args << "-n";
84                 }
85         }
86
87         if(!startProcess(process, m_binary, args, QFileInfo(outputFile).canonicalPath()))
88         {
89                 return AbstractFilter::FILTER_FAILURE;
90         }
91
92         int prevProgress = -1;
93         QRegExp regExp("In:(\\d+)(\\.\\d+)*%");
94
95         const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &regExp](const QString &text)
96         {
97                 if (regExp.lastIndexIn(text) >= 0)
98                 {
99                         qint32 newProgress;
100                         if (MUtils::regexp_parse_int32(regExp, newProgress))
101                         {
102                                 if (newProgress > prevProgress)
103                                 {
104                                         emit statusUpdated(newProgress);
105                                         prevProgress = NEXT_PROGRESS(newProgress);
106                                 }
107                         }
108                         return true;
109                 }
110                 return false;
111         });
112
113         if (result != RESULT_SUCCESS)
114         {
115                 return AbstractFilter::FILTER_FAILURE;
116         }
117         
118         return AbstractFilter::FILTER_SUCCESS;
119 }