OSDN Git Service

Added new function to detect the current date. Slightly more robust against manipulat...
[lamexp/LameXP.git] / src / Encoder_AAC.cpp
index 7ad1f96..35f8573 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 #include <QProcess>
 #include <QDir>
 
-#define max(a,b) (((a) > (b)) ? (a) : (b))
-#define min(a,b) (((a) < (b)) ? (a) : (b))
-#define IS_UNICODE(STR) (qstricmp(STR.toUtf8().constData(), QString::fromLocal8Bit(STR.toLocal8Bit()).toUtf8().constData()))
-
 AACEncoder::AACEncoder(void)
 :
        m_binary_enc(lamexp_lookup_tool("neroAacEnc.exe")),
-       m_binary_tag(lamexp_lookup_tool("neroAacTag.exe"))
+       m_binary_tag(lamexp_lookup_tool("neroAacTag.exe")),
+       m_binary_sox(lamexp_lookup_tool("sox.exe"))
 {
-       if(m_binary_enc.isEmpty() || m_binary_tag.isEmpty())
+       if(m_binary_enc.isEmpty() || m_binary_tag.isEmpty() || m_binary_sox.isEmpty())
        {
                throw "Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!";
        }
@@ -51,6 +48,8 @@ AACEncoder::~AACEncoder(void)
 
 bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
 {
+       const unsigned int fileDuration = metaInfo.fileDuration();
+       
        QProcess process;
        QStringList args;
        const QString baseName = QFileInfo(outputFile).fileName();
@@ -58,13 +57,13 @@ bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInf
        switch(m_configRCMode)
        {
        case SettingsModel::VBRMode:
-               args << "-q" << QString().sprintf("%.2f", min(1.0, max(0.0, static_cast<double>(m_configBitrate * 5) / 100.0)));
+               args << "-q" << QString().sprintf("%.2f", qBound(0.0, static_cast<double>(m_configBitrate * 5) / 100.0, 1.0));
                break;
        case SettingsModel::ABRMode:
-               args << "-br" << QString::number(max(32, min(500, (m_configBitrate * 8))) * 1000);
+               args << "-br" << QString::number(qMax(32, qMin(500, (m_configBitrate * 8))) * 1000);
                break;
        case SettingsModel::CBRMode:
-               args << "-cbr" << QString::number(max(32, min(500, (m_configBitrate * 8))) * 1000) << "-2pass";
+               args << "-cbr" << QString::number(qMax(32, qMin(500, (m_configBitrate * 8))) * 1000);
                break;
        default:
                throw "Bad rate-control mode!";
@@ -101,6 +100,8 @@ bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInf
 
        bool bTimeout = false;
        bool bAborted = false;
+       int prevProgress = -1;
+
 
        QRegExp regExp("Processed\\s+(\\d+)\\s+seconds");
        QRegExp regExp_pass1("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
@@ -115,11 +116,12 @@ bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInf
                        emit messageLogged("\nABORTED BY USER !!!");
                        break;
                }
-               process.waitForReadyRead();
+               process.waitForReadyRead(m_processTimeoutInterval);
                if(!process.bytesAvailable() && process.state() == QProcess::Running)
                {
                        process.kill();
                        qWarning("NeroAacEnc process timed out <-- killing!");
+                       emit messageLogged("\nPROCESS TIMEOUT !!!");
                        bTimeout = true;
                        break;
                }
@@ -131,27 +133,42 @@ bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInf
                        {
                                bool ok = false;
                                int progress = regExp_pass1.cap(1).toInt(&ok);
-                               if(ok && metaInfo.fileDuration() > 0)
+                               if(ok && (fileDuration > 0))
                                {
-                                       emit statusUpdated(static_cast<int>((static_cast<double>(progress) / static_cast<double>(metaInfo.fileDuration())) * 50.0));
+                                       int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(fileDuration)) * 50.0);
+                                       if(newProgress > prevProgress)
+                                       {
+                                               emit statusUpdated(newProgress);
+                                               prevProgress = qMin(newProgress + 2, 99);
+                                       }
                                }
                        }
                        else if(regExp_pass2.lastIndexIn(text) >= 0)
                        {
                                bool ok = false;
                                int progress = regExp_pass2.cap(1).toInt(&ok);
-                               if(ok && metaInfo.fileDuration() > 0)
+                               if(ok && (fileDuration > 0))
                                {
-                                       emit statusUpdated(static_cast<int>((static_cast<double>(progress) / static_cast<double>(metaInfo.fileDuration())) * 50.0) + 50);
+                                       int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(fileDuration)) * 50.0) + 50;
+                                       if(newProgress > prevProgress)
+                                       {
+                                               emit statusUpdated(newProgress);
+                                               prevProgress = qMin(newProgress + 2, 99);
+                                       }
                                }
                        }
                        else if(regExp.lastIndexIn(text) >= 0)
                        {
                                bool ok = false;
                                int progress = regExp.cap(1).toInt(&ok);
-                               if(ok && metaInfo.fileDuration() > 0)
+                               if(ok && (fileDuration > 0))
                                {
-                                       emit statusUpdated(static_cast<int>((static_cast<double>(progress) / static_cast<double>(metaInfo.fileDuration())) * 100.0));
+                                       int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(fileDuration)) * 100.0);
+                                       if(newProgress > prevProgress)
+                                       {
+                                               emit statusUpdated(newProgress);
+                                               prevProgress = qMin(newProgress + 2, 99);
+                                       }
                                }
                        }
                        else if(!text.isEmpty())
