comparison data/fileio/AudioFileReaderFactory.cpp @ 316:3a6725f285d6

* Make RemoteFile far more pervasive, and use it for local files as well so that we can handle both transparently. Make it shallow copy with reference counting, so it can be used by value without having to worry about the cache file lifetime. Use RemoteFile for MainWindow file-open functions, etc
author Chris Cannam
date Thu, 18 Oct 2007 15:31:20 +0000
parents 14e0f60435b8
children c324d410b096
comparison
equal deleted inserted replaced
315:96ef9746c560 316:3a6725f285d6
52 52
53 return rv; 53 return rv;
54 } 54 }
55 55
56 AudioFileReader * 56 AudioFileReader *
57 AudioFileReaderFactory::createReader(QString path, size_t targetRate) 57 AudioFileReaderFactory::createReader(RemoteFile source, size_t targetRate)
58 { 58 {
59 QString err; 59 QString err;
60 60
61 std::cerr << "AudioFileReaderFactory::createReader(\"" << path.toStdString() << "\"): Requested rate: " << targetRate << std::endl; 61 std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\"): Requested rate: " << targetRate << std::endl;
62
63 if (!source.isOK() || !source.isAvailable()) {
64 std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\": Source unavailable" << std::endl;
65 return 0;
66 }
62 67
63 AudioFileReader *reader = 0; 68 AudioFileReader *reader = 0;
64 69
65 // First try to construct a preferred reader based on the 70 // Try to construct a preferred reader based on the extension or
66 // extension. If we can't identify one or it fails to load the 71 // MIME type.
67 // file, fall back to trying all readers in no particular order.
68 72
69 QString ext = QFileInfo(path).suffix().toLower(); 73 if (WavFileReader::supports(source)) {
70 std::set<QString> extensions;
71 74
72 WavFileReader::getSupportedExtensions(extensions); 75 reader = new WavFileReader(source);
73 if (extensions.find(ext) != extensions.end()) {
74 reader = new WavFileReader(path);
75 76
76 if (targetRate != 0 && 77 if (targetRate != 0 &&
77 reader->isOK() && 78 reader->isOK() &&
78 reader->getSampleRate() != targetRate) { 79 reader->getSampleRate() != targetRate) {
79 80
80 std::cerr << "AudioFileReaderFactory::createReader: WAV file rate: " << reader->getSampleRate() << ", creating resampling reader" << std::endl; 81 std::cerr << "AudioFileReaderFactory::createReader: WAV file rate: " << reader->getSampleRate() << ", creating resampling reader" << std::endl;
81 82
82 delete reader; 83 delete reader;
83 reader = new ResamplingWavFileReader 84 reader = new ResamplingWavFileReader
84 (path, 85 (source,
85 ResamplingWavFileReader::ResampleThreaded, 86 ResamplingWavFileReader::ResampleThreaded,
86 ResamplingWavFileReader::CacheInTemporaryFile, 87 ResamplingWavFileReader::CacheInTemporaryFile,
87 targetRate); 88 targetRate);
88 } 89 }
89 } 90 }
90 91
91 #ifdef HAVE_OGGZ 92 #ifdef HAVE_OGGZ
92 #ifdef HAVE_FISHSOUND 93 #ifdef HAVE_FISHSOUND
93 if (!reader) { 94 if (!reader) {
94 extensions.clear(); 95 if (OggVorbisFileReader::supports(source)) {
95 OggVorbisFileReader::getSupportedExtensions(extensions);
96 if (extensions.find(ext) != extensions.end()) {
97 reader = new OggVorbisFileReader 96 reader = new OggVorbisFileReader
98 (path, 97 (source,
99 OggVorbisFileReader::DecodeThreaded, 98 OggVorbisFileReader::DecodeThreaded,
100 OggVorbisFileReader::CacheInTemporaryFile, 99 OggVorbisFileReader::CacheInTemporaryFile,
101 targetRate); 100 targetRate);
102 } 101 }
103 } 102 }
104 #endif 103 #endif
105 #endif 104 #endif
106 105
107 #ifdef HAVE_MAD 106 #ifdef HAVE_MAD
108 if (!reader) { 107 if (!reader) {
109 extensions.clear(); 108 if (MP3FileReader::supports(source)) {
110 MP3FileReader::getSupportedExtensions(extensions);
111 if (extensions.find(ext) != extensions.end()) {
112 reader = new MP3FileReader 109 reader = new MP3FileReader
113 (path, 110 (source,
114 MP3FileReader::DecodeThreaded, 111 MP3FileReader::DecodeThreaded,
115 MP3FileReader::CacheInTemporaryFile, 112 MP3FileReader::CacheInTemporaryFile,
116 targetRate); 113 targetRate);
117 } 114 }
118 } 115 }
119 #endif 116 #endif
120 117
121 #ifdef HAVE_QUICKTIME 118 #ifdef HAVE_QUICKTIME
122 if (!reader) { 119 if (!reader) {
123 extensions.clear(); 120 if (QuickTimeFileReader::supports(source)) {
124 QuickTimeFileReader::getSupportedExtensions(extensions);
125 if (extensions.find(ext) != extensions.end()) {
126 reader = new QuickTimeFileReader 121 reader = new QuickTimeFileReader
127 (path, 122 (source,
128 QuickTimeFileReader::DecodeThreaded, 123 QuickTimeFileReader::DecodeThreaded,
129 QuickTimeFileReader::CacheInTemporaryFile, 124 QuickTimeFileReader::CacheInTemporaryFile,
130 targetRate); 125 targetRate);
131 } 126 }
132 } 127 }
133 #endif 128 #endif
134 129
135 if (reader) { 130 if (reader) {
136 if (reader->isOK()) return reader; 131 if (reader->isOK()) {
132 std::cerr << "AudioFileReaderFactory: Reader is OK" << std::endl;
133 return reader;
134 }
135 std::cerr << "AudioFileReaderFactory: Preferred reader for "
136 << "url \"" << source.getLocation().toStdString()
137 << "\" (content type \""
138 << source.getContentType().toStdString() << "\") failed";
139
137 if (reader->getError() != "") { 140 if (reader->getError() != "") {
138 std::cerr << "AudioFileReaderFactory: Preferred reader for " 141 std::cerr << ": \"" << reader->getError().toStdString() << "\"";
139 << "extension \"" << ext.toStdString() << "\" failed: \"" 142 }
140 << reader->getError().toStdString() << "\"" << std::endl; 143 std::cerr << std::endl;
141 } else {
142 std::cerr << "AudioFileReaderFactory: Preferred reader for "
143 << "extension \"" << ext.toStdString() << "\" failed"
144 << std::endl;
145 }
146 delete reader; 144 delete reader;
147 reader = 0; 145 reader = 0;
148 } 146 }
149 147
148 std::cerr << "AudioFileReaderFactory: No reader" << std::endl;
150 return reader; 149 return reader;
151 } 150 }
152 151