Chris@235
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@235
|
2
|
Chris@235
|
3 /*
|
Chris@235
|
4 Sonic Visualiser
|
Chris@235
|
5 An audio file viewer and annotation editor.
|
Chris@235
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@235
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@235
|
8
|
Chris@235
|
9 This program is free software; you can redistribute it and/or
|
Chris@235
|
10 modify it under the terms of the GNU General Public License as
|
Chris@235
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@235
|
12 License, or (at your option) any later version. See the file
|
Chris@235
|
13 COPYING included with this distribution for more information.
|
Chris@235
|
14 */
|
Chris@235
|
15
|
Chris@235
|
16 #include "AudioFileReaderFactory.h"
|
Chris@235
|
17
|
Chris@235
|
18 #include "WavFileReader.h"
|
Chris@235
|
19 #include "OggVorbisFileReader.h"
|
Chris@235
|
20 #include "MP3FileReader.h"
|
Chris@281
|
21 #include "QuickTimeFileReader.h"
|
Chris@235
|
22
|
Chris@235
|
23 #include <QString>
|
Chris@241
|
24 #include <QFileInfo>
|
Chris@235
|
25 #include <iostream>
|
Chris@235
|
26
|
Chris@285
|
27 std::string
|
Chris@235
|
28 AudioFileReaderFactory::getKnownExtensions()
|
Chris@235
|
29 {
|
Chris@285
|
30 std::set<std::string> extensions;
|
Chris@235
|
31
|
Chris@235
|
32 WavFileReader::getSupportedExtensions(extensions);
|
Chris@235
|
33 #ifdef HAVE_MAD
|
Chris@235
|
34 MP3FileReader::getSupportedExtensions(extensions);
|
Chris@235
|
35 #endif
|
Chris@235
|
36 #ifdef HAVE_OGGZ
|
Chris@235
|
37 #ifdef HAVE_FISHSOUND
|
Chris@235
|
38 OggVorbisFileReader::getSupportedExtensions(extensions);
|
Chris@235
|
39 #endif
|
Chris@235
|
40 #endif
|
Chris@281
|
41 #ifdef HAVE_QUICKTIME
|
Chris@281
|
42 QuickTimeFileReader::getSupportedExtensions(extensions);
|
Chris@281
|
43 #endif
|
Chris@235
|
44
|
Chris@285
|
45 std::string rv;
|
Chris@285
|
46 for (std::set<std::string>::const_iterator i = extensions.begin();
|
Chris@235
|
47 i != extensions.end(); ++i) {
|
Chris@235
|
48 if (i != extensions.begin()) rv += " ";
|
Chris@235
|
49 rv += "*." + *i;
|
Chris@235
|
50 }
|
Chris@235
|
51
|
Chris@235
|
52 return rv;
|
Chris@235
|
53 }
|
Chris@235
|
54
|
Chris@235
|
55 AudioFileReader *
|
Chris@285
|
56 AudioFileReaderFactory::createReader(std::string path)
|
Chris@235
|
57 {
|
Chris@285
|
58 std::string err;
|
Chris@235
|
59
|
Chris@235
|
60 AudioFileReader *reader = 0;
|
Chris@235
|
61
|
Chris@241
|
62 // First try to construct a preferred reader based on the
|
Chris@241
|
63 // extension. If we can't identify one or it fails to load the
|
Chris@241
|
64 // file, fall back to trying all readers in no particular order.
|
Chris@241
|
65
|
Chris@285
|
66 std::string ext;
|
Chris@285
|
67 std::string::size_type idx = path.rfind('.');
|
Chris@285
|
68 if (idx != std::string::npos) {
|
Chris@285
|
69 ext = path.substr(idx + 1);
|
Chris@285
|
70 for (size_t i = 0; i < ext.length(); ++i) {
|
Chris@285
|
71 ext[i] = std::tolower(ext[i]);
|
Chris@285
|
72 }
|
Chris@285
|
73 }
|
Chris@285
|
74
|
Chris@285
|
75 std::set<std::string> extensions;
|
Chris@241
|
76
|
Chris@241
|
77 WavFileReader::getSupportedExtensions(extensions);
|
Chris@241
|
78 if (extensions.find(ext) != extensions.end()) {
|
Chris@241
|
79 reader = new WavFileReader(path);
|
Chris@241
|
80 }
|
Chris@241
|
81
|
Chris@241
|
82 #ifdef HAVE_OGGZ
|
Chris@241
|
83 #ifdef HAVE_FISHSOUND
|
Chris@241
|
84 if (!reader) {
|
Chris@241
|
85 extensions.clear();
|
Chris@241
|
86 OggVorbisFileReader::getSupportedExtensions(extensions);
|
Chris@241
|
87 if (extensions.find(ext) != extensions.end()) {
|
Chris@241
|
88 reader = new OggVorbisFileReader
|
Chris@263
|
89 (path,
|
Chris@265
|
90 OggVorbisFileReader::DecodeThreaded,
|
Chris@263
|
91 OggVorbisFileReader::CacheInTemporaryFile);
|
Chris@241
|
92 }
|
Chris@241
|
93 }
|
Chris@241
|
94 #endif
|
Chris@241
|
95 #endif
|
Chris@241
|
96
|
Chris@281
|
97 #ifdef HAVE_MAD
|
Chris@281
|
98 if (!reader) {
|
Chris@281
|
99 extensions.clear();
|
Chris@281
|
100 MP3FileReader::getSupportedExtensions(extensions);
|
Chris@281
|
101 if (extensions.find(ext) != extensions.end()) {
|
Chris@281
|
102 reader = new MP3FileReader
|
Chris@281
|
103 (path,
|
Chris@281
|
104 MP3FileReader::DecodeThreaded,
|
Chris@281
|
105 MP3FileReader::CacheInTemporaryFile);
|
Chris@281
|
106 }
|
Chris@281
|
107 }
|
Chris@281
|
108 #endif
|
Chris@281
|
109
|
Chris@281
|
110 #ifdef HAVE_QUICKTIME
|
Chris@281
|
111 if (!reader) {
|
Chris@281
|
112 extensions.clear();
|
Chris@281
|
113 QuickTimeFileReader::getSupportedExtensions(extensions);
|
Chris@281
|
114 if (extensions.find(ext) != extensions.end()) {
|
Chris@281
|
115 reader = new QuickTimeFileReader
|
Chris@281
|
116 (path,
|
Chris@281
|
117 QuickTimeFileReader::DecodeThreaded,
|
Chris@281
|
118 QuickTimeFileReader::CacheInTemporaryFile);
|
Chris@281
|
119 }
|
Chris@281
|
120 }
|
Chris@281
|
121 #endif
|
Chris@281
|
122
|
Chris@241
|
123 if (reader) {
|
Chris@241
|
124 if (reader->isOK()) return reader;
|
Chris@241
|
125 if (reader->getError() != "") {
|
Chris@241
|
126 std::cerr << "AudioFileReaderFactory: Preferred reader for "
|
Chris@285
|
127 << "extension \"" << ext << "\" failed: \""
|
Chris@285
|
128 << reader->getError() << "\"" << std::endl;
|
Chris@241
|
129 } else {
|
Chris@241
|
130 std::cerr << "AudioFileReaderFactory: Preferred reader for "
|
Chris@285
|
131 << "extension \"" << ext << "\" failed"
|
Chris@241
|
132 << std::endl;
|
Chris@241
|
133 }
|
Chris@241
|
134 delete reader;
|
Chris@241
|
135 reader = 0;
|
Chris@241
|
136 }
|
Chris@241
|
137
|
Chris@235
|
138 reader = new WavFileReader(path);
|
Chris@235
|
139 if (reader->isOK()) return reader;
|
Chris@241
|
140 if (reader->getError() != "") {
|
Chris@241
|
141 std::cerr << "AudioFileReaderFactory: WAV file reader error: \""
|
Chris@285
|
142 << reader->getError() << "\"" << std::endl;
|
Chris@241
|
143 } else {
|
Chris@241
|
144 std::cerr << "AudioFileReaderFactory: WAV file reader failed"
|
Chris@241
|
145 << std::endl;
|
Chris@241
|
146 }
|
Chris@235
|
147 delete reader;
|
Chris@235
|
148
|
Chris@235
|
149 #ifdef HAVE_OGGZ
|
Chris@235
|
150 #ifdef HAVE_FISHSOUND
|
Chris@241
|
151 reader = new OggVorbisFileReader
|
Chris@263
|
152 (path,
|
Chris@265
|
153 OggVorbisFileReader::DecodeThreaded,
|
Chris@263
|
154 OggVorbisFileReader::CacheInTemporaryFile);
|
Chris@235
|
155 if (reader->isOK()) return reader;
|
Chris@241
|
156 if (reader->getError() != "") {
|
Chris@241
|
157 std::cerr << "AudioFileReaderFactory: Ogg file reader error: \""
|
Chris@285
|
158 << reader->getError() << "\"" << std::endl;
|
Chris@241
|
159 } else {
|
Chris@241
|
160 std::cerr << "AudioFileReaderFactory: Ogg file reader failed"
|
Chris@241
|
161 << std::endl;
|
Chris@241
|
162 }
|
Chris@235
|
163 delete reader;
|
Chris@235
|
164 #endif
|
Chris@235
|
165 #endif
|
Chris@235
|
166
|
Chris@235
|
167 #ifdef HAVE_MAD
|
Chris@241
|
168 reader = new MP3FileReader
|
Chris@263
|
169 (path,
|
Chris@265
|
170 MP3FileReader::DecodeThreaded,
|
Chris@263
|
171 MP3FileReader::CacheInTemporaryFile);
|
Chris@235
|
172 if (reader->isOK()) return reader;
|
Chris@241
|
173 if (reader->getError() != "") {
|
Chris@241
|
174 std::cerr << "AudioFileReaderFactory: MP3 file reader error: \""
|
Chris@285
|
175 << reader->getError() << "\"" << std::endl;
|
Chris@241
|
176 } else {
|
Chris@241
|
177 std::cerr << "AudioFileReaderFactory: MP3 file reader failed"
|
Chris@241
|
178 << std::endl;
|
Chris@241
|
179 }
|
Chris@235
|
180 delete reader;
|
Chris@235
|
181 #endif
|
Chris@235
|
182
|
Chris@281
|
183 #ifdef HAVE_QUICKTIME
|
Chris@281
|
184 reader = new QuickTimeFileReader
|
Chris@281
|
185 (path,
|
Chris@281
|
186 QuickTimeFileReader::DecodeThreaded,
|
Chris@281
|
187 QuickTimeFileReader::CacheInTemporaryFile);
|
Chris@281
|
188 if (reader->isOK()) return reader;
|
Chris@281
|
189 if (reader->getError() != "") {
|
Chris@281
|
190 std::cerr << "AudioFileReaderFactory: QuickTime file reader error: \""
|
Chris@285
|
191 << reader->getError() << "\"" << std::endl;
|
Chris@281
|
192 } else {
|
Chris@281
|
193 std::cerr << "AudioFileReaderFactory: QuickTime file reader failed"
|
Chris@281
|
194 << std::endl;
|
Chris@281
|
195 }
|
Chris@281
|
196 delete reader;
|
Chris@281
|
197 #endif
|
Chris@281
|
198
|
Chris@235
|
199 return 0;
|
Chris@235
|
200 }
|
Chris@235
|
201
|