OSDN Git Service

Welcome to year 2013 ;-)
[lamexp/LameXP.git] / src / Filter_Resample.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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_Resample.h"
23
24 #include "Global.h"
25 #include "Model_AudioFile.h"
26
27 #include <QDir>
28 #include <QProcess>
29 #include <QRegExp>
30
31 static __inline int multipleOf(int value, int base)
32 {
33         return qRound(static_cast<double>(value) / static_cast<double>(base)) * base;
34 }
35
36 ResampleFilter::ResampleFilter(int samplingRate, int bitDepth)
37 :
38         m_binary(lamexp_lookup_tool("sox.exe"))
39 {
40         if(m_binary.isEmpty())
41         {
42                 throw "Error initializing SoX filter. Tool 'sox.exe' is not registred!";
43         }
44
45         m_samplingRate = (samplingRate > 0) ? qBound(8000, samplingRate, 192000) : 0;
46         m_bitDepth = (bitDepth > 0) ? qBound(8, multipleOf(bitDepth, 8), 32) : 0;
47
48         if((m_samplingRate == 0) && (m_bitDepth == 0))
49         {
50                 qWarning("ResampleFilter: Nothing to do, filter will be NOP!");
51         }
52 }
53
54 ResampleFilter::~ResampleFilter(void)
55 {
56 }
57
58 bool ResampleFilter::apply(const QString &sourceFile, const QString &outputFile, AudioFileModel *formatInfo, volatile bool *abortFlag)
59 {
60         QProcess process;
61         QStringList args;
62
63         if((m_samplingRate == formatInfo->formatAudioSamplerate()) && (m_bitDepth == formatInfo->formatAudioBitdepth()))
64         {
65                 messageLogged("Skipping resample filter!");
66                 qDebug("Resampling filter target samplerate/bitdepth is equals to the format of the input file, skipping!");
67                 return true;
68         }
69
70         process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
71
72         args << "-V3" << "-S";
73         args << "--guard" << "--temp" << ".";
74         args << QDir::toNativeSeparators(sourceFile);
75
76         if(m_bitDepth)
77         {
78                 args << "-b" << QString::number(m_bitDepth);
79         }
80
81         args << QDir::toNativeSeparators(outputFile);
82
83         if(m_samplingRate)
84         {
85                 args << "rate";
86                 args << ((m_bitDepth > 16) ? "-v" : "-h");                      //if resampling at/to > 16 bit depth (i.e. most commonly 24-bit), use VHQ (-v), otherwise, use HQ (-h)
87                 args << ((m_samplingRate > 40000) ? "-L" : "-I");       //if resampling to < 40k, use intermediate phase (-I), otherwise use linear phase (-L)
88                 args << QString::number(m_samplingRate);
89         }
90
91         if((m_bitDepth || m_samplingRate) && (m_bitDepth <= 16))
92         {
93                 args << "dither" << "-s";                                       //if you're mastering to 16-bit, you also need to add 'dither' (and in most cases noise-shaping) after the rate
94         }
95
96         if(!startProcess(process, m_binary, args))
97         {
98                 return false;
99         }
100
101         bool bTimeout = false;
102         bool bAborted = false;
103
104         QRegExp regExp("In:(\\d+)(\\.\\d+)*%");
105
106         while(process.state() != QProcess::NotRunning)
107         {
108                 if(*abortFlag)
109                 {
110                         process.kill();
111                         bAborted = true;
112                         emit messageLogged("\nABORTED BY USER !!!");
113                         break;
114                 }
115                 process.waitForReadyRead(m_processTimeoutInterval);
116                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
117                 {
118                         process.kill();
119                         qWarning("SoX process timed out <-- killing!");
120                         emit messageLogged("\nPROCESS TIMEOUT !!!");
121                         bTimeout = true;
122                         break;
123                 }
124                 while(process.bytesAvailable() > 0)
125                 {
126                         QByteArray line = process.readLine();
127                         QString text = QString::fromUtf8(line.constData()).simplified();
128                         if(regExp.lastIndexIn(text) >= 0)
129                         {
130                                 bool ok = false;
131                                 int progress = regExp.cap(1).toInt(&ok);
132                                 if(ok) emit statusUpdated(progress);
133                         }
134                         else if(!text.isEmpty())
135                         {
136                                 emit messageLogged(text);
137                         }
138                 }
139         }
140
141         process.waitForFinished();
142         if(process.state() != QProcess::NotRunning)
143         {
144                 process.kill();
145                 process.waitForFinished(-1);
146         }
147         
148         emit statusUpdated(100);
149         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
150
151         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0)
152         {
153                 return false;
154         }
155         
156         if(m_samplingRate) formatInfo->setFormatAudioSamplerate(m_samplingRate);
157         if(m_bitDepth) formatInfo->setFormatAudioBitdepth(m_bitDepth);
158
159         return true;
160 }