OSDN Git Service

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