Added SDL_RWops support for reading MP3 files
authorSam Lantinga <slouken@libsdl.org>
Tue, 21 Dec 2004 20:08:57 +0000
changeset 2566d7e102500c9
parent 255 16119b6dc45a
child 257 8a32c0a3c609
Added SDL_RWops support for reading MP3 files
CHANGES
music.c
playmus.c
     1.1 --- a/CHANGES	Tue Dec 21 17:52:16 2004 +0000
     1.2 +++ b/CHANGES	Tue Dec 21 20:08:57 2004 +0000
     1.3 @@ -2,6 +2,7 @@
     1.4  Sam Lantinga - Tue Dec 21 09:51:29 PST 2004
     1.5   * Fixed building mikmod support on UNIX
     1.6   * Always build SDL_RWops music support
     1.7 + * Added SDL_RWops support for reading MP3 files
     1.8  
     1.9  1.2.6:
    1.10  Jonathan Atkins - Wed, 15 Sep 2004 23:26:42 -0500
     2.1 --- a/music.c	Tue Dec 21 17:52:16 2004 +0000
     2.2 +++ b/music.c	Tue Dec 21 20:08:57 2004 +0000
     2.3 @@ -560,14 +560,14 @@
     2.4  	if ( (ext && MIX_string_equals(ext, "MPG")) ||
     2.5  	     (ext && MIX_string_equals(ext, "MP3")) ||
     2.6  	     (ext && MIX_string_equals(ext, "MPEG")) ||
     2.7 -	     magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
     2.8 +	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ) {
     2.9  		SMPEG_Info info;
    2.10  		music->type = MUS_MP3;
    2.11  		music->data.mp3 = SMPEG_new(file, &info, 0);
    2.12 -		if(!info.has_audio){
    2.13 +		if ( !info.has_audio ) {
    2.14  			Mix_SetError("MPEG file does not have any audio stream.");
    2.15  			music->error = 1;
    2.16 -		}else{
    2.17 +		} else {
    2.18  			SMPEG_actualSpec(music->data.mp3, &used_mixer);
    2.19  		}
    2.20  	} else
    2.21 @@ -1330,6 +1330,19 @@
    2.22  		}
    2.23  	} else
    2.24  #endif
    2.25 +#ifdef MP3_MUSIC
    2.26 +	if ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0 ) {
    2.27 +		SMPEG_Info info;
    2.28 +		music->type = MUS_MP3;
    2.29 +		music->data.mp3 = SMPEG_new_rwops(rw, &info, 0);
    2.30 +		if ( !info.has_audio ) {
    2.31 +			Mix_SetError("MPEG file does not have any audio stream.");
    2.32 +			music->error = 1;
    2.33 +		} else {
    2.34 +			SMPEG_actualSpec(music->data.mp3, &used_mixer);
    2.35 +		}
    2.36 +	} else
    2.37 +#endif
    2.38  #if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
    2.39  	if (1) {
    2.40  		music->type=MUS_MOD;
     3.1 --- a/playmus.c	Tue Dec 21 17:52:16 2004 +0000
     3.2 +++ b/playmus.c	Tue Dec 21 20:08:57 2004 +0000
     3.3 @@ -57,7 +57,7 @@
     3.4  
     3.5  void Usage(char *argv0)
     3.6  {
     3.7 -	fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] <musicfile>\n", argv0);
     3.8 +	fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] <musicfile>\n", argv0);
     3.9  }
    3.10  
    3.11  void Menu(void)
    3.12 @@ -93,6 +93,7 @@
    3.13  
    3.14  int main(int argc, char *argv[])
    3.15  {
    3.16 +	SDL_RWops *rwfp;
    3.17  	int audio_rate;
    3.18  	Uint16 audio_format;
    3.19  	int audio_channels;
    3.20 @@ -100,6 +101,7 @@
    3.21  	int audio_volume = MIX_MAX_VOLUME;
    3.22  	int looping = 0;
    3.23  	int interactive = 0;
    3.24 +	int rwops = 0;
    3.25  	int i;
    3.26  
    3.27  	/* Initialize variables */
    3.28 @@ -137,6 +139,9 @@
    3.29  		} else
    3.30  		if ( strcmp(argv[i], "-8") == 0 ) {
    3.31  			audio_format = AUDIO_U8;
    3.32 +		} else
    3.33 +		if ( strcmp(argv[i], "-rwops") == 0 ) {
    3.34 +			rwops = 1;
    3.35  		} else {
    3.36  			Usage(argv[0]);
    3.37  			return(1);
    3.38 @@ -180,7 +185,12 @@
    3.39  		next_track = 0;
    3.40  		
    3.41  		/* Load the requested music file */
    3.42 -		music = Mix_LoadMUS(argv[i]);
    3.43 +		if ( rwops ) {
    3.44 +			rwfp = SDL_RWFromFile(argv[i], "rb");
    3.45 +			music = Mix_LoadMUS(argv[i]);
    3.46 +		} else {
    3.47 +			music = Mix_LoadMUS(argv[i]);
    3.48 +		}
    3.49  		if ( music == NULL ) {
    3.50  			fprintf(stderr, "Couldn't load %s: %s\n",
    3.51  				argv[i], SDL_GetError());
    3.52 @@ -197,6 +207,9 @@
    3.53  				SDL_Delay(100);
    3.54  		}
    3.55  		Mix_FreeMusic(music);
    3.56 +		if ( rwops ) {
    3.57 +			SDL_FreeRW(rwfp);
    3.58 +		}
    3.59  		music = NULL;
    3.60  
    3.61  		/* If the user presses Ctrl-C more than once, exit. */