annotate data/fileio/AudioFileReaderFactory.cpp @ 370:e73a1a1e8f24

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