comparison data/fileio/MP3FileReader.cpp @ 384:6f6ab834449d spectrogram-cache-rejig

* Merge from trunk
author Chris Cannam
date Wed, 27 Feb 2008 11:59:42 +0000
parents aa8dbac62024
children
comparison
equal deleted inserted replaced
337:a6fab10ff9e6 384:6f6ab834449d
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"
21
20 #include "system/System.h" 22 #include "system/System.h"
21 23
22 #include <sys/types.h> 24 #include <sys/types.h>
23 #include <sys/stat.h> 25 #include <sys/stat.h>
24 #include <fcntl.h> 26 #include <fcntl.h>
109 if (dynamic_cast<QApplication *>(QCoreApplication::instance())) { 111 if (dynamic_cast<QApplication *>(QCoreApplication::instance())) {
110 m_progress = new QProgressDialog 112 m_progress = new QProgressDialog
111 (QObject::tr("Decoding %1...").arg(QFileInfo(m_path).fileName()), 113 (QObject::tr("Decoding %1...").arg(QFileInfo(m_path).fileName()),
112 QObject::tr("Stop"), 0, 100); 114 QObject::tr("Stop"), 0, 100);
113 m_progress->hide(); 115 m_progress->hide();
116 } else {
117 ProgressPrinter *pp = new ProgressPrinter(tr("Decoding..."), this);
118 connect(this, SIGNAL(progress(int)), pp, SLOT(progress(int)));
114 } 119 }
115 120
116 if (!decode(m_filebuffer, m_fileSize)) { 121 if (!decode(m_filebuffer, m_fileSize)) {
117 m_error = QString("Failed to decode file %1.").arg(m_path); 122 m_error = QString("Failed to decode file %1.").arg(m_path);
118 } 123 }
128 } else { 133 } else {
129 134
130 m_decodeThread = new DecodeThread(this); 135 m_decodeThread = new DecodeThread(this);
131 m_decodeThread->start(); 136 m_decodeThread->start();
132 137
133 while (m_channelCount == 0 && !m_done) { 138 while ((m_channelCount == 0 || m_fileRate == 0) && !m_done) {
134 usleep(10); 139 usleep(10);
135 } 140 }
136 } 141 }
137 } 142 }
138 143
204 } 209 }
205 210
206 unsigned int nstrings = id3_field_getnstrings(&frame->fields[1]); 211 unsigned int nstrings = id3_field_getnstrings(&frame->fields[1]);
207 if (nstrings == 0) { 212 if (nstrings == 0) {
208 #ifdef DEBUG_ID3TAG 213 #ifdef DEBUG_ID3TAG
209 std::cerr << "MP3FileReader::loadTags: No data for \"" << name << "\" in ID3 tag" << std::endl; 214 std::cerr << "MP3FileReader::loadTags: No strings for \"" << name << "\" in ID3 tag" << std::endl;
210 #endif 215 #endif
211 return ""; 216 return "";
212 } 217 }
213 218
214 id3_ucs4_t const *ustr = id3_field_getstrings(&frame->fields[1], 0); 219 id3_ucs4_t const *ustr = id3_field_getstrings(&frame->fields[1], 0);
289 MP3FileReader::input(void *dp, struct mad_stream *stream) 294 MP3FileReader::input(void *dp, struct mad_stream *stream)
290 { 295 {
291 DecoderData *data = (DecoderData *)dp; 296 DecoderData *data = (DecoderData *)dp;
292 297
293 if (!data->length) return MAD_FLOW_STOP; 298 if (!data->length) return MAD_FLOW_STOP;
294 mad_stream_buffer(stream, data->start, data->length); 299
300 unsigned char const *start = data->start;
301 unsigned long length = data->length;
302
303 #ifdef HAVE_ID3TAG
304 if (length > ID3_TAG_QUERYSIZE) {
305 int taglen = id3_tag_query(start, ID3_TAG_QUERYSIZE);
306 if (taglen > 0) {
307 // std::cerr << "ID3 tag length to skip: " << taglen << std::endl;
308 start += taglen;
309 length -= taglen;
310 }
311 }
312 #endif
313
314 mad_stream_buffer(stream, start, length);
295 data->length = 0; 315 data->length = 0;
296 316
297 return MAD_FLOW_CONTINUE; 317 return MAD_FLOW_CONTINUE;
298 } 318 }
299 319
326 m_channelCount = channels; 346 m_channelCount = channels;
327 347
328 initialiseDecodeCache(); 348 initialiseDecodeCache();
329 349
330 if (m_cacheMode == CacheInTemporaryFile) { 350 if (m_cacheMode == CacheInTemporaryFile) {
331 m_completion = 1; 351 // m_completion = 1;
332 std::cerr << "MP3FileReader::accept: channel count " << m_channelCount << ", file rate " << m_fileRate << ", about to start serialised section" << std::endl; 352 std::cerr << "MP3FileReader::accept: channel count " << m_channelCount << ", file rate " << m_fileRate << ", about to start serialised section" << std::endl;
333 startSerialised("MP3FileReader::Decode"); 353 startSerialised("MP3FileReader::Decode");
334 } 354 }
335 } 355 }
336 356
337 if (m_bitrateDenom > 0) { 357 if (m_bitrateDenom > 0) {
338 double bitrate = m_bitrateNum / m_bitrateDenom; 358 double bitrate = m_bitrateNum / m_bitrateDenom;
339 double duration = double(m_fileSize * 8) / bitrate; 359 double duration = double(m_fileSize * 8) / bitrate;
340 double elapsed = double(m_frameCount) / m_sampleRate; 360 double elapsed = double(m_frameCount) / m_sampleRate;
341 double percent = ((elapsed * 100.0) / duration); 361 double percent = 100;
342 int progress = int(percent); 362 if (duration > 0.0) percent = ((elapsed * 100.0) / duration);
343 if (progress < 1) progress = 1; 363 int p = int(percent);
344 if (progress > 99) progress = 99; 364 if (p < 1) p = 1;
345 m_completion = progress; 365 if (p > 99) p = 99;
346 if (m_progress) { 366 if (m_completion != p || (m_progress && !m_progress->isVisible())) {
347 if (progress > m_progress->value()) { 367 m_completion = p;
348 m_progress->setValue(progress); 368 emit progress(m_completion);
349 m_progress->show(); 369 if (m_progress) {
350 m_progress->raise(); 370 if (m_completion > m_progress->value()) {
351 qApp->processEvents(); 371 m_progress->setValue(m_completion);
352 if (m_progress->wasCanceled()) { 372 m_progress->show();
353 m_cancelled = true; 373 m_progress->raise();
374 qApp->processEvents();
375 if (m_progress->wasCanceled()) {
376 m_cancelled = true;
377 }
354 } 378 }
355 } 379 }
356 } 380 }
357 } 381 }
358 382