OSDN Git Service

Implement filter framework + currently only the "downmix" filter is implemented,...
[lamexp/LameXP.git] / src / Filter_Downmix.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 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_Downmix.h"
23
24 #include "Global.h"
25
26 #include <QDir>
27 #include <QProcess>
28 #include <QRegExp>
29
30 DownmixFilter::DownmixFilter(void)
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
40 DownmixFilter::~DownmixFilter(void)
41 {
42 }
43
44 bool DownmixFilter::apply(const QString &sourceFile, const QString &outputFile, volatile bool *abortFlag)
45 {
46         QProcess process;
47         QStringList args;
48
49         args << "-V3";
50         args << QDir::toNativeSeparators(sourceFile);
51         args << "-c2";
52         args << QDir::toNativeSeparators(outputFile);
53
54         if(!startProcess(process, m_binary, args))
55         {
56                 return false;
57         }
58
59         bool bTimeout = false;
60         bool bAborted = false;
61
62         while(process.state() != QProcess::NotRunning)
63         {
64                 if(*abortFlag)
65                 {
66                         process.kill();
67                         bAborted = true;
68                         emit messageLogged("\nABORTED BY USER !!!");
69                         break;
70                 }
71                 process.waitForReadyRead();
72                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
73                 {
74                         process.kill();
75                         qWarning("SoX process timed out <-- killing!");
76                         bTimeout = true;
77                         break;
78                 }
79                 while(process.bytesAvailable() > 0)
80                 {
81                         QByteArray line = process.readLine();
82                         QString text = QString::fromUtf8(line.constData()).simplified();
83                         if(!text.isEmpty())
84                         {
85                                 emit messageLogged(text);
86                         }
87                 }
88         }
89
90         process.waitForFinished();
91         if(process.state() != QProcess::NotRunning)
92         {
93                 process.kill();
94                 process.waitForFinished(-1);
95         }
96         
97         emit statusUpdated(100);
98         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
99
100         if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit || QFileInfo(outputFile).size() == 0)
101         {
102                 return false;
103         }
104         
105         return true;
106 }