OSDN Git Service

Set creation/modified time of the encoded file the same value as the original file...
[lamexp/LameXP.git] / src / Filter_Normalize.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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 bool NormalizeFilter::apply(const QString &sourceFile, const QString &outputFile, AudioFileModel_TechInfo *formatInfo, volatile bool *abortFlag)
61 {
62         QProcess process;
63         QStringList args;
64
65         process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
66
67         args << "-V3" << "-S";
68         args << "--temp" << ".";
69         args << QDir::toNativeSeparators(sourceFile);
70         args << QDir::toNativeSeparators(outputFile);
71
72         if(!m_useDynAudNorm)
73         {
74                 args << "gain";
75                 args << (m_channelsCoupled ? "-n" : "-nb");
76                 args << QString().sprintf("%.2f", static_cast<double>(m_peakVolume) / 100.0);
77         }
78         else
79         {
80                 args << "dynaudnorm";
81                 args << "-p" << QString().sprintf("%.2f", qBound(0.1, dbToLinear(static_cast<double>(m_peakVolume) / 100.0), 1.0));
82                 args << "-g" << QString().sprintf("%d", m_filterLength);
83                 if(!m_channelsCoupled)
84                 {
85                         args << "-n";
86                 }
87         }
88
89         if(!startProcess(process, m_binary, args))
90         {
91                 return false;
92         }
93
94         bool bTimeout = false;
95         bool bAborted = false;
96
97         QRegExp regExp("In:(\\d+)(\\.\\d+)*%");
98
99         while(process.state() != QProcess::NotRunning)
100         {
101                 if(*abortFlag)
102                 {
103                         process.kill();
104                         bAborted = true;
105                         emit messageLogged("\nABORTED BY USER !!!");
106                         break;
107                 }
108                 process.waitForReadyRead(m_processTimeoutInterval);
109                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
110                 {
111                         process.kill();
112                         qWarning("SoX process timed out <-- killing!");
113                         emit messageLogged("\nPROCESS TIMEOUT !!!");
114                         bTimeout = true;
115                         break;
116                 }
117                 while(process.bytesAvailable() > 0)
118                 {
119                         QByteArray line = process.readLine();
120                         QString text = QString::fromUtf8(line.constData()).simplified();
121                         if(regExp.lastIndexIn(text) >= 0)
122                         {
123                                 bool ok = false;
124                                 int progress = regExp.cap(1).toInt(&ok);
125                                 if(ok) emit statusUpdated(progress);
126                         }
127                         else if(!text.isEmpty())
128                         {
129                                 emit messageLogged(text);
130                         }
131                 }
132         }
133
134         process.waitForFinished();
135         if(process.state() != QProcess::NotRunning)
136         {
137                 process.kill();
138                 process.waitForFinished(-1);
139         }
140         
141         emit statusUpdated(100);
142         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
143
144         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0)
145         {
146                 return false;
147         }
148         
149         return true;
150 }