comparison data/fileio/MP3FileReader.cpp @ 392:183ee2a55fc7

* More work to abstract out interactive components used in the data library, so that it does not need to depend on QtGui.
author Chris Cannam
date Fri, 14 Mar 2008 17:14:21 +0000
parents e6d11871e4c9
children be49bf95d4a5
comparison
equal deleted inserted replaced
391:5858cc462d0a 392:183ee2a55fc7
15 */ 15 */
16 16
17 #ifdef HAVE_MAD 17 #ifdef HAVE_MAD
18 18
19 #include "MP3FileReader.h" 19 #include "MP3FileReader.h"
20 #include "ProgressPrinter.h" 20 #include "base/ProgressReporter.h"
21 21
22 #include "system/System.h" 22 #include "system/System.h"
23 23
24 #include <sys/types.h> 24 #include <sys/types.h>
25 #include <sys/stat.h> 25 #include <sys/stat.h>
30 #ifdef HAVE_ID3TAG 30 #ifdef HAVE_ID3TAG
31 #include <id3tag.h> 31 #include <id3tag.h>
32 #endif 32 #endif
33 #define DEBUG_ID3TAG 1 33 #define DEBUG_ID3TAG 1
34 34
35 #include <QApplication>
36 #include <QFileInfo> 35 #include <QFileInfo>
37 #include <QProgressDialog>
38 36
39 MP3FileReader::MP3FileReader(FileSource source, DecodeMode decodeMode, 37 MP3FileReader::MP3FileReader(FileSource source, DecodeMode decodeMode,
40 CacheMode mode, size_t targetRate) : 38 CacheMode mode, size_t targetRate,
39 ProgressReporter *reporter) :
41 CodedAudioFileReader(mode, targetRate), 40 CodedAudioFileReader(mode, targetRate),
42 m_source(source), 41 m_source(source),
43 m_path(source.getLocalFilename()), 42 m_path(source.getLocalFilename()),
44 m_decodeThread(0) 43 m_decodeThread(0)
45 { 44 {
49 m_bitrateNum = 0; 48 m_bitrateNum = 0;
50 m_bitrateDenom = 0; 49 m_bitrateDenom = 0;
51 m_cancelled = false; 50 m_cancelled = false;
52 m_completion = 0; 51 m_completion = 0;
53 m_done = false; 52 m_done = false;
54 m_progress = 0; 53 m_reporter = reporter;
55 54
56 struct stat stat; 55 struct stat stat;
57 if (::stat(m_path.toLocal8Bit().data(), &stat) == -1 || stat.st_size == 0) { 56 if (::stat(m_path.toLocal8Bit().data(), &stat) == -1 || stat.st_size == 0) {
58 m_error = QString("File %1 does not exist.").arg(m_path); 57 m_error = QString("File %1 does not exist.").arg(m_path);
59 return; 58 return;
106 105
107 loadTags(); 106 loadTags();
108 107
109 if (decodeMode == DecodeAtOnce) { 108 if (decodeMode == DecodeAtOnce) {
110 109
111 if (dynamic_cast<QApplication *>(QCoreApplication::instance())) { 110 if (m_reporter) {
112 m_progress = new QProgressDialog 111 connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled()));
113 (QObject::tr("Decoding %1...").arg(QFileInfo(m_path).fileName()), 112 m_reporter->setMessage
114 QObject::tr("Stop"), 0, 100); 113 (tr("Decoding %1...").arg(QFileInfo(m_path).fileName()));
115 m_progress->hide();
116 } else {
117 ProgressPrinter *pp = new ProgressPrinter(tr("Decoding..."), this);
118 connect(this, SIGNAL(progress(int)), pp, SLOT(progress(int)));
119 } 114 }
120 115
121 if (!decode(m_filebuffer, m_fileSize)) { 116 if (!decode(m_filebuffer, m_fileSize)) {
122 m_error = QString("Failed to decode file %1.").arg(m_path); 117 m_error = QString("Failed to decode file %1.").arg(m_path);
123 } 118 }
125 delete[] m_filebuffer; 120 delete[] m_filebuffer;
126 m_filebuffer = 0; 121 m_filebuffer = 0;
127 122
128 if (isDecodeCacheInitialised()) finishDecodeCache(); 123 if (isDecodeCacheInitialised()) finishDecodeCache();
129 124
130 delete m_progress;
131 m_progress = 0;
132
133 } else { 125 } else {
126
127 if (m_reporter) m_reporter->setProgress(100);
134 128
135 m_decodeThread = new DecodeThread(this); 129 m_decodeThread = new DecodeThread(this);
136 m_decodeThread->start(); 130 m_decodeThread->start();
137 131
138 while ((m_channelCount == 0 || m_fileRate == 0 || m_sampleRate == 0) 132 while ((m_channelCount == 0 || m_fileRate == 0 || m_sampleRate == 0)
149 if (m_decodeThread) { 143 if (m_decodeThread) {
150 m_cancelled = true; 144 m_cancelled = true;
151 m_decodeThread->wait(); 145 m_decodeThread->wait();
152 delete m_decodeThread; 146 delete m_decodeThread;
153 } 147 }
148 }
149
150 void
151 MP3FileReader::cancelled()
152 {
153 m_cancelled = true;
154 } 154 }
155 155
156 void 156 void
157 MP3FileReader::loadTags() 157 MP3FileReader::loadTags()
158 { 158 {
364 double percent = 100; 364 double percent = 100;
365 if (duration > 0.0) percent = ((elapsed * 100.0) / duration); 365 if (duration > 0.0) percent = ((elapsed * 100.0) / duration);
366 int p = int(percent); 366 int p = int(percent);
367 if (p < 1) p = 1; 367 if (p < 1) p = 1;
368 if (p > 99) p = 99; 368 if (p > 99) p = 99;
369 if (m_completion != p || (m_progress && !m_progress->isVisible())) { 369 if (m_completion != p && m_reporter) {
370 m_completion = p; 370 m_completion = p;
371 emit progress(m_completion); 371 m_reporter->setProgress(m_completion);
372 if (m_progress) {
373 if (m_completion > m_progress->value()) {
374 m_progress->setValue(m_completion);
375 m_progress->show();
376 m_progress->raise();
377 qApp->processEvents();
378 if (m_progress->wasCanceled()) {
379 m_cancelled = true;
380 }
381 }
382 }
383 } 372 }
384 } 373 }
385 374
386 if (m_cancelled) return MAD_FLOW_STOP; 375 if (m_cancelled) return MAD_FLOW_STOP;
387 376