OSDN Git Service

Updated Monkey's Audio binary to v4.10 (2011-04-16).
[lamexp/LameXP.git] / src / Filter_ToneAdjust.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_ToneAdjust.h"
23
24 #include "Global.h"
25
26 #include <QDir>
27 #include <QProcess>
28 #include <QRegExp>
29 #include <QFileInfo>
30
31 ToneAdjustFilter::ToneAdjustFilter(int bass, int treble)
32 :
33         m_binary(lamexp_lookup_tool("sox.exe"))
34 {
35         if(m_binary.isEmpty())
36         {
37                 throw "Error initializing SoX filter. Tool 'sox.exe' is not registred!";
38         }
39
40         m_bass = qMax(-2000, qMin(2000, bass));
41         m_treble = qMax(-2000, qMin(2000, treble));
42 }
43
44 ToneAdjustFilter::~ToneAdjustFilter(void)
45 {
46 }
47
48 bool ToneAdjustFilter::apply(const QString &sourceFile, const QString &outputFile, AudioFileModel *formatInfo, volatile bool *abortFlag)
49 {
50         QProcess process;
51         QStringList args;
52
53         process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
54
55         args << "-V3" << "-S";
56         args << "--guard" << "--temp" << ".";
57         args << QDir::toNativeSeparators(sourceFile);
58         args << QDir::toNativeSeparators(outputFile);
59
60         if(m_bass != 0)
61         {
62                 args << "bass" << QString().sprintf("%s%.2f", ((m_bass < 0) ? "-" : "+"), static_cast<double>(abs(m_bass)) / 100.0);
63         }
64         if(m_treble != 0)
65         {
66                 args << "treble" << QString().sprintf("%s%.2f", ((m_treble < 0) ? "-" : "+"), static_cast<double>(abs(m_treble)) / 100.0);
67         }
68
69         if(!startProcess(process, m_binary, args))
70         {
71                 return false;
72         }
73
74         bool bTimeout = false;
75         bool bAborted = false;
76
77         QRegExp regExp("In:(\\d+)(\\.\\d+)*%");
78
79         while(process.state() != QProcess::NotRunning)
80         {
81                 if(*abortFlag)
82                 {
83                         process.kill();
84                         bAborted = true;
85                         emit messageLogged("\nABORTED BY USER !!!");
86                         break;
87                 }
88                 process.waitForReadyRead(m_processTimeoutInterval);
89                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
90                 {
91                         process.kill();
92                         qWarning("SoX process timed out <-- killing!");
93                         emit messageLogged("\nPROCESS TIMEOUT !!!");
94                         bTimeout = true;
95                         break;
96                 }
97                 while(process.bytesAvailable() > 0)
98                 {
99                         QByteArray line = process.readLine();
100                         QString text = QString::fromUtf8(line.constData()).simplified();
101                         if(regExp.lastIndexIn(text) >= 0)
102                         {
103                                 bool ok = false;
104                                 int progress = regExp.cap(1).toInt(&ok);
105                                 if(ok) emit statusUpdated(progress);
106                         }
107                         else if(!text.isEmpty())
108                         {
109                                 emit messageLogged(text);
110                         }
111                 }
112         }
113
114         process.waitForFinished();
115         if(process.state() != QProcess::NotRunning)
116         {
117                 process.kill();
118                 process.waitForFinished(-1);
119         }
120         
121         emit statusUpdated(100);
122         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
123
124         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0)
125         {
126                 return false;
127         }
128         
129         return true;
130 }