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