File: AudioFileReaderFactory.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (246 lines) | stat: -rw-r--r-- 9,210 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2006 Chris Cannam and QMUL.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "AudioFileReaderFactory.h"

#include "WavFileReader.h"
#include "DecodingWavFileReader.h"
#include "MP3FileReader.h"
#include "BQAFileReader.h"
#include "AudioFileSizeEstimator.h"

#include "base/StorageAdviser.h"

#include <QString>
#include <QFileInfo>
#include <iostream>

using namespace std;

namespace sv {

QString
AudioFileReaderFactory::getKnownExtensions()
{
    set<QString> extensions;

#ifndef WITHOUT_LIBSNDFILE // See note in WavFileReader.h
    WavFileReader::getSupportedExtensions(extensions);
#endif
    
#ifdef HAVE_MAD
    MP3FileReader::getSupportedExtensions(extensions);
#endif
    BQAFileReader::getSupportedExtensions(extensions);

    QString rv;
    for (set<QString>::const_iterator i = extensions.begin();
         i != extensions.end(); ++i) {
        if (i != extensions.begin()) rv += " ";
        rv += "*." + *i;
    }

    return rv;
}

bool
AudioFileReaderFactory::isSupported(FileSource source)
{
#ifdef HAVE_MAD
    if (MP3FileReader::supports(source)) {
        return true;
    }
#endif
#ifndef WITHOUT_LIBSNDFILE // See note in WavFileReader.h
    if (WavFileReader::supports(source)) {
        return true;
    }
#endif
    if (BQAFileReader::supports(source)) {
        return true;
    }
    return false;
}

AudioFileReader *
AudioFileReaderFactory::createReader(FileSource source,
                                     Parameters params,
                                     ProgressReporter *reporter)
{
    QString err;

    SVDEBUG << "AudioFileReaderFactory: url \"" << source.getLocation() << "\": requested rate: " << params.targetRate << (params.targetRate == 0 ? " (use source rate)" : "") << endl;
    SVDEBUG << "AudioFileReaderFactory: local filename \"" << source.getLocalFilename() << "\", content type \"" << source.getContentType() << "\"" << endl;

    if (!source.isOK()) {
        SVDEBUG << "AudioFileReaderFactory::createReader(\"" << source.getLocation() << "\": Failed to retrieve source (transmission error?): " << source.getErrorString() << endl;
        return nullptr;
    }

    if (!source.isAvailable()) {
        SVDEBUG << "AudioFileReaderFactory::createReader(\"" << source.getLocation() << "\": Source not found" << endl;
        return nullptr;
    }

    AudioFileReader *reader = nullptr;

    sv_samplerate_t targetRate = params.targetRate;
    bool normalised = (params.normalisation == Normalisation::Peak);
  
    sv_frame_t estimatedSamples = 
        AudioFileSizeEstimator::estimate(source, targetRate);
    
    CodedAudioFileReader::CacheMode cacheMode =
        CodedAudioFileReader::CacheInTemporaryFile;

    if (estimatedSamples > 0) {
        size_t kb = (estimatedSamples * sizeof(float)) / 1024;
        SVDEBUG << "AudioFileReaderFactory: checking where to potentially cache "
                << kb << "K of sample data" << endl;
        StorageAdviser::Recommendation rec =
            StorageAdviser::recommend(StorageAdviser::SpeedCritical, kb, kb);
        if ((rec & StorageAdviser::UseMemory) ||
            (rec & StorageAdviser::PreferMemory)) {
            SVDEBUG << "AudioFileReaderFactory: cacheing (if at all) in memory" << endl;
            cacheMode = CodedAudioFileReader::CacheInMemory;
        } else {
            SVDEBUG << "AudioFileReaderFactory: cacheing (if at all) on disc" << endl;
        }
    }
    
    CodedAudioFileReader::DecodeMode decodeMode =
        (params.threadingMode == ThreadingMode::Threaded ?
         CodedAudioFileReader::DecodeThreaded :
         CodedAudioFileReader::DecodeAtOnce);

    bool fileUpdating = params.fileUpdating;
    
    // We go through the set of supported readers at most twice: once
    // picking out only the readers that claim to support the given
    // file's extension or MIME type, and (if that fails) again
    // providing the file to every reader in turn regardless of
    // extension or type. (If none of the readers claim to support a
    // file, that may just mean its extension is missing or
    // misleading. We have to be confident that the reader won't open
    // just any old text file or whatever and pretend it's succeeded.)

    for (int any = 0; any <= 1; ++any) {

        bool anyReader = (any > 0);

        if (!anyReader) {
            SVDEBUG << "AudioFileReaderFactory: Checking whether any reader officially handles this source" << endl;
        } else {
            SVDEBUG << "AudioFileReaderFactory: Source not officially handled by any reader, trying again with each reader in turn"
                    << endl;
        }

#ifdef HAVE_MAD
        // Having said we'll try any reader on the second pass, we
        // actually don't want to try the mp3 reader for anything not
        // identified as an mp3 - it can't identify files by header,
        // it'll try to read any data and then fail with
        // synchronisation errors - causing misleading and potentially
        // alarming warning messages at the least
        if (!anyReader) {
            if (MP3FileReader::supports(source)) {

                MP3FileReader::GaplessMode gapless =
                    params.gaplessMode == GaplessMode::Gapless ?
                    MP3FileReader::GaplessMode::Gapless :
                    MP3FileReader::GaplessMode::Gappy;
            
                reader = new MP3FileReader
                    (source, decodeMode, cacheMode, gapless,
                     targetRate, normalised, reporter);

                if (reader->isOK()) {
                    if (fileUpdating && !reader->isUpdating()) {
                        SVDEBUG << "AudioFileReaderFactory: WARNING: fileUpdating set to true, but MP3 reader doesn't support it" << endl;
                    }
                    SVDEBUG << "AudioFileReaderFactory: MP3 file reader is OK, returning it" << endl;
                    return reader;
                } else {
                    delete reader;
                }
            }
        }
#endif

#ifndef WITHOUT_LIBSNDFILE // See note in WavFileReader.h
        if (anyReader || WavFileReader::supports(source)) {

            reader = new WavFileReader(source, fileUpdating);

            sv_samplerate_t fileRate = reader->getSampleRate();

            if (reader->isOK() &&
                (!reader->isQuicklySeekable() ||
                 normalised ||
                 (cacheMode == CodedAudioFileReader::CacheInMemory &&
                  !fileUpdating) ||
                 (targetRate != 0 && fileRate != targetRate))) {

                SVDEBUG << "AudioFileReaderFactory: WAV file reader rate: " << reader->getSampleRate() << ", normalised " << normalised << ", seekable " << reader->isQuicklySeekable() << ", in memory " << (cacheMode == CodedAudioFileReader::CacheInMemory) << ", fileUpdating " << fileUpdating << ", creating decoding reader" << endl;
            
                delete reader;
                reader = new DecodingWavFileReader
                    (source,
                     decodeMode, cacheMode,
                     targetRate ? targetRate : fileRate,
                     normalised,
                     reporter);
            }

            if (reader->isOK()) {
                if (fileUpdating && !reader->isUpdating()) {
                    SVDEBUG << "AudioFileReaderFactory: WARNING: fileUpdating set to true, but WAV reader is reporting it doesn't support it - did we create it in the wrong mode?" << endl;
                }
                SVDEBUG << "AudioFileReaderFactory: WAV file reader is OK, returning it" << endl;
                return reader;
            } else {
                delete reader;
            }
        }
#endif
        
        if (anyReader || BQAFileReader::supports(source)) {

            reader = new BQAFileReader
                (source, decodeMode, cacheMode, 
                 targetRate, normalised, reporter);

            if (reader->isOK()) {
                if (fileUpdating && !reader->isUpdating()) {
                    SVDEBUG << "AudioFileReaderFactory: WARNING: fileUpdating set to true, but BQA reader doesn't support it" << endl;
                }
                SVDEBUG << "AudioFileReaderFactory: BQA reader is OK, returning it" << endl;
                return reader;
            } else {
                delete reader;
            }
        }
    }
    
    SVDEBUG << "AudioFileReaderFactory::Failed to create a reader for "
            << "url \"" << source.getLocation()
            << "\" (local filename \"" << source.getLocalFilename()
            << "\", content type \""
            << source.getContentType() << "\")" << endl;
    return nullptr;
}

}