rink.nu / projects / musixbox / lib/core/decoderfactory.cc@98411bdf3103
lib/core/decoderfactory.cc
author Rink Springer <rink@rink.nu>
Sun Oct 16 13:40:26 2011 +0200 (7 months ago ago)
changeset 313 98411bdf3103
parent 311 126b48c35619
permissions -rw-r--r--
Implement resource 'tone:' as a x Hz tone

This uses the tone generator, which is great for debugging purposes.
     1 #include <algorithm>
     2 #include <string.h>
     3 #include "config.h"
     4 #include "decoderfactory.h"
     5 #include "core/decode_tone.h"
     6 #ifdef WITH_MAD
     7 #include "core/decode_mp3.h"
     8 #endif
     9 #ifdef WITH_ID3TAG
    10 #include "core/info_mp3.h"
    11 #endif
    12 #ifdef WITH_VORBIS
    13 #include "core/decode_ogg.h"
    14 #include "core/info_ogg.h"
    15 #endif
    16 #ifdef WITH_FLAC
    17 #include "core/decode_flac.h"
    18 #include "core/info_flac.h"
    19 #endif
    20 #ifdef WITH_MIKMOD
    21 #include "core/decode_module.h"
    22 #include "core/info_module.h"
    23 #endif
    24 #ifdef WITH_SIDPLAY2
    25 #include "core/decode_sid.h"
    26 #include "core/info_sid.h"
    27 #endif
    28 #ifdef WITH_ADPLUG
    29 #include "core/decode_adlib.h"
    30 #include "core/info_adlib.h"
    31 #endif
    32 #ifdef WITH_SDL_MIXER
    33 #include "core/decode_midi.h"
    34 #endif
    35 #include "core/input_file.h"
    36 #ifdef WITH_CURL
    37 #include "core/input_remote.h"
    38 #endif
    39 #ifdef WITH_MPG123
    40 #include "core/decode_mp3_mpg123.h"
    41 #include "core/info_mp3_mpg123.h"
    42 #endif
    43 #ifdef WITH_ID3LIB
    44 #include "core/info_mp3_id3lib.h"
    45 #endif
    46 #include "core/exceptions.h"
    48 using namespace std;
    50 void
    51 DecoderFactory::construct(string resource, Player* player, Output* output, Visualizer* visualizer, Input** input, Decoder** decoder, Info** info)
    52 {
    53 	/* Initially, nothing has been constructed yet */
    54 	*input = NULL; *decoder = NULL; *info = NULL;
    56 	/*
    57 	 * If the resource starts with 'tone:', assume the tone generator is to be
    58 	 * used.
    59 	 */
    60 	if (resource.find("tone:") == 0) {
    61 		float tone = atof(resource.c_str() + 5);
    62 		if (tone > 0.0f) {
    63 			*decoder = new DecoderTone(player, NULL, output, visualizer);
    64 			(dynamic_cast<DecoderTone*>(*decoder))->setFrequency(tone);
    65 			return;
    66 		}
    67 	}
    69 #ifdef WITH_CURL
    70 	/*
    71 	 * If we find :// in the filename and CURL is available, assume we
    72 	 * are playing a stream.
    73 	 */
    74 	if (resource.find("://") != string::npos) {
    75 		*input = new InputRemote(resource);
    76 	} else
    77 #endif
    78 		*input = new InputFile(resource);
    80 	string extension = string(resource.begin() + resource.find_last_of(".") + 1, resource.end());
    81 	transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
    82 	try {
    83 #ifdef WITH_VORBIS
    84 		if (checkExtension(DecoderOgg::getExtensions(), extension)) {
    85 			*decoder = new DecoderOgg(player, *input, output, visualizer);
    86 			*info = new InfoOgg(*decoder);
    87 		} else
    88 #endif /* WITH_VORBIS */
    89 #ifdef WITH_FLAC
    90 		if (checkExtension(DecoderFLAC::getExtensions(), extension)) {
    91 			*decoder = new DecoderFLAC(player, *input, output, visualizer);
    92 			*info = new InfoFLAC(*decoder);
    93 		} else
    94 #endif /* WITH_FLAC */
    95 #ifdef WITH_SDL_MIXER
    96 		if (checkExtension(DecoderMIDI::getExtensions(), extension)) {
    97 			*decoder = new DecoderMIDI(player, *input, output, visualizer);
    98 		} else
    99 #endif /* WITH_SDL_MIXER */
   100 #ifdef WITH_ADPLUG
   101 		if (checkExtension(DecoderAdLib::getExtensions(), extension)) {
   102 			*decoder = new DecoderAdLib(player, *input, output, visualizer);
   103 			*info = new InfoAdLib(*decoder);
   104 		} else
   105 #endif /* WITH_ADPLUG */
   106 #ifdef WITH_MIKMOD
   107 		if (checkExtension(DecoderModule::getExtensions(), extension)) {
   108 			*decoder = new DecoderModule(player, *input, output, visualizer);
   109 			*info = new InfoModule(*decoder);
   110 		} else
   111 #endif /* WITH_MIKMOD */
   112 #ifdef WITH_SIDPLAY2
   113 		if (checkExtension(DecoderSID::getExtensions(), extension)) {
   114 			*decoder = new DecoderSID(player, *input, output, visualizer);
   115 			*info = new InfoSID(*decoder);
   116 		} else
   117 #endif /* WITH_SIDPLAY2 */
   118 #if defined(WITH_MAD) || defined(WITH_MPG123)
   119 		{
   120 			/* assume MP3 */
   121 #ifdef WITH_MPG123
   122 			*decoder = new DecoderMP3_MPG123(player, *input, output, visualizer);
   123 #ifdef WITH_ID3LIB
   124 			*info = new InfoMP3_ID3Lib(*decoder);
   125 #else
   126 			*info = new InfoMP3_MPG123(*decoder);
   127 #endif /* WITH_ID3LIB */
   128 #else
   129 			*decoder = new DecoderMP3(player, *input, output, visualizer);
   130 #ifdef WITH_ID3LIB
   131 			*info = new InfoMP3_ID3Lib(*decoder);
   132 #elif defined(WITH_ID3TAG)
   133 			*info = new InfoMP3(*decoder);
   134 #endif
   135 #endif
   136 #else /* WITH_MAD && WITH_MPG123 */
   137 		{
   138 			delete *input; *input = NULL;
   139 			return;
   140 #endif /* !WITH_MAD && !WITH_MPG123 */
   141 		}
   142 		if (*info != NULL)
   143 			(*info)->load(resource.c_str());
   144 	} catch (InfoException &e) {
   145 		/* Failure to obtain information should not be critical */
   146 		*info = NULL;
   147 	}
   148 }
   150 bool
   151 DecoderFactory::checkExtension(std::list<string> extensions, std::string ext)
   152 {
   153 	return find(extensions.begin(), extensions.end(), ext) != extensions.end();
   154 }
   156 void
   157 DecoderFactory::getExtensions(std::list<std::string>& extensions)
   158 {
   159 #define ADD_EXTENSIONS(exts,cl) do {                                      \
   160 	list<string> l = cl::getExtensions();                                   \
   161 	for (list<string>::iterator it = l.begin(); it != l.end(); it++)        \
   162 		exts.push_back(*it);                                                  \
   163 } while (0);
   165 #ifdef WITH_VORBIS
   166 	ADD_EXTENSIONS(extensions, DecoderOgg);
   167 #endif
   168 #ifdef WITH_FLAC
   169 	ADD_EXTENSIONS(extensions, DecoderFLAC);
   170 #endif
   171 #ifdef WITH_MIKMOD
   172 	ADD_EXTENSIONS(extensions, DecoderModule);
   173 #endif
   174 #ifdef WITH_SIDPLAY2
   175 	ADD_EXTENSIONS(extensions, DecoderSID);
   176 #endif
   177 #ifdef WITH_ADPLUG
   178 	ADD_EXTENSIONS(extensions, DecoderAdLib);
   179 #endif
   180 #ifdef WITH_SDL_MIXER
   181 	ADD_EXTENSIONS(extensions, DecoderMIDI);
   182 #endif
   183 #ifdef WITH_MPG123
   184 	ADD_EXTENSIONS(extensions, DecoderMP3_MPG123);
   185 #else
   186 #ifdef WITH_MAD
   187 	ADD_EXTENSIONS(extensions, DecoderMP3);
   188 #endif
   189 #endif
   190 }
   192 /* vim:set ts=2 sw=2: */
Powered by FreeBSD, PostgreSQL and Perl
© 2001 - 2011 Rink Springer