comparison data/fileio/OggVorbisFileReader.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 #ifdef HAVE_OGGZ 16 #ifdef HAVE_OGGZ
17 #ifdef HAVE_FISHSOUND 17 #ifdef HAVE_FISHSOUND
18 18
19 #include "OggVorbisFileReader.h" 19 #include "OggVorbisFileReader.h"
20 #include "ProgressPrinter.h" 20
21 21 #include "base/ProgressReporter.h"
22 #include "base/Profiler.h" 22 #include "base/Profiler.h"
23 #include "system/System.h" 23 #include "system/System.h"
24 24
25 #include <sys/types.h> 25 #include <sys/types.h>
26 #include <sys/stat.h> 26 #include <sys/stat.h>
27 #include <sys/mman.h> 27 #include <sys/mman.h>
28 #include <fcntl.h> 28 #include <fcntl.h>
29 #include <cmath> 29 #include <cmath>
30 30
31 #include <QApplication>
32 #include <QFileInfo> 31 #include <QFileInfo>
33 #include <QProgressDialog>
34 32
35 static int instances = 0; 33 static int instances = 0;
36 34
37 OggVorbisFileReader::OggVorbisFileReader(FileSource source, 35 OggVorbisFileReader::OggVorbisFileReader(FileSource source,
38 DecodeMode decodeMode, 36 DecodeMode decodeMode,
39 CacheMode mode, 37 CacheMode mode,
40 size_t targetRate) : 38 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_progress(0), 43 m_reporter(reporter),
45 m_fileSize(0), 44 m_fileSize(0),
46 m_bytesRead(0), 45 m_bytesRead(0),
47 m_commentsRead(false), 46 m_commentsRead(false),
48 m_cancelled(false), 47 m_cancelled(false),
49 m_completion(0), 48 m_completion(0),
70 fish_sound_set_decoded_callback(m_fishSound, acceptFrames, this); 69 fish_sound_set_decoded_callback(m_fishSound, acceptFrames, this);
71 oggz_set_read_callback(m_oggz, -1, readPacket, this); 70 oggz_set_read_callback(m_oggz, -1, readPacket, this);
72 71
73 if (decodeMode == DecodeAtOnce) { 72 if (decodeMode == DecodeAtOnce) {
74 73
75 if (dynamic_cast<QApplication *>(QCoreApplication::instance())) { 74 if (m_reporter) {
76 m_progress = new QProgressDialog 75 connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled()));
77 (QObject::tr("Decoding %1...").arg(QFileInfo(m_path).fileName()), 76 m_reporter->setMessage
78 QObject::tr("Stop"), 0, 100); 77 (tr("Decoding %1...").arg(QFileInfo(m_path).fileName()));
79 m_progress->hide();
80 } else {
81 ProgressPrinter *pp = new ProgressPrinter(tr("Decoding..."), this);
82 connect(this, SIGNAL(progress(int)), pp, SLOT(progress(int)));
83 } 78 }
84 79
85 while (oggz_read(m_oggz, 1024) > 0); 80 while (oggz_read(m_oggz, 1024) > 0);
86 81
87 fish_sound_delete(m_fishSound); 82 fish_sound_delete(m_fishSound);
89 oggz_close(m_oggz); 84 oggz_close(m_oggz);
90 m_oggz = 0; 85 m_oggz = 0;
91 86
92 if (isDecodeCacheInitialised()) finishDecodeCache(); 87 if (isDecodeCacheInitialised()) finishDecodeCache();
93 88
94 delete m_progress;
95 m_progress = 0;
96
97 } else { 89 } else {
90
91 if (m_reporter) m_reporter->setProgress(100);
98 92
99 while (oggz_read(m_oggz, 1024) > 0 && 93 while (oggz_read(m_oggz, 1024) > 0 &&
100 (m_channelCount == 0 || m_fileRate == 0 || m_sampleRate == 0)); 94 (m_channelCount == 0 || m_fileRate == 0 || m_sampleRate == 0));
101 95
102 if (m_channelCount > 0) { 96 if (m_channelCount > 0) {
112 if (m_decodeThread) { 106 if (m_decodeThread) {
113 m_cancelled = true; 107 m_cancelled = true;
114 m_decodeThread->wait(); 108 m_decodeThread->wait();
115 delete m_decodeThread; 109 delete m_decodeThread;
116 } 110 }
111 }
112
113 void
114 OggVorbisFileReader::cancelled()
115 {
116 m_cancelled = true;
117 } 117 }
118 118
119 void 119 void
120 OggVorbisFileReader::DecodeThread::run() 120 OggVorbisFileReader::DecodeThread::run()
121 { 121 {
154 double(reader->m_fileSize)); 154 double(reader->m_fileSize));
155 if (p > 99) p = 99; 155 if (p > 99) p = 99;
156 reader->m_completion = p; 156 reader->m_completion = p;
157 reader->progress(p); 157 reader->progress(p);
158 158
159 if (reader->m_fileSize > 0 && reader->m_progress) { 159 if (reader->m_fileSize > 0 && reader->m_reporter) {
160 if (p > reader->m_progress->value()) { 160 reader->m_reporter->setProgress(p);
161 reader->m_progress->setValue(p);
162 reader->m_progress->show();
163 reader->m_progress->raise();
164 qApp->processEvents();
165 if (reader->m_progress->wasCanceled()) {
166 reader->m_cancelled = true;
167 }
168 }
169 } 161 }
170 162
171 if (reader->m_cancelled) return 1; 163 if (reader->m_cancelled) return 1;
172 return 0; 164 return 0;
173 } 165 }