Updated Opus encoder/decoder libraries to v1.3-RC-1 (2018-06-01) and Opus-Tools to...
[LameXP.git] / src / Filter_Resample.cpp
blob590e2d130081c1b587cb3bf28e72a5a04ae43dfb
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2018 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_Resample.h"
25 //Internal
26 #include "Global.h"
27 #include "Model_AudioFile.h"
29 //MUtils
30 #include <MUtils/Exception.h>
32 //Qt
33 #include <QDir>
34 #include <QProcess>
35 #include <QRegExp>
37 static __inline int multipleOf(int value, int base)
39 return qRound(static_cast<double>(value) / static_cast<double>(base)) * base;
42 ResampleFilter::ResampleFilter(int samplingRate, int bitDepth)
44 m_binary(lamexp_tools_lookup("sox.exe"))
46 if(m_binary.isEmpty())
48 MUTILS_THROW("Error initializing SoX filter. Tool 'sox.exe' is not registred!");
51 m_samplingRate = (samplingRate > 0) ? qBound(8000, samplingRate, 192000) : 0;
52 m_bitDepth = (bitDepth > 0) ? qBound(8, multipleOf(bitDepth, 8), 32) : 0;
54 if((m_samplingRate == 0) && (m_bitDepth == 0))
56 qWarning("ResampleFilter: Nothing to do, filter will be NOP!");
60 ResampleFilter::~ResampleFilter(void)
64 AbstractFilter::FilterResult ResampleFilter::apply(const QString &sourceFile, const QString &outputFile, AudioFileModel_TechInfo *const formatInfo, QAtomicInt &abortFlag)
66 QProcess process;
67 QStringList args;
69 if((m_samplingRate == formatInfo->audioSamplerate()) && (m_bitDepth == formatInfo->audioBitdepth()))
71 messageLogged("Skipping resample filter!");
72 qDebug("Resampling filter target samplerate/bitdepth is equals to the format of the input file, skipping!");
73 return AbstractFilter::FILTER_SKIPPED;
76 args << "-V3" << "-S";
77 args << "--guard" << "--temp" << ".";
78 args << QDir::toNativeSeparators(sourceFile);
80 if(m_bitDepth)
82 args << "-b" << QString::number(m_bitDepth);
85 args << QDir::toNativeSeparators(outputFile);
87 if(m_samplingRate)
89 args << "rate";
90 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)
91 args << ((m_samplingRate > 40000) ? "-L" : "-I"); //if resampling to < 40k, use intermediate phase (-I), otherwise use linear phase (-L)
92 args << QString::number(m_samplingRate);
95 if((m_bitDepth || m_samplingRate) && (m_bitDepth <= 16))
97 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 if(!startProcess(process, m_binary, args, QFileInfo(outputFile).canonicalPath()))
102 return AbstractFilter::FILTER_FAILURE;
105 int prevProgress = -1;
106 QRegExp regExp("In:(\\d+)(\\.\\d+)*%");
108 const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &regExp](const QString &text)
110 if (regExp.lastIndexIn(text) >= 0)
112 qint32 newProgress;
113 if (MUtils::regexp_parse_int32(regExp, newProgress))
115 if (newProgress > prevProgress)
117 emit statusUpdated(newProgress);
118 prevProgress = NEXT_PROGRESS(newProgress);
121 return true;
123 return false;
126 if (result != RESULT_SUCCESS)
128 return AbstractFilter::FILTER_FAILURE;
131 if (m_samplingRate)
133 formatInfo->setAudioSamplerate(m_samplingRate);
135 if (m_bitDepth)
137 formatInfo->setAudioBitdepth(m_bitDepth);
140 return AbstractFilter::FILTER_SUCCESS;