music.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 13 Apr 2010 22:28:38 -0700
changeset 487 485a29de2b2d
parent 484 a77df8583d18
child 499 6ab2b1f10df9
permissions -rw-r--r--
Fixed uninitialized variable which could definitely have caused problems.
     1 /*
     2     SDL_mixer:  An audio mixer library based on the SDL library
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    24 
    25 #include <stdlib.h>
    26 #include <string.h>
    27 #include <ctype.h>
    28 #include <assert.h>
    29 #include "SDL_endian.h"
    30 #include "SDL_audio.h"
    31 #include "SDL_timer.h"
    32 
    33 #include "SDL_mixer.h"
    34 
    35 #ifdef CMD_MUSIC
    36 #include "music_cmd.h"
    37 #endif
    38 #ifdef WAV_MUSIC
    39 #include "wavestream.h"
    40 #endif
    41 #ifdef MODPLUG_MUSIC
    42 #include "music_modplug.h"
    43 #endif
    44 #ifdef MOD_MUSIC
    45 #include "music_mod.h"
    46 #endif
    47 #ifdef MID_MUSIC
    48 #  ifdef USE_TIMIDITY_MIDI
    49 #    include "timidity.h"
    50 #  endif
    51 #  ifdef USE_NATIVE_MIDI
    52 #    include "native_midi.h"
    53 #  endif
    54 #  if defined(USE_TIMIDITY_MIDI) && defined(USE_NATIVE_MIDI)
    55 #    define MIDI_ELSE	else
    56 #  else
    57 #    define MIDI_ELSE
    58 #  endif
    59 #endif
    60 #ifdef OGG_MUSIC
    61 #include "music_ogg.h"
    62 #endif
    63 #ifdef MP3_MUSIC
    64 #include "dynamic_mp3.h"
    65 #endif
    66 #ifdef MP3_MAD_MUSIC
    67 #include "music_mad.h"
    68 #endif
    69 #ifdef FLAC_MUSIC
    70 #include "music_flac.h"
    71 #endif
    72 
    73 #if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
    74 static SDL_AudioSpec used_mixer;
    75 #endif
    76 
    77 
    78 int volatile music_active = 1;
    79 static int volatile music_stopped = 0;
    80 static int music_loops = 0;
    81 static char *music_cmd = NULL;
    82 static Mix_Music * volatile music_playing = NULL;
    83 static int music_volume = MIX_MAX_VOLUME;
    84 
    85 struct _Mix_Music {
    86 	Mix_MusicType type;
    87 	union {
    88 #ifdef CMD_MUSIC
    89 		MusicCMD *cmd;
    90 #endif
    91 #ifdef WAV_MUSIC
    92 		WAVStream *wave;
    93 #endif
    94 #ifdef MODPLUG_MUSIC
    95 		modplug_data *modplug;
    96 #endif
    97 #ifdef MOD_MUSIC
    98 		struct MODULE *module;
    99 #endif
   100 #ifdef MID_MUSIC
   101 #ifdef USE_TIMIDITY_MIDI
   102 		MidiSong *midi;
   103 #endif
   104 #ifdef USE_NATIVE_MIDI
   105 		NativeMidiSong *nativemidi;
   106 #endif
   107 #endif
   108 #ifdef OGG_MUSIC
   109 		OGG_music *ogg;
   110 #endif
   111 #ifdef MP3_MUSIC
   112 		SMPEG *mp3;
   113 #endif
   114 #ifdef MP3_MAD_MUSIC
   115 		mad_data *mp3_mad;
   116 #endif
   117 #ifdef FLAC_MUSIC
   118 		FLAC_music *flac;
   119 #endif
   120 	} data;
   121 	Mix_Fading fading;
   122 	int fade_step;
   123 	int fade_steps;
   124 	int error;
   125 };
   126 #ifdef MID_MUSIC
   127 #ifdef USE_TIMIDITY_MIDI
   128 static int timidity_ok;
   129 static int samplesize;
   130 #endif
   131 #ifdef USE_NATIVE_MIDI
   132 static int native_midi_ok;
   133 #endif
   134 #endif
   135 
   136 /* Used to calculate fading steps */
   137 static int ms_per_step;
   138 
   139 /* rcg06042009 report available decoders at runtime. */
   140 static const char **music_decoders = NULL;
   141 static int num_decoders = 0;
   142 
   143 int Mix_GetNumMusicDecoders(void)
   144 {
   145 	return(num_decoders);
   146 }
   147 
   148 const char *Mix_GetMusicDecoder(int index)
   149 {
   150 	if ((index < 0) || (index >= num_decoders)) {
   151 		return NULL;
   152 	}
   153 	return(music_decoders[index]);
   154 }
   155 
   156 static void add_music_decoder(const char *decoder)
   157 {
   158 	void *ptr = realloc(music_decoders, (num_decoders + 1) * sizeof (const char **));
   159 	if (ptr == NULL) {
   160 		return;  /* oh well, go on without it. */
   161 	}
   162 	music_decoders = (const char **) ptr;
   163 	music_decoders[num_decoders++] = decoder;
   164 }
   165 
   166 /* Local low-level functions prototypes */
   167 static void music_internal_initialize_volume(void);
   168 static void music_internal_volume(int volume);
   169 static int  music_internal_play(Mix_Music *music, double position);
   170 static int  music_internal_position(double position);
   171 static int  music_internal_playing();
   172 static void music_internal_halt(void);
   173 
   174 
   175 /* Support for hooking when the music has finished */
   176 static void (*music_finished_hook)(void) = NULL;
   177 
   178 void Mix_HookMusicFinished(void (*music_finished)(void))
   179 {
   180 	SDL_LockAudio();
   181 	music_finished_hook = music_finished;
   182 	SDL_UnlockAudio();
   183 }
   184 
   185 
   186 /* If music isn't playing, halt it if no looping is required, restart it */
   187 /* otherwhise. NOP if the music is playing */
   188 static int music_halt_or_loop (void)
   189 {
   190 	/* Restart music if it has to loop */
   191 	
   192 	if (!music_internal_playing()) 
   193 	{
   194 		/* Restart music if it has to loop at a high level */
   195 		if (music_loops && --music_loops)
   196 		{
   197 			Mix_Fading current_fade = music_playing->fading;
   198 			music_internal_play(music_playing, 0.0);
   199 			music_playing->fading = current_fade;
   200 		} 
   201 		else 
   202 		{
   203 			music_internal_halt();
   204 			if (music_finished_hook)
   205 				music_finished_hook();
   206 			
   207 			return 0;
   208 		}
   209 	}
   210 	
   211 	return 1;
   212 }
   213 
   214 
   215 
   216 /* Mixing function */
   217 void music_mixer(void *udata, Uint8 *stream, int len)
   218 {
   219 	int left = 0;
   220 
   221 	if ( music_playing && music_active ) {
   222 		/* Handle fading */
   223 		if ( music_playing->fading != MIX_NO_FADING ) {
   224 			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
   225 				int volume;
   226 				int fade_step = music_playing->fade_step;
   227 				int fade_steps = music_playing->fade_steps;
   228 
   229 				if ( music_playing->fading == MIX_FADING_OUT ) {
   230 					volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
   231 				} else { /* Fading in */
   232 					volume = (music_volume * fade_step) / fade_steps;
   233 				}
   234 				music_internal_volume(volume);
   235 			} else {
   236 				if ( music_playing->fading == MIX_FADING_OUT ) {
   237 					music_internal_halt();
   238 					if ( music_finished_hook ) {
   239 						music_finished_hook();
   240 					}
   241 					return;
   242 				}
   243 				music_playing->fading = MIX_NO_FADING;
   244 			}
   245 		}
   246 		
   247 		if (music_halt_or_loop() == 0)
   248 			return;
   249 		
   250 		
   251 		switch (music_playing->type) {
   252 #ifdef CMD_MUSIC
   253 			case MUS_CMD:
   254 				/* The playing is done externally */
   255 				break;
   256 #endif
   257 #ifdef WAV_MUSIC
   258 			case MUS_WAV:
   259 				left = WAVStream_PlaySome(stream, len);
   260 				break;
   261 #endif
   262 #ifdef MODPLUG_MUSIC
   263 			case MUS_MODPLUG:
   264 				left = modplug_playAudio(music_playing->data.modplug, stream, len);
   265 				break;
   266 #endif
   267 #ifdef MOD_MUSIC
   268 			case MUS_MOD:
   269 				left = MOD_playAudio(music_playing->data.module, stream, len);
   270 				break;
   271 #endif
   272 #ifdef MID_MUSIC
   273 #ifdef USE_TIMIDITY_MIDI
   274 			case MUS_MID:
   275 				if ( timidity_ok ) {
   276 					int samples = len / samplesize;
   277   					Timidity_PlaySome(stream, samples);
   278 				}
   279 				break;
   280 #endif
   281 #endif
   282 #ifdef OGG_MUSIC
   283 			case MUS_OGG:
   284 				
   285 				left = OGG_playAudio(music_playing->data.ogg, stream, len);
   286 				break;
   287 #endif
   288 #ifdef FLAC_MUSIC
   289 			case MUS_FLAC:
   290 				left = FLAC_playAudio(music_playing->data.flac, stream, len);
   291 				break;
   292 #endif
   293 #ifdef MP3_MUSIC
   294 			case MUS_MP3:
   295 				left = (len - smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len));
   296 				break;
   297 #endif
   298 #ifdef MP3_MAD_MUSIC
   299 			case MUS_MP3_MAD:
   300 				left = mad_getSamples(music_playing->data.mp3_mad, stream, len);
   301 				break;
   302 #endif
   303 			default:
   304 				/* Unknown music type?? */
   305 				break;
   306 		}
   307 	}
   308 
   309 	/* Handle seamless music looping */
   310 	if (left > 0 && left < len && music_halt_or_loop()) {
   311 		music_mixer(udata, stream+(len-left), left);
   312 	}
   313 }
   314 
   315 /* Initialize the music players with a certain desired audio format */
   316 int open_music(SDL_AudioSpec *mixer)
   317 {
   318 #ifdef WAV_MUSIC
   319 	if ( WAVStream_Init(mixer) == 0 ) {
   320 		add_music_decoder("WAVE");
   321 	}
   322 #endif
   323 #ifdef MODPLUG_MUSIC
   324 	if ( modplug_init(mixer) == 0 ) {
   325 		add_music_decoder("MODPLUG");
   326 	}
   327 #endif
   328 #ifdef MOD_MUSIC
   329 	if ( MOD_init(mixer) == 0 ) {
   330 		add_music_decoder("MIKMOD");
   331 	}
   332 #endif
   333 #ifdef MID_MUSIC
   334 #ifdef USE_TIMIDITY_MIDI
   335 	samplesize = mixer->size / mixer->samples;
   336 	if ( Timidity_Init(mixer->freq, mixer->format,
   337 	                    mixer->channels, mixer->samples) == 0 ) {
   338 		timidity_ok = 1;
   339 		add_music_decoder("TIMIDITY");
   340 	} else {
   341 		timidity_ok = 0;
   342 	}
   343 #endif
   344 #ifdef USE_NATIVE_MIDI
   345 #ifdef USE_TIMIDITY_MIDI
   346 	native_midi_ok = !timidity_ok;
   347 	if ( !native_midi_ok ) {
   348 		native_midi_ok = (getenv("SDL_NATIVE_MUSIC") != NULL);
   349 	}
   350 	if ( native_midi_ok )
   351 #endif
   352 		native_midi_ok = native_midi_detect();
   353 	if ( native_midi_ok )
   354 		add_music_decoder("NATIVEMIDI");
   355 #endif
   356 #endif
   357 #ifdef OGG_MUSIC
   358 	if ( OGG_init(mixer) == 0 ) {
   359 		add_music_decoder("OGG");
   360 	}
   361 #endif
   362 #ifdef FLAC_MUSIC
   363 	if ( FLAC_init(mixer) == 0 ) {
   364 		add_music_decoder("FLAC");
   365 	}
   366 #endif
   367 #if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
   368 	/* Keep a copy of the mixer */
   369 	used_mixer = *mixer;
   370 	add_music_decoder("MP3");
   371 #endif
   372 
   373 	music_playing = NULL;
   374 	music_stopped = 0;
   375 	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
   376 
   377 	/* Calculate the number of ms for each callback */
   378 	ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
   379 
   380 	return(0);
   381 }
   382 
   383 /* Portable case-insensitive string compare function */
   384 int MIX_string_equals(const char *str1, const char *str2)
   385 {
   386 	while ( *str1 && *str2 ) {
   387 		if ( toupper((unsigned char)*str1) !=
   388 		     toupper((unsigned char)*str2) )
   389 			break;
   390 		++str1;
   391 		++str2;
   392 	}
   393 	return (!*str1 && !*str2);
   394 }
   395 
   396 /* Load a music file */
   397 Mix_Music *Mix_LoadMUS(const char *file)
   398 {
   399 	FILE *fp;
   400 	char *ext;
   401 	Uint8 magic[5], moremagic[9];
   402 	Mix_Music *music;
   403 
   404 	/* Figure out what kind of file this is */
   405 	fp = fopen(file, "rb");
   406 	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
   407 		if ( fp != NULL ) {
   408 			fclose(fp);
   409 		}
   410 		Mix_SetError("Couldn't read from '%s'", file);
   411 		return(NULL);
   412 	}
   413 	if (!fread(moremagic, 8, 1, fp)) {
   414 		Mix_SetError("Couldn't read from '%s'", file);
   415 		return(NULL);
   416 	}
   417 	magic[4] = '\0';
   418 	moremagic[8] = '\0';
   419 	fclose(fp);
   420 
   421 	/* Figure out the file extension, so we can determine the type */
   422 	ext = strrchr(file, '.');
   423 	if ( ext ) ++ext; /* skip the dot in the extension */
   424 
   425 	/* Allocate memory for the music structure */
   426 	music = (Mix_Music *)malloc(sizeof(Mix_Music));
   427 	if ( music == NULL ) {
   428 		Mix_SetError("Out of memory");
   429 		return(NULL);
   430 	}
   431 	music->error = 0;
   432 
   433 #ifdef CMD_MUSIC
   434 	if ( music_cmd ) {
   435 		music->type = MUS_CMD;
   436 		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
   437 		if ( music->data.cmd == NULL ) {
   438 			music->error = 1;
   439 		}
   440 	} else
   441 #endif
   442 #ifdef WAV_MUSIC
   443 	/* WAVE files have the magic four bytes "RIFF"
   444 	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
   445 	 */
   446 	if ( (ext && MIX_string_equals(ext, "WAV")) ||
   447 	     ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
   448 	     (strcmp((char *)magic, "FORM") == 0) ) {
   449 		music->type = MUS_WAV;
   450 		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
   451 		if ( music->data.wave == NULL ) {
   452 		  	Mix_SetError("Unable to load WAV file");
   453 			music->error = 1;
   454 		}
   455 	} else
   456 #endif
   457 #ifdef MID_MUSIC
   458 	/* MIDI files have the magic four bytes "MThd" */
   459 	if ( (ext && MIX_string_equals(ext, "MID")) ||
   460 	     (ext && MIX_string_equals(ext, "MIDI")) ||
   461 	     strcmp((char *)magic, "MThd") == 0  ||
   462 	     ( strcmp((char *)magic, "RIFF") == 0  &&
   463 	  	strcmp((char *)(moremagic+4), "RMID") == 0 ) ) {
   464 		music->type = MUS_MID;
   465 #ifdef USE_NATIVE_MIDI
   466   		if ( native_midi_ok ) {
   467   			music->data.nativemidi = native_midi_loadsong(file);
   468 	  		if ( music->data.nativemidi == NULL ) {
   469 		  		Mix_SetError("%s", native_midi_error());
   470 			  	music->error = 1;
   471 			}
   472 	  	} MIDI_ELSE
   473 #endif
   474 #ifdef USE_TIMIDITY_MIDI
   475 		if ( timidity_ok ) {
   476 			music->data.midi = Timidity_LoadSong(file);
   477 			if ( music->data.midi == NULL ) {
   478 				Mix_SetError("%s", Timidity_Error());
   479 				music->error = 1;
   480 			}
   481 		} else {
   482 			Mix_SetError("%s", Timidity_Error());
   483 			music->error = 1;
   484 		}
   485 #endif
   486 	} else
   487 #endif
   488 #ifdef OGG_MUSIC
   489 	/* Ogg Vorbis files have the magic four bytes "OggS" */
   490 	if ( (ext && MIX_string_equals(ext, "OGG")) ||
   491 	     strcmp((char *)magic, "OggS") == 0 ) {
   492 		music->type = MUS_OGG;
   493 		music->data.ogg = OGG_new(file);
   494 		if ( music->data.ogg == NULL ) {
   495 			music->error = 1;
   496 		}
   497 	} else
   498 #endif
   499 #ifdef FLAC_MUSIC
   500 	/* FLAC files have the magic four bytes "fLaC" */
   501 	if ( (ext && MIX_string_equals(ext, "FLAC")) ||
   502 		 strcmp((char *)magic, "fLaC") == 0 ) {
   503 		music->type = MUS_FLAC;
   504 		music->data.flac = FLAC_new(file);
   505 		if ( music->data.flac == NULL ) {
   506 			music->error = 1;
   507 		}
   508 	} else
   509 #endif
   510 #ifdef MP3_MUSIC
   511 	if ( (ext && MIX_string_equals(ext, "MPG")) ||
   512 	     (ext && MIX_string_equals(ext, "MP3")) ||
   513 	     (ext && MIX_string_equals(ext, "MPEG")) ||
   514 	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ||
   515 	     (strncmp((char *)magic, "ID3", 3) == 0) ) {
   516 		if ( Mix_Init(MIX_INIT_MP3) ) {
   517 			SMPEG_Info info;
   518 			music->type = MUS_MP3;
   519 			music->data.mp3 = smpeg.SMPEG_new(file, &info, 0);
   520 			if ( !info.has_audio ) {
   521 				Mix_SetError("MPEG file does not have any audio stream.");
   522 				music->error = 1;
   523 			} else {
   524 				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
   525 			}
   526 		} else {
   527 			music->error = 1;
   528 		}
   529 	} else
   530 #endif
   531 #ifdef MP3_MAD_MUSIC
   532 	if ( (ext && MIX_string_equals(ext, "MPG")) ||
   533 	     (ext && MIX_string_equals(ext, "MP3")) ||
   534 	     (ext && MIX_string_equals(ext, "MPEG")) ||
   535 	     (ext && MIX_string_equals(ext, "MAD")) ||
   536 	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ||
   537 	     (strncmp((char *)magic, "ID3", 3) == 0) ) {
   538 		music->type = MUS_MP3_MAD;
   539 		music->data.mp3_mad = mad_openFile(file, &used_mixer);
   540 		if (music->data.mp3_mad == 0) {
   541 		    Mix_SetError("Could not initialize MPEG stream.");
   542 			music->error = 1;
   543 		}
   544 	} else
   545 #endif
   546 #ifdef MODPLUG_MUSIC
   547 	if ( 1 ) {
   548 		music->type = MUS_MODPLUG;
   549 		music->data.modplug = modplug_new(file);
   550 		if ( music->data.modplug == NULL ) {
   551 			music->error = 1;
   552 		}
   553 	} else
   554 #endif
   555 #ifdef MOD_MUSIC
   556 	if ( 1 ) {
   557 		music->type = MUS_MOD;
   558 		music->data.module = MOD_new(file);
   559 		if ( music->data.module == NULL ) {
   560 			music->error = 1;
   561 		}
   562 	} else
   563 #endif
   564 	{
   565 		Mix_SetError("Unrecognized music format");
   566 		music->error = 1;
   567 	}
   568 	if ( music->error ) {
   569 		free(music);
   570 		music = NULL;
   571 	}
   572 	return(music);
   573 }
   574 
   575 /* Free a music chunk previously loaded */
   576 void Mix_FreeMusic(Mix_Music *music)
   577 {
   578 	if ( music ) {
   579 		/* Stop the music if it's currently playing */
   580 		SDL_LockAudio();
   581 		if ( music == music_playing ) {
   582 			/* Wait for any fade out to finish */
   583 			while ( music->fading == MIX_FADING_OUT ) {
   584 				SDL_UnlockAudio();
   585 				SDL_Delay(100);
   586 				SDL_LockAudio();
   587 			}
   588 			if ( music == music_playing ) {
   589 				music_internal_halt();
   590 			}
   591 		}
   592 		SDL_UnlockAudio();
   593 		switch (music->type) {
   594 #ifdef CMD_MUSIC
   595 			case MUS_CMD:
   596 				MusicCMD_FreeSong(music->data.cmd);
   597 				break;
   598 #endif
   599 #ifdef WAV_MUSIC
   600 			case MUS_WAV:
   601 				WAVStream_FreeSong(music->data.wave);
   602 				break;
   603 #endif
   604 #ifdef MODPLUG_MUSIC
   605 			case MUS_MODPLUG:
   606 				modplug_delete(music->data.modplug);
   607 				break;
   608 #endif
   609 #ifdef MOD_MUSIC
   610 			case MUS_MOD:
   611 				MOD_delete(music->data.module);
   612 				break;
   613 #endif
   614 #ifdef MID_MUSIC
   615 			case MUS_MID:
   616 #ifdef USE_NATIVE_MIDI
   617   				if ( native_midi_ok ) {
   618 					native_midi_freesong(music->data.nativemidi);
   619 				} MIDI_ELSE
   620 #endif
   621 #ifdef USE_TIMIDITY_MIDI
   622 				if ( timidity_ok ) {
   623 					Timidity_FreeSong(music->data.midi);
   624 				}
   625 #endif
   626 				break;
   627 #endif
   628 #ifdef OGG_MUSIC
   629 			case MUS_OGG:
   630 				OGG_delete(music->data.ogg);
   631 				break;
   632 #endif
   633 #ifdef FLAC_MUSIC
   634 			case MUS_FLAC:
   635 				FLAC_delete(music->data.flac);
   636 				break;
   637 #endif
   638 #ifdef MP3_MUSIC
   639 			case MUS_MP3:
   640 				smpeg.SMPEG_delete(music->data.mp3);
   641 				break;
   642 #endif
   643 #ifdef MP3_MAD_MUSIC
   644 			case MUS_MP3_MAD:
   645 				mad_closeFile(music->data.mp3_mad);
   646 				break;
   647 #endif
   648 			default:
   649 				/* Unknown music type?? */
   650 				break;
   651 		}
   652 		free(music);
   653 	}
   654 }
   655 
   656 /* Find out the music format of a mixer music, or the currently playing
   657    music, if 'music' is NULL.
   658 */
   659 Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
   660 {
   661 	Mix_MusicType type = MUS_NONE;
   662 
   663 	if ( music ) {
   664 		type = music->type;
   665 	} else {
   666 		SDL_LockAudio();
   667 		if ( music_playing ) {
   668 			type = music_playing->type;
   669 		}
   670 		SDL_UnlockAudio();
   671 	}
   672 	return(type);
   673 }
   674 
   675 /* Play a music chunk.  Returns 0, or -1 if there was an error.
   676  */
   677 static int music_internal_play(Mix_Music *music, double position)
   678 {
   679 	int retval = 0;
   680 
   681 	/* Note the music we're playing */
   682 	if ( music_playing ) {
   683 		music_internal_halt();
   684 	}
   685 	music_playing = music;
   686 
   687 	/* Set the initial volume */
   688 	if ( music->type != MUS_MOD ) {
   689 		music_internal_initialize_volume();
   690 	}
   691 
   692 	/* Set up for playback */
   693 	switch (music->type) {
   694 #ifdef CMD_MUSIC
   695 	    case MUS_CMD:
   696 		MusicCMD_Start(music->data.cmd);
   697 		break;
   698 #endif
   699 #ifdef WAV_MUSIC
   700 	    case MUS_WAV:
   701 		WAVStream_Start(music->data.wave);
   702 		break;
   703 #endif
   704 #ifdef MODPLUG_MUSIC
   705 	    case MUS_MODPLUG:
   706 		/* can't set volume until file is loaded, so finally set it now */
   707 		music_internal_initialize_volume();
   708 		modplug_play(music->data.modplug);
   709 		break;
   710 #endif
   711 #ifdef MOD_MUSIC
   712 	    case MUS_MOD:
   713 		MOD_play(music->data.module);
   714 		/* Player_SetVolume() does nothing before Player_Start() */
   715 		music_internal_initialize_volume();
   716 		break;
   717 #endif
   718 #ifdef MID_MUSIC
   719 	    case MUS_MID:
   720 #ifdef USE_NATIVE_MIDI
   721 		if ( native_midi_ok ) {
   722 			native_midi_start(music->data.nativemidi);
   723 		} MIDI_ELSE
   724 #endif
   725 #ifdef USE_TIMIDITY_MIDI
   726 		if ( timidity_ok ) {
   727 			Timidity_Start(music->data.midi);
   728 		}
   729 #endif
   730 		break;
   731 #endif
   732 #ifdef OGG_MUSIC
   733 	    case MUS_OGG:
   734 		OGG_play(music->data.ogg);
   735 		break;
   736 #endif
   737 #ifdef FLAC_MUSIC
   738 	    case MUS_FLAC:
   739 		FLAC_play(music->data.flac);
   740 		break;
   741 #endif
   742 #ifdef MP3_MUSIC
   743 	    case MUS_MP3:
   744 		smpeg.SMPEG_enableaudio(music->data.mp3,1);
   745 		smpeg.SMPEG_enablevideo(music->data.mp3,0);
   746 		smpeg.SMPEG_play(music_playing->data.mp3);
   747 		break;
   748 #endif
   749 #ifdef MP3_MAD_MUSIC
   750 	    case MUS_MP3_MAD:
   751 		mad_start(music->data.mp3_mad);
   752 		break;
   753 #endif
   754 	    default:
   755 		Mix_SetError("Can't play unknown music type");
   756 		retval = -1;
   757 		break;
   758 	}
   759 
   760 	/* Set the playback position, note any errors if an offset is used */
   761 	if ( retval == 0 ) {
   762 		if ( position > 0.0 ) {
   763 			if ( music_internal_position(position) < 0 ) {
   764 				Mix_SetError("Position not implemented for music type");
   765 				retval = -1;
   766 			}
   767 		} else {
   768 			music_internal_position(0.0);
   769 		}
   770 	}
   771 
   772 	/* If the setup failed, we're not playing any music anymore */
   773 	if ( retval < 0 ) {
   774 		music_playing = NULL;
   775 	}
   776 	return(retval);
   777 }
   778 int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
   779 {
   780 	int retval;
   781 
   782 	/* Don't play null pointers :-) */
   783 	if ( music == NULL ) {
   784 		Mix_SetError("music parameter was NULL");
   785 		return(-1);
   786 	}
   787 
   788 	/* Setup the data */
   789 	if ( ms ) {
   790 		music->fading = MIX_FADING_IN;
   791 	} else {
   792 		music->fading = MIX_NO_FADING;
   793 	}
   794 	music->fade_step = 0;
   795 	music->fade_steps = ms/ms_per_step;
   796 
   797 	/* Play the puppy */
   798 	SDL_LockAudio();
   799 	/* If the current music is fading out, wait for the fade to complete */
   800 	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
   801 		SDL_UnlockAudio();
   802 		SDL_Delay(100);
   803 		SDL_LockAudio();
   804 	}
   805 	music_active = 1;
   806 	music_loops = loops;
   807 	retval = music_internal_play(music, position);
   808 	SDL_UnlockAudio();
   809 
   810 	return(retval);
   811 }
   812 int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
   813 {
   814 	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
   815 }
   816 int Mix_PlayMusic(Mix_Music *music, int loops)
   817 {
   818 	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
   819 }
   820 
   821 /* Set the playing music position */
   822 int music_internal_position(double position)
   823 {
   824 	int retval = 0;
   825 
   826 	switch (music_playing->type) {
   827 #ifdef MODPLUG_MUSIC
   828 	    case MUS_MODPLUG:
   829 		modplug_jump_to_time(music_playing->data.modplug, position);
   830 		break;
   831 #endif
   832 #ifdef MOD_MUSIC
   833 	    case MUS_MOD:
   834 		MOD_jump_to_time(music_playing->data.module, position);
   835 		break;
   836 #endif
   837 #ifdef OGG_MUSIC
   838 	    case MUS_OGG:
   839 		OGG_jump_to_time(music_playing->data.ogg, position);
   840 		break;
   841 #endif
   842 #ifdef FLAC_MUSIC
   843 	    case MUS_FLAC:
   844 		FLAC_jump_to_time(music_playing->data.flac, position);
   845 		break;
   846 #endif
   847 #ifdef MP3_MUSIC
   848 	    case MUS_MP3:
   849 		if ( position > 0.0 ) {
   850 			smpeg.SMPEG_skip(music_playing->data.mp3, (float)position);
   851 		} else {
   852 			smpeg.SMPEG_rewind(music_playing->data.mp3);
   853 			smpeg.SMPEG_play(music_playing->data.mp3);
   854 		}
   855 		break;
   856 #endif
   857 #ifdef MP3_MAD_MUSIC
   858 	    case MUS_MP3_MAD:
   859 		mad_seek(music_playing->data.mp3_mad, position);
   860 		break;
   861 #endif
   862 	    default:
   863 		/* TODO: Implement this for other music backends */
   864 		retval = -1;
   865 		break;
   866 	}
   867 	return(retval);
   868 }
   869 int Mix_SetMusicPosition(double position)
   870 {
   871 	int retval;
   872 
   873 	SDL_LockAudio();
   874 	if ( music_playing ) {
   875 		retval = music_internal_position(position);
   876 		if ( retval < 0 ) {
   877 			Mix_SetError("Position not implemented for music type");
   878 		}
   879 	} else {
   880 		Mix_SetError("Music isn't playing");
   881 		retval = -1;
   882 	}
   883 	SDL_UnlockAudio();
   884 
   885 	return(retval);
   886 }
   887 
   888 /* Set the music's initial volume */
   889 static void music_internal_initialize_volume(void)
   890 {
   891 	if ( music_playing->fading == MIX_FADING_IN ) {
   892 		music_internal_volume(0);
   893 	} else {
   894 		music_internal_volume(music_volume);
   895 	}
   896 }
   897 
   898 /* Set the music volume */
   899 static void music_internal_volume(int volume)
   900 {
   901 	switch (music_playing->type) {
   902 #ifdef CMD_MUSIC
   903 	    case MUS_CMD:
   904 		MusicCMD_SetVolume(volume);
   905 		break;
   906 #endif
   907 #ifdef WAV_MUSIC
   908 	    case MUS_WAV:
   909 		WAVStream_SetVolume(volume);
   910 		break;
   911 #endif
   912 #ifdef MODPLUG_MUSIC
   913 	    case MUS_MODPLUG:
   914 		modplug_setvolume(music_playing->data.modplug, volume);
   915 		break;
   916 #endif
   917 #ifdef MOD_MUSIC
   918 	    case MUS_MOD:
   919 		MOD_setvolume(music_playing->data.module, volume);
   920 		break;
   921 #endif
   922 #ifdef MID_MUSIC
   923 	    case MUS_MID:
   924 #ifdef USE_NATIVE_MIDI
   925 		if ( native_midi_ok ) {
   926 			native_midi_setvolume(volume);
   927 		} MIDI_ELSE
   928 #endif
   929 #ifdef USE_TIMIDITY_MIDI
   930 		if ( timidity_ok ) {
   931 			Timidity_SetVolume(volume);
   932 		}
   933 #endif
   934 		break;
   935 #endif
   936 #ifdef OGG_MUSIC
   937 	    case MUS_OGG:
   938 		OGG_setvolume(music_playing->data.ogg, volume);
   939 		break;
   940 #endif
   941 #ifdef FLAC_MUSIC
   942 	    case MUS_FLAC:
   943 		FLAC_setvolume(music_playing->data.flac, volume);
   944 		break;
   945 #endif
   946 #ifdef MP3_MUSIC
   947 	    case MUS_MP3:
   948 		smpeg.SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
   949 		break;
   950 #endif
   951 #ifdef MP3_MAD_MUSIC
   952 	    case MUS_MP3_MAD:
   953 		mad_setVolume(music_playing->data.mp3_mad, volume);
   954 		break;
   955 #endif
   956 	    default:
   957 		/* Unknown music type?? */
   958 		break;
   959 	}
   960 }
   961 int Mix_VolumeMusic(int volume)
   962 {
   963 	int prev_volume;
   964 
   965 	prev_volume = music_volume;
   966 	if ( volume < 0 ) {
   967 		return prev_volume;
   968 	}
   969 	if ( volume > SDL_MIX_MAXVOLUME ) {
   970 		volume = SDL_MIX_MAXVOLUME;
   971 	}
   972 	music_volume = volume;
   973 	SDL_LockAudio();
   974 	if ( music_playing ) {
   975 		music_internal_volume(music_volume);
   976 	}
   977 	SDL_UnlockAudio();
   978 	return(prev_volume);
   979 }
   980 
   981 /* Halt playing of music */
   982 static void music_internal_halt(void)
   983 {
   984 	switch (music_playing->type) {
   985 #ifdef CMD_MUSIC
   986 	    case MUS_CMD:
   987 		MusicCMD_Stop(music_playing->data.cmd);
   988 		break;
   989 #endif
   990 #ifdef WAV_MUSIC
   991 	    case MUS_WAV:
   992 		WAVStream_Stop();
   993 		break;
   994 #endif
   995 #ifdef MODPLUG_MUSIC
   996 	    case MUS_MODPLUG:
   997 		modplug_stop(music_playing->data.modplug);
   998 		break;
   999 #endif
  1000 #ifdef MOD_MUSIC
  1001 	    case MUS_MOD:
  1002 		MOD_stop(music_playing->data.module);
  1003 		break;
  1004 #endif
  1005 #ifdef MID_MUSIC
  1006 	    case MUS_MID:
  1007 #ifdef USE_NATIVE_MIDI
  1008 		if ( native_midi_ok ) {
  1009 			native_midi_stop();
  1010 		} MIDI_ELSE
  1011 #endif
  1012 #ifdef USE_TIMIDITY_MIDI
  1013 		if ( timidity_ok ) {
  1014 			Timidity_Stop();
  1015 		}
  1016 #endif
  1017 		break;
  1018 #endif
  1019 #ifdef OGG_MUSIC
  1020 	    case MUS_OGG:
  1021 		OGG_stop(music_playing->data.ogg);
  1022 		break;
  1023 #endif
  1024 #ifdef FLAC_MUSIC
  1025 	    case MUS_FLAC:
  1026 		FLAC_stop(music_playing->data.flac);
  1027 		break;
  1028 #endif
  1029 #ifdef MP3_MUSIC
  1030 	    case MUS_MP3:
  1031 		smpeg.SMPEG_stop(music_playing->data.mp3);
  1032 		break;
  1033 #endif
  1034 #ifdef MP3_MAD_MUSIC
  1035 	    case MUS_MP3_MAD:
  1036 		mad_stop(music_playing->data.mp3_mad);
  1037 		break;
  1038 #endif
  1039 	    default:
  1040 		/* Unknown music type?? */
  1041 		return;
  1042 	}
  1043 	music_playing->fading = MIX_NO_FADING;
  1044 	music_playing = NULL;
  1045 }
  1046 int Mix_HaltMusic(void)
  1047 {
  1048 	SDL_LockAudio();
  1049 	if ( music_playing ) {
  1050 		music_internal_halt();
  1051 	}
  1052 	SDL_UnlockAudio();
  1053 
  1054 	return(0);
  1055 }
  1056 
  1057 /* Progressively stop the music */
  1058 int Mix_FadeOutMusic(int ms)
  1059 {
  1060 	int retval = 0;
  1061 
  1062 	if (ms <= 0) {  /* just halt immediately. */
  1063 		Mix_HaltMusic();
  1064 		return 1;
  1065 	}
  1066 
  1067 	SDL_LockAudio();
  1068 	if ( music_playing) {
  1069                 int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
  1070                 if ( music_playing->fading == MIX_NO_FADING ) {
  1071 	        	music_playing->fade_step = 0;
  1072                 } else {
  1073                         int step;
  1074                         int old_fade_steps = music_playing->fade_steps;
  1075                         if ( music_playing->fading == MIX_FADING_OUT ) {
  1076                                 step = music_playing->fade_step;
  1077                         } else {
  1078                                 step = old_fade_steps
  1079                                         - music_playing->fade_step + 1;
  1080                         }
  1081                         music_playing->fade_step = (step * fade_steps)
  1082                                 / old_fade_steps;
  1083                 }
  1084 		music_playing->fading = MIX_FADING_OUT;
  1085 		music_playing->fade_steps = fade_steps;
  1086 		retval = 1;
  1087 	}
  1088 	SDL_UnlockAudio();
  1089 
  1090 	return(retval);
  1091 }
  1092 
  1093 Mix_Fading Mix_FadingMusic(void)
  1094 {
  1095 	Mix_Fading fading = MIX_NO_FADING;
  1096 
  1097 	SDL_LockAudio();
  1098 	if ( music_playing ) {
  1099 		fading = music_playing->fading;
  1100 	}
  1101 	SDL_UnlockAudio();
  1102 
  1103 	return(fading);
  1104 }
  1105 
  1106 /* Pause/Resume the music stream */
  1107 void Mix_PauseMusic(void)
  1108 {
  1109 	music_active = 0;
  1110 }
  1111 
  1112 void Mix_ResumeMusic(void)
  1113 {
  1114 	music_active = 1;
  1115 }
  1116 
  1117 void Mix_RewindMusic(void)
  1118 {
  1119 	Mix_SetMusicPosition(0.0);
  1120 }
  1121 
  1122 int Mix_PausedMusic(void)
  1123 {
  1124 	return (music_active == 0);
  1125 }
  1126 
  1127 /* Check the status of the music */
  1128 static int music_internal_playing()
  1129 {
  1130 	int playing = 1;
  1131 	switch (music_playing->type) {
  1132 #ifdef CMD_MUSIC
  1133 	    case MUS_CMD:
  1134 		if (!MusicCMD_Active(music_playing->data.cmd)) {
  1135 			playing = 0;
  1136 		}
  1137 		break;
  1138 #endif
  1139 #ifdef WAV_MUSIC
  1140 	    case MUS_WAV:
  1141 		if ( ! WAVStream_Active() ) {
  1142 			playing = 0;
  1143 		}
  1144 		break;
  1145 #endif
  1146 #ifdef MODPLUG_MUSIC
  1147 	    case MUS_MODPLUG:
  1148 		if ( ! modplug_playing(music_playing->data.modplug) ) {
  1149 			playing = 0;
  1150 		}
  1151 		break;
  1152 #endif
  1153 #ifdef MOD_MUSIC
  1154 	    case MUS_MOD:
  1155 		if ( ! MOD_playing(music_playing->data.module) ) {
  1156 			playing = 0;
  1157 		}
  1158 		break;
  1159 #endif
  1160 #ifdef MID_MUSIC
  1161 	    case MUS_MID:
  1162 #ifdef USE_NATIVE_MIDI
  1163 		if ( native_midi_ok ) {
  1164 			if ( ! native_midi_active() )
  1165 				playing = 0;
  1166 		} MIDI_ELSE
  1167 #endif
  1168 #ifdef USE_TIMIDITY_MIDI
  1169 		if ( timidity_ok ) {
  1170 			if ( ! Timidity_Active() )
  1171 				playing = 0;
  1172 		}
  1173 #endif
  1174 		break;
  1175 #endif
  1176 #ifdef OGG_MUSIC
  1177 	    case MUS_OGG:
  1178 		if ( ! OGG_playing(music_playing->data.ogg) ) {
  1179 			playing = 0;
  1180 		}
  1181 		break;
  1182 #endif
  1183 #ifdef FLAC_MUSIC
  1184 	    case MUS_FLAC:
  1185 		if ( ! FLAC_playing(music_playing->data.flac) ) {
  1186 			playing = 0;
  1187 		}
  1188 		break;
  1189 #endif
  1190 #ifdef MP3_MUSIC
  1191 	    case MUS_MP3:
  1192 		if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
  1193 			playing = 0;
  1194 		break;
  1195 #endif
  1196 #ifdef MP3_MAD_MUSIC
  1197 	    case MUS_MP3_MAD:
  1198 		if (!mad_isPlaying(music_playing->data.mp3_mad)) {
  1199 			playing = 0;
  1200 		}
  1201 		break;
  1202 #endif
  1203 	    default:
  1204 		playing = 0;
  1205 		break;
  1206 	}
  1207 	return(playing);
  1208 }
  1209 int Mix_PlayingMusic(void)
  1210 {
  1211 	int playing = 0;
  1212 
  1213 	SDL_LockAudio();
  1214 	if ( music_playing ) {
  1215 		playing = music_internal_playing();
  1216 	}
  1217 	SDL_UnlockAudio();
  1218 
  1219 	return(playing);
  1220 }
  1221 
  1222 /* Set the external music playback command */
  1223 int Mix_SetMusicCMD(const char *command)
  1224 {
  1225 	Mix_HaltMusic();
  1226 	if ( music_cmd ) {
  1227 		free(music_cmd);
  1228 		music_cmd = NULL;
  1229 	}
  1230 	if ( command ) {
  1231 		music_cmd = (char *)malloc(strlen(command)+1);
  1232 		if ( music_cmd == NULL ) {
  1233 			return(-1);
  1234 		}
  1235 		strcpy(music_cmd, command);
  1236 	}
  1237 	return(0);
  1238 }
  1239 
  1240 int Mix_SetSynchroValue(int i)
  1241 {
  1242 	/* Not supported by any players at this time */
  1243 	return(-1);
  1244 }
  1245 
  1246 int Mix_GetSynchroValue(void)
  1247 {
  1248 	/* Not supported by any players at this time */
  1249 	return(-1);
  1250 }
  1251 
  1252 
  1253 /* Uninitialize the music players */
  1254 void close_music(void)
  1255 {
  1256 	Mix_HaltMusic();
  1257 #ifdef CMD_MUSIC
  1258 	Mix_SetMusicCMD(NULL);
  1259 #endif
  1260 #ifdef MODPLUG_MUSIC
  1261 	modplug_exit();
  1262 #endif
  1263 #ifdef MOD_MUSIC
  1264 	MOD_exit();
  1265 #endif
  1266 #ifdef MID_MUSIC
  1267 # ifdef USE_TIMIDITY_MIDI
  1268 	Timidity_Close();
  1269 # endif
  1270 #endif
  1271 
  1272 	/* rcg06042009 report available decoders at runtime. */
  1273 	free(music_decoders);
  1274 	music_decoders = NULL;
  1275 	num_decoders = 0;
  1276 }
  1277 
  1278 Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
  1279 {
  1280 	Uint8 magic[5];	  /*Apparently there is no way to check if the file is really a MOD,*/
  1281 	/*		    or there are too many formats supported by MikMod or MikMod does */
  1282 	/*		    this check by itself. If someone implements other formats (e.g. MP3) */
  1283 	/*		    the check can be uncommented */
  1284 	Uint8 moremagic[9];
  1285 	Mix_Music *music;
  1286 	int start;
  1287 
  1288 	if (!rw) {
  1289 		Mix_SetError("RWops pointer is NULL");
  1290 		return NULL;
  1291 	}
  1292 
  1293 	/* Figure out what kind of file this is */
  1294 	start = SDL_RWtell(rw);
  1295 	if ( SDL_RWread(rw,magic,1,4) != 4 ||
  1296 	     SDL_RWread(rw,moremagic,1,8) != 8 ) {
  1297 		Mix_SetError("Couldn't read from RWops");
  1298 		return NULL;
  1299 	}
  1300 	SDL_RWseek(rw, start, RW_SEEK_SET);
  1301 	magic[4]='\0';
  1302 	moremagic[8] = '\0';
  1303 
  1304 	/* Allocate memory for the music structure */
  1305 	music=(Mix_Music *)malloc(sizeof(Mix_Music));
  1306 	if (music==NULL ) {
  1307 		Mix_SetError("Out of memory");
  1308 		return(NULL);
  1309 	}
  1310 	music->error = 0;
  1311 
  1312 #ifdef WAV_MUSIC
  1313 	/* WAVE files have the magic four bytes "RIFF"
  1314 	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
  1315 	 */
  1316 	if ( ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
  1317 	     (strcmp((char *)magic, "FORM") == 0) ) {
  1318 		music->type = MUS_WAV;
  1319 		music->data.wave = WAVStream_LoadSong_RW(rw, (char *)magic);
  1320 		if ( music->data.wave == NULL ) {
  1321 			music->error = 1;
  1322 		}
  1323 
  1324 	} else
  1325 #endif
  1326 #ifdef OGG_MUSIC
  1327 	/* Ogg Vorbis files have the magic four bytes "OggS" */
  1328 	if ( strcmp((char *)magic, "OggS") == 0 ) {
  1329 		music->type = MUS_OGG;
  1330 		music->data.ogg = OGG_new_RW(rw);
  1331 		if ( music->data.ogg == NULL ) {
  1332 			music->error = 1;
  1333 		}
  1334 	} else
  1335 #endif
  1336 #ifdef FLAC_MUSIC
  1337 	/* FLAC files have the magic four bytes "fLaC" */
  1338 	if ( strcmp((char *)magic, "fLaC") == 0 ) {
  1339 		music->type = MUS_FLAC;
  1340 		music->data.flac = FLAC_new_RW(rw);
  1341 		if ( music->data.flac == NULL ) {
  1342 			music->error = 1;
  1343 		}
  1344 	} else
  1345 #endif
  1346 #ifdef MP3_MUSIC
  1347 	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
  1348 		if ( Mix_Init(MIX_INIT_MP3) ) {
  1349 			SMPEG_Info info;
  1350 			music->type = MUS_MP3;
  1351 			music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
  1352 			if ( !info.has_audio ) {
  1353 				Mix_SetError("MPEG file does not have any audio stream.");
  1354 				music->error = 1;
  1355 			} else {
  1356 				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
  1357 			}
  1358 		} else {
  1359 			music->error = 1;
  1360 		}
  1361 	} else
  1362 #endif
  1363 #ifdef MP3_MAD_MUSIC
  1364 	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
  1365 		music->type = MUS_MP3_MAD;
  1366 		music->data.mp3_mad = mad_openFileRW(rw, &used_mixer);
  1367 		if (music->data.mp3_mad == 0) {
  1368 			Mix_SetError("Could not initialize MPEG stream.");
  1369 			music->error = 1;
  1370 		}
  1371 	} else
  1372 #endif
  1373 #ifdef MID_MUSIC
  1374 	/* MIDI files have the magic four bytes "MThd" */
  1375 	if ( strcmp((char *)magic, "MThd") == 0 ) {
  1376 		music->type = MUS_MID;
  1377 #ifdef USE_NATIVE_MIDI
  1378 		if ( native_midi_ok ) {
  1379 			music->data.nativemidi = native_midi_loadsong_RW(rw);
  1380 	  		if ( music->data.nativemidi == NULL ) {
  1381 		  		Mix_SetError("%s", native_midi_error());
  1382 			  	music->error = 1;
  1383 			}
  1384 		} MIDI_ELSE
  1385 #endif
  1386 #ifdef USE_TIMIDITY_MIDI
  1387 		if ( timidity_ok ) {
  1388 			music->data.midi = Timidity_LoadSong_RW(rw);
  1389 			if ( music->data.midi == NULL ) {
  1390 				Mix_SetError("%s", Timidity_Error());
  1391 				music->error = 1;
  1392 			}
  1393 		} else {
  1394 			Mix_SetError("%s", Timidity_Error());
  1395 			music->error = 1;
  1396 		}
  1397 #endif
  1398 	} else
  1399 #endif
  1400 #if defined(MODPLUG_MUSIC) || defined(MODPLUG_MUSIC)
  1401 	if (1) {
  1402 		music->error = 1;
  1403 #ifdef MODPLUG_MUSIC
  1404 		if ( music->error ) {
  1405 			music->type = MUS_MODPLUG;
  1406 			music->data.modplug = modplug_new_RW(rw);
  1407 			if ( music->data.modplug ) {
  1408 				music->error = 0;
  1409 			}
  1410 		}
  1411 #endif
  1412 #ifdef MOD_MUSIC
  1413 		if ( music->error ) {
  1414 			music->type = MUS_MOD;
  1415 			music->data.module = MOD_new_RW(rw);
  1416 			if ( music->data.module ) {
  1417 				music->error = 0;
  1418 			}
  1419 		}
  1420 #endif
  1421 	} else
  1422 #endif /* MODPLUG_MUSIC || MOD_MUSIC */
  1423 	{
  1424 		Mix_SetError("Unrecognized music format");
  1425 		music->error=1;
  1426 	}
  1427 	if (music->error) {
  1428 		free(music);
  1429 		music=NULL;
  1430 	}
  1431 	return(music);
  1432 }