Fixed a type (reported by VzK)
[LameXP.git] / src / Filter_Normalize.cpp
blob9b1b5474bdd422c2bffa13f665e87d6b59a9d7b2
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Filter_Normalize.h"
24 #include "Global.h"
26 #include <QDir>
27 #include <QProcess>
28 #include <QRegExp>
30 NormalizeFilter::NormalizeFilter(int peakVolume, int equalizationMode)
32 m_binary(lamexp_lookup_tool("sox.exe"))
34 if(m_binary.isEmpty())
36 throw "Error initializing SoX filter. Tool 'sox.exe' is not registred!";
39 m_peakVolume = qMin(-50, qMax(-3200, peakVolume));
40 m_equalizationMode = qMin(2, qMax(0, equalizationMode));
43 NormalizeFilter::~NormalizeFilter(void)
47 bool NormalizeFilter::apply(const QString &sourceFile, const QString &outputFile, volatile bool *abortFlag)
49 QProcess process;
50 QStringList args;
51 QString eqMode = (m_equalizationMode == 0) ? "-ne" : ((m_equalizationMode == 1) ? "-nb" : "-n");
53 process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
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);
62 if(!startProcess(process, m_binary, args))
64 return false;
67 bool bTimeout = false;
68 bool bAborted = false;
70 QRegExp regExp("In:(\\d+)(\\.\\d+)*%");
72 while(process.state() != QProcess::NotRunning)
74 if(*abortFlag)
76 process.kill();
77 bAborted = true;
78 emit messageLogged("\nABORTED BY USER !!!");
79 break;
81 process.waitForReadyRead(m_processTimeoutInterval);
82 if(!process.bytesAvailable() && process.state() == QProcess::Running)
84 process.kill();
85 qWarning("SoX process timed out <-- killing!");
86 emit messageLogged("\nPROCESS TIMEOUT !!!");
87 bTimeout = true;
88 break;
90 while(process.bytesAvailable() > 0)
92 QByteArray line = process.readLine();
93 QString text = QString::fromUtf8(line.constData()).simplified();
94 if(regExp.lastIndexIn(text) >= 0)
96 bool ok = false;
97 int progress = regExp.cap(1).toInt(&ok);
98 if(ok) emit statusUpdated(progress);
100 else if(!text.isEmpty())
102 emit messageLogged(text);
107 process.waitForFinished();
108 if(process.state() != QProcess::NotRunning)
110 process.kill();
111 process.waitForFinished(-1);
114 emit statusUpdated(100);
115 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
117 if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit || QFileInfo(outputFile).size() == 0)
119 return false;
122 return true;