Added support for dynamically loading SMPEG library
authorSam Lantinga <slouken@libsdl.org>
Fri, 12 May 2006 09:42:24 +0000
changeset 3148c791a6ba872
parent 313 db79bfffb09b
child 315 ac5228eca8b2
Added support for dynamically loading SMPEG library
configure.in
dynamic_mp3.c
dynamic_mp3.h
dynamic_ogg.c
music.c
     1.1 --- a/configure.in	Fri May 12 08:31:36 2006 +0000
     1.2 +++ b/configure.in	Fri May 12 09:42:24 2006 +0000
     1.3 @@ -265,8 +265,8 @@
     1.4      AM_PATH_SMPEG($SMPEG_VERSION, have_smpeg=yes, have_smpeg=no)
     1.5      if test x$have_smpeg = xyes; then
     1.6          AC_ARG_ENABLE([music-mp3-shared],
     1.7 -AC_HELP_STRING([--enable-music-mp3-shared], [dynamically load MP3 support [[default=no]]]),
     1.8 -                      [], [enable_music_mp3_shared=no])
     1.9 +AC_HELP_STRING([--enable-music-mp3-shared], [dynamically load MP3 support [[default=yes]]]),
    1.10 +                      [], [enable_music_mp3_shared=yes])
    1.11          case "$host" in
    1.12              *-*-darwin*)
    1.13                  smpeg_lib=''
    1.14 @@ -282,6 +282,7 @@
    1.15                  done
    1.16                  ;;
    1.17          esac
    1.18 +        SOURCES="$SOURCES $srcdir/*_mp3.c"
    1.19          EXTRA_CFLAGS="$EXTRA_CFLAGS -DMP3_MUSIC $SMPEG_CFLAGS"
    1.20          if test x$enable_music_mp3_shared = xyes && test x$smpeg_lib != x; then
    1.21              echo "-- dynamic libsmpeg -> $smpeg_lib"
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/dynamic_mp3.c	Fri May 12 09:42:24 2006 +0000
     2.3 @@ -0,0 +1,180 @@
     2.4 +/*
     2.5 +    SDL_mixer:  An audio mixer library based on the SDL library
     2.6 +    Copyright (C) 1997-2004 Sam Lantinga
     2.7 +
     2.8 +    This library is free software; you can redistribute it and/or
     2.9 +    modify it under the terms of the GNU Library General Public
    2.10 +    License as published by the Free Software Foundation; either
    2.11 +    version 2 of the License, or (at your option) any later version.
    2.12 +
    2.13 +    This library is distributed in the hope that it will be useful,
    2.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.16 +    Library General Public License for more details.
    2.17 +
    2.18 +    You should have received a copy of the GNU Library General Public
    2.19 +    License along with this library; if not, write to the Free
    2.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    2.21 +
    2.22 +    Sam Lantinga
    2.23 +    slouken@libsdl.org
    2.24 +*/
    2.25 +
    2.26 +#ifdef MP3_MUSIC
    2.27 +
    2.28 +#include "SDL_loadso.h"
    2.29 +
    2.30 +#include "dynamic_mp3.h"
    2.31 +
    2.32 +smpeg_loader smpeg = {
    2.33 +	0, NULL
    2.34 +};
    2.35 +
    2.36 +#ifdef MP3_DYNAMIC
    2.37 +int Mix_InitMP3()
    2.38 +{
    2.39 +	if ( smpeg.loaded == 0 ) {
    2.40 +		smpeg.handle = SDL_LoadObject(MP3_DYNAMIC);
    2.41 +		if ( smpeg.handle == NULL ) {
    2.42 +			return -1;
    2.43 +		}
    2.44 +		smpeg.SMPEG_actualSpec =
    2.45 +			(void (*)( SMPEG *, SDL_AudioSpec * ))
    2.46 +			SDL_LoadFunction(smpeg.handle, "SMPEG_actualSpec");
    2.47 +		if ( smpeg.SMPEG_actualSpec == NULL ) {
    2.48 +			SDL_UnloadObject(smpeg.handle);
    2.49 +			return -1;
    2.50 +		}
    2.51 +		smpeg.SMPEG_delete =
    2.52 +			(void (*)( SMPEG* ))
    2.53 +			SDL_LoadFunction(smpeg.handle, "SMPEG_delete");
    2.54 +		if ( smpeg.SMPEG_delete == NULL ) {
    2.55 +			SDL_UnloadObject(smpeg.handle);
    2.56 +			return -1;
    2.57 +		}
    2.58 +		smpeg.SMPEG_enableaudio =
    2.59 +			(void (*)( SMPEG*, int ))
    2.60 +			SDL_LoadFunction(smpeg.handle, "SMPEG_enableaudio");
    2.61 +		if ( smpeg.SMPEG_enableaudio == NULL ) {
    2.62 +			SDL_UnloadObject(smpeg.handle);
    2.63 +			return -1;
    2.64 +		}
    2.65 +		smpeg.SMPEG_enablevideo =
    2.66 +			(void (*)( SMPEG*, int ))
    2.67 +			SDL_LoadFunction(smpeg.handle, "SMPEG_enablevideo");
    2.68 +		if ( smpeg.SMPEG_enablevideo == NULL ) {
    2.69 +			SDL_UnloadObject(smpeg.handle);
    2.70 +			return -1;
    2.71 +		}
    2.72 +		smpeg.SMPEG_new =
    2.73 +			(SMPEG* (*)(const char *, SMPEG_Info*, int))
    2.74 +			SDL_LoadFunction(smpeg.handle, "SMPEG_new");
    2.75 +		if ( smpeg.SMPEG_new == NULL ) {
    2.76 +			SDL_UnloadObject(smpeg.handle);
    2.77 +			return -1;
    2.78 +		}
    2.79 +		smpeg.SMPEG_new_rwops =
    2.80 +			(SMPEG* (*)(SDL_RWops *, SMPEG_Info*, int))
    2.81 +			SDL_LoadFunction(smpeg.handle, "SMPEG_new_rwops");
    2.82 +		if ( smpeg.SMPEG_new_rwops == NULL ) {
    2.83 +			SDL_UnloadObject(smpeg.handle);
    2.84 +			return -1;
    2.85 +		}
    2.86 +		smpeg.SMPEG_play =
    2.87 +			(void (*)( SMPEG* ))
    2.88 +			SDL_LoadFunction(smpeg.handle, "SMPEG_play");
    2.89 +		if ( smpeg.SMPEG_play == NULL ) {
    2.90 +			SDL_UnloadObject(smpeg.handle);
    2.91 +			return -1;
    2.92 +		}
    2.93 +		smpeg.SMPEG_playAudio =
    2.94 +			(int (*)( SMPEG *, Uint8 *, int ))
    2.95 +			SDL_LoadFunction(smpeg.handle, "SMPEG_playAudio");
    2.96 +		if ( smpeg.SMPEG_playAudio == NULL ) {
    2.97 +			SDL_UnloadObject(smpeg.handle);
    2.98 +			return -1;
    2.99 +		}
   2.100 +		smpeg.SMPEG_rewind =
   2.101 +			(void (*)( SMPEG* ))
   2.102 +			SDL_LoadFunction(smpeg.handle, "SMPEG_rewind");
   2.103 +		if ( smpeg.SMPEG_rewind == NULL ) {
   2.104 +			SDL_UnloadObject(smpeg.handle);
   2.105 +			return -1;
   2.106 +		}
   2.107 +		smpeg.SMPEG_setvolume =
   2.108 +			(void (*)( SMPEG*, int ))
   2.109 +			SDL_LoadFunction(smpeg.handle, "SMPEG_setvolume");
   2.110 +		if ( smpeg.SMPEG_setvolume == NULL ) {
   2.111 +			SDL_UnloadObject(smpeg.handle);
   2.112 +			return -1;
   2.113 +		}
   2.114 +		smpeg.SMPEG_skip =
   2.115 +			(void (*)( SMPEG*, float ))
   2.116 +			SDL_LoadFunction(smpeg.handle, "SMPEG_skip");
   2.117 +		if ( smpeg.SMPEG_skip == NULL ) {
   2.118 +			SDL_UnloadObject(smpeg.handle);
   2.119 +			return -1;
   2.120 +		}
   2.121 +		smpeg.SMPEG_status =
   2.122 +			(SMPEGstatus (*)( SMPEG* ))
   2.123 +			SDL_LoadFunction(smpeg.handle, "SMPEG_status");
   2.124 +		if ( smpeg.SMPEG_status == NULL ) {
   2.125 +			SDL_UnloadObject(smpeg.handle);
   2.126 +			return -1;
   2.127 +		}
   2.128 +		smpeg.SMPEG_stop =
   2.129 +			(void (*)( SMPEG* ))
   2.130 +			SDL_LoadFunction(smpeg.handle, "SMPEG_stop");
   2.131 +		if ( smpeg.SMPEG_stop == NULL ) {
   2.132 +			SDL_UnloadObject(smpeg.handle);
   2.133 +			return -1;
   2.134 +		}
   2.135 +	}
   2.136 +	++smpeg.loaded;
   2.137 +
   2.138 +	return 0;
   2.139 +}
   2.140 +void Mix_QuitMP3()
   2.141 +{
   2.142 +	if ( smpeg.loaded == 0 ) {
   2.143 +		return;
   2.144 +	}
   2.145 +	if ( smpeg.loaded == 1 ) {
   2.146 +		SDL_UnloadObject(smpeg.handle);
   2.147 +	}
   2.148 +	--smpeg.loaded;
   2.149 +}
   2.150 +#else
   2.151 +int Mix_InitMP3()
   2.152 +{
   2.153 +	if ( smpeg.loaded == 0 ) {
   2.154 +		smpeg.SMPEG_actualSpec = SMPEG_actualSpec;
   2.155 +		smpeg.SMPEG_delete = SMPEG_delete;
   2.156 +		smpeg.SMPEG_enableaudio = SMPEG_enableaudio;
   2.157 +		smpeg.SMPEG_enablevideo = SMPEG_enablevideo;
   2.158 +		smpeg.SMPEG_new = SMPEG_new;
   2.159 +		smpeg.SMPEG_new_rwops = SMPEG_new_rwops;
   2.160 +		smpeg.SMPEG_play = SMPEG_play;
   2.161 +		smpeg.SMPEG_playAudio = SMPEG_playAudio;
   2.162 +		smpeg.SMPEG_rewind = SMPEG_rewind;
   2.163 +		smpeg.SMPEG_setvolume = SMPEG_setvolume;
   2.164 +		smpeg.SMPEG_skip = SMPEG_skip;
   2.165 +		smpeg.SMPEG_status = SMPEG_status;
   2.166 +		smpeg.SMPEG_stop = SMPEG_stop;
   2.167 +	}
   2.168 +	++smpeg.loaded;
   2.169 +
   2.170 +	return 0;
   2.171 +}
   2.172 +void Mix_QuitMP3()
   2.173 +{
   2.174 +	if ( smpeg.loaded == 0 ) {
   2.175 +		return;
   2.176 +	}
   2.177 +	if ( smpeg.loaded == 1 ) {
   2.178 +	}
   2.179 +	--smpeg.loaded;
   2.180 +}
   2.181 +#endif /* MP3_DYNAMIC */
   2.182 +
   2.183 +#endif /* MP3_MUSIC */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/dynamic_mp3.h	Fri May 12 09:42:24 2006 +0000
     3.3 @@ -0,0 +1,49 @@
     3.4 +/*
     3.5 +    SDL_mixer:  An audio mixer library based on the SDL library
     3.6 +    Copyright (C) 1997-2004 Sam Lantinga
     3.7 +
     3.8 +    This library is free software; you can redistribute it and/or
     3.9 +    modify it under the terms of the GNU Library General Public
    3.10 +    License as published by the Free Software Foundation; either
    3.11 +    version 2 of the License, or (at your option) any later version.
    3.12 +
    3.13 +    This library is distributed in the hope that it will be useful,
    3.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.16 +    Library General Public License for more details.
    3.17 +
    3.18 +    You should have received a copy of the GNU Library General Public
    3.19 +    License along with this library; if not, write to the Free
    3.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    3.21 +
    3.22 +    Sam Lantinga
    3.23 +    slouken@libsdl.org
    3.24 +*/
    3.25 +
    3.26 +#ifdef MP3_MUSIC
    3.27 +#include "smpeg.h"
    3.28 +
    3.29 +typedef struct {
    3.30 +	int loaded;
    3.31 +	void *handle;
    3.32 +	void (*SMPEG_actualSpec)( SMPEG *mpeg, SDL_AudioSpec *spec );
    3.33 +	void (*SMPEG_delete)( SMPEG* mpeg );
    3.34 +	void (*SMPEG_enableaudio)( SMPEG* mpeg, int enable );
    3.35 +	void (*SMPEG_enablevideo)( SMPEG* mpeg, int enable );
    3.36 +	SMPEG* (*SMPEG_new)(const char *file, SMPEG_Info* info, int sdl_audio);
    3.37 +	SMPEG* (*SMPEG_new_rwops)(SDL_RWops *src, SMPEG_Info* info, int sdl_audio);
    3.38 +	void (*SMPEG_play)( SMPEG* mpeg );
    3.39 +	int (*SMPEG_playAudio)( SMPEG *mpeg, Uint8 *stream, int len );
    3.40 +	void (*SMPEG_rewind)( SMPEG* mpeg );
    3.41 +	void (*SMPEG_setvolume)( SMPEG* mpeg, int volume );
    3.42 +	void (*SMPEG_skip)( SMPEG* mpeg, float seconds );
    3.43 +	SMPEGstatus (*SMPEG_status)( SMPEG* mpeg );
    3.44 +	void (*SMPEG_stop)( SMPEG* mpeg );
    3.45 +} smpeg_loader;
    3.46 +
    3.47 +extern smpeg_loader smpeg;
    3.48 +
    3.49 +extern int Mix_InitMP3();
    3.50 +extern void Mix_QuitMP3();
    3.51 +
    3.52 +#endif
     4.1 --- a/dynamic_ogg.c	Fri May 12 08:31:36 2006 +0000
     4.2 +++ b/dynamic_ogg.c	Fri May 12 09:42:24 2006 +0000
     4.3 @@ -115,6 +115,8 @@
     4.4  		vorbis.ov_time_seek = ov_time_seek;
     4.5  	}
     4.6  	++vorbis.loaded;
     4.7 +
     4.8 +	return 0;
     4.9  }
    4.10  void Mix_QuitOgg()
    4.11  {
     5.1 --- a/music.c	Fri May 12 08:31:36 2006 +0000
     5.2 +++ b/music.c	Fri May 12 09:42:24 2006 +0000
     5.3 @@ -84,7 +84,7 @@
     5.4  #include "music_ogg.h"
     5.5  #endif
     5.6  #ifdef MP3_MUSIC
     5.7 -#include "smpeg.h"
     5.8 +#include "dynamic_mp3.h"
     5.9  
    5.10  static SDL_AudioSpec used_mixer;
    5.11  #endif
    5.12 @@ -338,7 +338,7 @@
    5.13  #endif
    5.14  #ifdef MP3_MUSIC
    5.15  			case MUS_MP3:
    5.16 -				SMPEG_playAudio(music_playing->data.mp3, stream, len);
    5.17 +				smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len);
    5.18  				break;
    5.19  #endif
    5.20  			default:
    5.21 @@ -593,14 +593,18 @@
    5.22  	     (ext && MIX_string_equals(ext, "MP3")) ||
    5.23  	     (ext && MIX_string_equals(ext, "MPEG")) ||
    5.24  	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ) {
    5.25 -		SMPEG_Info info;
    5.26 -		music->type = MUS_MP3;
    5.27 -		music->data.mp3 = SMPEG_new(file, &info, 0);
    5.28 -		if ( !info.has_audio ) {
    5.29 -			Mix_SetError("MPEG file does not have any audio stream.");
    5.30 +		if ( Mix_InitMP3() == 0 ) {
    5.31 +			SMPEG_Info info;
    5.32 +			music->type = MUS_MP3;
    5.33 +			music->data.mp3 = smpeg.SMPEG_new(file, &info, 0);
    5.34 +			if ( !info.has_audio ) {
    5.35 +				Mix_SetError("MPEG file does not have any audio stream.");
    5.36 +				music->error = 1;
    5.37 +			} else {
    5.38 +				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
    5.39 +			}
    5.40 +		} else {
    5.41  			music->error = 1;
    5.42 -		} else {
    5.43 -			SMPEG_actualSpec(music->data.mp3, &used_mixer);
    5.44  		}
    5.45  	} else
    5.46  #endif
    5.47 @@ -690,7 +694,8 @@
    5.48  #endif
    5.49  #ifdef MP3_MUSIC
    5.50  			case MUS_MP3:
    5.51 -				SMPEG_delete(music->data.mp3);
    5.52 +				smpeg.SMPEG_delete(music->data.mp3);
    5.53 +				Mix_QuitMP3();
    5.54  				break;
    5.55  #endif
    5.56  			default:
    5.57 @@ -777,9 +782,9 @@
    5.58  #endif
    5.59  #ifdef MP3_MUSIC
    5.60  	    case MUS_MP3:
    5.61 -		SMPEG_enableaudio(music->data.mp3,1);
    5.62 -		SMPEG_enablevideo(music->data.mp3,0);
    5.63 -		SMPEG_play(music_playing->data.mp3);
    5.64 +		smpeg.SMPEG_enableaudio(music->data.mp3,1);
    5.65 +		smpeg.SMPEG_enablevideo(music->data.mp3,0);
    5.66 +		smpeg.SMPEG_play(music_playing->data.mp3);
    5.67  		break;
    5.68  #endif
    5.69  	    default:
    5.70 @@ -868,10 +873,10 @@
    5.71  #ifdef MP3_MUSIC
    5.72  	    case MUS_MP3:
    5.73  		if ( position > 0.0 ) {
    5.74 -			SMPEG_skip(music_playing->data.mp3, (float)position);
    5.75 +			smpeg.SMPEG_skip(music_playing->data.mp3, (float)position);
    5.76  		} else {
    5.77 -			SMPEG_rewind(music_playing->data.mp3);
    5.78 -			SMPEG_play(music_playing->data.mp3);
    5.79 +			smpeg.SMPEG_rewind(music_playing->data.mp3);
    5.80 +			smpeg.SMPEG_play(music_playing->data.mp3);
    5.81  		}
    5.82  		break;
    5.83  #endif
    5.84 @@ -951,7 +956,7 @@
    5.85  #endif
    5.86  #ifdef MP3_MUSIC
    5.87  	    case MUS_MP3:
    5.88 -		SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
    5.89 +		smpeg.SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
    5.90  		break;
    5.91  #endif
    5.92  	    default:
    5.93 @@ -1019,7 +1024,7 @@
    5.94  #endif
    5.95  #ifdef MP3_MUSIC
    5.96  	    case MUS_MP3:
    5.97 -		SMPEG_stop(music_playing->data.mp3);
    5.98 +		smpeg.SMPEG_stop(music_playing->data.mp3);
    5.99  		break;
   5.100  #endif
   5.101  	    default:
   5.102 @@ -1161,7 +1166,7 @@
   5.103  #endif
   5.104  #ifdef MP3_MUSIC
   5.105  	    case MUS_MP3:
   5.106 -		if ( SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
   5.107 +		if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
   5.108  			playing = 0;
   5.109  		break;
   5.110  #endif
   5.111 @@ -1388,14 +1393,18 @@
   5.112  #endif
   5.113  #ifdef MP3_MUSIC
   5.114  	if ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0 ) {
   5.115 -		SMPEG_Info info;
   5.116 -		music->type = MUS_MP3;
   5.117 -		music->data.mp3 = SMPEG_new_rwops(rw, &info, 0);
   5.118 -		if ( !info.has_audio ) {
   5.119 -			Mix_SetError("MPEG file does not have any audio stream.");
   5.120 +		if ( Mix_InitMP3() == 0 ) {
   5.121 +			SMPEG_Info info;
   5.122 +			music->type = MUS_MP3;
   5.123 +			music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
   5.124 +			if ( !info.has_audio ) {
   5.125 +				Mix_SetError("MPEG file does not have any audio stream.");
   5.126 +				music->error = 1;
   5.127 +			} else {
   5.128 +				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
   5.129 +			}
   5.130 +		} else {
   5.131  			music->error = 1;
   5.132 -		} else {
   5.133 -			SMPEG_actualSpec(music->data.mp3, &used_mixer);
   5.134  		}
   5.135  	} else
   5.136  #endif