annotate data/fileio/AudioFileReaderFactory.cpp @ 248:084ae1c213ee

* Add display of selection extents in status bar when dragging a selection * Some build fixes after that i18n update
author Chris Cannam
date Wed, 07 Mar 2007 18:00:49 +0000
parents 2b40f83e7627
children 71dfc6ab3b54
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@235 21
Chris@235 22 #include <QString>
Chris@241 23 #include <QFileInfo>
Chris@235 24 #include <iostream>
Chris@235 25
Chris@235 26 QString
Chris@235 27 AudioFileReaderFactory::getKnownExtensions()
Chris@235 28 {
Chris@235 29 std::set<QString> extensions;
Chris@235 30
Chris@235 31 WavFileReader::getSupportedExtensions(extensions);
Chris@235 32 #ifdef HAVE_MAD
Chris@235 33 MP3FileReader::getSupportedExtensions(extensions);
Chris@235 34 #endif
Chris@235 35 #ifdef HAVE_OGGZ
Chris@235 36 #ifdef HAVE_FISHSOUND
Chris@235 37 OggVorbisFileReader::getSupportedExtensions(extensions);
Chris@235 38 #endif
Chris@235 39 #endif
Chris@235 40
Chris@235 41 QString rv;
Chris@235 42 for (std::set<QString>::const_iterator i = extensions.begin();
Chris@235 43 i != extensions.end(); ++i) {
Chris@235 44 if (i != extensions.begin()) rv += " ";
Chris@235 45 rv += "*." + *i;
Chris@235 46 }
Chris@235 47
Chris@235 48 return rv;
Chris@235 49 }
Chris@235 50
Chris@235 51 AudioFileReader *
Chris@235 52 AudioFileReaderFactory::createReader(QString path)
Chris@235 53 {
Chris@235 54 QString err;
Chris@235 55
Chris@235 56 AudioFileReader *reader = 0;
Chris@235 57
Chris@241 58 // First try to construct a preferred reader based on the
Chris@241 59 // extension. If we can't identify one or it fails to load the
Chris@241 60 // file, fall back to trying all readers in no particular order.
Chris@241 61
Chris@241 62 QString ext = QFileInfo(path).suffix().toLower();
Chris@241 63 std::set<QString> extensions;
Chris@241 64
Chris@241 65 WavFileReader::getSupportedExtensions(extensions);
Chris@241 66 if (extensions.find(ext) != extensions.end()) {
Chris@241 67 reader = new WavFileReader(path);
Chris@241 68 }
Chris@241 69
Chris@241 70 #ifdef HAVE_MAD
Chris@241 71 if (!reader) {
Chris@241 72 extensions.clear();
Chris@241 73 MP3FileReader::getSupportedExtensions(extensions);
Chris@241 74 if (extensions.find(ext) != extensions.end()) {
Chris@241 75 reader = new MP3FileReader
Chris@241 76 (path, true, MP3FileReader::CacheInTemporaryFile);
Chris@241 77 }
Chris@241 78 }
Chris@241 79 #endif
Chris@241 80 #ifdef HAVE_OGGZ
Chris@241 81 #ifdef HAVE_FISHSOUND
Chris@241 82 if (!reader) {
Chris@241 83 extensions.clear();
Chris@241 84 OggVorbisFileReader::getSupportedExtensions(extensions);
Chris@241 85 if (extensions.find(ext) != extensions.end()) {
Chris@241 86 reader = new OggVorbisFileReader
Chris@241 87 (path, true, OggVorbisFileReader::CacheInTemporaryFile);
Chris@241 88 }
Chris@241 89 }
Chris@241 90 #endif
Chris@241 91 #endif
Chris@241 92
Chris@241 93 if (reader) {
Chris@241 94 if (reader->isOK()) return reader;
Chris@241 95 if (reader->getError() != "") {
Chris@241 96 std::cerr << "AudioFileReaderFactory: Preferred reader for "
Chris@241 97 << "extension \"" << ext.toStdString() << "\" failed: \""
Chris@241 98 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 99 } else {
Chris@241 100 std::cerr << "AudioFileReaderFactory: Preferred reader for "
Chris@241 101 << "extension \"" << ext.toStdString() << "\" failed"
Chris@241 102 << std::endl;
Chris@241 103 }
Chris@241 104 delete reader;
Chris@241 105 reader = 0;
Chris@241 106 }
Chris@241 107
Chris@235 108 reader = new WavFileReader(path);
Chris@235 109 if (reader->isOK()) return reader;
Chris@241 110 if (reader->getError() != "") {
Chris@241 111 std::cerr << "AudioFileReaderFactory: WAV file reader error: \""
Chris@241 112 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 113 } else {
Chris@241 114 std::cerr << "AudioFileReaderFactory: WAV file reader failed"
Chris@241 115 << std::endl;
Chris@241 116 }
Chris@235 117 delete reader;
Chris@235 118
Chris@235 119 #ifdef HAVE_OGGZ
Chris@235 120 #ifdef HAVE_FISHSOUND
Chris@241 121 reader = new OggVorbisFileReader
Chris@241 122 (path, true, OggVorbisFileReader::CacheInTemporaryFile);
Chris@235 123 if (reader->isOK()) return reader;
Chris@241 124 if (reader->getError() != "") {
Chris@241 125 std::cerr << "AudioFileReaderFactory: Ogg file reader error: \""
Chris@241 126 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 127 } else {
Chris@241 128 std::cerr << "AudioFileReaderFactory: Ogg file reader failed"
Chris@241 129 << std::endl;
Chris@241 130 }
Chris@235 131 delete reader;
Chris@235 132 #endif
Chris@235 133 #endif
Chris@235 134
Chris@235 135 #ifdef HAVE_MAD
Chris@241 136 reader = new MP3FileReader
Chris@241 137 (path, true, MP3FileReader::CacheInTemporaryFile);
Chris@235 138 if (reader->isOK()) return reader;
Chris@241 139 if (reader->getError() != "") {
Chris@241 140 std::cerr << "AudioFileReaderFactory: MP3 file reader error: \""
Chris@241 141 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 142 } else {
Chris@241 143 std::cerr << "AudioFileReaderFactory: MP3 file reader failed"
Chris@241 144 << std::endl;
Chris@241 145 }
Chris@235 146 delete reader;
Chris@235 147 #endif
Chris@235 148
Chris@235 149 return 0;
Chris@235 150 }
Chris@235 151