Added missing image.
[LameXP.git] / src / Filter_Downmix.cpp
blob5894d4b162f0712865e304fa079520d6e0a58431
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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.
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.
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.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Filter_Downmix.h"
25 //Internal
26 #include "Global.h"
27 #include "Tool_WaveProperties.h"
28 #include "Model_AudioFile.h"
30 //MUtils
31 #include <MUtils/Exception.h>
33 //Qt
34 #include <QDir>
35 #include <QProcess>
36 #include <QRegExp>
38 #define IS_VALID(X) (((X) != 0U) && ((X) != UINT_MAX))
40 DownmixFilter::DownmixFilter(void)
42 m_binary(lamexp_tools_lookup("sox.exe"))
44 if(m_binary.isEmpty())
46 MUTILS_THROW("Error initializing SoX filter. Tool 'sox.exe' is not registred!");
50 DownmixFilter::~DownmixFilter(void)
54 AbstractFilter::FilterResult DownmixFilter::apply(const QString &sourceFile, const QString &outputFile, AudioFileModel_TechInfo *const formatInfo, volatile bool *abortFlag)
56 unsigned int channels = formatInfo->audioChannels();
57 emit messageLogged(QString().sprintf("--> Number of channels is: %d\n", channels));
59 if(IS_VALID(channels) && (channels <= 2))
61 messageLogged("Skipping downmix!");
62 qDebug("Dowmmix not required/possible for Mono or Stereo input, skipping!");
63 return AbstractFilter::FILTER_SKIPPED;
66 QProcess process;
67 QStringList args;
69 args << "-V3" << "-S";
70 args << "--guard" << "--temp" << ".";
71 args << QDir::toNativeSeparators(sourceFile);
72 args << QDir::toNativeSeparators(outputFile);
74 switch(channels)
76 case 3: //3.0 (L/R/C)
77 args << "remix" << "1v0.66,3v0.34" << "2v0.66,3v0.34";
78 break;
79 case 4: //3.1 (L/R/C/LFE)
80 args << "remix" << "1v0.5,3v0.25,4v0.25" << "2v0.5,3v0.25,4v0.25";
81 break;
82 case 5: //5.0 (L/R/C/BL/BR)
83 args << "remix" << "1v0.5,3v0.25,4v0.25" << "2v0.5,3v0.25,5v0.25";
84 break;
85 case 6: //5.1 (L/R/C/LFE/BL/BR)
86 args << "remix" << "1v0.4,3v0.2,4v0.2,5v0.2" << "2v0.4,3v0.2,4v0.2,6v0.2";
87 break;
88 case 7: //7.0 (L/R/C/BL/BR/SL/SR)
89 args << "remix" << "1v0.4,3v0.2,4v0.2,6v0.2" << "2v0.4,3v0.2,5v0.2,7v0.2";
90 break;
91 case 8: //7.1 (L/R/C/LFE/BL/BR/SL/SR)
92 args << "remix" << "1v0.36,3v0.16,4v0.16,5v0.16,7v0.16" << "2v0.36,3v0.16,4v0.16,6v0.16,8v0.16";
93 break;
94 case 9: //8.1 (L/R/C/LFE/BL/BR/SL/SR/BC)
95 args << "remix" << "1v0.308,3v0.154,4v0.154,5v0.154,7v0.154,9v0.076" << "2v0.308,3v0.154,4v0.154,6v0.154,8v0.154,9v0.076";
96 break;
97 default: //Unknown
98 qWarning("Downmixer: Unknown channel configuration!");
99 args << "channels" << QString::number(2);
100 break;
103 if(!startProcess(process, m_binary, args, QFileInfo(outputFile).canonicalPath()))
105 return AbstractFilter::FILTER_FAILURE;
108 bool bTimeout = false;
109 bool bAborted = false;
111 QRegExp regExp("In:(\\d+)(\\.\\d+)*%");
113 while(process.state() != QProcess::NotRunning)
115 if(*abortFlag)
117 process.kill();
118 bAborted = true;
119 emit messageLogged("\nABORTED BY USER !!!");
120 break;
122 process.waitForReadyRead(m_processTimeoutInterval);
123 if(!process.bytesAvailable() && process.state() == QProcess::Running)
125 process.kill();
126 qWarning("SoX process timed out <-- killing!");
127 emit messageLogged("\nPROCESS TIMEOUT !!!");
128 bTimeout = true;
129 break;
131 while(process.bytesAvailable() > 0)
133 QByteArray line = process.readLine();
134 QString text = QString::fromUtf8(line.constData()).simplified();
135 if(regExp.lastIndexIn(text) >= 0)
137 bool ok = false;
138 int progress = regExp.cap(1).toInt(&ok);
139 if(ok) emit statusUpdated(progress);
141 else if(!text.isEmpty())
143 emit messageLogged(text);
148 process.waitForFinished();
149 if(process.state() != QProcess::NotRunning)
151 process.kill();
152 process.waitForFinished(-1);
155 emit statusUpdated(100);
156 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
158 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0)
160 return AbstractFilter::FILTER_FAILURE;
163 formatInfo->setAudioChannels(2);
164 return AbstractFilter::FILTER_SUCCESS;