%3").arg(tr("Failed to load the Cue Sheet file:"), QDir::toNativeSeparators(cueFile), tr("The specified file could not be found!")).replace("-", "−");
+ QString text = QString("%1 %2
%3").arg(tr("Failed to load the Cue Sheet file:"), QDir::toNativeSeparators(m_cueFileName), tr("The specified file could not be found!")).replace("-", "−");
QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
progress->close();
LAMEXP_DELETE(progress);
return CueSheetModel::ErrorIOFailure;
}
- int iResult = m_model->loadCueSheet(cueFile, QApplication::instance());
+ outputDir.mkdir(cueFileInfo.completeBaseName());
+ if(outputDir.cd(cueFileInfo.completeBaseName()))
+ {
+ m_outputDir = outputDir.canonicalPath();
+ }
+
+ int iResult = m_model->loadCueSheet(m_cueFileName, QApplication::instance());
if(iResult != CueSheetModel::ErrorSuccess)
{
QString errorMsg = tr("An unknown error has occured!");
@@ -130,7 +142,7 @@ int CueImportDialog::exec(const QString &cueFile)
break;
}
- QString text = QString("%1 %2
%3").arg(tr("Failed to load the Cue Sheet file:"), QDir::toNativeSeparators(cueFile), errorMsg).replace("-", "−");
+ QString text = QString("%1 %2
%3").arg(tr("Failed to load the Cue Sheet file:"), QDir::toNativeSeparators(m_cueFileName), errorMsg).replace("-", "−");
QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
progress->close();
LAMEXP_DELETE(progress);
@@ -184,31 +196,51 @@ void CueImportDialog::importButtonClicked(void)
}
importCueSheet();
+ accept();
}
void CueImportDialog::analyzedFile(const AudioFileModel &file)
{
- qWarning("Received results for: %s", file.filePath().toLatin1().constData());
- m_fileInfo.insert(file.filePath(), file);
+ qDebug("Received result: <%s> <%s/%s>", file.filePath().toLatin1().constData(), file.formatContainerType().toLatin1().constData(), file.formatAudioType().toLatin1().constData());
+ m_fileInfo << file;
}
////////////////////////////////////////////////////////////
-// Private FUnctions
+// Private Functions
////////////////////////////////////////////////////////////
void CueImportDialog::importCueSheet(void)
{
QStringList files;
- int nFiles = m_model->getFileCount();
- //Fetch all files that are referenced in the Cue Sheet
+ //Fetch all files that are referenced in the Cue Sheet and lock them
+ int nFiles = m_model->getFileCount();
for(int i = 0; i < nFiles; i++)
{
- files << m_model->getFileName(i);
+ QString temp = m_model->getFileName(i);
+ try
+ {
+ m_locks << new LockedFile(temp);
+ }
+ catch(char *err)
+ {
+ qWarning("Failed to lock file: %s", err);
+ continue;
+ }
+ files << temp;
}
//Analyze all source files
analyzeFiles(files);
+
+ //Now split files according to Cue Sheet
+ splitFiles();
+
+ //Release locks
+ while(!m_locks.isEmpty())
+ {
+ delete m_locks.takeFirst();
+ }
}
void CueImportDialog::analyzeFiles(QStringList &files)
@@ -217,10 +249,54 @@ void CueImportDialog::analyzeFiles(QStringList &files)
WorkingBanner *progress = new WorkingBanner(dynamic_cast(parent()));
FileAnalyzer *analyzer = new FileAnalyzer(files);
+
connect(analyzer, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
connect(analyzer, SIGNAL(fileAnalyzed(AudioFileModel)), this, SLOT(analyzedFile(AudioFileModel)), Qt::QueuedConnection);
- progress->show(tr("Adding file(s), please wait..."), analyzer);
+ progress->show(tr("Analyzing file(s), please wait..."), analyzer);
progress->close();
LAMEXP_DELETE(progress);
}
+
+void CueImportDialog::splitFiles(void)
+{
+ WorkingBanner *progress = new WorkingBanner(this);
+ CueSplitter *splitter = new CueSplitter(m_outputDir, QFileInfo(m_cueFileName).completeBaseName().replace(".", " ").left(42).trimmed(), m_fileInfo);
+
+ connect(splitter, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
+ connect(splitter, SIGNAL(fileSplit(AudioFileModel)), m_fileList, SLOT(addFile(AudioFileModel)), Qt::QueuedConnection);
+
+ int nFiles = m_model->getFileCount();
+ for(int i = 0; i < nFiles; i++)
+ {
+ QString currentFileName = m_model->getFileName(i);
+ int nTracks = m_model->getTrackCount(i);
+
+ for(int j = 0; j < nTracks; j++)
+ {
+ int trackNo = m_model->getTrackNo(i, j);
+ double startIndex = std::numeric_limits::quiet_NaN();
+ double duration = std::numeric_limits::quiet_NaN();
+ m_model->getTrackIndex(i, j, &startIndex, &duration);
+
+ AudioFileModel metaInfo(QString().sprintf("cue://File%02d/Track%02d", i, j));
+ metaInfo.setFileName(m_model->getTrackTitle(i, j));
+ metaInfo.setFileArtist(m_model->getTrackPerformer(i, j));
+
+ try
+ {
+ splitter->addTrack(trackNo, currentFileName, startIndex, duration, metaInfo);
+ }
+ catch(char *err)
+ {
+ qWarning("Failed to add track #%02d: %s", trackNo, err);
+ }
+ }
+ }
+
+ progress->show(tr("Splitting file(s), please wait..."), splitter);
+ progress->close();
+
+ LAMEXP_DELETE(splitter);
+ LAMEXP_DELETE(progress);
+}
diff --git a/src/Dialog_CueImport.h b/src/Dialog_CueImport.h
index 5fc542de..523306ab 100644
--- a/src/Dialog_CueImport.h
+++ b/src/Dialog_CueImport.h
@@ -27,16 +27,18 @@
#include "Model_AudioFile.h"
class CueSheetModel;
+class LockedFile;
+class FileListModel;
class CueImportDialog : public QDialog, private Ui::CueSheetImport
{
Q_OBJECT
public:
- CueImportDialog(QWidget *parent);
+ CueImportDialog(QWidget *parent, FileListModel *fileList, const QString &cueFile);
~CueImportDialog(void);
- int exec(const QString &cueFile);
+ int exec(void);
protected:
void CueImportDialog::showEvent(QShowEvent *event);
@@ -50,8 +52,13 @@ private slots:
private:
void importCueSheet(void);
void analyzeFiles(QStringList &files);
+ void CueImportDialog::splitFiles(void);
CueSheetModel *m_model;
- QMap m_fileInfo;
+ FileListModel *m_fileList;
+
+ QList m_locks;
+ QList m_fileInfo;
+ QString m_cueFileName;
QString m_outputDir;
};
diff --git a/src/Dialog_MainWindow.cpp b/src/Dialog_MainWindow.cpp
index 1aa069bc..baec9ad6 100644
--- a/src/Dialog_MainWindow.cpp
+++ b/src/Dialog_MainWindow.cpp
@@ -2571,8 +2571,8 @@ void MainWindow::importCueSheetActionTriggered(bool checked)
QString selectedCueFile = QFileDialog::getOpenFileName(this, tr("Open Cue Sheet"), QString(), QString("%1 (*.cue)").arg(tr("Cue Sheet File")));
if(!selectedCueFile.isEmpty())
{
- CueImportDialog *cueImporter = new CueImportDialog(this);
- cueImporter->exec(selectedCueFile);
+ CueImportDialog *cueImporter = new CueImportDialog(this, m_fileListModel, selectedCueFile);
+ cueImporter->exec();
LAMEXP_DELETE(cueImporter);
}
)
diff --git a/src/Model_CueSheet.cpp b/src/Model_CueSheet.cpp
index 91e89121..4a9a9f79 100644
--- a/src/Model_CueSheet.cpp
+++ b/src/Model_CueSheet.cpp
@@ -346,6 +346,20 @@ int CueSheetModel::getTrackCount(int fileIndex)
return m_files.at(fileIndex)->trackCount();
}
+int CueSheetModel::getTrackNo(int fileIndex, int trackIndex)
+{
+ if(fileIndex >= 0 && fileIndex < m_files.count())
+ {
+ CueSheetFile *currentFile = m_files.at(fileIndex);
+ if(trackIndex >= 0 && trackIndex < currentFile->trackCount())
+ {
+ return currentFile->track(trackIndex)->trackNo();
+ }
+ }
+
+ return -1;
+}
+
void CueSheetModel::getTrackIndex(int fileIndex, int trackIndex, double *startIndex, double *duration)
{
*startIndex = std::numeric_limits::quiet_NaN();
@@ -363,6 +377,34 @@ void CueSheetModel::getTrackIndex(int fileIndex, int trackIndex, double *startIn
}
}
+QString CueSheetModel::getTrackPerformer(int fileIndex, int trackIndex)
+{
+ if(fileIndex >= 0 && fileIndex < m_files.count())
+ {
+ CueSheetFile *currentFile = m_files.at(fileIndex);
+ if(trackIndex >= 0 && trackIndex < currentFile->trackCount())
+ {
+ CueSheetTrack *currentTrack = currentFile->track(trackIndex);
+ return currentTrack->performer();
+ }
+ }
+ return QString();
+}
+
+QString CueSheetModel::getTrackTitle(int fileIndex, int trackIndex)
+{
+ if(fileIndex >= 0 && fileIndex < m_files.count())
+ {
+ CueSheetFile *currentFile = m_files.at(fileIndex);
+ if(trackIndex >= 0 && trackIndex < currentFile->trackCount())
+ {
+ CueSheetTrack *currentTrack = currentFile->track(trackIndex);
+ return currentTrack->title();
+ }
+ }
+ return QString();
+}
+
////////////////////////////////////////////////////////////
// Cue Sheet Parser
////////////////////////////////////////////////////////////
@@ -624,6 +666,7 @@ int CueSheetModel::parseCueFile(QFile &cueFile, const QDir &baseDir, QCoreApplic
//Sanity check of track numbers
if(nFiles > 0)
{
+ int previousTrackNo = -1;
bool trackNo[100];
for(int i = 0; i < 100; i++)
{
@@ -640,7 +683,7 @@ int CueSheetModel::parseCueFile(QFile &cueFile, const QDir &baseDir, QCoreApplic
int nTracks = currentFile->trackCount();
if(nTracks > 1)
{
- for(int j = 1; j < nTracks; j++)
+ for(int j = 0; j < nTracks; j++)
{
int currentTrackNo = currentFile->track(j)->trackNo();
if(currentTrackNo > 99)
@@ -648,12 +691,18 @@ int CueSheetModel::parseCueFile(QFile &cueFile, const QDir &baseDir, QCoreApplic
qWarning("Track #%02d is invalid (maximum is 99), Cue Sheet is inconsistent!", currentTrackNo);
return ErrorInconsistent;
}
+ if(currentTrackNo <= previousTrackNo)
+ {
+ qWarning("Non-increasing track numbers, Cue Sheet is inconsistent!", currentTrackNo);
+ return ErrorInconsistent;
+ }
if(trackNo[currentTrackNo])
{
qWarning("Track #%02d exists multiple times, Cue Sheet is inconsistent!", currentTrackNo);
return ErrorInconsistent;
}
trackNo[currentTrackNo] = true;
+ previousTrackNo = currentTrackNo;
}
}
}
diff --git a/src/Model_CueSheet.h b/src/Model_CueSheet.h
index fa509f33..d0ff6403 100644
--- a/src/Model_CueSheet.h
+++ b/src/Model_CueSheet.h
@@ -61,7 +61,10 @@ public:
int CueSheetModel::getFileCount(void);
QString getFileName(int fileIndex);
int getTrackCount(int fileIndex);
+ int getTrackNo(int fileIndex, int trackIndex);
void getTrackIndex(int fileIndex, int trackIndex, double *startIndex, double *duration);
+ QString getTrackPerformer(int fileIndex, int trackIndex);
+ QString getTrackTitle(int fileIndex, int trackIndex);
//Cue Sheet functions
int loadCueSheet(const QString &cueFile, QCoreApplication *application = NULL);
diff --git a/src/Thread_CueSplitter.cpp b/src/Thread_CueSplitter.cpp
new file mode 100644
index 00000000..1f01f4ae
--- /dev/null
+++ b/src/Thread_CueSplitter.cpp
@@ -0,0 +1,253 @@
+///////////////////////////////////////////////////////////////////////////////
+// LameXP - Audio Encoder Front-End
+// Copyright (C) 2004-2011 LoRd_MuldeR
+//
+// 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
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// http://www.gnu.org/licenses/gpl-2.0.txt
+///////////////////////////////////////////////////////////////////////////////
+
+#include "Thread_CueSplitter.h"
+
+#include "Global.h"
+#include "LockedFile.h"
+#include "Model_AudioFile.h"
+#include "PlaylistImporter.h"
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include
+
+////////////////////////////////////////////////////////////
+// Constructor
+////////////////////////////////////////////////////////////
+
+CueSplitter::CueSplitter(const QString &outputDir, const QString &baseName, const QList &inputFiles)
+:
+ m_outputDir(outputDir),
+ m_baseName(baseName),
+ m_soxBin(lamexp_lookup_tool("sox.exe"))
+{
+ if(m_soxBin.isEmpty())
+ {
+ qFatal("Invalid path to SoX binary. Tool not initialized properly.");
+ }
+
+ qDebug("\n[CueSplitter::CueSplitter]");
+
+ int nInputFiles = inputFiles.count();
+ for(int i = 0; i < nInputFiles; i++)
+ {
+ m_inputFiles.insert(inputFiles[i].filePath(), inputFiles[i]);
+ qDebug("%02d <%s>", i, inputFiles[i].filePath());
+ }
+
+ qDebug("All input files added.");
+ m_bSuccess = false;
+}
+
+////////////////////////////////////////////////////////////
+// Thread Main
+////////////////////////////////////////////////////////////
+
+void CueSplitter::run()
+{
+ m_bSuccess = false;
+ m_abortFlag = false;
+
+ int nTracks = min(min(min(m_trackFile.count(), m_trackNo.count()), min(m_trackOffset.count(), m_trackLength.count())), m_trackMetaInfo.count());
+
+ if(!QDir(m_outputDir).exists())
+ {
+ qWarning("Output directory \"%s\" does not exist!", m_outputDir.toUtf8().constData());
+ return;
+ }
+
+ for(int i = 0; i < nTracks; i++)
+ {
+ QString outputFile = QString("%1/%2 - Track %3.wav").arg(m_outputDir, m_baseName, QString().sprintf("%02d", m_trackNo.at(i)));
+ for(int n = 2; QFileInfo(outputFile).exists(); n++)
+ {
+ outputFile = QString("%1/%2 - Track %3 (%4).wav").arg(m_outputDir, m_baseName, QString().sprintf("%02d", m_trackNo.at(i)), QString::number(n));
+ }
+
+ emit fileSelected(QFileInfo(outputFile).fileName());
+ splitFile(outputFile, m_trackNo.at(i), m_trackFile.at(i), m_trackOffset.at(i), m_trackLength.at(i), m_trackMetaInfo.at(i));
+ }
+
+ qDebug("All files were split.\n");
+ m_bSuccess = true;
+}
+
+void CueSplitter::addTrack(const int trackNo, const QString &file, const double offset, const double length, const AudioFileModel &metaInfo)
+{
+
+ if(m_inputFiles.contains(file))
+ {
+ m_trackFile << file;
+ m_trackNo << trackNo;
+ m_trackOffset << offset;
+ m_trackLength << length;
+ m_trackMetaInfo << metaInfo;
+ }
+ else
+ {
+ throw "Unknown input file!";
+ }
+}
+
+////////////////////////////////////////////////////////////
+// Privtae Functions
+////////////////////////////////////////////////////////////
+
+void CueSplitter::splitFile(const QString &output, const int trackNo, const QString &file, const double offset, const double length, const AudioFileModel &metaInfo)
+{
+ qDebug("\n[Track %02d]", trackNo);
+ qDebug("File: <%s>", file.toUtf8().constData());
+ qDebug("Offset: %f", offset);
+ qDebug("Length: %f", length);
+ qDebug("Artist: <%s>", metaInfo.fileArtist().toUtf8().constData());
+ qDebug("Title: <%s>", metaInfo.fileName().toUtf8().constData());
+
+ QRegExp regExp("In:(\\d+)(\\.\\d+)*%");
+ QString baseName = QFileInfo(output).fileName();
+
+ if(!m_inputFiles.contains(file))
+ {
+ qWarning("Unknown input file, skipping!");
+ return;
+ }
+
+ AudioFileModel &inputFileInfo = m_inputFiles[file];
+ if(inputFileInfo.formatContainerType().compare("Wave", Qt::CaseInsensitive) || inputFileInfo.formatAudioType().compare("PCM", Qt::CaseInsensitive))
+ {
+ qWarning("Sorry, only Wave/PCM files are supported at this time!");
+ return;
+ }
+
+ QStringList args;
+ args << "-S" << "-V3";
+ args << "--guard" << "--temp" << ".";
+ args << QDir::toNativeSeparators(file);
+ args << QDir::toNativeSeparators(output);
+ args << "trim";
+ args << indexToString(offset);
+
+ if((length != std::numeric_limits::quiet_NaN()) && (length != std::numeric_limits::infinity()))
+ {
+ args << indexToString(length);
+ }
+
+ QProcess process;
+ process.setProcessChannelMode(QProcess::MergedChannels);
+ process.setReadChannel(QProcess::StandardOutput);
+ process.setWorkingDirectory(m_outputDir);
+ process.start(m_soxBin, args);
+
+ if(!process.waitForStarted())
+ {
+ qWarning("SoX process failed to create!");
+ qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
+ process.kill();
+ process.waitForFinished(-1);
+ return;
+ }
+
+ while(process.state() != QProcess::NotRunning)
+ {
+ if(m_abortFlag)
+ {
+ process.kill();
+ break;
+ }
+ process.waitForReadyRead();
+ if(!process.bytesAvailable() && process.state() == QProcess::Running)
+ {
+ process.kill();
+ qWarning("SoX process timed out <-- killing!");
+ break;
+ }
+ while(process.bytesAvailable() > 0)
+ {
+ QByteArray line = process.readLine();
+ QString text = QString::fromUtf8(line.constData()).simplified();
+ if(regExp.lastIndexIn(text) >= 0)
+ {
+ bool ok = false;
+ int progress = regExp.cap(1).toInt(&ok);
+ if(ok)
+ {
+ emit fileSelected(QString("%1 [%2%]").arg(baseName, QString::number(progress)));
+ }
+ }
+ }
+ }
+
+ if(process.state() != QProcess::NotRunning)
+ {
+ process.kill();
+ process.waitForFinished(-1);
+ }
+
+ if(process.exitStatus() != QProcess::NormalExit || QFileInfo(output).size() == 0)
+ {
+ qWarning("Splitting has failed!");
+ return;
+ }
+
+ AudioFileModel outFileInfo(metaInfo);
+ outFileInfo.setFilePath(output);
+ outFileInfo.setFormatContainerType(inputFileInfo.formatContainerType());
+ outFileInfo.setFormatAudioType(inputFileInfo.formatAudioType());
+ outFileInfo.setFileDuration(static_cast(abs(length)));
+ emit fileSplit(outFileInfo);
+}
+
+QString CueSplitter::indexToString(const double index) const
+{
+ if(index == std::numeric_limits::quiet_NaN() || index == std::numeric_limits::infinity() || index < 0.0)
+ {
+ return QString();
+ }
+
+ int temp = static_cast(index * 1000.0);
+
+ int msec = temp % 1000;
+ int secs = temp / 1000;
+
+ if(secs >= 3600)
+ {
+ return QString().sprintf("%d:%02d:%02d.%03d", min(99, secs / 3600), min(59, (secs % 3600) / 60), min(59, (secs % 3600) % 60), min(99, msec));
+ }
+ else if(secs >= 60)
+ {
+ return QString().sprintf("%d:%02d.%03d", min(99, secs / 60), min(59, secs % 60), min(99, msec));
+ }
+ else
+ {
+ return QString().sprintf("%d.%03d", min(59, secs % 60), min(99, msec));
+ }
+}
+
+////////////////////////////////////////////////////////////
+// EVENTS
+////////////////////////////////////////////////////////////
+
+/*NONE*/
diff --git a/src/Thread_CueSplitter.h b/src/Thread_CueSplitter.h
new file mode 100644
index 00000000..b31efad1
--- /dev/null
+++ b/src/Thread_CueSplitter.h
@@ -0,0 +1,67 @@
+///////////////////////////////////////////////////////////////////////////////
+// LameXP - Audio Encoder Front-End
+// Copyright (C) 2004-2011 LoRd_MuldeR
+//
+// 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
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// http://www.gnu.org/licenses/gpl-2.0.txt
+///////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#include
+#include
+#include
+
+class AudioFileModel;
+class QFile;
+class QDir;
+class QFileInfo;
+
+////////////////////////////////////////////////////////////
+// Splash Thread
+////////////////////////////////////////////////////////////
+
+class CueSplitter: public QThread
+{
+ Q_OBJECT
+
+public:
+ CueSplitter(const QString &outputDir, const QString &baseName, const QList &inputFiles);
+ void run();
+ bool getSuccess(void) { return !isRunning() && m_bSuccess; }
+ void addTrack(const int trackNo, const QString &file, const double offset, const double length, const AudioFileModel &metaInfo);
+
+signals:
+ void fileSelected(const QString &fileName);
+ void fileSplit(const AudioFileModel &file);
+
+private:
+ void splitFile(const QString &output, const int trackNo, const QString &file, const double offset, const double length, const AudioFileModel &metaInfo);
+ QString indexToString(const double index) const;
+
+ const QString m_soxBin;
+ const QString m_outputDir;
+ const QString m_baseName;
+ bool m_bSuccess;
+ volatile bool m_abortFlag;
+
+ QMap m_inputFiles;
+ QList m_trackFile;
+ QList m_trackNo;
+ QList m_trackOffset;
+ QList m_trackLength;
+ QList m_trackMetaInfo;
+};