#include "Global.h"
#include "LockedFile.h"
#include "Model_AudioFile.h"
-#include "PlaylistImporter.h"
+#include "Thread_FileAnalyzer_Task.h"
#include <QDir>
#include <QFileInfo>
#include <QTime>
#include <QDebug>
#include <QImage>
-
-#include <math.h>
-
-#define IS_KEY(KEY) (key.compare(KEY, Qt::CaseInsensitive) == 0)
-#define IS_SEC(SEC) (key.startsWith((SEC "_"), Qt::CaseInsensitive))
-#define FIRST_TOK(STR) (STR.split(" ", QString::SkipEmptyParts).first())
+#include <QThreadPool>
+#include <QTime>
////////////////////////////////////////////////////////////
// Constructor
FileAnalyzer::FileAnalyzer(const QStringList &inputFiles)
:
+ m_abortFlag(false),
m_inputFiles(inputFiles),
- m_mediaInfoBin(lamexp_lookup_tool("mediainfo.exe")),
- m_avs2wavBin(lamexp_lookup_tool("avs2wav.exe")),
- m_templateFile(NULL),
- m_abortFlag(false)
+ m_templateFile(NULL)
{
m_bSuccess = false;
m_bAborted = false;
-
- if(m_mediaInfoBin.isEmpty())
- {
- qFatal("Invalid path to MediaInfo binary. Tool not initialized properly.");
- }
-
- m_filesAccepted = 0;
- m_filesRejected = 0;
- m_filesDenied = 0;
- m_filesDummyCDDA = 0;
- m_filesCueSheet = 0;
}
FileAnalyzer::~FileAnalyzer(void)
LAMEXP_DELETE(m_templateFile);
if(QFile::exists(templatePath)) QFile::remove(templatePath);
}
+
+ AnalyzeTask::reset();
}
////////////////////////////////////////////////////////////
const char *FileAnalyzer::g_tags_aud[] =
{
"ID",
+ "Source",
"Format",
"Format_Profile",
"Format_Version",
"BitDepth",
"BitRate",
"BitRate_Mode",
+ "Encoded_Library",
NULL
};
void FileAnalyzer::run()
{
- m_bSuccess = false;
+ m_abortFlag = false;
+
m_bAborted = false;
+ m_bSuccess = false;
+
+ int nFiles = m_inputFiles.count();
- m_filesAccepted = 0;
- m_filesRejected = 0;
- m_filesDenied = 0;
- m_filesDummyCDDA = 0;
- m_filesCueSheet = 0;
+ emit progressMaxChanged(nFiles);
+ emit progressValChanged(0);
m_inputFiles.sort();
- m_recentlyAdded.clear();
- m_abortFlag = false;
if(!m_templateFile)
{
}
}
- while(!m_inputFiles.isEmpty())
- {
- int fileType = fileTypeNormal;
- QString currentFile = QDir::fromNativeSeparators(m_inputFiles.takeFirst());
- qDebug("Analyzing: %s", currentFile.toUtf8().constData());
- emit fileSelected(QFileInfo(currentFile).fileName());
- AudioFileModel file = analyzeFile(currentFile, &fileType);
-
- if(m_abortFlag)
- {
- MessageBeep(MB_ICONERROR);
- m_bAborted = true;
- qWarning("Operation cancelled by user!");
- return;
- }
- if(fileType == fileTypeSkip)
- {
- qWarning("File was recently added, skipping!");
- continue;
- }
- if(fileType == fileTypeDenied)
- {
- m_filesDenied++;
- qWarning("Cannot access file for reading, skipping!");
- continue;
- }
- if(fileType == fileTypeCDDA)
- {
- m_filesDummyCDDA++;
- qWarning("Dummy CDDA file detected, skipping!");
- continue;
- }
-
- if(file.fileName().isEmpty() || file.formatContainerType().isEmpty() || file.formatAudioType().isEmpty())
- {
- if(PlaylistImporter::importPlaylist(m_inputFiles, currentFile))
- {
- qDebug("Imported playlist file.");
- }
- else if(!QFileInfo(currentFile).suffix().compare("cue", Qt::CaseInsensitive))
- {
- qWarning("Cue Sheet file detected, skipping!");
- m_filesCueSheet++;
- }
- else if(!QFileInfo(currentFile).suffix().compare("avs", Qt::CaseInsensitive))
- {
- qDebug("Found a potential Avisynth script, investigating...");
- if(analyzeAvisynthFile(currentFile, file))
- {
- m_filesAccepted++;
- emit fileAnalyzed(file);
- }
- else
- {
- qDebug("Rejected Avisynth file: %s", file.filePath().toUtf8().constData());
- m_filesRejected++;
- }
- }
- else
- {
- qDebug("Rejected file of unknown type: %s", file.filePath().toUtf8().constData());
- m_filesRejected++;
- }
- continue;
- }
-
- m_filesAccepted++;
- m_recentlyAdded.append(file.filePath());
- emit fileAnalyzed(file);
- }
-
- qDebug("All files added.\n");
- m_bSuccess = true;
-}
-
-////////////////////////////////////////////////////////////
-// Privtae Functions
-////////////////////////////////////////////////////////////
-
-const AudioFileModel FileAnalyzer::analyzeFile(const QString &filePath, int *type)
-{
- *type = fileTypeNormal;
-
- AudioFileModel audioFile(filePath);
-
- if(m_recentlyAdded.contains(filePath, Qt::CaseInsensitive))
- {
- *type = fileTypeSkip;
- return audioFile;
- }
-
- QFile readTest(filePath);
- if(!readTest.open(QIODevice::ReadOnly))
- {
- *type = fileTypeDenied;
- return audioFile;
- }
- if(checkFile_CDDA(readTest))
- {
- *type = fileTypeCDDA;
- return audioFile;
- }
- readTest.close();
-
- bool skipNext = false;
- unsigned int id_val[2] = {UINT_MAX, UINT_MAX};
- cover_t coverType = coverNone;
- QByteArray coverData;
+ AnalyzeTask::reset();
+ QThreadPool *pool = new QThreadPool();
+ QThread::msleep(333);
- QStringList params;
- params << QString("--Inform=file://%1").arg(QDir::toNativeSeparators(m_templateFile->filePath()));
- params << QDir::toNativeSeparators(filePath);
-
- QProcess process;
- process.setProcessChannelMode(QProcess::MergedChannels);
- process.setReadChannel(QProcess::StandardOutput);
- process.start(m_mediaInfoBin, params);
-
- if(!process.waitForStarted())
- {
- qWarning("MediaInfo process failed to create!");
- qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
- process.kill();
- process.waitForFinished(-1);
- return audioFile;
- }
+ pool->setMaxThreadCount(qBound(2, ((QThread::idealThreadCount() * 3) / 2), 12));
- while(process.state() != QProcess::NotRunning)
+ while(!(m_inputFiles.isEmpty() || m_bAborted))
{
- if(m_abortFlag)
- {
- process.kill();
- qWarning("Process was aborted on user request!");
- break;
- }
-
- if(!process.waitForReadyRead())
+ while(!(m_inputFiles.isEmpty() || m_bAborted))
{
- if(process.state() == QProcess::Running)
+ if(!AnalyzeTask::waitForFreeSlot(&m_abortFlag))
{
- qWarning("MediaInfo time out. Killing process and skipping file!");
- process.kill();
- process.waitForFinished(-1);
- return audioFile;
+ qWarning("Timeout in AnalyzeTask::waitForFreeSlot() !!!");
}
- }
- QByteArray data;
-
- while(process.canReadLine())
- {
- QString line = QString::fromUtf8(process.readLine().constData()).simplified();
- if(!line.isEmpty())
+ if(m_abortFlag)
{
- //qDebug("Line:%s", line.toUtf8().constData());
-
- int index = line.indexOf('=');
- if(index > 0)
- {
- QString key = line.left(index).trimmed();
- QString val = line.mid(index+1).trimmed();
- if(!key.isEmpty())
- {
- updateInfo(audioFile, &skipNext, id_val, &coverType, &coverData, key, val);
- }
- }
+ MessageBeep(MB_ICONERROR);
+ m_bAborted = true;
+ break;
}
- }
- }
-
- if(audioFile.fileName().isEmpty())
- {
- QString baseName = QFileInfo(filePath).fileName();
- int index = baseName.lastIndexOf(".");
-
- if(index >= 0)
- {
- baseName = baseName.left(index);
- }
-
- baseName = baseName.replace("_", " ").simplified();
- index = baseName.lastIndexOf(" - ");
-
- if(index >= 0)
- {
- baseName = baseName.mid(index + 3).trimmed();
- }
-
- audioFile.setFileName(baseName);
- }
-
- process.waitForFinished();
- if(process.state() != QProcess::NotRunning)
- {
- process.kill();
- process.waitForFinished(-1);
- }
+
+ if(!m_bAborted)
+ {
+ const QString currentFile = QDir::fromNativeSeparators(m_inputFiles.takeFirst());
- if((coverType != coverNone) && (!coverData.isEmpty()))
- {
- retrieveCover(audioFile, coverType, coverData);
- }
+ AnalyzeTask *task = new AnalyzeTask(currentFile, m_templateFile->filePath(), &m_abortFlag);
+ connect(task, SIGNAL(fileSelected(QString)), this, SIGNAL(fileSelected(QString)), Qt::DirectConnection);
+ connect(task, SIGNAL(progressValChanged(unsigned int)), this, SIGNAL(progressValChanged(unsigned int)), Qt::DirectConnection);
+ connect(task, SIGNAL(progressMaxChanged(unsigned int)), this, SIGNAL(progressMaxChanged(unsigned int)), Qt::DirectConnection);
+ connect(task, SIGNAL(fileAnalyzed(AudioFileModel)), this, SIGNAL(fileAnalyzed(AudioFileModel)), Qt::DirectConnection);
- return audioFile;
-}
+ pool->start(task);
-void FileAnalyzer::updateInfo(AudioFileModel &audioFile, bool *skipNext, unsigned int *id_val, cover_t *coverType, QByteArray *coverData, const QString &key, const QString &value)
-{
- //qWarning("'%s' -> '%s'", key.toUtf8().constData(), value.toUtf8().constData());
-
- /*New Stream*/
- if(IS_KEY("Gen_ID") || IS_KEY("Aud_ID"))
- {
- if(value.isEmpty())
- {
- *skipNext = false;
- }
- else
- {
- //We ignore all ID's, except for the lowest one!
- bool ok = false;
- unsigned int id = value.toUInt(&ok);
- if(ok)
- {
- if(IS_KEY("Gen_ID")) { id_val[0] = qMin(id_val[0], id); *skipNext = (id > id_val[0]); }
- if(IS_KEY("Aud_ID")) { id_val[1] = qMin(id_val[1], id); *skipNext = (id > id_val[1]); }
- }
- else
- {
- *skipNext = true;
+ if(int count = AnalyzeTask::getAdditionalFiles(m_inputFiles))
+ {
+ emit progressMaxChanged(nFiles += count);
+ }
}
}
- if(*skipNext)
- {
- qWarning("Skipping info for non-primary stream!");
- }
- return;
- }
- /*Skip?*/
- if((*skipNext) || value.isEmpty())
- {
- return;
- }
-
- /*General Section*/
- if(IS_SEC("Gen"))
- {
- if(IS_KEY("Gen_Format"))
- {
- if(value.compare("HLS", Qt::CaseInsensitive)) //MediaInfo detects "HLS" for .m3u files, we'll ignore that
- {
- audioFile.setFormatContainerType(value);
- }
- }
- else if(IS_KEY("Gen_Format_Profile"))
- {
- audioFile.setFormatContainerProfile(value);
- }
- else if(IS_KEY("Gen_Title") || IS_KEY("Gen_Track"))
- {
- audioFile.setFileName(value);
- }
- else if(IS_KEY("Gen_Duration"))
- {
- unsigned int tmp = parseDuration(value);
- if(tmp > 0) audioFile.setFileDuration(tmp);
- }
- else if(IS_KEY("Gen_Artist") || IS_KEY("Gen_Performer"))
- {
- audioFile.setFileArtist(value);
- }
- else if(IS_KEY("Gen_Album"))
- {
- audioFile.setFileAlbum(value);
- }
- else if(IS_KEY("Gen_Genre"))
- {
- audioFile.setFileGenre(value);
- }
- else if(IS_KEY("Gen_Released_Date") || IS_KEY("Gen_Recorded_Date"))
+ //One of the Analyze tasks may have gathered additional files from a playlist!
+ if(!m_bAborted)
{
- unsigned int tmp = parseYear(value);
- if(tmp > 0) audioFile.setFileYear(tmp);
- }
- else if(IS_KEY("Gen_Comment"))
- {
- audioFile.setFileComment(value);
- }
- else if(IS_KEY("Gen_Track/Position"))
- {
- bool ok = false;
- unsigned int tmp = value.toUInt(&ok);
- if(ok) audioFile.setFilePosition(tmp);
- }
- else if(IS_KEY("Gen_Cover") || IS_KEY("Gen_Cover_Type"))
- {
- if(*coverType == coverNone)
+ pool->waitForDone();
+ if(int count = AnalyzeTask::getAdditionalFiles(m_inputFiles))
{
- *coverType = coverJpeg;
+ emit progressMaxChanged(nFiles += count);
}
}
- else if(IS_KEY("Gen_Cover_Mime"))
- {
- QString temp = FIRST_TOK(value);
- if(!temp.compare("image/jpeg", Qt::CaseInsensitive)) *coverType = coverJpeg;
- else if(!temp.compare("image/png", Qt::CaseInsensitive)) *coverType = coverPng;
- else if(!temp.compare("image/gif", Qt::CaseInsensitive)) *coverType = coverGif;
- }
- else if(IS_KEY("Gen_Cover_Data"))
- {
- if(!coverData->isEmpty()) coverData->clear();
- coverData->append(QByteArray::fromBase64(FIRST_TOK(value).toLatin1()));
- }
- else
- {
- qWarning("Unknown key '%s' with value '%s' found!", key.toUtf8().constData(), value.toUtf8().constData());
- }
- return;
}
-
- /*Audio Section*/
- if(IS_SEC("Aud"))
- {
-
- if(IS_KEY("Aud_Format"))
- {
- audioFile.setFormatAudioType(value);
- }
- else if(IS_KEY("Aud_Format_Profile"))
- {
- audioFile.setFormatAudioProfile(value);
- }
- else if(IS_KEY("Aud_Format_Version"))
- {
- audioFile.setFormatAudioVersion(value);
- }
- else if(IS_KEY("Aud_Channel(s)"))
- {
- bool ok = false;
- unsigned int tmp = value.toUInt(&ok);
- if(ok) audioFile.setFormatAudioChannels(tmp);
- }
- else if(IS_KEY("Aud_SamplingRate"))
- {
- bool ok = false;
- unsigned int tmp = value.toUInt(&ok);
- if(ok) audioFile.setFormatAudioSamplerate(tmp);
- }
- else if(IS_KEY("Aud_BitDepth"))
- {
- bool ok = false;
- unsigned int tmp = value.toUInt(&ok);
- if(ok) audioFile.setFormatAudioBitdepth(tmp);
- }
- else if(IS_KEY("Aud_Duration"))
- {
- unsigned int tmp = parseDuration(value);
- if(tmp > 0) audioFile.setFileDuration(tmp);
- }
- else if(IS_KEY("Aud_BitRate"))
- {
- bool ok = false;
- unsigned int tmp = value.toUInt(&ok);
- if(ok) audioFile.setFormatAudioBitrate(tmp/1000);
- }
- else if(IS_KEY("Aud_BitRate_Mode"))
- {
- if(!value.compare("CBR", Qt::CaseInsensitive)) audioFile.setFormatAudioBitrateMode(AudioFileModel::BitrateModeConstant);
- if(!value.compare("VBR", Qt::CaseInsensitive)) audioFile.setFormatAudioBitrateMode(AudioFileModel::BitrateModeVariable);
- }
- else
- {
- qWarning("Unknown key '%s' with value '%s' found!", key.toUtf8().constData(), value.toUtf8().constData());
- }
- return;
- }
-
- /*Section not recognized*/
- qWarning("Unknown section: %s", key.toUtf8().constData());
-}
-
-bool FileAnalyzer::checkFile_CDDA(QFile &file)
-{
- file.reset();
- QByteArray data = file.read(128);
- int i = data.indexOf("RIFF");
- int j = data.indexOf("CDDA");
- int k = data.indexOf("fmt ");
-
- return ((i >= 0) && (j >= 0) && (k >= 0) && (k > j) && (j > i));
-}
-
-void FileAnalyzer::retrieveCover(AudioFileModel &audioFile, cover_t coverType, const QByteArray &coverData)
-{
- qDebug("Retrieving cover!");
- QString extension;
+ pool->waitForDone();
+ LAMEXP_DELETE(pool);
- switch(coverType)
+ if(m_bAborted)
{
- case coverPng:
- extension = QString::fromLatin1("png");
- break;
- case coverGif:
- extension = QString::fromLatin1("gif");
- break;
- default:
- extension = QString::fromLatin1("jpg");
- break;
+ qWarning("Operation cancelled by user!");
+ return;
}
- if(!(QImage::fromData(coverData, extension.toUpper().toLatin1().constData()).isNull()))
- {
- QFile coverFile(QString("%1/%2.%3").arg(lamexp_temp_folder2(), lamexp_rand_str(), extension));
- if(coverFile.open(QIODevice::WriteOnly))
- {
- coverFile.write(coverData);
- coverFile.close();
- audioFile.setFileCover(coverFile.fileName(), true);
- }
- }
- else
- {
- qWarning("Image data seems to be invalid :-(");
- }
+ qDebug("All files added.\n");
+ m_bSuccess = true;
}
-bool FileAnalyzer::analyzeAvisynthFile(const QString &filePath, AudioFileModel &info)
-{
- QProcess process;
- process.setProcessChannelMode(QProcess::MergedChannels);
- process.setReadChannel(QProcess::StandardOutput);
- process.start(m_avs2wavBin, QStringList() << QDir::toNativeSeparators(filePath) << "?");
-
- if(!process.waitForStarted())
- {
- qWarning("AVS2WAV process failed to create!");
- qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
- process.kill();
- process.waitForFinished(-1);
- return false;
- }
-
- bool bInfoHeaderFound = false;
-
- while(process.state() != QProcess::NotRunning)
- {
- if(m_abortFlag)
- {
- process.kill();
- qWarning("Process was aborted on user request!");
- break;
- }
-
- if(!process.waitForReadyRead())
- {
- if(process.state() == QProcess::Running)
- {
- qWarning("AVS2WAV time out. Killing process and skipping file!");
- process.kill();
- process.waitForFinished(-1);
- return false;
- }
- }
-
- QByteArray data;
-
- while(process.canReadLine())
- {
- QString line = QString::fromUtf8(process.readLine().constData()).simplified();
- if(!line.isEmpty())
- {
- int index = line.indexOf(':');
- if(index > 0)
- {
- QString key = line.left(index).trimmed();
- QString val = line.mid(index+1).trimmed();
-
- if(bInfoHeaderFound && !key.isEmpty() && !val.isEmpty())
- {
- if(key.compare("TotalSeconds", Qt::CaseInsensitive) == 0)
- {
- bool ok = false;
- unsigned int duration = val.toUInt(&ok);
- if(ok) info.setFileDuration(duration);
- }
- if(key.compare("SamplesPerSec", Qt::CaseInsensitive) == 0)
- {
- bool ok = false;
- unsigned int samplerate = val.toUInt(&ok);
- if(ok) info.setFormatAudioSamplerate (samplerate);
- }
- if(key.compare("Channels", Qt::CaseInsensitive) == 0)
- {
- bool ok = false;
- unsigned int channels = val.toUInt(&ok);
- if(ok) info.setFormatAudioChannels(channels);
- }
- if(key.compare("BitsPerSample", Qt::CaseInsensitive) == 0)
- {
- bool ok = false;
- unsigned int bitdepth = val.toUInt(&ok);
- if(ok) info.setFormatAudioBitdepth(bitdepth);
- }
- }
- }
- else
- {
- if(line.contains("[Audio Info]", Qt::CaseInsensitive))
- {
- info.setFormatAudioType("Avisynth");
- info.setFormatContainerType("Avisynth");
- bInfoHeaderFound = true;
- }
- }
- }
- }
- }
-
- process.waitForFinished();
- if(process.state() != QProcess::NotRunning)
- {
- process.kill();
- process.waitForFinished(-1);
- }
-
- //Check exit code
- switch(process.exitCode())
- {
- case 0:
- qDebug("Avisynth script was analyzed successfully.");
- return true;
- break;
- case -5:
- qWarning("It appears that Avisynth is not installed on the system!");
- return false;
- break;
- default:
- qWarning("Failed to open the Avisynth script, bad AVS file?");
- return false;
- break;
- }
-}
+////////////////////////////////////////////////////////////
+// Privtae Functions
+////////////////////////////////////////////////////////////
bool FileAnalyzer::createTemplate(void)
{
return true;
}
-unsigned int FileAnalyzer::parseYear(const QString &str)
-{
- if(str.startsWith("UTC", Qt::CaseInsensitive))
- {
- QDate date = QDate::fromString(str.mid(3).trimmed().left(10), "yyyy-MM-dd");
- if(date.isValid())
- {
- return date.year();
- }
- else
- {
- return 0;
- }
- }
- else
- {
- bool ok = false;
- int year = str.toInt(&ok);
- if(ok && year > 0)
- {
- return year;
- }
- else
- {
- return 0;
- }
- }
-}
-
-unsigned int FileAnalyzer::parseDuration(const QString &str)
-{
- bool ok = false;
- unsigned int value = str.toUInt(&ok);
- return ok ? (value/1000) : 0;
-}
-
////////////////////////////////////////////////////////////
// Public Functions
////////////////////////////////////////////////////////////
unsigned int FileAnalyzer::filesAccepted(void)
{
- return m_filesAccepted;
+ return AnalyzeTask::filesAccepted();
}
unsigned int FileAnalyzer::filesRejected(void)
{
- return m_filesRejected;
+ return AnalyzeTask::filesRejected();
}
unsigned int FileAnalyzer::filesDenied(void)
{
- return m_filesDenied;
+ return AnalyzeTask::filesDenied();
}
unsigned int FileAnalyzer::filesDummyCDDA(void)
{
- return m_filesDummyCDDA;
+ return AnalyzeTask::filesDummyCDDA();
}
unsigned int FileAnalyzer::filesCueSheet(void)
{
- return m_filesCueSheet;
+ return AnalyzeTask::filesCueSheet();
}
////////////////////////////////////////////////////////////