@@ -171,7 +188,7 @@ bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInf
        emit statusUpdated(100);
        emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
 
-       if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
+       if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
        {
                return false;
        }
@@ -181,13 +198,14 @@ bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInf
        args.clear();
        args << QDir::toNativeSeparators(outputFile);
 
-       if(!metaInfo.fileName().isEmpty()) args << QString("-meta:title=%1").arg(metaInfo.fileName());
-       if(!metaInfo.fileArtist().isEmpty()) args << QString("-meta:artist=%1").arg(metaInfo.fileArtist());
-       if(!metaInfo.fileAlbum().isEmpty()) args << QString("-meta:album=%1").arg(metaInfo.fileAlbum());
-       if(!metaInfo.fileGenre().isEmpty()) args << QString("-meta:genre=%1").arg(metaInfo.fileGenre());
-       if(!metaInfo.fileComment().isEmpty()) args << QString("-meta:comment=%1").arg(metaInfo.fileComment());
+       if(!metaInfo.fileName().isEmpty()) args << QString("-meta:title=%1").arg(cleanTag(metaInfo.fileName()));
+       if(!metaInfo.fileArtist().isEmpty()) args << QString("-meta:artist=%1").arg(cleanTag(metaInfo.fileArtist()));
+       if(!metaInfo.fileAlbum().isEmpty()) args << QString("-meta:album=%1").arg(cleanTag(metaInfo.fileAlbum()));
+       if(!metaInfo.fileGenre().isEmpty()) args << QString("-meta:genre=%1").arg(cleanTag(metaInfo.fileGenre()));
+       if(!metaInfo.fileComment().isEmpty()) args << QString("-meta:comment=%1").arg(cleanTag(metaInfo.fileComment()));
        if(metaInfo.fileYear()) args << QString("-meta:year=%1").arg(QString::number(metaInfo.fileYear()));
        if(metaInfo.filePosition()) args << QString("-meta:track=%1").arg(QString::number(metaInfo.filePosition()));
+       if(!metaInfo.fileCover().isEmpty()) args << QString("-add-cover:%1:%2").arg("front", metaInfo.fileCover());
        
        if(!startProcess(process, m_binary_tag, args))
        {
@@ -205,11 +223,12 @@ bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInf
                        emit messageLogged("\nABORTED BY USER !!!");
                        break;
                }
-               process.waitForReadyRead();
+               process.waitForReadyRead(m_processTimeoutInterval);
                if(!process.bytesAvailable() && process.state() == QProcess::Running)
                {
                        process.kill();
                        qWarning("NeroAacTag process timed out <-- killing!");
+                       emit messageLogged("\nPROCESS TIMEOUT !!!");
                        bTimeout = true;
                        break;
                }
@@ -233,7 +252,7 @@ bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInf
                
        emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
 
-       if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
+       if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
        {
                return false;
        }
@@ -259,7 +278,6 @@ bool AACEncoder::isFormatSupported(const QString &containerType, const QString &
        return false;
 }
 
-
 void AACEncoder::setProfile(int profile)
 {
        m_configProfile = profile;
@@ -269,3 +287,8 @@ void AACEncoder::setEnable2Pass(bool enabled)
 {
        m_configEnable2Pass = enabled;
 }
+
+const bool AACEncoder::needsTimingInfo(void)
+{
+       return true;
+}