OSDN Git Service

Updated Ukrainian translation file.
[lamexp/LameXP.git] / src / Filter_Normalize.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Filter_Normalize.h"
23
24 #include "Global.h"
25
26 #include <QDir>
27 #include <QProcess>
28 #include <QRegExp>
29
30 NormalizeFilter::NormalizeFilter(int peakVolume, int equalizationMode)
31 :
32         m_binary(lamexp_lookup_tool("sox.exe"))
33 {
34         if(m_binary.isEmpty())
35         {
36                 throw "Error initializing SoX filter. Tool 'sox.exe' is not registred!";
37         }
38
39         m_peakVolume = qMin(-50, qMax(-3200, peakVolume));
40         m_equalizationMode = qMin(2, qMax(0, equalizationMode));
41 }
42
43 NormalizeFilter::~NormalizeFilter(void)
44 {
45 }
46
47 bool NormalizeFilter::apply(const QString &sourceFile, const QString &outputFile, AudioFileModel *formatInfo, volatile bool *abortFlag)
48 {
49         QProcess process;
50         QStringList args;
51         QString eqMode = (m_equalizationMode == 0) ? "-n" : ((m_equalizationMode == 1) ? "-ne" : "-nb");
52
53         process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
54
55         args << "-V3" << "-S";
56         args << "--temp" << ".";
57         args << QDir::toNativeSeparators(sourceFile);
58         args << QDir::toNativeSeparators(outputFile);
59         args << "gain";
60         args << eqMode << QString().sprintf("%.2f", static_cast<double>(m_peakVolume) / 100.0);
61
62         if(!startProcess(process, m_binary, args))
63         {
64                 return false;
65         }
66
67         bool bTimeout = false;
68         bool bAborted = false;
69
70         QRegExp regExp("In:(\\d+)(\\.\\d+)*%");
71
72         while(process.state() != QProcess::NotRunning)
73         {
74                 if(*abortFlag)
75                 {
76                         process.kill();
77                         bAborted = true;
78                         emit messageLogged("\nABORTED BY USER !!!");
79                         break;
80                 }
81                 process.waitForReadyRead(m_processTimeoutInterval);
82                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
83                 {
84                         process.kill();
85                         qWarning("SoX process timed out <-- killing!");
86                         emit messageLogged("\nPROCESS TIMEOUT !!!");
87                         bTimeout = true;
88                         break;
89                 }
90                 while(process.bytesAvailable() > 0)
91                 {
92                         QByteArray line = process.readLine();
93                         QString text = QString::fromUtf8(line.constData()).simplified();
94                         if(regExp.lastIndexIn(text) >= 0)
95                         {
96                                 bool ok = false;
97                                 int progress = regExp.cap(1).toInt(&ok);
98                                 if(ok) emit statusUpdated(progress);
99                         }
100                         else if(!text.isEmpty())
101                         {
102                                 emit messageLogged(text);
103                         }
104                 }
105         }
106
107         process.waitForFinished();
108         if(process.state() != QProcess::NotRunning)
109         {
110                 process.kill();
111                 process.waitForFinished(-1);
112         }
113         
114         emit statusUpdated(100);
115         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
116
117         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0)
118         {
119                 return false;
120         }
121         
122         return true;
123 }