annotate data/fileio/AudioFileReaderFactory.cpp @ 301:73537d900d4b

* Add MIDI file export (closes FR#1643721)
author Chris Cannam
date Thu, 04 Oct 2007 11:52:38 +0000
parents c022976d18e8
children 14e0f60435b8
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@235 150 reader = new WavFileReader(path);
Chris@297 151
Chris@297 152 if (targetRate != 0 &&
Chris@297 153 reader->isOK() &&
Chris@297 154 reader->getSampleRate() != targetRate) {
Chris@297 155
Chris@297 156 delete reader;
Chris@297 157 reader = new ResamplingWavFileReader
Chris@297 158 (path,
Chris@297 159 ResamplingWavFileReader::ResampleThreaded,
Chris@297 160 ResamplingWavFileReader::CacheInTemporaryFile,
Chris@297 161 targetRate);
Chris@297 162 }
Chris@297 163
Chris@235 164 if (reader->isOK()) return reader;
Chris@297 165
Chris@241 166 if (reader->getError() != "") {
Chris@241 167 std::cerr << "AudioFileReaderFactory: WAV file reader error: \""
Chris@290 168 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 169 } else {
Chris@241 170 std::cerr << "AudioFileReaderFactory: WAV file reader failed"
Chris@241 171 << std::endl;
Chris@241 172 }
Chris@235 173 delete reader;
Chris@235 174
Chris@235 175 #ifdef HAVE_OGGZ
Chris@235 176 #ifdef HAVE_FISHSOUND
Chris@241 177 reader = new OggVorbisFileReader
Chris@263 178 (path,
Chris@265 179 OggVorbisFileReader::DecodeThreaded,
Chris@263 180 OggVorbisFileReader::CacheInTemporaryFile);
Chris@235 181 if (reader->isOK()) return reader;
Chris@241 182 if (reader->getError() != "") {
Chris@241 183 std::cerr << "AudioFileReaderFactory: Ogg file reader error: \""
Chris@290 184 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 185 } else {
Chris@241 186 std::cerr << "AudioFileReaderFactory: Ogg file reader failed"
Chris@241 187 << std::endl;
Chris@241 188 }
Chris@235 189 delete reader;
Chris@235 190 #endif
Chris@235 191 #endif
Chris@235 192
Chris@235 193 #ifdef HAVE_MAD
Chris@241 194 reader = new MP3FileReader
Chris@263 195 (path,
Chris@265 196 MP3FileReader::DecodeThreaded,
Chris@263 197 MP3FileReader::CacheInTemporaryFile);
Chris@235 198 if (reader->isOK()) return reader;
Chris@241 199 if (reader->getError() != "") {
Chris@241 200 std::cerr << "AudioFileReaderFactory: MP3 file reader error: \""
Chris@290 201 << reader->getError().toStdString() << "\"" << std::endl;
Chris@241 202 } else {
Chris@241 203 std::cerr << "AudioFileReaderFactory: MP3 file reader failed"
Chris@241 204 << std::endl;
Chris@241 205 }
Chris@235 206 delete reader;
Chris@235 207 #endif
Chris@235 208
Chris@281 209 #ifdef HAVE_QUICKTIME
Chris@281 210 reader = new QuickTimeFileReader
Chris@281 211 (path,
Chris@281 212 QuickTimeFileReader::DecodeThreaded,
Chris@281 213 QuickTimeFileReader::CacheInTemporaryFile);
Chris@281 214 if (reader->isOK()) return reader;
Chris@281 215 if (reader->getError() != "") {
Chris@281 216 std::cerr << "AudioFileReaderFactory: QuickTime file reader error: \""
Chris@290 217 << reader->getError().toStdString() << "\"" << std::endl;
Chris@281 218 } else {
Chris@281 219 std::cerr << "AudioFileReaderFactory: QuickTime file reader failed"
Chris@281 220 << std::endl;
Chris@281 221 }
Chris@281 222 delete reader;
Chris@281 223 #endif
Chris@281 224
Chris@235 225 return 0;
Chris@235 226 }
Chris@235 227