dynamic_mp3.c
branchSDL-1.2
changeset 902 6c862e733898
parent 542 3de4970b36d4
child 908 6b860486ce24
     1.1 --- a/dynamic_mp3.c	Sun Oct 07 17:56:56 2018 +0300
     1.2 +++ b/dynamic_mp3.c	Thu Oct 11 11:50:10 2018 +0300
     1.3 @@ -25,147 +25,64 @@
     1.4  
     1.5  #include "dynamic_mp3.h"
     1.6  
     1.7 -smpeg_loader smpeg = {
     1.8 +mpg123_loader mpg123 = {
     1.9  	0, NULL
    1.10  };
    1.11  
    1.12 -#ifdef MP3_DYNAMIC
    1.13 -int Mix_InitMP3()
    1.14 +#ifdef MPG123_DYNAMIC
    1.15 +#define FUNCTION_LOADER(FUNC, SIG) \
    1.16 +	mpg123.FUNC = (SIG) SDL_LoadFunction(mpg123.handle, #FUNC); \
    1.17 +	if (mpg123.FUNC == NULL) { SDL_UnloadObject(mpg123.handle); return -1; }
    1.18 +#else
    1.19 +#define FUNCTION_LOADER(FUNC, SIG) \
    1.20 +	mpg123.FUNC = FUNC;
    1.21 +#endif
    1.22 +
    1.23 +int Mix_InitMP3(void)
    1.24  {
    1.25 -	if ( smpeg.loaded == 0 ) {
    1.26 -		smpeg.handle = SDL_LoadObject(MP3_DYNAMIC);
    1.27 -		if ( smpeg.handle == NULL ) {
    1.28 +	if (mpg123.loaded == 0) {
    1.29 +#ifdef MPG123_DYNAMIC
    1.30 +		mpg123.handle = SDL_LoadObject(MPG123_DYNAMIC);
    1.31 +		if (mpg123.handle == NULL) {
    1.32  			return -1;
    1.33  		}
    1.34 -		smpeg.SMPEG_actualSpec =
    1.35 -			(void (*)( SMPEG *, SDL_AudioSpec * ))
    1.36 -			SDL_LoadFunction(smpeg.handle, "SMPEG_actualSpec");
    1.37 -		if ( smpeg.SMPEG_actualSpec == NULL ) {
    1.38 -			SDL_UnloadObject(smpeg.handle);
    1.39 -			return -1;
    1.40 -		}
    1.41 -		smpeg.SMPEG_delete =
    1.42 -			(void (*)( SMPEG* ))
    1.43 -			SDL_LoadFunction(smpeg.handle, "SMPEG_delete");
    1.44 -		if ( smpeg.SMPEG_delete == NULL ) {
    1.45 -			SDL_UnloadObject(smpeg.handle);
    1.46 -			return -1;
    1.47 -		}
    1.48 -		smpeg.SMPEG_enableaudio =
    1.49 -			(void (*)( SMPEG*, int ))
    1.50 -			SDL_LoadFunction(smpeg.handle, "SMPEG_enableaudio");
    1.51 -		if ( smpeg.SMPEG_enableaudio == NULL ) {
    1.52 -			SDL_UnloadObject(smpeg.handle);
    1.53 -			return -1;
    1.54 -		}
    1.55 -		smpeg.SMPEG_enablevideo =
    1.56 -			(void (*)( SMPEG*, int ))
    1.57 -			SDL_LoadFunction(smpeg.handle, "SMPEG_enablevideo");
    1.58 -		if ( smpeg.SMPEG_enablevideo == NULL ) {
    1.59 -			SDL_UnloadObject(smpeg.handle);
    1.60 -			return -1;
    1.61 -		}
    1.62 -		smpeg.SMPEG_new_rwops =
    1.63 -			(SMPEG* (*)(SDL_RWops *, SMPEG_Info*, int))
    1.64 -			SDL_LoadFunction(smpeg.handle, "SMPEG_new_rwops");
    1.65 -		if ( smpeg.SMPEG_new_rwops == NULL ) {
    1.66 -			SDL_UnloadObject(smpeg.handle);
    1.67 -			return -1;
    1.68 -		}
    1.69 -		smpeg.SMPEG_play =
    1.70 -			(void (*)( SMPEG* ))
    1.71 -			SDL_LoadFunction(smpeg.handle, "SMPEG_play");
    1.72 -		if ( smpeg.SMPEG_play == NULL ) {
    1.73 -			SDL_UnloadObject(smpeg.handle);
    1.74 -			return -1;
    1.75 -		}
    1.76 -		smpeg.SMPEG_playAudio =
    1.77 -			(int (*)( SMPEG *, Uint8 *, int ))
    1.78 -			SDL_LoadFunction(smpeg.handle, "SMPEG_playAudio");
    1.79 -		if ( smpeg.SMPEG_playAudio == NULL ) {
    1.80 -			SDL_UnloadObject(smpeg.handle);
    1.81 -			return -1;
    1.82 -		}
    1.83 -		smpeg.SMPEG_rewind =
    1.84 -			(void (*)( SMPEG* ))
    1.85 -			SDL_LoadFunction(smpeg.handle, "SMPEG_rewind");
    1.86 -		if ( smpeg.SMPEG_rewind == NULL ) {
    1.87 -			SDL_UnloadObject(smpeg.handle);
    1.88 -			return -1;
    1.89 -		}
    1.90 -		smpeg.SMPEG_setvolume =
    1.91 -			(void (*)( SMPEG*, int ))
    1.92 -			SDL_LoadFunction(smpeg.handle, "SMPEG_setvolume");
    1.93 -		if ( smpeg.SMPEG_setvolume == NULL ) {
    1.94 -			SDL_UnloadObject(smpeg.handle);
    1.95 -			return -1;
    1.96 -		}
    1.97 -		smpeg.SMPEG_skip =
    1.98 -			(void (*)( SMPEG*, float ))
    1.99 -			SDL_LoadFunction(smpeg.handle, "SMPEG_skip");
   1.100 -		if ( smpeg.SMPEG_skip == NULL ) {
   1.101 -			SDL_UnloadObject(smpeg.handle);
   1.102 -			return -1;
   1.103 -		}
   1.104 -		smpeg.SMPEG_status =
   1.105 -			(SMPEGstatus (*)( SMPEG* ))
   1.106 -			SDL_LoadFunction(smpeg.handle, "SMPEG_status");
   1.107 -		if ( smpeg.SMPEG_status == NULL ) {
   1.108 -			SDL_UnloadObject(smpeg.handle);
   1.109 -			return -1;
   1.110 -		}
   1.111 -		smpeg.SMPEG_stop =
   1.112 -			(void (*)( SMPEG* ))
   1.113 -			SDL_LoadFunction(smpeg.handle, "SMPEG_stop");
   1.114 -		if ( smpeg.SMPEG_stop == NULL ) {
   1.115 -			SDL_UnloadObject(smpeg.handle);
   1.116 +#endif
   1.117 +		FUNCTION_LOADER(mpg123_close, int (*)(mpg123_handle *mh))
   1.118 +		FUNCTION_LOADER(mpg123_delete, void (*)(mpg123_handle *mh))
   1.119 +		FUNCTION_LOADER(mpg123_exit, void (*)(void))
   1.120 +		FUNCTION_LOADER(mpg123_format, int (*)( mpg123_handle *mh, long rate, int channels, int encodings ))
   1.121 +		FUNCTION_LOADER(mpg123_format_none, int (*)(mpg123_handle *mh))
   1.122 +		FUNCTION_LOADER(mpg123_getformat, int (*)( mpg123_handle *mh, long *rate, int *channels, int *encoding ))
   1.123 +		FUNCTION_LOADER(mpg123_init, int (*)(void))
   1.124 +		FUNCTION_LOADER(mpg123_new, mpg123_handle *(*)(const char* decoder, int *error))
   1.125 +		FUNCTION_LOADER(mpg123_open_handle, int (*)(mpg123_handle *mh, void *iohandle))
   1.126 +		FUNCTION_LOADER(mpg123_plain_strerror, const char* (*)(int errcode))
   1.127 +		FUNCTION_LOADER(mpg123_rates, void (*)(const long **list, size_t *number));
   1.128 +		FUNCTION_LOADER(mpg123_read, int (*)(mpg123_handle *mh, unsigned char *outmemory, size_t outmemsize, size_t *done ))
   1.129 +		FUNCTION_LOADER(mpg123_replace_reader_handle, int (*)( mpg123_handle *mh, ssize_t (*r_read) (void *, void *, size_t), off_t (*r_lseek)(void *, off_t, int), void (*cleanup)(void*) ))
   1.130 +		FUNCTION_LOADER(mpg123_seek, off_t (*)( mpg123_handle *mh, off_t sampleoff, int whence ))
   1.131 +		FUNCTION_LOADER(mpg123_strerror, const char* (*)(mpg123_handle *mh))
   1.132 +		if (mpg123.mpg123_init() != MPG123_OK) {
   1.133  			return -1;
   1.134  		}
   1.135  	}
   1.136 -	++smpeg.loaded;
   1.137 +	++mpg123.loaded;
   1.138  
   1.139  	return 0;
   1.140  }
   1.141 -void Mix_QuitMP3()
   1.142 +
   1.143 +void Mix_QuitMP3(void)
   1.144  {
   1.145 -	if ( smpeg.loaded == 0 ) {
   1.146 +	if (mpg123.loaded == 0) {
   1.147  		return;
   1.148  	}
   1.149 -	if ( smpeg.loaded == 1 ) {
   1.150 -		SDL_UnloadObject(smpeg.handle);
   1.151 +	if (mpg123.loaded == 1) {
   1.152 +		mpg123.mpg123_exit();
   1.153 +#ifdef MPG123_DYNAMIC
   1.154 +		SDL_UnloadObject(mpg123.handle);
   1.155 +#endif
   1.156  	}
   1.157 -	--smpeg.loaded;
   1.158 +	--mpg123.loaded;
   1.159  }
   1.160 -#else
   1.161 -int Mix_InitMP3()
   1.162 -{
   1.163 -	if ( smpeg.loaded == 0 ) {
   1.164 -		smpeg.SMPEG_actualSpec = SMPEG_actualSpec;
   1.165 -		smpeg.SMPEG_delete = SMPEG_delete;
   1.166 -		smpeg.SMPEG_enableaudio = SMPEG_enableaudio;
   1.167 -		smpeg.SMPEG_enablevideo = SMPEG_enablevideo;
   1.168 -		smpeg.SMPEG_new_rwops = SMPEG_new_rwops;
   1.169 -		smpeg.SMPEG_play = SMPEG_play;
   1.170 -		smpeg.SMPEG_playAudio = SMPEG_playAudio;
   1.171 -		smpeg.SMPEG_rewind = SMPEG_rewind;
   1.172 -		smpeg.SMPEG_setvolume = SMPEG_setvolume;
   1.173 -		smpeg.SMPEG_skip = SMPEG_skip;
   1.174 -		smpeg.SMPEG_status = SMPEG_status;
   1.175 -		smpeg.SMPEG_stop = SMPEG_stop;
   1.176 -	}
   1.177 -	++smpeg.loaded;
   1.178 -
   1.179 -	return 0;
   1.180 -}
   1.181 -void Mix_QuitMP3()
   1.182 -{
   1.183 -	if ( smpeg.loaded == 0 ) {
   1.184 -		return;
   1.185 -	}
   1.186 -	if ( smpeg.loaded == 1 ) {
   1.187 -	}
   1.188 -	--smpeg.loaded;
   1.189 -}
   1.190 -#endif /* MP3_DYNAMIC */
   1.191  
   1.192  #endif /* MP3_MUSIC */