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@823
|
19 #include "DecodingWavFileReader.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@920
|
61 AudioFileReaderFactory::createReader(FileSource source,
|
Chris@1040
|
62 sv_samplerate_t targetRate,
|
Chris@920
|
63 bool normalised,
|
Chris@392
|
64 ProgressReporter *reporter)
|
Chris@386
|
65 {
|
Chris@920
|
66 return create(source, targetRate, normalised, false, reporter);
|
Chris@386
|
67 }
|
Chris@386
|
68
|
Chris@386
|
69 AudioFileReader *
|
Chris@920
|
70 AudioFileReaderFactory::createThreadingReader(FileSource source,
|
Chris@1040
|
71 sv_samplerate_t targetRate,
|
Chris@920
|
72 bool normalised,
|
Chris@392
|
73 ProgressReporter *reporter)
|
Chris@386
|
74 {
|
Chris@920
|
75 return create(source, targetRate, normalised, true, reporter);
|
Chris@386
|
76 }
|
Chris@386
|
77
|
Chris@386
|
78 AudioFileReader *
|
Chris@920
|
79 AudioFileReaderFactory::create(FileSource source,
|
Chris@1040
|
80 sv_samplerate_t targetRate,
|
Chris@920
|
81 bool normalised,
|
Chris@920
|
82 bool threading,
|
Chris@392
|
83 ProgressReporter *reporter)
|
Chris@386
|
84 {
|
Chris@386
|
85 QString err;
|
Chris@386
|
86
|
Chris@742
|
87 SVDEBUG << "AudioFileReaderFactory::createReader(\"" << source.getLocation() << "\"): Requested rate: " << targetRate << endl;
|
Chris@386
|
88
|
Chris@667
|
89 if (!source.isOK()) {
|
Chris@843
|
90 cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation() << "\": Failed to retrieve source (transmission error?): " << source.getErrorString() << endl;
|
Chris@667
|
91 return 0;
|
Chris@667
|
92 }
|
Chris@667
|
93
|
Chris@667
|
94 if (!source.isAvailable()) {
|
Chris@690
|
95 SVDEBUG << "AudioFileReaderFactory::createReader(\"" << source.getLocation() << "\": Source not found" << endl;
|
Chris@386
|
96 return 0;
|
Chris@386
|
97 }
|
Chris@386
|
98
|
Chris@386
|
99 AudioFileReader *reader = 0;
|
Chris@386
|
100
|
Chris@1097
|
101 CodedAudioFileReader::CacheMode cacheMode =
|
Chris@1097
|
102 CodedAudioFileReader::CacheInTemporaryFile;
|
Chris@1097
|
103
|
Chris@1097
|
104 CodedAudioFileReader::DecodeMode decodeMode =
|
Chris@1097
|
105 (threading ?
|
Chris@1097
|
106 CodedAudioFileReader::DecodeThreaded :
|
Chris@1097
|
107 CodedAudioFileReader::DecodeAtOnce);
|
Chris@1097
|
108
|
Chris@386
|
109 // Try to construct a preferred reader based on the extension or
|
Chris@386
|
110 // MIME type.
|
Chris@386
|
111
|
Chris@1097
|
112 #define CHECK(reader) if (!reader->isOK()) { delete reader; reader = 0; }
|
Chris@1097
|
113
|
Chris@386
|
114 if (WavFileReader::supports(source)) {
|
Chris@386
|
115
|
Chris@386
|
116 reader = new WavFileReader(source);
|
Chris@386
|
117
|
Chris@1040
|
118 sv_samplerate_t fileRate = reader->getSampleRate();
|
Chris@386
|
119
|
Chris@823
|
120 if (reader->isOK() &&
|
Chris@823
|
121 (!reader->isQuicklySeekable() ||
|
Chris@920
|
122 normalised ||
|
Chris@823
|
123 (targetRate != 0 && fileRate != targetRate))) {
|
Chris@823
|
124
|
Chris@920
|
125 SVDEBUG << "AudioFileReaderFactory::createReader: WAV file rate: " << reader->getSampleRate() << ", normalised " << normalised << ", seekable " << reader->isQuicklySeekable() << ", creating decoding reader" << endl;
|
Chris@386
|
126
|
Chris@386
|
127 delete reader;
|
Chris@823
|
128 reader = new DecodingWavFileReader
|
Chris@386
|
129 (source,
|
Chris@1097
|
130 decodeMode, cacheMode,
|
Chris@823
|
131 targetRate ? targetRate : fileRate,
|
Chris@920
|
132 normalised,
|
Chris@392
|
133 reporter);
|
Chris@1097
|
134 CHECK(reader);
|
Chris@386
|
135 }
|
Chris@386
|
136 }
|
Chris@386
|
137
|
Chris@386
|
138 #ifdef HAVE_OGGZ
|
Chris@386
|
139 #ifdef HAVE_FISHSOUND
|
Chris@1097
|
140 if (!reader && OggVorbisFileReader::supports(source)) {
|
Chris@1097
|
141 reader = new OggVorbisFileReader
|
Chris@1097
|
142 (source, decodeMode, cacheMode, targetRate, normalised, reporter);
|
Chris@1097
|
143 CHECK(reader);
|
Chris@386
|
144 }
|
Chris@386
|
145 #endif
|
Chris@386
|
146 #endif
|
Chris@386
|
147
|
Chris@386
|
148 #ifdef HAVE_MAD
|
Chris@1097
|
149 if (!reader && MP3FileReader::supports(source)) {
|
Chris@1097
|
150 reader = new MP3FileReader
|
Chris@1097
|
151 (source, decodeMode, cacheMode, targetRate, normalised, reporter);
|
Chris@1097
|
152 CHECK(reader);
|
Chris@386
|
153 }
|
Chris@386
|
154 #endif
|
Chris@386
|
155
|
Chris@386
|
156 #ifdef HAVE_QUICKTIME
|
Chris@1097
|
157 if (!reader && QuickTimeFileReader::supports(source)) {
|
Chris@1097
|
158 reader = new QuickTimeFileReader
|
Chris@1097
|
159 (source, decodeMode, cacheMode, targetRate, normalised, reporter);
|
Chris@1097
|
160 CHECK(reader);
|
Chris@440
|
161 }
|
Chris@440
|
162 #endif
|
Chris@440
|
163
|
luisf@665
|
164 #ifdef HAVE_COREAUDIO
|
Chris@1097
|
165 if (!reader && CoreAudioFileReader::supports(source)) {
|
Chris@1097
|
166 reader = new CoreAudioFileReader
|
Chris@1097
|
167 (source, decodeMode, cacheMode, targetRate, normalised, reporter);
|
Chris@1097
|
168 CHECK(reader);
|
luisf@665
|
169 }
|
luisf@665
|
170 #endif
|
luisf@665
|
171
|
Chris@1097
|
172 if (reader) {
|
Chris@1097
|
173 // The happy case: a reader recognised the file extension &
|
Chris@1097
|
174 // succeeded in opening the file
|
Chris@1097
|
175 return reader;
|
Chris@1097
|
176 }
|
Chris@1097
|
177
|
Chris@440
|
178 // If none of the readers claimed to support this file extension,
|
Chris@440
|
179 // perhaps the extension is missing or misleading. Try again,
|
Chris@440
|
180 // ignoring it. We have to be confident that the reader won't
|
Chris@440
|
181 // open just any old text file or whatever and pretend it's
|
Chris@440
|
182 // succeeded
|
Chris@440
|
183
|
Chris@1097
|
184 reader = new WavFileReader(source);
|
Chris@440
|
185
|
Chris@1097
|
186 sv_samplerate_t fileRate = reader->getSampleRate();
|
Chris@440
|
187
|
Chris@1097
|
188 if (reader->isOK() &&
|
Chris@1097
|
189 (!reader->isQuicklySeekable() ||
|
Chris@1097
|
190 normalised ||
|
Chris@1097
|
191 (targetRate != 0 && fileRate != targetRate))) {
|
Chris@440
|
192
|
Chris@1097
|
193 SVDEBUG << "AudioFileReaderFactory::createReader: WAV file rate: " << reader->getSampleRate() << ", normalised " << normalised << ", seekable " << reader->isQuicklySeekable() << ", creating decoding reader" << endl;
|
Chris@823
|
194
|
Chris@1097
|
195 delete reader;
|
Chris@1097
|
196 reader = new DecodingWavFileReader
|
Chris@1097
|
197 (source,
|
Chris@1097
|
198 decodeMode, cacheMode,
|
Chris@1097
|
199 targetRate ? targetRate : fileRate,
|
Chris@1097
|
200 normalised,
|
Chris@1097
|
201 reporter);
|
Chris@1097
|
202 }
|
Chris@440
|
203
|
Chris@1097
|
204 CHECK(reader);
|
Chris@440
|
205
|
Chris@440
|
206 #ifdef HAVE_OGGZ
|
Chris@440
|
207 #ifdef HAVE_FISHSOUND
|
Chris@440
|
208 if (!reader) {
|
Chris@440
|
209 reader = new OggVorbisFileReader
|
Chris@1097
|
210 (source, decodeMode, cacheMode, targetRate, normalised, reporter);
|
Chris@1097
|
211 CHECK(reader);
|
Chris@440
|
212 }
|
Chris@440
|
213 #endif
|
Chris@440
|
214 #endif
|
Chris@440
|
215
|
Chris@440
|
216 #ifdef HAVE_MAD
|
Chris@440
|
217 if (!reader) {
|
Chris@440
|
218 reader = new MP3FileReader
|
Chris@1097
|
219 (source, decodeMode, cacheMode, targetRate, normalised, reporter);
|
Chris@1097
|
220 CHECK(reader);
|
Chris@440
|
221 }
|
Chris@440
|
222 #endif
|
Chris@440
|
223
|
Chris@440
|
224 #ifdef HAVE_QUICKTIME
|
Chris@440
|
225 if (!reader) {
|
Chris@440
|
226 reader = new QuickTimeFileReader
|
Chris@1097
|
227 (source, decodeMode, cacheMode, targetRate, normalised, reporter);
|
Chris@1097
|
228 CHECK(reader);
|
Chris@386
|
229 }
|
Chris@386
|
230 #endif
|
Chris@386
|
231
|
luisf@665
|
232 #ifdef HAVE_COREAUDIO
|
luisf@665
|
233 if (!reader) {
|
luisf@665
|
234 reader = new CoreAudioFileReader
|
Chris@1097
|
235 (source, decodeMode, cacheMode, targetRate, normalised, reporter);
|
Chris@1097
|
236 CHECK(reader);
|
luisf@665
|
237 }
|
luisf@665
|
238 #endif
|
luisf@665
|
239
|
Chris@1097
|
240 if (!reader) {
|
Chris@1097
|
241 cerr << "AudioFileReaderFactory::Failed to create a reader for "
|
Chris@1097
|
242 << "url \"" << source.getLocation()
|
Chris@1097
|
243 << "\" (content type \""
|
Chris@1097
|
244 << source.getContentType() << "\")" << endl;
|
Chris@1097
|
245 return nullptr;
|
Chris@386
|
246 }
|
Chris@1097
|
247
|
Chris@386
|
248 return reader;
|
Chris@386
|
249 }
|
Chris@386
|
250
|