Chris@386
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@386
|
2
|
Chris@386
|
3 /*
|
Chris@386
|
4 Sonic Visualiser
|
Chris@386
|
5 An audio file viewer and annotation editor.
|
Chris@386
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@386
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@386
|
8
|
Chris@386
|
9 This program is free software; you can redistribute it and/or
|
Chris@386
|
10 modify it under the terms of the GNU General Public License as
|
Chris@386
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@386
|
12 License, or (at your option) any later version. See the file
|
Chris@386
|
13 COPYING included with this distribution for more information.
|
Chris@386
|
14 */
|
Chris@386
|
15
|
Chris@386
|
16 #include "AudioFileReaderFactory.h"
|
Chris@386
|
17
|
Chris@386
|
18 #include "WavFileReader.h"
|
Chris@386
|
19 #include "ResamplingWavFileReader.h"
|
Chris@386
|
20 #include "OggVorbisFileReader.h"
|
Chris@386
|
21 #include "MP3FileReader.h"
|
Chris@386
|
22 #include "QuickTimeFileReader.h"
|
luisf@665
|
23 #include "CoreAudioFileReader.h"
|
Chris@386
|
24
|
Chris@386
|
25 #include <QString>
|
Chris@386
|
26 #include <QFileInfo>
|
Chris@386
|
27 #include <iostream>
|
Chris@386
|
28
|
Chris@386
|
29 QString
|
Chris@386
|
30 AudioFileReaderFactory::getKnownExtensions()
|
Chris@386
|
31 {
|
Chris@386
|
32 std::set<QString> extensions;
|
Chris@386
|
33
|
Chris@386
|
34 WavFileReader::getSupportedExtensions(extensions);
|
Chris@386
|
35 #ifdef HAVE_MAD
|
Chris@386
|
36 MP3FileReader::getSupportedExtensions(extensions);
|
Chris@386
|
37 #endif
|
Chris@386
|
38 #ifdef HAVE_OGGZ
|
Chris@386
|
39 #ifdef HAVE_FISHSOUND
|
Chris@386
|
40 OggVorbisFileReader::getSupportedExtensions(extensions);
|
Chris@386
|
41 #endif
|
Chris@386
|
42 #endif
|
Chris@386
|
43 #ifdef HAVE_QUICKTIME
|
Chris@386
|
44 QuickTimeFileReader::getSupportedExtensions(extensions);
|
Chris@386
|
45 #endif
|
luisf@665
|
46 #ifdef HAVE_COREAUDIO
|
luisf@665
|
47 CoreAudioFileReader::getSupportedExtensions(extensions);
|
luisf@665
|
48 #endif
|
Chris@386
|
49
|
Chris@386
|
50 QString rv;
|
Chris@386
|
51 for (std::set<QString>::const_iterator i = extensions.begin();
|
Chris@386
|
52 i != extensions.end(); ++i) {
|
Chris@386
|
53 if (i != extensions.begin()) rv += " ";
|
Chris@386
|
54 rv += "*." + *i;
|
Chris@386
|
55 }
|
Chris@386
|
56
|
Chris@386
|
57 return rv;
|
Chris@386
|
58 }
|
Chris@386
|
59
|
Chris@386
|
60 AudioFileReader *
|
Chris@392
|
61 AudioFileReaderFactory::createReader(FileSource source, size_t targetRate,
|
Chris@392
|
62 ProgressReporter *reporter)
|
Chris@386
|
63 {
|
Chris@392
|
64 return create(source, targetRate, false, reporter);
|
Chris@386
|
65 }
|
Chris@386
|
66
|
Chris@386
|
67 AudioFileReader *
|
Chris@392
|
68 AudioFileReaderFactory::createThreadingReader(FileSource source, size_t targetRate,
|
Chris@392
|
69 ProgressReporter *reporter)
|
Chris@386
|
70 {
|
Chris@392
|
71 return create(source, targetRate, true, reporter);
|
Chris@386
|
72 }
|
Chris@386
|
73
|
Chris@386
|
74 AudioFileReader *
|
Chris@392
|
75 AudioFileReaderFactory::create(FileSource source, size_t targetRate, bool threading,
|
Chris@392
|
76 ProgressReporter *reporter)
|
Chris@386
|
77 {
|
Chris@386
|
78 QString err;
|
Chris@386
|
79
|
luisf@665
|
80 std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\"): Requested rate: " << targetRate << std::endl;
|
Chris@386
|
81
|
Chris@386
|
82 if (!source.isOK() || !source.isAvailable()) {
|
Chris@386
|
83 std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\": Source unavailable" << std::endl;
|
Chris@386
|
84 return 0;
|
Chris@386
|
85 }
|
Chris@386
|
86
|
Chris@386
|
87 AudioFileReader *reader = 0;
|
Chris@386
|
88
|
Chris@386
|
89 // Try to construct a preferred reader based on the extension or
|
Chris@386
|
90 // MIME type.
|
Chris@386
|
91
|
Chris@386
|
92 if (WavFileReader::supports(source)) {
|
Chris@386
|
93
|
Chris@386
|
94 reader = new WavFileReader(source);
|
Chris@386
|
95
|
Chris@386
|
96 if (targetRate != 0 &&
|
Chris@386
|
97 reader->isOK() &&
|
Chris@386
|
98 reader->getSampleRate() != targetRate) {
|
Chris@386
|
99
|
Chris@386
|
100 std::cerr << "AudioFileReaderFactory::createReader: WAV file rate: " << reader->getSampleRate() << ", creating resampling reader" << std::endl;
|
Chris@386
|
101
|
Chris@386
|
102 delete reader;
|
Chris@386
|
103 reader = new ResamplingWavFileReader
|
Chris@386
|
104 (source,
|
Chris@386
|
105 threading ?
|
Chris@386
|
106 ResamplingWavFileReader::ResampleThreaded :
|
Chris@386
|
107 ResamplingWavFileReader::ResampleAtOnce,
|
Chris@386
|
108 ResamplingWavFileReader::CacheInTemporaryFile,
|
Chris@392
|
109 targetRate,
|
Chris@392
|
110 reporter);
|
Chris@440
|
111 if (!reader->isOK()) {
|
Chris@440
|
112 delete reader;
|
Chris@440
|
113 reader = 0;
|
Chris@440
|
114 }
|
Chris@386
|
115 }
|
Chris@386
|
116 }
|
Chris@386
|
117
|
Chris@386
|
118 #ifdef HAVE_OGGZ
|
Chris@386
|
119 #ifdef HAVE_FISHSOUND
|
Chris@386
|
120 if (!reader) {
|
Chris@386
|
121 if (OggVorbisFileReader::supports(source)) {
|
Chris@386
|
122 reader = new OggVorbisFileReader
|
Chris@386
|
123 (source,
|
Chris@386
|
124 threading ?
|
Chris@386
|
125 OggVorbisFileReader::DecodeThreaded :
|
Chris@386
|
126 OggVorbisFileReader::DecodeAtOnce,
|
Chris@386
|
127 OggVorbisFileReader::CacheInTemporaryFile,
|
Chris@392
|
128 targetRate,
|
Chris@392
|
129 reporter);
|
Chris@440
|
130 if (!reader->isOK()) {
|
Chris@440
|
131 delete reader;
|
Chris@440
|
132 reader = 0;
|
Chris@440
|
133 }
|
Chris@386
|
134 }
|
Chris@386
|
135 }
|
Chris@386
|
136 #endif
|
Chris@386
|
137 #endif
|
Chris@386
|
138
|
Chris@386
|
139 #ifdef HAVE_MAD
|
Chris@386
|
140 if (!reader) {
|
Chris@386
|
141 if (MP3FileReader::supports(source)) {
|
Chris@386
|
142 reader = new MP3FileReader
|
Chris@386
|
143 (source,
|
Chris@386
|
144 threading ?
|
Chris@386
|
145 MP3FileReader::DecodeThreaded :
|
Chris@386
|
146 MP3FileReader::DecodeAtOnce,
|
Chris@386
|
147 MP3FileReader::CacheInTemporaryFile,
|
Chris@392
|
148 targetRate,
|
Chris@392
|
149 reporter);
|
Chris@440
|
150 if (!reader->isOK()) {
|
Chris@440
|
151 delete reader;
|
Chris@440
|
152 reader = 0;
|
Chris@440
|
153 }
|
Chris@386
|
154 }
|
Chris@386
|
155 }
|
Chris@386
|
156 #endif
|
Chris@386
|
157
|
Chris@386
|
158 #ifdef HAVE_QUICKTIME
|
Chris@386
|
159 if (!reader) {
|
Chris@386
|
160 if (QuickTimeFileReader::supports(source)) {
|
Chris@386
|
161 reader = new QuickTimeFileReader
|
Chris@386
|
162 (source,
|
Chris@386
|
163 threading ?
|
Chris@386
|
164 QuickTimeFileReader::DecodeThreaded :
|
Chris@386
|
165 QuickTimeFileReader::DecodeAtOnce,
|
Chris@386
|
166 QuickTimeFileReader::CacheInTemporaryFile,
|
Chris@392
|
167 targetRate,
|
Chris@392
|
168 reporter);
|
Chris@440
|
169 if (!reader->isOK()) {
|
Chris@440
|
170 delete reader;
|
Chris@440
|
171 reader = 0;
|
Chris@440
|
172 }
|
Chris@440
|
173 }
|
Chris@440
|
174 }
|
Chris@440
|
175 #endif
|
Chris@440
|
176
|
luisf@665
|
177 #ifdef HAVE_COREAUDIO
|
luisf@665
|
178 if (!reader) {
|
luisf@665
|
179 if (CoreAudioFileReader::supports(source)) {
|
luisf@665
|
180 reader = new CoreAudioFileReader
|
luisf@665
|
181 (source,
|
luisf@665
|
182 threading ?
|
luisf@665
|
183 CoreAudioFileReader::DecodeThreaded :
|
luisf@665
|
184 CoreAudioFileReader::DecodeAtOnce,
|
luisf@665
|
185 CoreAudioFileReader::CacheInTemporaryFile,
|
luisf@665
|
186 targetRate,
|
luisf@665
|
187 reporter);
|
luisf@665
|
188 if (!reader->isOK()) {
|
luisf@665
|
189 delete reader;
|
luisf@665
|
190 reader = 0;
|
luisf@665
|
191 }
|
luisf@665
|
192 }
|
luisf@665
|
193 }
|
luisf@665
|
194 #endif
|
luisf@665
|
195
|
luisf@665
|
196
|
Chris@440
|
197 // If none of the readers claimed to support this file extension,
|
Chris@440
|
198 // perhaps the extension is missing or misleading. Try again,
|
Chris@440
|
199 // ignoring it. We have to be confident that the reader won't
|
Chris@440
|
200 // open just any old text file or whatever and pretend it's
|
Chris@440
|
201 // succeeded
|
Chris@440
|
202
|
Chris@440
|
203 if (!reader) {
|
Chris@440
|
204
|
Chris@440
|
205 reader = new WavFileReader(source);
|
Chris@440
|
206
|
Chris@440
|
207 if (targetRate != 0 &&
|
Chris@440
|
208 reader->isOK() &&
|
Chris@440
|
209 reader->getSampleRate() != targetRate) {
|
Chris@440
|
210
|
Chris@440
|
211 std::cerr << "AudioFileReaderFactory::createReader: WAV file rate: " << reader->getSampleRate() << ", creating resampling reader" << std::endl;
|
Chris@440
|
212
|
Chris@440
|
213 delete reader;
|
Chris@440
|
214 reader = new ResamplingWavFileReader
|
Chris@440
|
215 (source,
|
Chris@440
|
216 threading ?
|
Chris@440
|
217 ResamplingWavFileReader::ResampleThreaded :
|
Chris@440
|
218 ResamplingWavFileReader::ResampleAtOnce,
|
Chris@440
|
219 ResamplingWavFileReader::CacheInTemporaryFile,
|
Chris@440
|
220 targetRate,
|
Chris@440
|
221 reporter);
|
Chris@440
|
222 }
|
Chris@440
|
223
|
Chris@440
|
224 if (!reader->isOK()) {
|
Chris@440
|
225 delete reader;
|
Chris@440
|
226 reader = 0;
|
Chris@440
|
227 }
|
Chris@440
|
228 }
|
Chris@440
|
229
|
Chris@440
|
230 #ifdef HAVE_OGGZ
|
Chris@440
|
231 #ifdef HAVE_FISHSOUND
|
Chris@440
|
232 if (!reader) {
|
Chris@440
|
233 reader = new OggVorbisFileReader
|
Chris@440
|
234 (source,
|
Chris@440
|
235 threading ?
|
Chris@440
|
236 OggVorbisFileReader::DecodeThreaded :
|
Chris@440
|
237 OggVorbisFileReader::DecodeAtOnce,
|
Chris@440
|
238 OggVorbisFileReader::CacheInTemporaryFile,
|
Chris@440
|
239 targetRate,
|
Chris@440
|
240 reporter);
|
Chris@440
|
241
|
Chris@440
|
242 if (!reader->isOK()) {
|
Chris@440
|
243 delete reader;
|
Chris@440
|
244 reader = 0;
|
Chris@440
|
245 }
|
Chris@440
|
246 }
|
Chris@440
|
247 #endif
|
Chris@440
|
248 #endif
|
Chris@440
|
249
|
Chris@440
|
250 #ifdef HAVE_MAD
|
Chris@440
|
251 if (!reader) {
|
Chris@440
|
252 reader = new MP3FileReader
|
Chris@440
|
253 (source,
|
Chris@440
|
254 threading ?
|
Chris@440
|
255 MP3FileReader::DecodeThreaded :
|
Chris@440
|
256 MP3FileReader::DecodeAtOnce,
|
Chris@440
|
257 MP3FileReader::CacheInTemporaryFile,
|
Chris@440
|
258 targetRate,
|
Chris@440
|
259 reporter);
|
Chris@440
|
260
|
Chris@440
|
261 if (!reader->isOK()) {
|
Chris@440
|
262 delete reader;
|
Chris@440
|
263 reader = 0;
|
Chris@440
|
264 }
|
Chris@440
|
265 }
|
Chris@440
|
266 #endif
|
Chris@440
|
267
|
Chris@440
|
268 #ifdef HAVE_QUICKTIME
|
Chris@440
|
269 if (!reader) {
|
Chris@440
|
270 reader = new QuickTimeFileReader
|
Chris@440
|
271 (source,
|
Chris@440
|
272 threading ?
|
Chris@440
|
273 QuickTimeFileReader::DecodeThreaded :
|
Chris@440
|
274 QuickTimeFileReader::DecodeAtOnce,
|
Chris@440
|
275 QuickTimeFileReader::CacheInTemporaryFile,
|
Chris@440
|
276 targetRate,
|
Chris@440
|
277 reporter);
|
Chris@440
|
278
|
Chris@440
|
279 if (!reader->isOK()) {
|
Chris@440
|
280 delete reader;
|
Chris@440
|
281 reader = 0;
|
Chris@386
|
282 }
|
Chris@386
|
283 }
|
Chris@386
|
284 #endif
|
Chris@386
|
285
|
luisf@665
|
286 #ifdef HAVE_COREAUDIO
|
luisf@665
|
287
|
luisf@665
|
288 std::cerr << "AudioFileReaderFactory: HAVE_COREAUDIO" << std::endl;
|
luisf@665
|
289
|
luisf@665
|
290 if (!reader) {
|
luisf@665
|
291 reader = new CoreAudioFileReader
|
luisf@665
|
292 (source,
|
luisf@665
|
293 threading ?
|
luisf@665
|
294 CoreAudioFileReader::DecodeThreaded :
|
luisf@665
|
295 CoreAudioFileReader::DecodeAtOnce,
|
luisf@665
|
296 CoreAudioFileReader::CacheInTemporaryFile,
|
luisf@665
|
297 targetRate,
|
luisf@665
|
298 reporter);
|
luisf@665
|
299
|
luisf@665
|
300 if (!reader->isOK()) {
|
luisf@665
|
301 delete reader;
|
luisf@665
|
302 reader = 0;
|
luisf@665
|
303 }
|
luisf@665
|
304 }
|
luisf@665
|
305 #endif
|
luisf@665
|
306
|
Chris@386
|
307 if (reader) {
|
Chris@386
|
308 if (reader->isOK()) {
|
luisf@665
|
309 std::cerr << "AudioFileReaderFactory: Reader is OK" << std::endl;
|
Chris@386
|
310 return reader;
|
Chris@386
|
311 }
|
Chris@386
|
312 std::cerr << "AudioFileReaderFactory: Preferred reader for "
|
Chris@386
|
313 << "url \"" << source.getLocation().toStdString()
|
Chris@386
|
314 << "\" (content type \""
|
Chris@386
|
315 << source.getContentType().toStdString() << "\") failed";
|
Chris@386
|
316
|
Chris@386
|
317 if (reader->getError() != "") {
|
Chris@386
|
318 std::cerr << ": \"" << reader->getError().toStdString() << "\"";
|
Chris@386
|
319 }
|
Chris@386
|
320 std::cerr << std::endl;
|
Chris@386
|
321 delete reader;
|
Chris@386
|
322 reader = 0;
|
Chris@386
|
323 }
|
Chris@386
|
324
|
Chris@386
|
325 std::cerr << "AudioFileReaderFactory: No reader" << std::endl;
|
Chris@386
|
326 return reader;
|
Chris@386
|
327 }
|
Chris@386
|
328
|