Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

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 #include "input_flac.h"
00006 #ifndef NO_MIKMOD
00007 #include "input_mod.h"
00008 #endif
00009 #include "input_mp3.h"
00010 #include "input_ogg.h"
00011 #include "input_wav.h"
00012 #include "internal.h"
00013 #include "utility.h"
00014 
00015 
00016 namespace audiere {
00017 
00018   bool end_is(const char* begin, const char* ext) {
00019     const char* end = begin + strlen(begin);
00020     int ext_length = strlen(ext);
00021     if (ext_length > end - begin) {
00022       return false;
00023     } else {
00024       return (strcmp_case(end - ext_length, ext) == 0);
00025     }
00026   }
00027 
00028 
00029   ADR_EXPORT(const char*, AdrGetSupportedFileFormats)() {
00030     return
00031       "MP3 Files:mp3"  ";"
00032       "Ogg Vorbis Files:ogg"  ";"
00033       "FLAC Files:flac"  ";"
00034 #ifndef NO_MIKMOD
00035       "Mod Files:mod,s3m,xm,it"  ";"
00036 #endif
00037       "WAV Files:wav";
00038   }
00039 
00040 
00041   template<typename T>
00042   static T* TryInputStream(File* file) {
00043 
00044     // initialize should never close the file
00045 
00046     T* source = new T();
00047     if (source->initialize(file)) {
00048       return source;
00049     } else {
00050       delete source;
00051       return 0;
00052     }
00053   }
00054 
00055 
00062   SampleSource* OpenSource(File* raw_file, const char* filename) {
00063     ADR_GUARD("OpenSource");
00064 
00065     RefPtr<File> file(raw_file);
00066 
00067     #define TRY_SOURCE(source_type) {                                 \
00068       source_type* source = TryInputStream<source_type>(file.get());  \
00069       if (source) {                                                   \
00070         return source;                                                \
00071       } else {                                                        \
00072         file->seek(0, File::BEGIN);                                   \
00073       }                                                               \
00074     }
00075 
00076     // if filename is available, use it as a hint
00077     if (filename) {
00078       if (end_is(filename, ".wav")) {
00079 
00080         TRY_SOURCE(WAVInputStream);
00081 
00082       } else if (end_is(filename, ".ogg")) {
00083 
00084         TRY_SOURCE(OGGInputStream);
00085 
00086 #ifndef NO_MIKMOD
00087       } else if (end_is(filename, ".it") ||
00088           end_is(filename, ".xm") ||
00089           end_is(filename, ".s3m") ||
00090           end_is(filename, ".mod")) {
00091 
00092         TRY_SOURCE(MODInputStream);
00093 #endif
00094       } else if (end_is(filename, ".mp3")) {
00095 
00096         TRY_SOURCE(MP3InputStream);
00097 
00098       } else if (end_is(filename, ".flac")) {
00099 
00100         TRY_SOURCE(FLACInputStream);
00101 
00102       }
00103     }
00104 
00105     // autodetect otherwise, in decreasing order of possibility of failure
00106 #ifndef NO_MIKMOD
00107     TRY_SOURCE(MODInputStream);
00108 #endif
00109     TRY_SOURCE(WAVInputStream);
00110     TRY_SOURCE(OGGInputStream);
00111     TRY_SOURCE(MP3InputStream);
00112     TRY_SOURCE(FLACInputStream);
00113 
00114     return 0;
00115   }
00116 
00117 
00118   ADR_EXPORT(SampleSource*, AdrOpenSampleSource)(const char* filename) {
00119     if (!filename) {
00120       return 0;
00121     }
00122     File* file = OpenDefaultFile(filename);
00123     if (!file) {
00124       return 0;
00125     }
00126     return OpenSource(file, filename);
00127   }
00128 
00129 
00130   ADR_EXPORT(SampleSource*, AdrOpenSampleSourceFromFile)(File* file) {
00131     if (!file) {
00132       return 0;
00133     }
00134     return OpenSource(file, 0);
00135   }
00136 
00137 }

Generated on Sat Oct 12 01:43:03 2002 for audiere by doxygen1.2.17