annotate data/fileio/AudioFileReaderFactory.cpp @ 271:822bd7fd526c

* Add support for reading mp3 and Ogg file title tags
author Chris Cannam
date Mon, 02 Jul 2007 13:53:38 +0000
parents e08f486e8d8c
children 9c447d664275
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@263 76 (path,
Chris@265 77 MP3FileReader::DecodeThreaded,
Chris@263 78 MP3FileReader::CacheInTemporaryFile);
Chris@241 79 }
Chris@241 80 }
Chris@241 81 #endif
Chris@241 82 #ifdef HAVE_OGGZ
Chris@241 83 #ifdef HAVE_FISHSOUND
Chris@241 84 if (!reader) {
Chris@241 85 extensions.clear();
Chris@241 86 OggVorbisFileReader::getSupportedExtensions(extensions);
Chris@241 87 if (extensions.find(ext) != extensions.end()) {
Chris@241 88 reader = new OggVorbisFileReader
Chris@263 89 (path,
Chris@265 90 OggVorbisFileReader::DecodeThreaded,
Chris@263 91 OggVorbisFileReader::CacheInTemporaryFile);
Chris@241 92 }
Chris@241 93 }
Chris@241 94 #endif
Chris@241 95 #endif
Chris@241 96
Chris@241 97 if (reader) {
Chris@241 98 if (reader->isOK()) return reader;
Chris@241 99 if (reader->getError() != "") {
Chris@241 100 std::cerr << "AudioFileReaderFactory: Preferred reader for "
Chris@241 101 << "extension \"" << ext.toStdString() << "\" failed: \""
Chris@241 102 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 103 } else {
Chris@241 104 std::cerr << "AudioFileReaderFactory: Preferred reader for "
Chris@241 105 << "extension \"" << ext.toStdString() << "\" failed"
Chris@241 106 << std::endl;
Chris@241 107 }
Chris@241 108 delete reader;
Chris@241 109 reader = 0;
Chris@241 110 }
Chris@241 111
Chris@235 112 reader = new WavFileReader(path);
Chris@235 113 if (reader->isOK()) return reader;
Chris@241 114 if (reader->getError() != "") {
Chris@241 115 std::cerr << "AudioFileReaderFactory: WAV file reader error: \""
Chris@241 116 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 117 } else {
Chris@241 118 std::cerr << "AudioFileReaderFactory: WAV file reader failed"
Chris@241 119 << std::endl;
Chris@241 120 }
Chris@235 121 delete reader;
Chris@235 122
Chris@235 123 #ifdef HAVE_OGGZ
Chris@235 124 #ifdef HAVE_FISHSOUND
Chris@241 125 reader = new OggVorbisFileReader
Chris@263 126 (path,
Chris@265 127 OggVorbisFileReader::DecodeThreaded,
Chris@263 128 OggVorbisFileReader::CacheInTemporaryFile);
Chris@235 129 if (reader->isOK()) return reader;
Chris@241 130 if (reader->getError() != "") {
Chris@241 131 std::cerr << "AudioFileReaderFactory: Ogg file reader error: \""
Chris@241 132 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 133 } else {
Chris@241 134 std::cerr << "AudioFileReaderFactory: Ogg file reader failed"
Chris@241 135 << std::endl;
Chris@241 136 }
Chris@235 137 delete reader;
Chris@235 138 #endif
Chris@235 139 #endif
Chris@235 140
Chris@235 141 #ifdef HAVE_MAD
Chris@241 142 reader = new MP3FileReader
Chris@263 143 (path,
Chris@265 144 MP3FileReader::DecodeThreaded,
Chris@263 145 MP3FileReader::CacheInTemporaryFile);
Chris@235 146 if (reader->isOK()) return reader;
Chris@241 147 if (reader->getError() != "") {
Chris@241 148 std::cerr << "AudioFileReaderFactory: MP3 file reader error: \""
Chris@241 149 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 150 } else {
Chris@241 151 std::cerr << "AudioFileReaderFactory: MP3 file reader failed"
Chris@241 152 << std::endl;
Chris@241 153 }
Chris@235 154 delete reader;
Chris@235 155 #endif
Chris@235 156
Chris@235 157 return 0;
Chris@235 158 }
Chris@235 159