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 "MP3FileReader.h"
|
Chris@1583
|
21 #include "BQAFileReader.h"
|
Chris@1098
|
22 #include "AudioFileSizeEstimator.h"
|
Chris@1098
|
23
|
Chris@1098
|
24 #include "base/StorageAdviser.h"
|
Chris@386
|
25
|
Chris@386
|
26 #include <QString>
|
Chris@386
|
27 #include <QFileInfo>
|
Chris@386
|
28 #include <iostream>
|
Chris@386
|
29
|
Chris@1583
|
30 using namespace std;
|
Chris@1583
|
31
|
Chris@386
|
32 QString
|
Chris@386
|
33 AudioFileReaderFactory::getKnownExtensions()
|
Chris@386
|
34 {
|
Chris@1583
|
35 set<QString> extensions;
|
Chris@386
|
36
|
Chris@386
|
37 WavFileReader::getSupportedExtensions(extensions);
|
Chris@386
|
38 #ifdef HAVE_MAD
|
Chris@386
|
39 MP3FileReader::getSupportedExtensions(extensions);
|
Chris@386
|
40 #endif
|
Chris@1583
|
41 BQAFileReader::getSupportedExtensions(extensions);
|
Chris@386
|
42
|
Chris@386
|
43 QString rv;
|
Chris@1583
|
44 for (set<QString>::const_iterator i = extensions.begin();
|
Chris@386
|
45 i != extensions.end(); ++i) {
|
Chris@386
|
46 if (i != extensions.begin()) rv += " ";
|
Chris@386
|
47 rv += "*." + *i;
|
Chris@386
|
48 }
|
Chris@386
|
49
|
Chris@386
|
50 return rv;
|
Chris@386
|
51 }
|
Chris@386
|
52
|
Chris@386
|
53 AudioFileReader *
|
Chris@1313
|
54 AudioFileReaderFactory::createReader(FileSource source,
|
Chris@1313
|
55 Parameters params,
|
Chris@392
|
56 ProgressReporter *reporter)
|
Chris@386
|
57 {
|
Chris@386
|
58 QString err;
|
Chris@386
|
59
|
Chris@1342
|
60 SVDEBUG << "AudioFileReaderFactory: url \"" << source.getLocation() << "\": requested rate: " << params.targetRate << (params.targetRate == 0 ? " (use source rate)" : "") << endl;
|
Chris@1342
|
61 SVDEBUG << "AudioFileReaderFactory: local filename \"" << source.getLocalFilename() << "\", content type \"" << source.getContentType() << "\"" << endl;
|
Chris@386
|
62
|
Chris@667
|
63 if (!source.isOK()) {
|
Chris@1342
|
64 SVCERR << "AudioFileReaderFactory::createReader(\"" << source.getLocation() << "\": Failed to retrieve source (transmission error?): " << source.getErrorString() << endl;
|
Chris@667
|
65 return 0;
|
Chris@667
|
66 }
|
Chris@667
|
67
|
Chris@667
|
68 if (!source.isAvailable()) {
|
Chris@1342
|
69 SVCERR << "AudioFileReaderFactory::createReader(\"" << source.getLocation() << "\": Source not found" << endl;
|
Chris@386
|
70 return 0;
|
Chris@386
|
71 }
|
Chris@386
|
72
|
Chris@386
|
73 AudioFileReader *reader = 0;
|
Chris@386
|
74
|
Chris@1313
|
75 sv_samplerate_t targetRate = params.targetRate;
|
Chris@1313
|
76 bool normalised = (params.normalisation == Normalisation::Peak);
|
Chris@1313
|
77
|
Chris@1098
|
78 sv_frame_t estimatedSamples =
|
Chris@1098
|
79 AudioFileSizeEstimator::estimate(source, targetRate);
|
Chris@1098
|
80
|
Chris@1097
|
81 CodedAudioFileReader::CacheMode cacheMode =
|
Chris@1097
|
82 CodedAudioFileReader::CacheInTemporaryFile;
|
Chris@1097
|
83
|
Chris@1098
|
84 if (estimatedSamples > 0) {
|
Chris@1098
|
85 size_t kb = (estimatedSamples * sizeof(float)) / 1024;
|
Chris@1342
|
86 SVDEBUG << "AudioFileReaderFactory: checking where to potentially cache "
|
Chris@1342
|
87 << kb << "K of sample data" << endl;
|
Chris@1098
|
88 StorageAdviser::Recommendation rec =
|
Chris@1098
|
89 StorageAdviser::recommend(StorageAdviser::SpeedCritical, kb, kb);
|
Chris@1277
|
90 if ((rec & StorageAdviser::UseMemory) ||
|
Chris@1277
|
91 (rec & StorageAdviser::PreferMemory)) {
|
Chris@1342
|
92 SVDEBUG << "AudioFileReaderFactory: cacheing (if at all) in memory" << endl;
|
Chris@1098
|
93 cacheMode = CodedAudioFileReader::CacheInMemory;
|
Chris@1342
|
94 } else {
|
Chris@1342
|
95 SVDEBUG << "AudioFileReaderFactory: cacheing (if at all) on disc" << endl;
|
Chris@1098
|
96 }
|
Chris@1098
|
97 }
|
Chris@1098
|
98
|
Chris@1097
|
99 CodedAudioFileReader::DecodeMode decodeMode =
|
Chris@1313
|
100 (params.threadingMode == ThreadingMode::Threaded ?
|
Chris@1097
|
101 CodedAudioFileReader::DecodeThreaded :
|
Chris@1097
|
102 CodedAudioFileReader::DecodeAtOnce);
|
Chris@386
|
103
|
Chris@1313
|
104 // We go through the set of supported readers at most twice: once
|
Chris@1313
|
105 // picking out only the readers that claim to support the given
|
Chris@1313
|
106 // file's extension or MIME type, and (if that fails) again
|
Chris@1313
|
107 // providing the file to every reader in turn regardless of
|
Chris@1313
|
108 // extension or type. (If none of the readers claim to support a
|
Chris@1313
|
109 // file, that may just mean its extension is missing or
|
Chris@1313
|
110 // misleading. We have to be confident that the reader won't open
|
Chris@1313
|
111 // just any old text file or whatever and pretend it's succeeded.)
|
Chris@1097
|
112
|
Chris@1313
|
113 for (int any = 0; any <= 1; ++any) {
|
Chris@386
|
114
|
Chris@1313
|
115 bool anyReader = (any > 0);
|
Chris@386
|
116
|
Chris@1342
|
117 if (!anyReader) {
|
Chris@1342
|
118 SVDEBUG << "AudioFileReaderFactory: Checking whether any reader officially handles this source" << endl;
|
Chris@1342
|
119 } else {
|
Chris@1342
|
120 SVDEBUG << "AudioFileReaderFactory: Source not officially handled by any reader, trying again with each reader in turn"
|
Chris@1342
|
121 << endl;
|
Chris@1342
|
122 }
|
Chris@1359
|
123
|
Chris@1583
|
124 #ifdef HAVE_MAD
|
Chris@1583
|
125 if (anyReader || MP3FileReader::supports(source)) {
|
Chris@1583
|
126
|
Chris@1583
|
127 MP3FileReader::GaplessMode gapless =
|
Chris@1583
|
128 params.gaplessMode == GaplessMode::Gapless ?
|
Chris@1583
|
129 MP3FileReader::GaplessMode::Gapless :
|
Chris@1583
|
130 MP3FileReader::GaplessMode::Gappy;
|
Chris@1583
|
131
|
Chris@1583
|
132 reader = new MP3FileReader
|
Chris@1583
|
133 (source, decodeMode, cacheMode, gapless,
|
Chris@1583
|
134 targetRate, normalised, reporter);
|
Chris@1359
|
135
|
Chris@1359
|
136 if (reader->isOK()) {
|
Chris@1583
|
137 SVDEBUG << "AudioFileReaderFactory: MP3 file reader is OK, returning it" << endl;
|
Chris@1359
|
138 return reader;
|
Chris@1359
|
139 } else {
|
Chris@1359
|
140 delete reader;
|
Chris@1359
|
141 }
|
Chris@1359
|
142 }
|
Chris@1359
|
143 #endif
|
Chris@1583
|
144
|
Chris@1583
|
145 if (anyReader || BQAFileReader::supports(source)) {
|
Chris@1583
|
146
|
Chris@1583
|
147 reader = new BQAFileReader
|
Chris@1583
|
148 (source, decodeMode, cacheMode,
|
Chris@1583
|
149 targetRate, normalised, reporter);
|
Chris@1583
|
150
|
Chris@1583
|
151 if (reader->isOK()) {
|
Chris@1583
|
152 SVDEBUG << "AudioFileReaderFactory: BQA reader is OK, returning it" << endl;
|
Chris@1583
|
153 return reader;
|
Chris@1583
|
154 } else {
|
Chris@1583
|
155 delete reader;
|
Chris@1583
|
156 }
|
Chris@1583
|
157 }
|
Chris@1342
|
158
|
Chris@1313
|
159 if (anyReader || WavFileReader::supports(source)) {
|
Chris@386
|
160
|
Chris@1313
|
161 reader = new WavFileReader(source);
|
Chris@823
|
162
|
Chris@1313
|
163 sv_samplerate_t fileRate = reader->getSampleRate();
|
Chris@1313
|
164
|
Chris@1313
|
165 if (reader->isOK() &&
|
Chris@1313
|
166 (!reader->isQuicklySeekable() ||
|
Chris@1313
|
167 normalised ||
|
Chris@1313
|
168 (cacheMode == CodedAudioFileReader::CacheInMemory) ||
|
Chris@1313
|
169 (targetRate != 0 && fileRate != targetRate))) {
|
Chris@1313
|
170
|
Chris@1342
|
171 SVDEBUG << "AudioFileReaderFactory: WAV file reader rate: " << reader->getSampleRate() << ", normalised " << normalised << ", seekable " << reader->isQuicklySeekable() << ", in memory " << (cacheMode == CodedAudioFileReader::CacheInMemory) << ", creating decoding reader" << endl;
|
Chris@1161
|
172
|
Chris@1313
|
173 delete reader;
|
Chris@1313
|
174 reader = new DecodingWavFileReader
|
Chris@1313
|
175 (source,
|
Chris@1313
|
176 decodeMode, cacheMode,
|
Chris@1313
|
177 targetRate ? targetRate : fileRate,
|
Chris@1313
|
178 normalised,
|
Chris@1313
|
179 reporter);
|
Chris@1313
|
180 }
|
Chris@1313
|
181
|
Chris@1313
|
182 if (reader->isOK()) {
|
Chris@1342
|
183 SVDEBUG << "AudioFileReaderFactory: WAV file reader is OK, returning it" << endl;
|
Chris@1313
|
184 return reader;
|
Chris@1313
|
185 } else {
|
Chris@1313
|
186 delete reader;
|
Chris@1313
|
187 }
|
Chris@386
|
188 }
|
Chris@1097
|
189 }
|
Chris@1097
|
190
|
Chris@1342
|
191 SVCERR << "AudioFileReaderFactory::Failed to create a reader for "
|
Chris@1342
|
192 << "url \"" << source.getLocation()
|
Chris@1342
|
193 << "\" (local filename \"" << source.getLocalFilename()
|
Chris@1342
|
194 << "\", content type \""
|
Chris@1342
|
195 << source.getContentType() << "\")" << endl;
|
Chris@1313
|
196 return nullptr;
|
Chris@386
|
197 }
|
Chris@386
|
198
|