annotate data/fileio/AudioFileReaderFactory.cpp @ 311:1c9143b2b658

...
author Chris Cannam
date Wed, 10 Oct 2007 15:32:16 +0000
parents 14e0f60435b8
children 3a6725f285d6
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@297 57 AudioFileReaderFactory::createReader(QString path, size_t targetRate)
Chris@235 58 {
Chris@290 59 QString err;
Chris@235 60
Chris@297 61 std::cerr << "AudioFileReaderFactory::createReader(\"" << path.toStdString() << "\"): Requested rate: " << targetRate << std::endl;
Chris@297 62
Chris@235 63 AudioFileReader *reader = 0;
Chris@235 64
Chris@241 65 // First try to construct a preferred reader based on the
Chris@241 66 // extension. If we can't identify one or it fails to load the
Chris@241 67 // file, fall back to trying all readers in no particular order.
Chris@241 68
Chris@290 69 QString ext = QFileInfo(path).suffix().toLower();
Chris@290 70 std::set<QString> extensions;
Chris@241 71
Chris@241 72 WavFileReader::getSupportedExtensions(extensions);
Chris@241 73 if (extensions.find(ext) != extensions.end()) {
Chris@241 74 reader = new WavFileReader(path);
Chris@297 75
Chris@297 76 if (targetRate != 0 &&
Chris@297 77 reader->isOK() &&
Chris@297 78 reader->getSampleRate() != targetRate) {
Chris@297 79
Chris@297 80 std::cerr << "AudioFileReaderFactory::createReader: WAV file rate: " << reader->getSampleRate() << ", creating resampling reader" << std::endl;
Chris@297 81
Chris@297 82 delete reader;
Chris@297 83 reader = new ResamplingWavFileReader
Chris@297 84 (path,
Chris@297 85 ResamplingWavFileReader::ResampleThreaded,
Chris@297 86 ResamplingWavFileReader::CacheInTemporaryFile,
Chris@297 87 targetRate);
Chris@297 88 }
Chris@241 89 }
Chris@241 90
Chris@241 91 #ifdef HAVE_OGGZ
Chris@241 92 #ifdef HAVE_FISHSOUND
Chris@241 93 if (!reader) {
Chris@241 94 extensions.clear();
Chris@241 95 OggVorbisFileReader::getSupportedExtensions(extensions);
Chris@241 96 if (extensions.find(ext) != extensions.end()) {
Chris@241 97 reader = new OggVorbisFileReader
Chris@263 98 (path,
Chris@265 99 OggVorbisFileReader::DecodeThreaded,
Chris@297 100 OggVorbisFileReader::CacheInTemporaryFile,
Chris@297 101 targetRate);
Chris@241 102 }
Chris@241 103 }
Chris@241 104 #endif
Chris@241 105 #endif
Chris@241 106
Chris@281 107 #ifdef HAVE_MAD
Chris@281 108 if (!reader) {
Chris@281 109 extensions.clear();
Chris@281 110 MP3FileReader::getSupportedExtensions(extensions);
Chris@281 111 if (extensions.find(ext) != extensions.end()) {
Chris@281 112 reader = new MP3FileReader
Chris@281 113 (path,
Chris@281 114 MP3FileReader::DecodeThreaded,
Chris@297 115 MP3FileReader::CacheInTemporaryFile,
Chris@297 116 targetRate);
Chris@281 117 }
Chris@281 118 }
Chris@281 119 #endif
Chris@281 120
Chris@281 121 #ifdef HAVE_QUICKTIME
Chris@281 122 if (!reader) {
Chris@281 123 extensions.clear();
Chris@281 124 QuickTimeFileReader::getSupportedExtensions(extensions);
Chris@281 125 if (extensions.find(ext) != extensions.end()) {
Chris@281 126 reader = new QuickTimeFileReader
Chris@281 127 (path,
Chris@281 128 QuickTimeFileReader::DecodeThreaded,
Chris@297 129 QuickTimeFileReader::CacheInTemporaryFile,
Chris@297 130 targetRate);
Chris@281 131 }
Chris@281 132 }
Chris@281 133 #endif
Chris@281 134
Chris@241 135 if (reader) {
Chris@241 136 if (reader->isOK()) return reader;
Chris@241 137 if (reader->getError() != "") {
Chris@241 138 std::cerr << "AudioFileReaderFactory: Preferred reader for "
Chris@290 139 << "extension \"" << ext.toStdString() << "\" failed: \""
Chris@290 140 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 141 } else {
Chris@241 142 std::cerr << "AudioFileReaderFactory: Preferred reader for "
Chris@290 143 << "extension \"" << ext.toStdString() << "\" failed"
Chris@241 144 << std::endl;
Chris@241 145 }
Chris@241 146 delete reader;
Chris@241 147 reader = 0;
Chris@241 148 }
Chris@241 149
Chris@308 150 return reader;
Chris@235 151 }
Chris@235 152