mirror of
				https://github.com/OpenMW/openmw.git
				synced 2025-10-26 14:56:39 +00:00 
			
		
		
		
	Added a manager for sndfile + mpg123, sorting on file type.
This commit is contained in:
		
							parent
							
								
									b0ded6a318
								
							
						
					
					
						commit
						56ecc6585b
					
				
					 2 changed files with 123 additions and 0 deletions
				
			
		
							
								
								
									
										33
									
								
								sound/filters/openal_sndfile_mpg123.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								sound/filters/openal_sndfile_mpg123.hpp
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,33 @@ | |||
| #ifndef MANGLE_MPG123_OPENAL_H | ||||
| #define MANGLE_MPG123_OPENAL_H | ||||
| 
 | ||||
| #include "input_filter.hpp" | ||||
| #include "source_splicer.hpp" | ||||
| #include "../sources/mpg123_source.hpp" | ||||
| #include "../sources/libsndfile.hpp" | ||||
| #include "../outputs/openal_out.hpp" | ||||
| 
 | ||||
| namespace Mangle { | ||||
| namespace Sound { | ||||
| 
 | ||||
| /// A InputFilter that uses OpenAL for output, and mpg123 (for MP3) +
 | ||||
| /// libsndfile (for everything else) to decode files. Can only load
 | ||||
| /// from the file system, and uses the file name to differentiate
 | ||||
| /// between mp3 and non-mp3 types.
 | ||||
| class OpenAL_SndFile_Mpg123_Factory : public InputFilter | ||||
| { | ||||
|  public: | ||||
|   OpenAL_SndFile_Mpg123_Factory() | ||||
|     { | ||||
|       SourceSplicer *splice = new SourceSplicer; | ||||
| 
 | ||||
|       splice->add("mp3", SampleSourceLoaderPtr(new Mpg123Loader)); | ||||
|       splice->setDefault(SampleSourceLoaderPtr(new SndFileLoader)); | ||||
| 
 | ||||
|       set(SoundFactoryPtr(new OpenAL_Factory), | ||||
|           SampleSourceLoaderPtr(splice)); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| }} | ||||
| #endif | ||||
							
								
								
									
										90
									
								
								sound/filters/source_splicer.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								sound/filters/source_splicer.hpp
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,90 @@ | |||
| #ifndef MANGLE_SOUND_SOURCE_SPLICE_H | ||||
| #define MANGLE_SOUND_SOURCE_SPLICE_H | ||||
| 
 | ||||
| #include "../source.hpp" | ||||
| #include "../../tools/str_exception.hpp" | ||||
| #include <string> | ||||
| #include <list> | ||||
| #include <assert.h> | ||||
| 
 | ||||
| namespace Mangle | ||||
| { | ||||
|   namespace Sound | ||||
|   { | ||||
|     class SourceSplicer : public SampleSourceLoader | ||||
|     { | ||||
|       struct SourceType | ||||
|       { | ||||
|         std::string type; | ||||
|         SampleSourceLoaderPtr loader; | ||||
|       }; | ||||
| 
 | ||||
|       typedef std::list<SourceType> TypeList; | ||||
|       TypeList list; | ||||
|       SampleSourceLoaderPtr catchAll; | ||||
| 
 | ||||
|       static bool isMatch(char a, char b) | ||||
|       { | ||||
|         if(a >= 'A' && a <= 'Z') | ||||
|           a += 'a' - 'A'; | ||||
|         if(b >= 'A' && b <= 'Z') | ||||
|           b += 'a' - 'A'; | ||||
|         return a == b; | ||||
|       } | ||||
| 
 | ||||
|     public: | ||||
|       SourceSplicer() | ||||
|       { | ||||
|         canLoadStream = false; | ||||
|         canLoadFile = true; | ||||
|       } | ||||
| 
 | ||||
|       void add(const std::string &type, SampleSourceLoaderPtr fact) | ||||
|       { | ||||
|         SourceType tp; | ||||
|         tp.type = type; | ||||
|         tp.loader = fact; | ||||
|         list.push_back(tp); | ||||
|       } | ||||
| 
 | ||||
|       void setDefault(SampleSourceLoaderPtr def) | ||||
|       { | ||||
|         catchAll = def; | ||||
|       } | ||||
| 
 | ||||
|       SampleSourcePtr load(const std::string &file) | ||||
|       { | ||||
|         // Search the list for this file type.
 | ||||
|         for(TypeList::iterator it = list.begin(); | ||||
|             it != list.end(); it++) | ||||
|           { | ||||
|             const std::string &t = it->type; | ||||
| 
 | ||||
|             int diff = file.size() - t.size(); | ||||
|             if(diff < 0) continue; | ||||
| 
 | ||||
|             bool match = true; | ||||
|             for(int i=0; i<t.size(); i++) | ||||
|               if(!isMatch(t[i], file[i+diff])) | ||||
|                 { | ||||
|                   match = false; | ||||
|                   break; | ||||
|                 } | ||||
| 
 | ||||
|             // Got something! We're done.
 | ||||
|             if(match) | ||||
|               return it->loader->load(file); | ||||
|           } | ||||
|         // If not found, use the catch-all
 | ||||
|         if(catchAll) | ||||
|           return catchAll->load(file); | ||||
| 
 | ||||
|         throw str_exception("No handler for sound file " + file); | ||||
|       } | ||||
| 
 | ||||
|       SampleSourcePtr load(Stream::StreamPtr input) { assert(0); } | ||||
|     }; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
		Loading…
	
		Reference in a new issue