annotate data/fileio/AudioFileReaderFactory.cpp @ 292:8c5762634f42

* avoid crash if m_reader is null
author Chris Cannam
date Fri, 24 Aug 2007 14:45:29 +0000
parents 92e8dbde73cd
children c022976d18e8
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@235 19 #include "OggVorbisFileReader.h"
Chris@235 20 #include "MP3FileReader.h"
Chris@281 21 #include "QuickTimeFileReader.h"
Chris@235 22
Chris@235 23 #include <QString>
Chris@241 24 #include <QFileInfo>
Chris@235 25 #include <iostream>
Chris@235 26
Chris@290 27 QString
Chris@235 28 AudioFileReaderFactory::getKnownExtensions()
Chris@235 29 {
Chris@290 30 std::set<QString> extensions;
Chris@235 31
Chris@235 32 WavFileReader::getSupportedExtensions(extensions);
Chris@235 33 #ifdef HAVE_MAD
Chris@235 34 MP3FileReader::getSupportedExtensions(extensions);
Chris@235 35 #endif
Chris@235 36 #ifdef HAVE_OGGZ
Chris@235 37 #ifdef HAVE_FISHSOUND
Chris@235 38 OggVorbisFileReader::getSupportedExtensions(extensions);
Chris@235 39 #endif
Chris@235 40 #endif
Chris@281 41 #ifdef HAVE_QUICKTIME
Chris@281 42 QuickTimeFileReader::getSupportedExtensions(extensions);
Chris@281 43 #endif
Chris@235 44
Chris@290 45 QString rv;
Chris@290 46 for (std::set<QString>::const_iterator i = extensions.begin();
Chris@235 47 i != extensions.end(); ++i) {
Chris@235 48 if (i != extensions.begin()) rv += " ";
Chris@235 49 rv += "*." + *i;
Chris@235 50 }
Chris@235 51
Chris@235 52 return rv;
Chris@235 53 }
Chris@235 54
Chris@235 55 AudioFileReader *
Chris@290 56 AudioFileReaderFactory::createReader(QString path)
Chris@235 57 {
Chris@290 58 QString err;
Chris@235 59
Chris@235 60 AudioFileReader *reader = 0;
Chris@235 61
Chris@241 62 // First try to construct a preferred reader based on the
Chris@241 63 // extension. If we can't identify one or it fails to load the
Chris@241 64 // file, fall back to trying all readers in no particular order.
Chris@241 65
Chris@290 66 QString ext = QFileInfo(path).suffix().toLower();
Chris@290 67 std::set<QString> extensions;
Chris@241 68
Chris@241 69 WavFileReader::getSupportedExtensions(extensions);
Chris@241 70 if (extensions.find(ext) != extensions.end()) {
Chris@241 71 reader = new WavFileReader(path);
Chris@241 72 }
Chris@241 73
Chris@241 74 #ifdef HAVE_OGGZ
Chris@241 75 #ifdef HAVE_FISHSOUND
Chris@241 76 if (!reader) {
Chris@241 77 extensions.clear();
Chris@241 78 OggVorbisFileReader::getSupportedExtensions(extensions);
Chris@241 79 if (extensions.find(ext) != extensions.end()) {
Chris@241 80 reader = new OggVorbisFileReader
Chris@263 81 (path,
Chris@265 82 OggVorbisFileReader::DecodeThreaded,
Chris@263 83 OggVorbisFileReader::CacheInTemporaryFile);
Chris@241 84 }
Chris@241 85 }
Chris@241 86 #endif
Chris@241 87 #endif
Chris@241 88
Chris@281 89 #ifdef HAVE_MAD
Chris@281 90 if (!reader) {
Chris@281 91 extensions.clear();
Chris@281 92 MP3FileReader::getSupportedExtensions(extensions);
Chris@281 93 if (extensions.find(ext) != extensions.end()) {
Chris@281 94 reader = new MP3FileReader
Chris@281 95 (path,
Chris@281 96 MP3FileReader::DecodeThreaded,
Chris@281 97 MP3FileReader::CacheInTemporaryFile);
Chris@281 98 }
Chris@281 99 }
Chris@281 100 #endif
Chris@281 101
Chris@281 102 #ifdef HAVE_QUICKTIME
Chris@281 103 if (!reader) {
Chris@281 104 extensions.clear();
Chris@281 105 QuickTimeFileReader::getSupportedExtensions(extensions);
Chris@281 106 if (extensions.find(ext) != extensions.end()) {
Chris@281 107 reader = new QuickTimeFileReader
Chris@281 108 (path,
Chris@281 109 QuickTimeFileReader::DecodeThreaded,
Chris@281 110 QuickTimeFileReader::CacheInTemporaryFile);
Chris@281 111 }
Chris@281 112 }
Chris@281 113 #endif
Chris@281 114
Chris@241 115 if (reader) {
Chris@241 116 if (reader->isOK()) return reader;
Chris@241 117 if (reader->getError() != "") {
Chris@241 118 std::cerr << "AudioFileReaderFactory: Preferred reader for "
Chris@290 119 << "extension \"" << ext.toStdString() << "\" failed: \""
Chris@290 120 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 121 } else {
Chris@241 122 std::cerr << "AudioFileReaderFactory: Preferred reader for "
Chris@290 123 << "extension \"" << ext.toStdString() << "\" failed"
Chris@241 124 << std::endl;
Chris@241 125 }
Chris@241 126 delete reader;
Chris@241 127 reader = 0;
Chris@241 128 }
Chris@241 129
Chris@235 130 reader = new WavFileReader(path);
Chris@235 131 if (reader->isOK()) return reader;
Chris@241 132 if (reader->getError() != "") {
Chris@241 133 std::cerr << "AudioFileReaderFactory: WAV file reader error: \""
Chris@290 134 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 135 } else {
Chris@241 136 std::cerr << "AudioFileReaderFactory: WAV file reader failed"
Chris@241 137 << std::endl;
Chris@241 138 }
Chris@235 139 delete reader;
Chris@235 140
Chris@235 141 #ifdef HAVE_OGGZ
Chris@235 142 #ifdef HAVE_FISHSOUND
Chris@241 143 reader = new OggVorbisFileReader
Chris@263 144 (path,
Chris@265 145 OggVorbisFileReader::DecodeThreaded,
Chris@263 146 OggVorbisFileReader::CacheInTemporaryFile);
Chris@235 147 if (reader->isOK()) return reader;
Chris@241 148 if (reader->getError() != "") {
Chris@241 149 std::cerr << "AudioFileReaderFactory: Ogg file reader error: \""
Chris@290 150 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 151 } else {
Chris@241 152 std::cerr << "AudioFileReaderFactory: Ogg file reader failed"
Chris@241 153 << std::endl;
Chris@241 154 }
Chris@235 155 delete reader;
Chris@235 156 #endif
Chris@235 157 #endif
Chris@235 158
Chris@235 159 #ifdef HAVE_MAD
Chris@241 160 reader = new MP3FileReader
Chris@263 161 (path,
Chris@265 162 MP3FileReader::DecodeThreaded,
Chris@263 163 MP3FileReader::CacheInTemporaryFile);
Chris@235 164 if (reader->isOK()) return reader;
Chris@241 165 if (reader->getError() != "") {
Chris@241 166 std::cerr << "AudioFileReaderFactory: MP3 file reader error: \""
Chris@290 167 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 168 } else {
Chris@241 169 std::cerr << "AudioFileReaderFactory: MP3 file reader failed"
Chris@241 170 << std::endl;
Chris@241 171 }
Chris@235 172 delete reader;
Chris@235 173 #endif
Chris@235 174
Chris@281 175 #ifdef HAVE_QUICKTIME
Chris@281 176 reader = new QuickTimeFileReader
Chris@281 177 (path,
Chris@281 178 QuickTimeFileReader::DecodeThreaded,
Chris@281 179 QuickTimeFileReader::CacheInTemporaryFile);
Chris@281 180 if (reader->isOK()) return reader;
Chris@281 181 if (reader->getError() != "") {
Chris@281 182 std::cerr << "AudioFileReaderFactory: QuickTime file reader error: \""
Chris@290 183 << reader->getError().toStdString() << "\"" << std::endl;
Chris@281 184 } else {
Chris@281 185 std::cerr << "AudioFileReaderFactory: QuickTime file reader failed"
Chris@281 186 << std::endl;
Chris@281 187 }
Chris@281 188 delete reader;
Chris@281 189 #endif
Chris@281 190
Chris@235 191 return 0;
Chris@235 192 }
Chris@235 193