comparison data/fileio/AudioFileReaderFactory.cpp @ 241:2b40f83e7627

* Make audio file reader factory try for a reader based on the file's extension, before trying all readers * Remove some debug output
author Chris Cannam
date Thu, 01 Mar 2007 12:15:08 +0000
parents 84ff7ba52993
children 71dfc6ab3b54
comparison
equal deleted inserted replaced
240:8133ae938704 241:2b40f83e7627
18 #include "WavFileReader.h" 18 #include "WavFileReader.h"
19 #include "OggVorbisFileReader.h" 19 #include "OggVorbisFileReader.h"
20 #include "MP3FileReader.h" 20 #include "MP3FileReader.h"
21 21
22 #include <QString> 22 #include <QString>
23 #include <QFileInfo>
23 #include <iostream> 24 #include <iostream>
24 25
25 QString 26 QString
26 AudioFileReaderFactory::getKnownExtensions() 27 AudioFileReaderFactory::getKnownExtensions()
27 { 28 {
52 { 53 {
53 QString err; 54 QString err;
54 55
55 AudioFileReader *reader = 0; 56 AudioFileReader *reader = 0;
56 57
58 // First try to construct a preferred reader based on the
59 // extension. If we can't identify one or it fails to load the
60 // file, fall back to trying all readers in no particular order.
61
62 QString ext = QFileInfo(path).suffix().toLower();
63 std::set<QString> extensions;
64
65 WavFileReader::getSupportedExtensions(extensions);
66 if (extensions.find(ext) != extensions.end()) {
67 reader = new WavFileReader(path);
68 }
69
70 #ifdef HAVE_MAD
71 if (!reader) {
72 extensions.clear();
73 MP3FileReader::getSupportedExtensions(extensions);
74 if (extensions.find(ext) != extensions.end()) {
75 reader = new MP3FileReader
76 (path, true, MP3FileReader::CacheInTemporaryFile);
77 }
78 }
79 #endif
80 #ifdef HAVE_OGGZ
81 #ifdef HAVE_FISHSOUND
82 if (!reader) {
83 extensions.clear();
84 OggVorbisFileReader::getSupportedExtensions(extensions);
85 if (extensions.find(ext) != extensions.end()) {
86 reader = new OggVorbisFileReader
87 (path, true, OggVorbisFileReader::CacheInTemporaryFile);
88 }
89 }
90 #endif
91 #endif
92
93 if (reader) {
94 if (reader->isOK()) return reader;
95 if (reader->getError() != "") {
96 std::cerr << "AudioFileReaderFactory: Preferred reader for "
97 << "extension \"" << ext.toStdString() << "\" failed: \""
98 << reader->getError().toStdString() << "\"" << std::endl;
99 } else {
100 std::cerr << "AudioFileReaderFactory: Preferred reader for "
101 << "extension \"" << ext.toStdString() << "\" failed"
102 << std::endl;
103 }
104 delete reader;
105 reader = 0;
106 }
107
57 reader = new WavFileReader(path); 108 reader = new WavFileReader(path);
58 if (reader->isOK()) return reader; 109 if (reader->isOK()) return reader;
59 if (reader->getError() != "") err = reader->getError(); 110 if (reader->getError() != "") {
111 std::cerr << "AudioFileReaderFactory: WAV file reader error: \""
112 << reader->getError().toStdString() << "\"" << std::endl;
113 } else {
114 std::cerr << "AudioFileReaderFactory: WAV file reader failed"
115 << std::endl;
116 }
60 delete reader; 117 delete reader;
61
62 if (err != "") {
63 std::cerr << "AudioFileReaderFactory: WAV file reader error: \""
64 << err.toStdString() << "\"" << std::endl;
65 }
66
67 118
68 #ifdef HAVE_OGGZ 119 #ifdef HAVE_OGGZ
69 #ifdef HAVE_FISHSOUND 120 #ifdef HAVE_FISHSOUND
70 reader = new OggVorbisFileReader(path, true, 121 reader = new OggVorbisFileReader
71 OggVorbisFileReader::CacheInTemporaryFile); 122 (path, true, OggVorbisFileReader::CacheInTemporaryFile);
72 if (reader->isOK()) return reader; 123 if (reader->isOK()) return reader;
73 if (reader->getError() != "") err = reader->getError(); 124 if (reader->getError() != "") {
125 std::cerr << "AudioFileReaderFactory: Ogg file reader error: \""
126 << reader->getError().toStdString() << "\"" << std::endl;
127 } else {
128 std::cerr << "AudioFileReaderFactory: Ogg file reader failed"
129 << std::endl;
130 }
74 delete reader; 131 delete reader;
75
76 if (err != "") {
77 std::cerr << "AudioFileReaderFactory: Ogg file reader error: \""
78 << err.toStdString() << "\"" << std::endl;
79 }
80
81 #endif 132 #endif
82 #endif 133 #endif
83 134
84 #ifdef HAVE_MAD 135 #ifdef HAVE_MAD
85 reader = new MP3FileReader(path, true, 136 reader = new MP3FileReader
86 MP3FileReader::CacheInTemporaryFile); 137 (path, true, MP3FileReader::CacheInTemporaryFile);
87 if (reader->isOK()) return reader; 138 if (reader->isOK()) return reader;
88 if (reader->getError() != "") err = reader->getError(); 139 if (reader->getError() != "") {
140 std::cerr << "AudioFileReaderFactory: MP3 file reader error: \""
141 << reader->getError().toStdString() << "\"" << std::endl;
142 } else {
143 std::cerr << "AudioFileReaderFactory: MP3 file reader failed"
144 << std::endl;
145 }
89 delete reader; 146 delete reader;
90
91 if (err != "") {
92 std::cerr << "AudioFileReaderFactory: mp3 file reader error: \""
93 << err.toStdString() << "\"" << std::endl;
94 }
95
96 #endif 147 #endif
97 148
98 return 0; 149 return 0;
99 } 150 }
100 151