Code refactoring.

This commit is contained in:
LoRd_MuldeR 2017-12-11 20:00:50 +01:00
parent d7006ccefa
commit fcdb087e20
33 changed files with 88 additions and 120 deletions

View File

@ -73,7 +73,7 @@ bool AACDecoder::decode(const QString &sourceFile, const QString &outputFile, QA
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -73,7 +73,7 @@ bool AC3Decoder::decode(const QString &sourceFile, const QString &outputFile, QA
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -75,7 +75,7 @@ bool ADPCMDecoder::decode(const QString &sourceFile, const QString &outputFile,
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -76,7 +76,7 @@ bool ALACDecoder::decode(const QString &sourceFile, const QString &outputFile, Q
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -74,7 +74,7 @@ bool AvisynthDecoder::decode(const QString &sourceFile, const QString &outputFil
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -74,7 +74,7 @@ bool FLACDecoder::decode(const QString &sourceFile, const QString &outputFile, Q
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -74,7 +74,7 @@ bool MACDecoder::decode(const QString &sourceFile, const QString &outputFile, QA
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -83,7 +83,7 @@ bool MP3Decoder::decode(const QString &sourceFile, const QString &outputFile, QA
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
}

View File

@ -75,7 +75,7 @@ bool MusepackDecoder::decode(const QString &sourceFile, const QString &outputFil
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -81,7 +81,7 @@ bool OpusDecoder::decode(const QString &sourceFile, const QString &outputFile, Q
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -75,7 +75,7 @@ bool TTADecoder::decode(const QString &sourceFile, const QString &outputFile, QA
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -73,7 +73,7 @@ bool VorbisDecoder::decode(const QString &sourceFile, const QString &outputFile,
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -75,7 +75,7 @@ bool WMADecoder::decode(const QString &sourceFile, const QString &outputFile, QA
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -74,7 +74,7 @@ bool WavPackDecoder::decode(const QString &sourceFile, const QString &outputFile
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -224,7 +224,7 @@ bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;
@ -238,7 +238,7 @@ bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -214,7 +214,7 @@ bool FDKAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaI
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -209,7 +209,7 @@ bool FHGAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaI
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -235,7 +235,7 @@ bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInf
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -202,7 +202,7 @@ bool AC3Encoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -168,7 +168,7 @@ bool DCAEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -177,7 +177,7 @@ bool FLACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInf
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -174,7 +174,7 @@ bool MACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -250,7 +250,7 @@ bool MP3Encoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -219,7 +219,7 @@ bool OpusEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInf
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -209,7 +209,7 @@ bool VorbisEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaI
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -148,7 +148,7 @@ bool WaveEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInf
}
else
{
emit messageLogged(checkFlag(abortFlag) ? L1S("Operation cancelled by user!") : L1S("Error: Failed to copy file!"));
emit messageLogged(CHECK_FLAG(abortFlag) ? L1S("Operation cancelled by user!") : L1S("Error: Failed to copy file!"));
}
return success;

View File

@ -118,7 +118,7 @@ AbstractFilter::FilterResult DownmixFilter::apply(const QString &sourceFile, con
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -102,7 +102,7 @@ AbstractFilter::FilterResult NormalizeFilter::apply(const QString &sourceFile, c
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -115,7 +115,7 @@ AbstractFilter::FilterResult ResampleFilter::apply(const QString &sourceFile, co
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -88,7 +88,7 @@ AbstractFilter::FilterResult ToneAdjustFilter::apply(const QString &sourceFile,
if (newProgress > prevProgress)
{
emit statusUpdated(newProgress);
prevProgress = (newProgress < 99) ? (newProgress + 1) : newProgress;
prevProgress = NEXT_PROGRESS(newProgress);
}
}
return true;

View File

@ -176,7 +176,7 @@ AbstractTool::result_t AbstractTool::awaitProcess(QProcess &process, QAtomicInt
while (process.state() != QProcess::NotRunning)
{
if (checkFlag(abortFlag))
if (CHECK_FLAG(abortFlag))
{
process.kill();
bAborted = true;
@ -199,11 +199,7 @@ AbstractTool::result_t AbstractTool::awaitProcess(QProcess &process, QAtomicInt
QByteArray line = process.readLine();
if (line.size() > 0)
{
static const char REPALCE_CHARS[3] = { '\r', '\b', '\t' };
for (size_t i = 0; i < MUTILS_ARR2LEN(REPALCE_CHARS); ++i)
{
line.replace(REPALCE_CHARS[i], char(0x20));
}
line.replace('\r', char(0x20)).replace('\b', char(0x20)).replace('\t', char(0x20));
const QString text = QString::fromUtf8(line.constData()).simplified();
if (!text.isEmpty())
{

View File

@ -59,11 +59,16 @@ protected:
}
result_t;
static __forceinline bool checkFlag(QAtomicInt &flag)
static __forceinline bool CHECK_FLAG(QAtomicInt &flag)
{
return MUTILS_BOOLIFY(flag);
}
static __forceinline int NEXT_PROGRESS(const int &progress)
{
return (progress < 99) ? qMax(0, progress + 1) : qMin(100, progress);
}
static QString commandline2string(const QString &program, const QStringList &arguments);
bool startProcess(QProcess &process, const QString &program, const QStringList &args, const QString &workingDir = QString());

View File

@ -59,101 +59,68 @@ bool WaveProperties::detect(const QString &sourceFile, AudioFileModel_TechInfo *
return false;
}
bool bTimeout = false;
bool bAborted = false;
int progress = 0;
QRegExp regExp_precision("Precision\\s*:\\s*(\\d+)-bit", Qt::CaseInsensitive);
QRegExp regExp_samplerate("Sample Rate\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
QRegExp regExp_duration("Duration\\s*:\\s*(\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d\\d)", Qt::CaseInsensitive);
QRegExp regExp_channels("Channels\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
QRegExp regExp_encoding("Sample Encoding\\s*:\\s*(\\d+)-bit\\s*Float", Qt::CaseInsensitive); //SoX returns a precision of 24-Bit for 32-Bit Float data, so we detect it this way!
QRegExp regExp_prc("Precision\\s*:\\s*(\\d+)-bit", Qt::CaseInsensitive);
QRegExp regExp_srt("Sample Rate\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
QRegExp regExp_drt("Duration\\s*:\\s*(\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d\\d)", Qt::CaseInsensitive);
QRegExp regExp_chl("Channels\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
QRegExp regExp_enc("Sample Encoding\\s*:\\s*(\\d+)-bit\\s*Float", Qt::CaseInsensitive); //SoX returns a precision of 24-Bit for 32-Bit Float data, so we detect it this way!
while(process.state() != QProcess::NotRunning)
const result_t result = awaitProcess(process, abortFlag, [this, &info, &progress, &regExp_prc, &regExp_srt, &regExp_drt, &regExp_chl, &regExp_enc](const QString &text)
{
if(checkFlag(abortFlag))
if (regExp_prc.lastIndexIn(text) >= 0)
{
process.kill();
bAborted = true;
emit messageLogged("\nABORTED BY USER !!!");
break;
quint32 tmp;
if (MUtils::regexp_parse_uint32(regExp_prc, tmp))
{
info->setAudioBitdepth(tmp);
emit statusUpdated(qMin(progress += 25, 100));
}
return true;
}
process.waitForReadyRead(m_processTimeoutInterval);
if(!process.bytesAvailable() && process.state() == QProcess::Running)
if (regExp_enc.lastIndexIn(text) >= 0)
{
process.kill();
qWarning("SoX process timed out <-- killing!");
emit messageLogged("\nPROCESS TIMEOUT !!!");
bTimeout = true;
break;
quint32 tmp;
if (MUtils::regexp_parse_uint32(regExp_enc, tmp))
{
info->setAudioBitdepth((tmp == 32) ? AudioFileModel::BITDEPTH_IEEE_FLOAT32 : tmp);
emit statusUpdated(qMin(progress += 25, 100));
}
return true;
}
while(process.bytesAvailable() > 0)
if (regExp_srt.lastIndexIn(text) >= 0)
{
QByteArray line = process.readLine();
QString text = QString::fromUtf8(line.constData()).simplified();
if(regExp_precision.lastIndexIn(text) >= 0)
quint32 tmp;
if (MUtils::regexp_parse_uint32(regExp_srt, tmp))
{
bool ok = false;
unsigned int tmp = regExp_precision.cap(1).toUInt(&ok);
if(ok) info->setAudioBitdepth(tmp);
info->setAudioSamplerate(tmp);
emit statusUpdated(qMin(progress += 25, 100));
}
if(regExp_encoding.lastIndexIn(text) >= 0)
{
bool ok = false;
unsigned int tmp = regExp_encoding.cap(1).toUInt(&ok);
if(ok) info->setAudioBitdepth((tmp == 32) ? AudioFileModel::BITDEPTH_IEEE_FLOAT32 : tmp);
emit statusUpdated(qMin(progress += 25, 100));
}
if(regExp_samplerate.lastIndexIn(text) >= 0)
{
bool ok = false;
unsigned int tmp = regExp_samplerate.cap(1).toUInt(&ok);
if(ok) info->setAudioSamplerate(tmp);
emit statusUpdated(qMin(progress += 25, 100));
}
if(regExp_duration.lastIndexIn(text) >= 0)
{
bool ok[4] = {false, false, false, false};
unsigned int tmp1 = regExp_duration.cap(1).toUInt(&ok[0]);
unsigned int tmp2 = regExp_duration.cap(2).toUInt(&ok[1]);
unsigned int tmp3 = regExp_duration.cap(3).toUInt(&ok[2]);
unsigned int tmp4 = regExp_duration.cap(4).toUInt(&ok[3]);
if(ok[0] && ok[1] && ok[2] && ok[3])
{
info->setDuration((tmp1 * 3600) + (tmp2 * 60) + tmp3 + qRound(static_cast<double>(tmp4) / 100.0));
}
emit statusUpdated(qMin(progress += 25, 100));
}
if(regExp_channels.lastIndexIn(text) >= 0)
{
bool ok = false;
unsigned int tmp = regExp_channels.cap(1).toUInt(&ok);
if(ok) info->setAudioChannels(tmp);
emit statusUpdated(qMin(progress += 25, 100));
}
if(!text.isEmpty())
{
emit messageLogged(text);
}
return true;
}
if (regExp_drt.lastIndexIn(text) >= 0)
{
quint32 tmp[4];
if (MUtils::regexp_parse_uint32(regExp_drt, tmp, 4))
{
info->setDuration((tmp[0] * 3600) + (tmp[1] * 60) + tmp[2] + qRound(static_cast<double>(tmp[3]) / 100.0));
emit statusUpdated(qMin(progress += 25, 100));
}
return true;
}
if (regExp_chl.lastIndexIn(text) >= 0)
{
quint32 tmp;
if (MUtils::regexp_parse_uint32(regExp_chl, tmp))
{
info->setAudioChannels(tmp);
emit statusUpdated(qMin(progress += 25, 100));
}
return true;
}
}
process.waitForFinished();
if(process.state() != QProcess::NotRunning)
{
process.kill();
process.waitForFinished(-1);
}
emit statusUpdated(100);
emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
{
return false;
}
});
return true;
return (result == RESULT_SUCCESS);
}