comparison data/fileio/MP3FileReader.cpp @ 316:3a6725f285d6

* Make RemoteFile far more pervasive, and use it for local files as well so that we can handle both transparently. Make it shallow copy with reference counting, so it can be used by value without having to worry about the cache file lifetime. Use RemoteFile for MainWindow file-open functions, etc
author Chris Cannam
date Thu, 18 Oct 2007 15:31:20 +0000
parents c022976d18e8
children c324d410b096
comparison
equal deleted inserted replaced
315:96ef9746c560 316:3a6725f285d6
32 32
33 #include <QApplication> 33 #include <QApplication>
34 #include <QFileInfo> 34 #include <QFileInfo>
35 #include <QProgressDialog> 35 #include <QProgressDialog>
36 36
37 MP3FileReader::MP3FileReader(QString path, DecodeMode decodeMode, 37 MP3FileReader::MP3FileReader(RemoteFile source, DecodeMode decodeMode,
38 CacheMode mode, size_t targetRate) : 38 CacheMode mode, size_t targetRate) :
39 CodedAudioFileReader(mode, targetRate), 39 CodedAudioFileReader(mode, targetRate),
40 m_path(path), 40 m_source(source),
41 m_path(source.getLocalFilename()),
41 m_decodeThread(0) 42 m_decodeThread(0)
42 { 43 {
43 m_channelCount = 0; 44 m_channelCount = 0;
44 m_fileRate = 0; 45 m_fileRate = 0;
45 m_fileSize = 0; 46 m_fileSize = 0;
49 m_completion = 0; 50 m_completion = 0;
50 m_done = false; 51 m_done = false;
51 m_progress = 0; 52 m_progress = 0;
52 53
53 struct stat stat; 54 struct stat stat;
54 if (::stat(path.toLocal8Bit().data(), &stat) == -1 || stat.st_size == 0) { 55 if (::stat(m_path.toLocal8Bit().data(), &stat) == -1 || stat.st_size == 0) {
55 m_error = QString("File %1 does not exist.").arg(path); 56 m_error = QString("File %1 does not exist.").arg(m_path);
56 return; 57 return;
57 } 58 }
58 59
59 m_fileSize = stat.st_size; 60 m_fileSize = stat.st_size;
60 61
61 int fd = -1; 62 int fd = -1;
62 if ((fd = ::open(path.toLocal8Bit().data(), O_RDONLY 63 if ((fd = ::open(m_path.toLocal8Bit().data(), O_RDONLY
63 #ifdef _WIN32 64 #ifdef _WIN32
64 | O_BINARY 65 | O_BINARY
65 #endif 66 #endif
66 , 0)) < 0) { 67 , 0)) < 0) {
67 m_error = QString("Failed to open file %1 for reading.").arg(path); 68 m_error = QString("Failed to open file %1 for reading.").arg(m_path);
68 return; 69 return;
69 } 70 }
70 71
71 m_filebuffer = 0; 72 m_filebuffer = 0;
72 m_samplebuffer = 0; 73 m_samplebuffer = 0;
84 size_t offset = 0; 85 size_t offset = 0;
85 while (offset < m_fileSize) { 86 while (offset < m_fileSize) {
86 sz = ::read(fd, m_filebuffer + offset, m_fileSize - offset); 87 sz = ::read(fd, m_filebuffer + offset, m_fileSize - offset);
87 if (sz < 0) { 88 if (sz < 0) {
88 m_error = QString("Read error for file %1 (after %2 bytes)") 89 m_error = QString("Read error for file %1 (after %2 bytes)")
89 .arg(path).arg(offset); 90 .arg(m_path).arg(offset);
90 delete[] m_filebuffer; 91 delete[] m_filebuffer;
91 ::close(fd); 92 ::close(fd);
92 return; 93 return;
93 } else if (sz == 0) { 94 } else if (sz == 0) {
94 std::cerr << QString("MP3FileReader::MP3FileReader: Warning: reached EOF after only %1 of %2 bytes") 95 std::cerr << QString("MP3FileReader::MP3FileReader: Warning: reached EOF after only %1 of %2 bytes")
104 loadTags(); 105 loadTags();
105 106
106 if (decodeMode == DecodeAtOnce) { 107 if (decodeMode == DecodeAtOnce) {
107 108
108 m_progress = new QProgressDialog 109 m_progress = new QProgressDialog
109 (QObject::tr("Decoding %1...").arg(QFileInfo(path).fileName()), 110 (QObject::tr("Decoding %1...").arg(QFileInfo(m_path).fileName()),
110 QObject::tr("Stop"), 0, 100); 111 QObject::tr("Stop"), 0, 100);
111 m_progress->hide(); 112 m_progress->hide();
112 113
113 if (!decode(m_filebuffer, m_fileSize)) { 114 if (!decode(m_filebuffer, m_fileSize)) {
114 m_error = QString("Failed to decode file %1.").arg(path); 115 m_error = QString("Failed to decode file %1.").arg(m_path);
115 } 116 }
116 117
117 delete[] m_filebuffer; 118 delete[] m_filebuffer;
118 m_filebuffer = 0; 119 m_filebuffer = 0;
119 120
392 MP3FileReader::getSupportedExtensions(std::set<QString> &extensions) 393 MP3FileReader::getSupportedExtensions(std::set<QString> &extensions)
393 { 394 {
394 extensions.insert("mp3"); 395 extensions.insert("mp3");
395 } 396 }
396 397
397 #endif 398 bool
399 MP3FileReader::supportsExtension(QString extension)
400 {
401 std::set<QString> extensions;
402 getSupportedExtensions(extensions);
403 return (extensions.find(extension.toLower()) != extensions.end());
404 }
405
406 bool
407 MP3FileReader::supportsContentType(QString type)
408 {
409 return (type == "audio/mpeg");
410 }
411
412 bool
413 MP3FileReader::supports(RemoteFile &source)
414 {
415 return (supportsExtension(source.getExtension()) ||
416 supportsContentType(source.getContentType()));
417 }
418
419
420 #endif