dynamic_mp3.c
branchSDL-1.2
changeset 902 6c862e733898
parent 542 3de4970b36d4
child 908 6b860486ce24
equal deleted inserted replaced
898:b5d14516f1df 902:6c862e733898
    23 
    23 
    24 #include "SDL_loadso.h"
    24 #include "SDL_loadso.h"
    25 
    25 
    26 #include "dynamic_mp3.h"
    26 #include "dynamic_mp3.h"
    27 
    27 
    28 smpeg_loader smpeg = {
    28 mpg123_loader mpg123 = {
    29 	0, NULL
    29 	0, NULL
    30 };
    30 };
    31 
    31 
    32 #ifdef MP3_DYNAMIC
    32 #ifdef MPG123_DYNAMIC
    33 int Mix_InitMP3()
    33 #define FUNCTION_LOADER(FUNC, SIG) \
       
    34 	mpg123.FUNC = (SIG) SDL_LoadFunction(mpg123.handle, #FUNC); \
       
    35 	if (mpg123.FUNC == NULL) { SDL_UnloadObject(mpg123.handle); return -1; }
       
    36 #else
       
    37 #define FUNCTION_LOADER(FUNC, SIG) \
       
    38 	mpg123.FUNC = FUNC;
       
    39 #endif
       
    40 
       
    41 int Mix_InitMP3(void)
    34 {
    42 {
    35 	if ( smpeg.loaded == 0 ) {
    43 	if (mpg123.loaded == 0) {
    36 		smpeg.handle = SDL_LoadObject(MP3_DYNAMIC);
    44 #ifdef MPG123_DYNAMIC
    37 		if ( smpeg.handle == NULL ) {
    45 		mpg123.handle = SDL_LoadObject(MPG123_DYNAMIC);
       
    46 		if (mpg123.handle == NULL) {
    38 			return -1;
    47 			return -1;
    39 		}
    48 		}
    40 		smpeg.SMPEG_actualSpec =
    49 #endif
    41 			(void (*)( SMPEG *, SDL_AudioSpec * ))
    50 		FUNCTION_LOADER(mpg123_close, int (*)(mpg123_handle *mh))
    42 			SDL_LoadFunction(smpeg.handle, "SMPEG_actualSpec");
    51 		FUNCTION_LOADER(mpg123_delete, void (*)(mpg123_handle *mh))
    43 		if ( smpeg.SMPEG_actualSpec == NULL ) {
    52 		FUNCTION_LOADER(mpg123_exit, void (*)(void))
    44 			SDL_UnloadObject(smpeg.handle);
    53 		FUNCTION_LOADER(mpg123_format, int (*)( mpg123_handle *mh, long rate, int channels, int encodings ))
    45 			return -1;
    54 		FUNCTION_LOADER(mpg123_format_none, int (*)(mpg123_handle *mh))
    46 		}
    55 		FUNCTION_LOADER(mpg123_getformat, int (*)( mpg123_handle *mh, long *rate, int *channels, int *encoding ))
    47 		smpeg.SMPEG_delete =
    56 		FUNCTION_LOADER(mpg123_init, int (*)(void))
    48 			(void (*)( SMPEG* ))
    57 		FUNCTION_LOADER(mpg123_new, mpg123_handle *(*)(const char* decoder, int *error))
    49 			SDL_LoadFunction(smpeg.handle, "SMPEG_delete");
    58 		FUNCTION_LOADER(mpg123_open_handle, int (*)(mpg123_handle *mh, void *iohandle))
    50 		if ( smpeg.SMPEG_delete == NULL ) {
    59 		FUNCTION_LOADER(mpg123_plain_strerror, const char* (*)(int errcode))
    51 			SDL_UnloadObject(smpeg.handle);
    60 		FUNCTION_LOADER(mpg123_rates, void (*)(const long **list, size_t *number));
    52 			return -1;
    61 		FUNCTION_LOADER(mpg123_read, int (*)(mpg123_handle *mh, unsigned char *outmemory, size_t outmemsize, size_t *done ))
    53 		}
    62 		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*) ))
    54 		smpeg.SMPEG_enableaudio =
    63 		FUNCTION_LOADER(mpg123_seek, off_t (*)( mpg123_handle *mh, off_t sampleoff, int whence ))
    55 			(void (*)( SMPEG*, int ))
    64 		FUNCTION_LOADER(mpg123_strerror, const char* (*)(mpg123_handle *mh))
    56 			SDL_LoadFunction(smpeg.handle, "SMPEG_enableaudio");
    65 		if (mpg123.mpg123_init() != MPG123_OK) {
    57 		if ( smpeg.SMPEG_enableaudio == NULL ) {
       
    58 			SDL_UnloadObject(smpeg.handle);
       
    59 			return -1;
       
    60 		}
       
    61 		smpeg.SMPEG_enablevideo =
       
    62 			(void (*)( SMPEG*, int ))
       
    63 			SDL_LoadFunction(smpeg.handle, "SMPEG_enablevideo");
       
    64 		if ( smpeg.SMPEG_enablevideo == NULL ) {
       
    65 			SDL_UnloadObject(smpeg.handle);
       
    66 			return -1;
       
    67 		}
       
    68 		smpeg.SMPEG_new_rwops =
       
    69 			(SMPEG* (*)(SDL_RWops *, SMPEG_Info*, int))
       
    70 			SDL_LoadFunction(smpeg.handle, "SMPEG_new_rwops");
       
    71 		if ( smpeg.SMPEG_new_rwops == NULL ) {
       
    72 			SDL_UnloadObject(smpeg.handle);
       
    73 			return -1;
       
    74 		}
       
    75 		smpeg.SMPEG_play =
       
    76 			(void (*)( SMPEG* ))
       
    77 			SDL_LoadFunction(smpeg.handle, "SMPEG_play");
       
    78 		if ( smpeg.SMPEG_play == NULL ) {
       
    79 			SDL_UnloadObject(smpeg.handle);
       
    80 			return -1;
       
    81 		}
       
    82 		smpeg.SMPEG_playAudio =
       
    83 			(int (*)( SMPEG *, Uint8 *, int ))
       
    84 			SDL_LoadFunction(smpeg.handle, "SMPEG_playAudio");
       
    85 		if ( smpeg.SMPEG_playAudio == NULL ) {
       
    86 			SDL_UnloadObject(smpeg.handle);
       
    87 			return -1;
       
    88 		}
       
    89 		smpeg.SMPEG_rewind =
       
    90 			(void (*)( SMPEG* ))
       
    91 			SDL_LoadFunction(smpeg.handle, "SMPEG_rewind");
       
    92 		if ( smpeg.SMPEG_rewind == NULL ) {
       
    93 			SDL_UnloadObject(smpeg.handle);
       
    94 			return -1;
       
    95 		}
       
    96 		smpeg.SMPEG_setvolume =
       
    97 			(void (*)( SMPEG*, int ))
       
    98 			SDL_LoadFunction(smpeg.handle, "SMPEG_setvolume");
       
    99 		if ( smpeg.SMPEG_setvolume == NULL ) {
       
   100 			SDL_UnloadObject(smpeg.handle);
       
   101 			return -1;
       
   102 		}
       
   103 		smpeg.SMPEG_skip =
       
   104 			(void (*)( SMPEG*, float ))
       
   105 			SDL_LoadFunction(smpeg.handle, "SMPEG_skip");
       
   106 		if ( smpeg.SMPEG_skip == NULL ) {
       
   107 			SDL_UnloadObject(smpeg.handle);
       
   108 			return -1;
       
   109 		}
       
   110 		smpeg.SMPEG_status =
       
   111 			(SMPEGstatus (*)( SMPEG* ))
       
   112 			SDL_LoadFunction(smpeg.handle, "SMPEG_status");
       
   113 		if ( smpeg.SMPEG_status == NULL ) {
       
   114 			SDL_UnloadObject(smpeg.handle);
       
   115 			return -1;
       
   116 		}
       
   117 		smpeg.SMPEG_stop =
       
   118 			(void (*)( SMPEG* ))
       
   119 			SDL_LoadFunction(smpeg.handle, "SMPEG_stop");
       
   120 		if ( smpeg.SMPEG_stop == NULL ) {
       
   121 			SDL_UnloadObject(smpeg.handle);
       
   122 			return -1;
    66 			return -1;
   123 		}
    67 		}
   124 	}
    68 	}
   125 	++smpeg.loaded;
    69 	++mpg123.loaded;
   126 
    70 
   127 	return 0;
    71 	return 0;
   128 }
    72 }
   129 void Mix_QuitMP3()
    73 
       
    74 void Mix_QuitMP3(void)
   130 {
    75 {
   131 	if ( smpeg.loaded == 0 ) {
    76 	if (mpg123.loaded == 0) {
   132 		return;
    77 		return;
   133 	}
    78 	}
   134 	if ( smpeg.loaded == 1 ) {
    79 	if (mpg123.loaded == 1) {
   135 		SDL_UnloadObject(smpeg.handle);
    80 		mpg123.mpg123_exit();
       
    81 #ifdef MPG123_DYNAMIC
       
    82 		SDL_UnloadObject(mpg123.handle);
       
    83 #endif
   136 	}
    84 	}
   137 	--smpeg.loaded;
    85 	--mpg123.loaded;
   138 }
    86 }
   139 #else
       
   140 int Mix_InitMP3()
       
   141 {
       
   142 	if ( smpeg.loaded == 0 ) {
       
   143 		smpeg.SMPEG_actualSpec = SMPEG_actualSpec;
       
   144 		smpeg.SMPEG_delete = SMPEG_delete;
       
   145 		smpeg.SMPEG_enableaudio = SMPEG_enableaudio;
       
   146 		smpeg.SMPEG_enablevideo = SMPEG_enablevideo;
       
   147 		smpeg.SMPEG_new_rwops = SMPEG_new_rwops;
       
   148 		smpeg.SMPEG_play = SMPEG_play;
       
   149 		smpeg.SMPEG_playAudio = SMPEG_playAudio;
       
   150 		smpeg.SMPEG_rewind = SMPEG_rewind;
       
   151 		smpeg.SMPEG_setvolume = SMPEG_setvolume;
       
   152 		smpeg.SMPEG_skip = SMPEG_skip;
       
   153 		smpeg.SMPEG_status = SMPEG_status;
       
   154 		smpeg.SMPEG_stop = SMPEG_stop;
       
   155 	}
       
   156 	++smpeg.loaded;
       
   157 
       
   158 	return 0;
       
   159 }
       
   160 void Mix_QuitMP3()
       
   161 {
       
   162 	if ( smpeg.loaded == 0 ) {
       
   163 		return;
       
   164 	}
       
   165 	if ( smpeg.loaded == 1 ) {
       
   166 	}
       
   167 	--smpeg.loaded;
       
   168 }
       
   169 #endif /* MP3_DYNAMIC */
       
   170 
    87 
   171 #endif /* MP3_MUSIC */
    88 #endif /* MP3_MUSIC */