OSDN Git Service

Set creation/modified time of the encoded file the same value as the original file...
[lamexp/LameXP.git] / src / Thread_CueSplitter.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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.
10 //
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.
15 //
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.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Thread_CueSplitter.h"
24
25 //Internal
26 #include "Global.h"
27 #include "LockedFile.h"
28 #include "Model_AudioFile.h"
29 #include "Model_CueSheet.h"
30 #include "Registry_Decoder.h"
31 #include "Decoder_Abstract.h"
32
33 //MUtils
34 #include <MUtils/Global.h>
35 #include <MUtils/OSSupport.h>
36
37 //Qt
38 #include <QDir>
39 #include <QFileInfo>
40 #include <QProcess>
41 #include <QDate>
42 #include <QTime>
43 #include <QDebug>
44
45 //CRT
46 #include <math.h>
47 #include <float.h>
48 #include <limits>
49
50 ////////////////////////////////////////////////////////////
51 // Constructor
52 ////////////////////////////////////////////////////////////
53
54 CueSplitter::CueSplitter(const QString &outputDir, const QString &baseName, CueSheetModel *model, const QList<AudioFileModel> &inputFilesInfo)
55 :
56         m_model(model),
57         m_outputDir(outputDir),
58         m_baseName(baseName),
59         m_soxBin(lamexp_tools_lookup("sox.exe"))
60 {
61         if(m_soxBin.isEmpty())
62         {
63                 qFatal("Invalid path to SoX binary. Tool not initialized properly.");
64         }
65
66         m_decompressedFiles.clear();
67         m_tempFiles.clear();
68
69         qDebug("\n[CueSplitter]");
70
71         int nInputFiles = inputFilesInfo.count();
72         for(int i = 0; i < nInputFiles; i++)
73         {
74                 m_inputFilesInfo.insert(inputFilesInfo[i].filePath(), inputFilesInfo[i]);
75                 qDebug("File %02d: <%s>", i, MUTILS_UTF8(inputFilesInfo[i].filePath()));
76         }
77         
78         qDebug("All input files added.");
79         m_bSuccess = false;
80 }
81
82 CueSplitter::~CueSplitter(void)
83 {
84         while(!m_tempFiles.isEmpty())
85         {
86                 MUtils::remove_file(m_tempFiles.takeFirst());
87         }
88 }
89
90 ////////////////////////////////////////////////////////////
91 // Thread Main
92 ////////////////////////////////////////////////////////////
93
94 void CueSplitter::run()
95 {
96         m_bSuccess = false;
97         m_bAborted = false;
98         m_abortFlag = false;
99         m_nTracksSuccess = 0;
100         m_nTracksSkipped = 0;
101         m_decompressedFiles.clear();
102         m_activeFile.clear();
103         
104         if(!QDir(m_outputDir).exists())
105         {
106                 qWarning("Output directory \"%s\" does not exist!", MUTILS_UTF8(m_outputDir));
107                 return;
108         }
109         
110         QStringList inputFileList = m_inputFilesInfo.keys();
111         int nInputFiles = inputFileList.count();
112         
113         emit progressMaxChanged(nInputFiles);
114         emit progressValChanged(0);
115
116         //Decompress all input files
117         for(int i = 0; i < nInputFiles; i++)
118         {
119                 const AudioFileModel_TechInfo &inputFileInfo = m_inputFilesInfo[inputFileList.at(i)].techInfo();
120                 if(inputFileInfo.containerType().compare("Wave", Qt::CaseInsensitive) || inputFileInfo.audioType().compare("PCM", Qt::CaseInsensitive))
121                 {
122                         AbstractDecoder *decoder = DecoderRegistry::lookup(inputFileInfo.containerType(), inputFileInfo.containerProfile(), inputFileInfo.audioType(), inputFileInfo.audioProfile(), inputFileInfo.audioVersion());
123                         if(decoder)
124                         {
125                                 m_activeFile = shortName(QFileInfo(inputFileList.at(i)).fileName());
126                                 
127                                 emit fileSelected(m_activeFile);
128                                 emit progressValChanged(i+1);
129                                 
130                                 QString tempFile = QString("%1/~%2.wav").arg(m_outputDir, MUtils::rand_str());
131                                 connect(decoder, SIGNAL(statusUpdated(int)), this, SLOT(handleUpdate(int)), Qt::DirectConnection);
132                                 
133                                 if(decoder->decode(inputFileList.at(i), tempFile, &m_abortFlag))
134                                 {
135                                         m_decompressedFiles.insert(inputFileList.at(i), tempFile);
136                                         m_tempFiles.append(tempFile);
137                                 }
138                                 else
139                                 {
140                                         qWarning("Failed to decompress file: <%s>", inputFileList.at(i).toLatin1().constData());
141                                         MUtils::remove_file(tempFile);
142                                 }
143                                 
144                                 m_activeFile.clear();
145                                 MUTILS_DELETE(decoder);
146                         }
147                         else
148                         {
149                                 qWarning("Unsupported input file: <%s>", inputFileList.at(i).toLatin1().constData());
150                         }
151                 }
152                 else
153                 {
154                         m_decompressedFiles.insert(inputFileList.at(i), inputFileList.at(i));
155                 }
156
157                 if(m_abortFlag)
158                 {
159                         m_bAborted = true;
160                         qWarning("The user has requested to abort the process!");
161                         return;
162                 }
163         }
164
165         int nFiles = m_model->getFileCount();
166         int nTracksTotal = 0, nTracksComplete = 0;
167
168         for(int i = 0; i < nFiles; i++)
169         {
170                 nTracksTotal += m_model->getTrackCount(i);
171         }
172
173         emit progressMaxChanged(10 * nTracksTotal);
174         emit progressValChanged(0);
175
176         const AudioFileModel_MetaInfo *albumInfo = m_model->getAlbumInfo();
177
178         //Now split all files
179         for(int i = 0; i < nFiles; i++)
180         {
181                 int nTracks = m_model->getTrackCount(i);
182                 QString trackFile = m_model->getFileName(i);
183
184                 //Process all tracks
185                 for(int j = 0; j < nTracks; j++)
186                 {
187                         const AudioFileModel_MetaInfo *trackInfo = m_model->getTrackInfo(i, j);
188                         const int trackNo = trackInfo->position();
189                         double trackOffset = std::numeric_limits<double>::quiet_NaN();
190                         double trackLength = std::numeric_limits<double>::quiet_NaN();
191                         m_model->getTrackIndex(i, j, &trackOffset, &trackLength);
192                         
193                         if((trackNo < 0) || _isnan(trackOffset) || _isnan(trackLength))
194                         {
195                                 qWarning("Failed to fetch information for track #%d of file #%d!", j, i);
196                                 continue;
197                         }
198                         
199                         //Setup meta info
200                         AudioFileModel_MetaInfo trackMetaInfo(*trackInfo);
201                         
202                         //Apply album meta data on files
203                         if(trackMetaInfo.title().trimmed().isEmpty())
204                         {
205                                 trackMetaInfo.setTitle(QString().sprintf("Track %02d", trackNo));
206                         }
207                         trackMetaInfo.update(*albumInfo, false);
208
209                         //Generate output file name
210                         QString trackTitle = trackMetaInfo.title().isEmpty() ? QString().sprintf("Track %02d", trackNo) : trackMetaInfo.title();
211                         QString outputFile = QString("%1/[%2] %3 - %4.wav").arg(m_outputDir, QString().sprintf("%02d", trackNo), MUtils::clean_file_name(m_baseName), MUtils::clean_file_name(trackTitle));
212                         for(int n = 2; QFileInfo(outputFile).exists(); n++)
213                         {
214                                 outputFile = QString("%1/[%2] %3 - %4 (%5).wav").arg(m_outputDir, QString().sprintf("%02d", trackNo), MUtils::clean_file_name(m_baseName), MUtils::clean_file_name(trackTitle), QString::number(n));
215                         }
216
217                         //Call split function
218                         emit fileSelected(shortName(QFileInfo(outputFile).fileName()));
219                         splitFile(outputFile, trackNo, trackFile, trackOffset, trackLength, trackMetaInfo, nTracksComplete);
220                         emit progressValChanged(nTracksComplete += 10);
221
222                         if(m_abortFlag)
223                         {
224                                 m_bAborted = true;
225                                 qWarning("The user has requested to abort the process!");
226                                 return;
227                         }
228                 }
229         }
230
231         emit progressValChanged(10 * nTracksTotal);
232         MUtils::OS::sleep_ms(333);
233
234         qDebug("All files were split.\n");
235         m_bSuccess = true;
236 }
237
238 ////////////////////////////////////////////////////////////
239 // Slots
240 ////////////////////////////////////////////////////////////
241
242 void CueSplitter::handleUpdate(int progress)
243 {
244         //QString("%1 [%2]").arg(m_activeFile, QString::number(progress)))
245 }
246
247 ////////////////////////////////////////////////////////////
248 // Privtae Functions
249 ////////////////////////////////////////////////////////////
250
251 void CueSplitter::splitFile(const QString &output, const int trackNo, const QString &file, const double offset, const double length, const AudioFileModel_MetaInfo &metaInfo, const int baseProgress)
252 {
253         qDebug("[Track %02d]", trackNo);
254         qDebug("File: <%s>", MUTILS_UTF8(file));
255         qDebug("Offset: <%f> <%s>", offset, indexToString(offset).toLatin1().constData());
256         qDebug("Length: <%f> <%s>", length, indexToString(length).toLatin1().constData());
257         qDebug("Artist: <%s>", MUTILS_UTF8(metaInfo.artist()));
258         qDebug("Title: <%s>", MUTILS_UTF8(metaInfo.title()));
259         qDebug("Album: <%s>", MUTILS_UTF8(metaInfo.album()));
260         
261         int prevProgress = baseProgress;
262
263         if(!m_decompressedFiles.contains(file))
264         {
265                 qWarning("Unknown or unsupported input file, skipping!");
266                 m_nTracksSkipped++;
267                 return;
268         }
269
270         QString baseName = shortName(QFileInfo(output).fileName());
271         QString decompressedInput = m_decompressedFiles[file];
272         qDebug("Input: <%s>", MUTILS_UTF8(decompressedInput));
273         
274         AudioFileModel outFileInfo(output);
275         outFileInfo.setMetaInfo(metaInfo);
276         
277         AudioFileModel_TechInfo &outFileTechInfo = outFileInfo.techInfo();
278         outFileTechInfo.setContainerType("Wave");
279         outFileTechInfo.setAudioType("PCM");
280         outFileTechInfo.setDuration(static_cast<unsigned int>(abs(length)));
281
282         QStringList args;
283         args << "-S" << "-V3";
284         args << "--guard" << "--temp" << ".";
285         args << QDir::toNativeSeparators(decompressedInput);
286         args << QDir::toNativeSeparators(output);
287         
288         //Add trim parameters, if needed
289         if(_finite(offset))
290         {
291                 args << "trim";
292                 args << indexToString(offset);
293                 
294                 if(_finite(length))
295                 {
296                         args << indexToString(length);
297                 }
298         }
299
300         QRegExp rxProgress("In:(\\d+)(\\.\\d+)*%", Qt::CaseInsensitive);
301         QRegExp rxChannels("Channels\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
302         QRegExp rxSamplerate("Sample Rate\\s*:\\s*(\\d+)", Qt::CaseInsensitive);
303         QRegExp rxPrecision("Precision\\s*:\\s*(\\d+)-bit", Qt::CaseInsensitive);
304         QRegExp rxDuration("Duration\\s*:\\s*(\\d\\d):(\\d\\d):(\\d\\d).(\\d\\d)", Qt::CaseInsensitive);
305
306         QProcess process;
307         MUtils::init_process(process, m_outputDir);
308
309         process.start(m_soxBin, args);
310                 
311         if(!process.waitForStarted())
312         {
313                 qWarning("SoX process failed to create!");
314                 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
315                 process.kill();
316                 process.waitForFinished(-1);
317                 m_nTracksSkipped++;
318                 return;
319         }
320
321         while(process.state() != QProcess::NotRunning)
322         {
323                 if(m_abortFlag)
324                 {
325                         process.kill();
326                         qWarning("Process was aborted on user request!");
327                         break;
328                 }
329                 process.waitForReadyRead(m_processTimeoutInterval);
330                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
331                 {
332                         process.kill();
333                         qWarning("SoX process timed out <-- killing!");
334                         break;
335                 }
336                 while(process.bytesAvailable() > 0)
337                 {
338                         QByteArray line = process.readLine();
339                         QString text = QString::fromUtf8(line.constData()).simplified();
340                         if(rxProgress.lastIndexIn(text) >= 0)
341                         {
342                                 bool ok = false;
343                                 int progress = rxProgress.cap(1).toInt(&ok);
344                                 if(ok)
345                                 {
346                                         const int newProgress = baseProgress + qRound(static_cast<double>(qBound(0, progress, 100)) / 10.0);
347                                         if(newProgress > prevProgress)
348                                         {
349                                                 emit progressValChanged(newProgress);
350                                                 prevProgress = newProgress;
351                                         }
352                                 }
353                         }
354                         else if(rxChannels.lastIndexIn(text) >= 0)
355                         {
356                                 bool ok = false;
357                                 unsigned int channels = rxChannels.cap(1).toUInt(&ok);
358                                 if(ok) outFileInfo.techInfo().setAudioChannels(channels);
359                         }
360                         else if(rxSamplerate.lastIndexIn(text) >= 0)
361                         {
362                                 bool ok = false;
363                                 unsigned int samplerate = rxSamplerate.cap(1).toUInt(&ok);
364                                 if(ok) outFileInfo.techInfo().setAudioSamplerate(samplerate);
365                         }
366                         else if(rxPrecision.lastIndexIn(text) >= 0)
367                         {
368                                 bool ok = false;
369                                 unsigned int precision = rxPrecision.cap(1).toUInt(&ok);
370                                 if(ok) outFileInfo.techInfo().setAudioBitdepth(precision);
371                         }
372                         else if(rxDuration.lastIndexIn(text) >= 0)
373                         {
374                                 bool ok1 = false, ok2 = false, ok3 = false;
375                                 unsigned int hh = rxDuration.cap(1).toUInt(&ok1);
376                                 unsigned int mm = rxDuration.cap(2).toUInt(&ok2);
377                                 unsigned int ss = rxDuration.cap(3).toUInt(&ok3);
378                                 if(ok1 && ok2 && ok3)
379                                 {
380                                         unsigned intputLen = (hh * 3600) + (mm * 60) + ss;
381                                         if(length == std::numeric_limits<double>::infinity())
382                                         {
383                                                 qDebug("Duration updated from SoX info!");
384                                                 int duration = intputLen - static_cast<int>(floor(offset + 0.5));
385                                                 if(duration < 0) qWarning("Track is out of bounds: Track offset exceeds input file duration!");
386                                                 outFileInfo.techInfo().setDuration(qMax(0, duration));
387                                         }
388                                         else
389                                         {
390                                                 unsigned int trackEnd = static_cast<unsigned int>(floor(offset + 0.5)) + static_cast<unsigned int>(floor(length + 0.5));
391                                                 if(trackEnd > intputLen) qWarning("Track is out of bounds: End of track exceeds input file duration!");
392                                         }
393                                 }
394                         }
395                 }
396         }
397
398         process.waitForFinished();
399         if(process.state() != QProcess::NotRunning)
400         {
401                 process.kill();
402                 process.waitForFinished(-1);
403         }
404
405         if(process.exitCode() != EXIT_SUCCESS || QFileInfo(output).size() == 0)
406         {
407                 qWarning("Splitting has failed !!!");
408                 m_nTracksSkipped++;
409                 return;
410         }
411
412         emit fileSplit(outFileInfo);
413         m_nTracksSuccess++;
414 }
415
416 QString CueSplitter::indexToString(const double index) const
417 {
418         if(!_finite(index) || (index < 0.0) || (index > 86400.0))
419         {
420                 return QString();
421         }
422         
423         QTime time = QTime().addMSecs(static_cast<int>(floor(0.5 + (index * 1000.0))));
424         return time.toString(time.hour() ? "H:mm:ss.zzz" : "m:ss.zzz");
425 }
426
427 QString CueSplitter::shortName(const QString &longName) const
428 {
429         static const int maxLen = 54;
430         
431         if(longName.length() > maxLen)
432         {
433                 return QString("%1...%2").arg(longName.left(maxLen/2).trimmed(), longName.right(maxLen/2).trimmed());
434         }
435
436         return longName;
437 }
438
439 ////////////////////////////////////////////////////////////
440 // EVENTS
441 ////////////////////////////////////////////////////////////
442
443 /*NONE*/