input.cpp

Go to the documentation of this file.
00001 #include <memory>
00002 #include <string.h>
00003 #include "debug.h"
00004 #include "default_file.h"
00005 #ifndef NO_FLAC
00006 #include "input_flac.h"
00007 #endif
00008 #ifndef NO_DUMB
00009 #include "input_mod.h"
00010 #endif
00011 #ifndef NO_MP3
00012 #include "input_mp3.h"
00013 #endif
00014 #ifndef NO_OGG
00015 #include "input_ogg.h"
00016 #endif
00017 #ifndef NO_SPEEX
00018 #include "input_speex.h"
00019 #endif
00020 #include "input_wav.h"
00021 #include "input_aiff.h"
00022 #include "internal.h"
00023 #include "utility.h"
00024 
00025 
00026 namespace audiere {
00027 
00028 
00029   ADR_EXPORT(const char*) AdrGetSupportedFileFormats() {
00030     return
00031       "AIFF Files:aiff,aifc"  ";"
00032 #ifndef NO_MP3
00033       "MP3 Files:mp3,mp2"  ";"
00034 #endif
00035 #ifndef NO_OGG
00036       "Ogg Vorbis Files:ogg"  ";"
00037 #endif
00038 #ifndef NO_FLAC
00039       "FLAC Files:flac"  ";"
00040 #endif
00041 #ifndef NO_DUMB
00042       "Mod Files:mod,s3m,xm,it"  ";"
00043 #endif
00044 #ifndef NO_SPEEX
00045       "Speex Files:spx"  ";"
00046 #endif
00047       "WAV Files:wav";
00048   }
00049 
00050 
00051   template<typename T>
00052   static T* TryInputStream(const FilePtr& file) {
00053 
00054     // initialize should never close the file
00055 
00056     T* source = new T();
00057     if (source->initialize(file)) {
00058       return source;
00059     } else {
00060       delete source;
00061       return 0;
00062     }
00063   }
00064 
00065 
00066 #define TRY_SOURCE(source_type) {                             \
00067   source_type* source = TryInputStream<source_type>(file);    \
00068   if (source) {                                               \
00069     return source;                                            \
00070   } else {                                                    \
00071     file->seek(0, File::BEGIN);                               \
00072   }                                                           \
00073 }
00074 
00075 
00076 #define TRY_OPEN(format) {                                    \
00077   SampleSource* source = OpenSource(file, filename, format);  \
00078   if (source) {                                               \
00079     return source;                                            \
00080   }                                                           \
00081 }
00082 
00083 
00084   bool end_is(const char* begin, const char* ext) {
00085     const char* end = begin + strlen(begin);
00086     int ext_length = strlen(ext);
00087     if (ext_length > end - begin) {
00088       return false;
00089     } else {
00090       return (strcmp_case(end - ext_length, ext) == 0);
00091     }
00092   }
00093 
00094 
00095   FileFormat GuessFormat(const char* filename) {
00096     if (end_is(filename, ".aiff")) {
00097       return FF_AIFF;
00098     } else if (end_is(filename, ".wav")) {
00099       return FF_WAV;
00100     } else if (end_is(filename, ".ogg")) {
00101       return FF_OGG;
00102     } else if (end_is(filename, ".flac")) {
00103       return FF_FLAC;
00104     } else if (end_is(filename, ".mp3")) {
00105       return FF_MP3;
00106     } else if (end_is(filename, ".it") ||
00107                end_is(filename, ".xm") ||
00108                end_is(filename, ".s3m") ||
00109                end_is(filename, ".mod")) {
00110       return FF_MOD;
00111     } else if (end_is(filename, ".spx")) {
00112       return FF_SPEEX;
00113     } else {
00114       return FF_AUTODETECT;
00115     }
00116   }
00117 
00118 
00126   SampleSource* OpenSource(
00127     const FilePtr& file,
00128     const char* filename,
00129     FileFormat file_format)
00130   {
00131     ADR_GUARD("OpenSource");
00132     ADR_ASSERT(file != 0, "file must not be null");
00133 
00134     switch (file_format) {
00135       case FF_AUTODETECT:
00136         
00137         // if filename is available, use it as a hint
00138         if (filename) {
00139           FileFormat format = GuessFormat(filename);
00140           if (format != FF_AUTODETECT) {
00141             TRY_OPEN(format);
00142           }
00143         }
00144 
00145         // autodetect otherwise, in decreasing order of possibility of failure
00146         TRY_OPEN(FF_AIFF);
00147         TRY_OPEN(FF_WAV);
00148         TRY_OPEN(FF_OGG);
00149         TRY_OPEN(FF_FLAC);
00150         TRY_OPEN(FF_SPEEX);
00151         TRY_OPEN(FF_MP3);
00152         TRY_OPEN(FF_MOD);
00153         return 0;
00154 
00155 #ifndef NO_DUMB
00156       case FF_MOD:
00157         TRY_SOURCE(MODInputStream);
00158         return 0;
00159 #endif
00160 
00161       case FF_AIFF:
00162         TRY_SOURCE(AIFFInputStream);
00163         return 0;
00164 
00165       case FF_WAV:
00166         TRY_SOURCE(WAVInputStream);
00167         return 0;
00168 
00169 #ifndef NO_OGG
00170       case FF_OGG:
00171         TRY_SOURCE(OGGInputStream);
00172         return 0;
00173 #endif
00174 
00175 #ifndef NO_MP3
00176       case FF_MP3:
00177         TRY_SOURCE(MP3InputStream);
00178         return 0;
00179 #endif
00180 
00181 #ifndef NO_FLAC
00182       case FF_FLAC:
00183         TRY_SOURCE(FLACInputStream);
00184         return 0;
00185 #endif
00186 
00187 #ifndef NO_SPEEX
00188       case FF_SPEEX:
00189         TRY_SOURCE(SpeexInputStream);
00190         return 0;
00191 #endif
00192 
00193       default:
00194         return 0;
00195     }
00196   }
00197 
00198 
00199   ADR_EXPORT(SampleSource*) AdrOpenSampleSource(
00200     const char* filename,
00201     FileFormat file_format)
00202   {
00203     if (!filename) {
00204       return 0;
00205     }
00206     FilePtr file = OpenFile(filename, false);
00207     if (!file) {
00208       return 0;
00209     }
00210     return OpenSource(file, filename, file_format);
00211   }
00212 
00213 
00214   ADR_EXPORT(SampleSource*) AdrOpenSampleSourceFromFile(
00215     File* file,
00216     FileFormat file_format)
00217   {
00218     if (!file) {
00219       return 0;
00220     }
00221     return OpenSource(file, 0, file_format);
00222   }
00223 
00224 }

Generated on Mon Feb 13 23:07:46 2006 for audiere by  doxygen 1.4.6