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"
|
Chris@386
|
23
|
Chris@386
|
24 #include <QString>
|
Chris@386
|
25 #include <QFileInfo>
|
Chris@386
|
26 #include <iostream>
|
Chris@386
|
27
|
Chris@386
|
28 QString
|
Chris@386
|
29 AudioFileReaderFactory::getKnownExtensions()
|
Chris@386
|
30 {
|
Chris@386
|
31 std::set<QString> extensions;
|
Chris@386
|
32
|
Chris@386
|
33 WavFileReader::getSupportedExtensions(extensions);
|
Chris@386
|
34 #ifdef HAVE_MAD
|
Chris@386
|
35 MP3FileReader::getSupportedExtensions(extensions);
|
Chris@386
|
36 #endif
|
Chris@386
|
37 #ifdef HAVE_OGGZ
|
Chris@386
|
38 #ifdef HAVE_FISHSOUND
|
Chris@386
|
39 OggVorbisFileReader::getSupportedExtensions(extensions);
|
Chris@386
|
40 #endif
|
Chris@386
|
41 #endif
|
Chris@386
|
42 #ifdef HAVE_QUICKTIME
|
Chris@386
|
43 QuickTimeFileReader::getSupportedExtensions(extensions);
|
Chris@386
|
44 #endif
|
Chris@386
|
45
|
Chris@386
|
46 QString rv;
|
Chris@386
|
47 for (std::set<QString>::const_iterator i = extensions.begin();
|
Chris@386
|
48 i != extensions.end(); ++i) {
|
Chris@386
|
49 if (i != extensions.begin()) rv += " ";
|
Chris@386
|
50 rv += "*." + *i;
|
Chris@386
|
51 }
|
Chris@386
|
52
|
Chris@386
|
53 return rv;
|
Chris@386
|
54 }
|
Chris@386
|
55
|
Chris@386
|
56 AudioFileReader *
|
Chris@392
|
57 AudioFileReaderFactory::createReader(FileSource source, size_t targetRate,
|
Chris@392
|
58 ProgressReporter *reporter)
|
Chris@386
|
59 {
|
Chris@392
|
60 return create(source, targetRate, false, reporter);
|
Chris@386
|
61 }
|
Chris@386
|
62
|
Chris@386
|
63 AudioFileReader *
|
Chris@392
|
64 AudioFileReaderFactory::createThreadingReader(FileSource source, size_t targetRate,
|
Chris@392
|
65 ProgressReporter *reporter)
|
Chris@386
|
66 {
|
Chris@392
|
67 return create(source, targetRate, true, reporter);
|
Chris@386
|
68 }
|
Chris@386
|
69
|
Chris@386
|
70 AudioFileReader *
|
Chris@392
|
71 AudioFileReaderFactory::create(FileSource source, size_t targetRate, bool threading,
|
Chris@392
|
72 ProgressReporter *reporter)
|
Chris@386
|
73 {
|
Chris@386
|
74 QString err;
|
Chris@386
|
75
|
Chris@386
|
76 // std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\"): Requested rate: " << targetRate << std::endl;
|
Chris@386
|
77
|
Chris@386
|
78 if (!source.isOK() || !source.isAvailable()) {
|
Chris@386
|
79 std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\": Source unavailable" << std::endl;
|
Chris@386
|
80 return 0;
|
Chris@386
|
81 }
|
Chris@386
|
82
|
Chris@386
|
83 AudioFileReader *reader = 0;
|
Chris@386
|
84
|
Chris@386
|
85 // Try to construct a preferred reader based on the extension or
|
Chris@386
|
86 // MIME type.
|
Chris@386
|
87
|
Chris@386
|
88 if (WavFileReader::supports(source)) {
|
Chris@386
|
89
|
Chris@386
|
90 reader = new WavFileReader(source);
|
Chris@386
|
91
|
Chris@386
|
92 if (targetRate != 0 &&
|
Chris@386
|
93 reader->isOK() &&
|
Chris@386
|
94 reader->getSampleRate() != targetRate) {
|
Chris@386
|
95
|
Chris@386
|
96 std::cerr << "AudioFileReaderFactory::createReader: WAV file rate: " << reader->getSampleRate() << ", creating resampling reader" << std::endl;
|
Chris@386
|
97
|
Chris@386
|
98 delete reader;
|
Chris@386
|
99 reader = new ResamplingWavFileReader
|
Chris@386
|
100 (source,
|
Chris@386
|
101 threading ?
|
Chris@386
|
102 ResamplingWavFileReader::ResampleThreaded :
|
Chris@386
|
103 ResamplingWavFileReader::ResampleAtOnce,
|
Chris@386
|
104 ResamplingWavFileReader::CacheInTemporaryFile,
|
Chris@392
|
105 targetRate,
|
Chris@392
|
106 reporter);
|
Chris@386
|
107 }
|
Chris@386
|
108 }
|
Chris@386
|
109
|
Chris@386
|
110 #ifdef HAVE_OGGZ
|
Chris@386
|
111 #ifdef HAVE_FISHSOUND
|
Chris@386
|
112 if (!reader) {
|
Chris@386
|
113 if (OggVorbisFileReader::supports(source)) {
|
Chris@386
|
114 reader = new OggVorbisFileReader
|
Chris@386
|
115 (source,
|
Chris@386
|
116 threading ?
|
Chris@386
|
117 OggVorbisFileReader::DecodeThreaded :
|
Chris@386
|
118 OggVorbisFileReader::DecodeAtOnce,
|
Chris@386
|
119 OggVorbisFileReader::CacheInTemporaryFile,
|
Chris@392
|
120 targetRate,
|
Chris@392
|
121 reporter);
|
Chris@386
|
122 }
|
Chris@386
|
123 }
|
Chris@386
|
124 #endif
|
Chris@386
|
125 #endif
|
Chris@386
|
126
|
Chris@386
|
127 #ifdef HAVE_MAD
|
Chris@386
|
128 if (!reader) {
|
Chris@386
|
129 if (MP3FileReader::supports(source)) {
|
Chris@386
|
130 reader = new MP3FileReader
|
Chris@386
|
131 (source,
|
Chris@386
|
132 threading ?
|
Chris@386
|
133 MP3FileReader::DecodeThreaded :
|
Chris@386
|
134 MP3FileReader::DecodeAtOnce,
|
Chris@386
|
135 MP3FileReader::CacheInTemporaryFile,
|
Chris@392
|
136 targetRate,
|
Chris@392
|
137 reporter);
|
Chris@386
|
138 }
|
Chris@386
|
139 }
|
Chris@386
|
140 #endif
|
Chris@386
|
141
|
Chris@386
|
142 #ifdef HAVE_QUICKTIME
|
Chris@386
|
143 if (!reader) {
|
Chris@386
|
144 if (QuickTimeFileReader::supports(source)) {
|
Chris@386
|
145 reader = new QuickTimeFileReader
|
Chris@386
|
146 (source,
|
Chris@386
|
147 threading ?
|
Chris@386
|
148 QuickTimeFileReader::DecodeThreaded :
|
Chris@386
|
149 QuickTimeFileReader::DecodeAtOnce,
|
Chris@386
|
150 QuickTimeFileReader::CacheInTemporaryFile,
|
Chris@392
|
151 targetRate,
|
Chris@392
|
152 reporter);
|
Chris@386
|
153 }
|
Chris@386
|
154 }
|
Chris@386
|
155 #endif
|
Chris@386
|
156
|
Chris@386
|
157 if (reader) {
|
Chris@386
|
158 if (reader->isOK()) {
|
Chris@386
|
159 // std::cerr << "AudioFileReaderFactory: Reader is OK" << std::endl;
|
Chris@386
|
160 return reader;
|
Chris@386
|
161 }
|
Chris@386
|
162 std::cerr << "AudioFileReaderFactory: Preferred reader for "
|
Chris@386
|
163 << "url \"" << source.getLocation().toStdString()
|
Chris@386
|
164 << "\" (content type \""
|
Chris@386
|
165 << source.getContentType().toStdString() << "\") failed";
|
Chris@386
|
166
|
Chris@386
|
167 if (reader->getError() != "") {
|
Chris@386
|
168 std::cerr << ": \"" << reader->getError().toStdString() << "\"";
|
Chris@386
|
169 }
|
Chris@386
|
170 std::cerr << std::endl;
|
Chris@386
|
171 delete reader;
|
Chris@386
|
172 reader = 0;
|
Chris@386
|
173 }
|
Chris@386
|
174
|
Chris@386
|
175 std::cerr << "AudioFileReaderFactory: No reader" << std::endl;
|
Chris@386
|
176 return reader;
|
Chris@386
|
177 }
|
Chris@386
|
178
|