music.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 23 Jan 2011 21:45:08 -0800
changeset 499 6ab2b1f10df9
parent 484 a77df8583d18
child 504 7a95f36a8bbf
permissions -rw-r--r--
Fixed divide by zero if the music hasn't been opened yet.
     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 	if ( ms_per_step == 0 ) {
   783 		SDL_SetError("Audio device hasn't been opened");
   784 		return(-1);
   785 	}
   786 
   787 	/* Don't play null pointers :-) */
   788 	if ( music == NULL ) {
   789 		Mix_SetError("music parameter was NULL");
   790 		return(-1);
   791 	}
   792 
   793 	/* Setup the data */
   794 	if ( ms ) {
   795 		music->fading = MIX_FADING_IN;
   796 	} else {
   797 		music->fading = MIX_NO_FADING;
   798 	}
   799 	music->fade_step = 0;
   800 	music->fade_steps = ms/ms_per_step;
   801 
   802 	/* Play the puppy */
   803 	SDL_LockAudio();
   804 	/* If the current music is fading out, wait for the fade to complete */
   805 	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
   806 		SDL_UnlockAudio();
   807 		SDL_Delay(100);
   808 		SDL_LockAudio();
   809 	}
   810 	music_active = 1;
   811 	music_loops = loops;
   812 	retval = music_internal_play(music, position);
   813 	SDL_UnlockAudio();
   814 
   815 	return(retval);
   816 }
   817 int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
   818 {
   819 	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
   820 }
   821 int Mix_PlayMusic(Mix_Music *music, int loops)
   822 {
   823 	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
   824 }
   825 
   826 /* Set the playing music position */
   827 int music_internal_position(double position)
   828 {
   829 	int retval = 0;
   830 
   831 	switch (music_playing->type) {
   832 #ifdef MODPLUG_MUSIC
   833 	    case MUS_MODPLUG:
   834 		modplug_jump_to_time(music_playing->data.modplug, position);
   835 		break;
   836 #endif
   837 #ifdef MOD_MUSIC
   838 	    case MUS_MOD:
   839 		MOD_jump_to_time(music_playing->data.module, position);
   840 		break;
   841 #endif
   842 #ifdef OGG_MUSIC
   843 	    case MUS_OGG:
   844 		OGG_jump_to_time(music_playing->data.ogg, position);
   845 		break;
   846 #endif
   847 #ifdef FLAC_MUSIC
   848 	    case MUS_FLAC:
   849 		FLAC_jump_to_time(music_playing->data.flac, position);
   850 		break;
   851 #endif
   852 #ifdef MP3_MUSIC
   853 	    case MUS_MP3:
   854 		if ( position > 0.0 ) {
   855 			smpeg.SMPEG_skip(music_playing->data.mp3, (float)position);
   856 		} else {
   857 			smpeg.SMPEG_rewind(music_playing->data.mp3);
   858 			smpeg.SMPEG_play(music_playing->data.mp3);
   859 		}
   860 		break;
   861 #endif
   862 #ifdef MP3_MAD_MUSIC
   863 	    case MUS_MP3_MAD:
   864 		mad_seek(music_playing->data.mp3_mad, position);
   865 		break;
   866 #endif
   867 	    default:
   868 		/* TODO: Implement this for other music backends */
   869 		retval = -1;
   870 		break;
   871 	}
   872 	return(retval);
   873 }
   874 int Mix_SetMusicPosition(double position)
   875 {
   876 	int retval;
   877 
   878 	SDL_LockAudio();
   879 	if ( music_playing ) {
   880 		retval = music_internal_position(position);
   881 		if ( retval < 0 ) {
   882 			Mix_SetError("Position not implemented for music type");
   883 		}
   884 	} else {
   885 		Mix_SetError("Music isn't playing");
   886 		retval = -1;
   887 	}
   888 	SDL_UnlockAudio();
   889 
   890 	return(retval);
   891 }
   892 
   893 /* Set the music's initial volume */
   894 static void music_internal_initialize_volume(void)
   895 {
   896 	if ( music_playing->fading == MIX_FADING_IN ) {
   897 		music_internal_volume(0);
   898 	} else {
   899 		music_internal_volume(music_volume);
   900 	}
   901 }
   902 
   903 /* Set the music volume */
   904 static void music_internal_volume(int volume)
   905 {
   906 	switch (music_playing->type) {
   907 #ifdef CMD_MUSIC
   908 	    case MUS_CMD:
   909 		MusicCMD_SetVolume(volume);
   910 		break;
   911 #endif
   912 #ifdef WAV_MUSIC
   913 	    case MUS_WAV:
   914 		WAVStream_SetVolume(volume);
   915 		break;
   916 #endif
   917 #ifdef MODPLUG_MUSIC
   918 	    case MUS_MODPLUG:
   919 		modplug_setvolume(music_playing->data.modplug, volume);
   920 		break;
   921 #endif
   922 #ifdef MOD_MUSIC
   923 	    case MUS_MOD:
   924 		MOD_setvolume(music_playing->data.module, volume);
   925 		break;
   926 #endif
   927 #ifdef MID_MUSIC
   928 	    case MUS_MID:
   929 #ifdef USE_NATIVE_MIDI
   930 		if ( native_midi_ok ) {
   931 			native_midi_setvolume(volume);
   932 		} MIDI_ELSE
   933 #endif
   934 #ifdef USE_TIMIDITY_MIDI
   935 		if ( timidity_ok ) {
   936 			Timidity_SetVolume(volume);
   937 		}
   938 #endif
   939 		break;
   940 #endif
   941 #ifdef OGG_MUSIC
   942 	    case MUS_OGG:
   943 		OGG_setvolume(music_playing->data.ogg, volume);
   944 		break;
   945 #endif
   946 #ifdef FLAC_MUSIC
   947 	    case MUS_FLAC:
   948 		FLAC_setvolume(music_playing->data.flac, volume);
   949 		break;
   950 #endif
   951 #ifdef MP3_MUSIC
   952 	    case MUS_MP3:
   953 		smpeg.SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
   954 		break;
   955 #endif
   956 #ifdef MP3_MAD_MUSIC
   957 	    case MUS_MP3_MAD:
   958 		mad_setVolume(music_playing->data.mp3_mad, volume);
   959 		break;
   960 #endif
   961 	    default:
   962 		/* Unknown music type?? */
   963 		break;
   964 	}
   965 }
   966 int Mix_VolumeMusic(int volume)
   967 {
   968 	int prev_volume;
   969 
   970 	prev_volume = music_volume;
   971 	if ( volume < 0 ) {
   972 		return prev_volume;
   973 	}
   974 	if ( volume > SDL_MIX_MAXVOLUME ) {
   975 		volume = SDL_MIX_MAXVOLUME;
   976 	}
   977 	music_volume = volume;
   978 	SDL_LockAudio();
   979 	if ( music_playing ) {
   980 		music_internal_volume(music_volume);
   981 	}
   982 	SDL_UnlockAudio();
   983 	return(prev_volume);
   984 }
   985 
   986 /* Halt playing of music */
   987 static void music_internal_halt(void)
   988 {
   989 	switch (music_playing->type) {
   990 #ifdef CMD_MUSIC
   991 	    case MUS_CMD:
   992 		MusicCMD_Stop(music_playing->data.cmd);
   993 		break;
   994 #endif
   995 #ifdef WAV_MUSIC
   996 	    case MUS_WAV:
   997 		WAVStream_Stop();
   998 		break;
   999 #endif
  1000 #ifdef MODPLUG_MUSIC
  1001 	    case MUS_MODPLUG:
  1002 		modplug_stop(music_playing->data.modplug);
  1003 		break;
  1004 #endif
  1005 #ifdef MOD_MUSIC
  1006 	    case MUS_MOD:
  1007 		MOD_stop(music_playing->data.module);
  1008 		break;
  1009 #endif
  1010 #ifdef MID_MUSIC
  1011 	    case MUS_MID:
  1012 #ifdef USE_NATIVE_MIDI
  1013 		if ( native_midi_ok ) {
  1014 			native_midi_stop();
  1015 		} MIDI_ELSE
  1016 #endif
  1017 #ifdef USE_TIMIDITY_MIDI
  1018 		if ( timidity_ok ) {
  1019 			Timidity_Stop();
  1020 		}
  1021 #endif
  1022 		break;
  1023 #endif
  1024 #ifdef OGG_MUSIC
  1025 	    case MUS_OGG:
  1026 		OGG_stop(music_playing->data.ogg);
  1027 		break;
  1028 #endif
  1029 #ifdef FLAC_MUSIC
  1030 	    case MUS_FLAC:
  1031 		FLAC_stop(music_playing->data.flac);
  1032 		break;
  1033 #endif
  1034 #ifdef MP3_MUSIC
  1035 	    case MUS_MP3:
  1036 		smpeg.SMPEG_stop(music_playing->data.mp3);
  1037 		break;
  1038 #endif
  1039 #ifdef MP3_MAD_MUSIC
  1040 	    case MUS_MP3_MAD:
  1041 		mad_stop(music_playing->data.mp3_mad);
  1042 		break;
  1043 #endif
  1044 	    default:
  1045 		/* Unknown music type?? */
  1046 		return;
  1047 	}
  1048 	music_playing->fading = MIX_NO_FADING;
  1049 	music_playing = NULL;
  1050 }
  1051 int Mix_HaltMusic(void)
  1052 {
  1053 	SDL_LockAudio();
  1054 	if ( music_playing ) {
  1055 		music_internal_halt();
  1056 	}
  1057 	SDL_UnlockAudio();
  1058 
  1059 	return(0);
  1060 }
  1061 
  1062 /* Progressively stop the music */
  1063 int Mix_FadeOutMusic(int ms)
  1064 {
  1065 	int retval = 0;
  1066 
  1067 	if ( ms_per_step == 0 ) {
  1068 		SDL_SetError("Audio device hasn't been opened");
  1069 		return 0;
  1070 	}
  1071 
  1072 	if (ms <= 0) {  /* just halt immediately. */
  1073 		Mix_HaltMusic();
  1074 		return 1;
  1075 	}
  1076 
  1077 	SDL_LockAudio();
  1078 	if ( music_playing) {
  1079                 int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
  1080                 if ( music_playing->fading == MIX_NO_FADING ) {
  1081 	        	music_playing->fade_step = 0;
  1082                 } else {
  1083                         int step;
  1084                         int old_fade_steps = music_playing->fade_steps;
  1085                         if ( music_playing->fading == MIX_FADING_OUT ) {
  1086                                 step = music_playing->fade_step;
  1087                         } else {
  1088                                 step = old_fade_steps
  1089                                         - music_playing->fade_step + 1;
  1090                         }
  1091                         music_playing->fade_step = (step * fade_steps)
  1092                                 / old_fade_steps;
  1093                 }
  1094 		music_playing->fading = MIX_FADING_OUT;
  1095 		music_playing->fade_steps = fade_steps;
  1096 		retval = 1;
  1097 	}
  1098 	SDL_UnlockAudio();
  1099 
  1100 	return(retval);
  1101 }
  1102 
  1103 Mix_Fading Mix_FadingMusic(void)
  1104 {
  1105 	Mix_Fading fading = MIX_NO_FADING;
  1106 
  1107 	SDL_LockAudio();
  1108 	if ( music_playing ) {
  1109 		fading = music_playing->fading;
  1110 	}
  1111 	SDL_UnlockAudio();
  1112 
  1113 	return(fading);
  1114 }
  1115 
  1116 /* Pause/Resume the music stream */
  1117 void Mix_PauseMusic(void)
  1118 {
  1119 	music_active = 0;
  1120 }
  1121 
  1122 void Mix_ResumeMusic(void)
  1123 {
  1124 	music_active = 1;
  1125 }
  1126 
  1127 void Mix_RewindMusic(void)
  1128 {
  1129 	Mix_SetMusicPosition(0.0);
  1130 }
  1131 
  1132 int Mix_PausedMusic(void)
  1133 {
  1134 	return (music_active == 0);
  1135 }
  1136 
  1137 /* Check the status of the music */
  1138 static int music_internal_playing()
  1139 {
  1140 	int playing = 1;
  1141 	switch (music_playing->type) {
  1142 #ifdef CMD_MUSIC
  1143 	    case MUS_CMD:
  1144 		if (!MusicCMD_Active(music_playing->data.cmd)) {
  1145 			playing = 0;
  1146 		}
  1147 		break;
  1148 #endif
  1149 #ifdef WAV_MUSIC
  1150 	    case MUS_WAV:
  1151 		if ( ! WAVStream_Active() ) {
  1152 			playing = 0;
  1153 		}
  1154 		break;
  1155 #endif
  1156 #ifdef MODPLUG_MUSIC
  1157 	    case MUS_MODPLUG:
  1158 		if ( ! modplug_playing(music_playing->data.modplug) ) {
  1159 			playing = 0;
  1160 		}
  1161 		break;
  1162 #endif
  1163 #ifdef MOD_MUSIC
  1164 	    case MUS_MOD:
  1165 		if ( ! MOD_playing(music_playing->data.module) ) {
  1166 			playing = 0;
  1167 		}
  1168 		break;
  1169 #endif
  1170 #ifdef MID_MUSIC
  1171 	    case MUS_MID:
  1172 #ifdef USE_NATIVE_MIDI
  1173 		if ( native_midi_ok ) {
  1174 			if ( ! native_midi_active() )
  1175 				playing = 0;
  1176 		} MIDI_ELSE
  1177 #endif
  1178 #ifdef USE_TIMIDITY_MIDI
  1179 		if ( timidity_ok ) {
  1180 			if ( ! Timidity_Active() )
  1181 				playing = 0;
  1182 		}
  1183 #endif
  1184 		break;
  1185 #endif
  1186 #ifdef OGG_MUSIC
  1187 	    case MUS_OGG:
  1188 		if ( ! OGG_playing(music_playing->data.ogg) ) {
  1189 			playing = 0;
  1190 		}
  1191 		break;
  1192 #endif
  1193 #ifdef FLAC_MUSIC
  1194 	    case MUS_FLAC:
  1195 		if ( ! FLAC_playing(music_playing->data.flac) ) {
  1196 			playing = 0;
  1197 		}
  1198 		break;
  1199 #endif
  1200 #ifdef MP3_MUSIC
  1201 	    case MUS_MP3:
  1202 		if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
  1203 			playing = 0;
  1204 		break;
  1205 #endif
  1206 #ifdef MP3_MAD_MUSIC
  1207 	    case MUS_MP3_MAD:
  1208 		if (!mad_isPlaying(music_playing->data.mp3_mad)) {
  1209 			playing = 0;
  1210 		}
  1211 		break;
  1212 #endif
  1213 	    default:
  1214 		playing = 0;
  1215 		break;
  1216 	}
  1217 	return(playing);
  1218 }
  1219 int Mix_PlayingMusic(void)
  1220 {
  1221 	int playing = 0;
  1222 
  1223 	SDL_LockAudio();
  1224 	if ( music_playing ) {
  1225 		playing = music_internal_playing();
  1226 	}
  1227 	SDL_UnlockAudio();
  1228 
  1229 	return(playing);
  1230 }
  1231 
  1232 /* Set the external music playback command */
  1233 int Mix_SetMusicCMD(const char *command)
  1234 {
  1235 	Mix_HaltMusic();
  1236 	if ( music_cmd ) {
  1237 		free(music_cmd);
  1238 		music_cmd = NULL;
  1239 	}
  1240 	if ( command ) {
  1241 		music_cmd = (char *)malloc(strlen(command)+1);
  1242 		if ( music_cmd == NULL ) {
  1243 			return(-1);
  1244 		}
  1245 		strcpy(music_cmd, command);
  1246 	}
  1247 	return(0);
  1248 }
  1249 
  1250 int Mix_SetSynchroValue(int i)
  1251 {
  1252 	/* Not supported by any players at this time */
  1253 	return(-1);
  1254 }
  1255 
  1256 int Mix_GetSynchroValue(void)
  1257 {
  1258 	/* Not supported by any players at this time */
  1259 	return(-1);
  1260 }
  1261 
  1262 
  1263 /* Uninitialize the music players */
  1264 void close_music(void)
  1265 {
  1266 	Mix_HaltMusic();
  1267 #ifdef CMD_MUSIC
  1268 	Mix_SetMusicCMD(NULL);
  1269 #endif
  1270 #ifdef MODPLUG_MUSIC
  1271 	modplug_exit();
  1272 #endif
  1273 #ifdef MOD_MUSIC
  1274 	MOD_exit();
  1275 #endif
  1276 #ifdef MID_MUSIC
  1277 # ifdef USE_TIMIDITY_MIDI
  1278 	Timidity_Close();
  1279 # endif
  1280 #endif
  1281 
  1282 	/* rcg06042009 report available decoders at runtime. */
  1283 	free(music_decoders);
  1284 	music_decoders = NULL;
  1285 	num_decoders = 0;
  1286 
  1287 	ms_per_step = 0;
  1288 }
  1289 
  1290 Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
  1291 {
  1292 	Uint8 magic[5];	  /*Apparently there is no way to check if the file is really a MOD,*/
  1293 	/*		    or there are too many formats supported by MikMod or MikMod does */
  1294 	/*		    this check by itself. If someone implements other formats (e.g. MP3) */
  1295 	/*		    the check can be uncommented */
  1296 	Uint8 moremagic[9];
  1297 	Mix_Music *music;
  1298 	int start;
  1299 
  1300 	if (!rw) {
  1301 		Mix_SetError("RWops pointer is NULL");
  1302 		return NULL;
  1303 	}
  1304 
  1305 	/* Figure out what kind of file this is */
  1306 	start = SDL_RWtell(rw);
  1307 	if ( SDL_RWread(rw,magic,1,4) != 4 ||
  1308 	     SDL_RWread(rw,moremagic,1,8) != 8 ) {
  1309 		Mix_SetError("Couldn't read from RWops");
  1310 		return NULL;
  1311 	}
  1312 	SDL_RWseek(rw, start, RW_SEEK_SET);
  1313 	magic[4]='\0';
  1314 	moremagic[8] = '\0';
  1315 
  1316 	/* Allocate memory for the music structure */
  1317 	music=(Mix_Music *)malloc(sizeof(Mix_Music));
  1318 	if (music==NULL ) {
  1319 		Mix_SetError("Out of memory");
  1320 		return(NULL);
  1321 	}
  1322 	music->error = 0;
  1323 
  1324 #ifdef WAV_MUSIC
  1325 	/* WAVE files have the magic four bytes "RIFF"
  1326 	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
  1327 	 */
  1328 	if ( ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
  1329 	     (strcmp((char *)magic, "FORM") == 0) ) {
  1330 		music->type = MUS_WAV;
  1331 		music->data.wave = WAVStream_LoadSong_RW(rw, (char *)magic);
  1332 		if ( music->data.wave == NULL ) {
  1333 			music->error = 1;
  1334 		}
  1335 
  1336 	} else
  1337 #endif
  1338 #ifdef OGG_MUSIC
  1339 	/* Ogg Vorbis files have the magic four bytes "OggS" */
  1340 	if ( strcmp((char *)magic, "OggS") == 0 ) {
  1341 		music->type = MUS_OGG;
  1342 		music->data.ogg = OGG_new_RW(rw);
  1343 		if ( music->data.ogg == NULL ) {
  1344 			music->error = 1;
  1345 		}
  1346 	} else
  1347 #endif
  1348 #ifdef FLAC_MUSIC
  1349 	/* FLAC files have the magic four bytes "fLaC" */
  1350 	if ( strcmp((char *)magic, "fLaC") == 0 ) {
  1351 		music->type = MUS_FLAC;
  1352 		music->data.flac = FLAC_new_RW(rw);
  1353 		if ( music->data.flac == NULL ) {
  1354 			music->error = 1;
  1355 		}
  1356 	} else
  1357 #endif
  1358 #ifdef MP3_MUSIC
  1359 	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
  1360 		if ( Mix_Init(MIX_INIT_MP3) ) {
  1361 			SMPEG_Info info;
  1362 			music->type = MUS_MP3;
  1363 			music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
  1364 			if ( !info.has_audio ) {
  1365 				Mix_SetError("MPEG file does not have any audio stream.");
  1366 				music->error = 1;
  1367 			} else {
  1368 				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
  1369 			}
  1370 		} else {
  1371 			music->error = 1;
  1372 		}
  1373 	} else
  1374 #endif
  1375 #ifdef MP3_MAD_MUSIC
  1376 	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
  1377 		music->type = MUS_MP3_MAD;
  1378 		music->data.mp3_mad = mad_openFileRW(rw, &used_mixer);
  1379 		if (music->data.mp3_mad == 0) {
  1380 			Mix_SetError("Could not initialize MPEG stream.");
  1381 			music->error = 1;
  1382 		}
  1383 	} else
  1384 #endif
  1385 #ifdef MID_MUSIC
  1386 	/* MIDI files have the magic four bytes "MThd" */
  1387 	if ( strcmp((char *)magic, "MThd") == 0 ) {
  1388 		music->type = MUS_MID;
  1389 #ifdef USE_NATIVE_MIDI
  1390 		if ( native_midi_ok ) {
  1391 			music->data.nativemidi = native_midi_loadsong_RW(rw);
  1392 	  		if ( music->data.nativemidi == NULL ) {
  1393 		  		Mix_SetError("%s", native_midi_error());
  1394 			  	music->error = 1;
  1395 			}
  1396 		} MIDI_ELSE
  1397 #endif
  1398 #ifdef USE_TIMIDITY_MIDI
  1399 		if ( timidity_ok ) {
  1400 			music->data.midi = Timidity_LoadSong_RW(rw);
  1401 			if ( music->data.midi == NULL ) {
  1402 				Mix_SetError("%s", Timidity_Error());
  1403 				music->error = 1;
  1404 			}
  1405 		} else {
  1406 			Mix_SetError("%s", Timidity_Error());
  1407 			music->error = 1;
  1408 		}
  1409 #endif
  1410 	} else
  1411 #endif
  1412 #if defined(MODPLUG_MUSIC) || defined(MODPLUG_MUSIC)
  1413 	if (1) {
  1414 		music->error = 1;
  1415 #ifdef MODPLUG_MUSIC
  1416 		if ( music->error ) {
  1417 			music->type = MUS_MODPLUG;
  1418 			music->data.modplug = modplug_new_RW(rw);
  1419 			if ( music->data.modplug ) {
  1420 				music->error = 0;
  1421 			}
  1422 		}
  1423 #endif
  1424 #ifdef MOD_MUSIC
  1425 		if ( music->error ) {
  1426 			music->type = MUS_MOD;
  1427 			music->data.module = MOD_new_RW(rw);
  1428 			if ( music->data.module ) {
  1429 				music->error = 0;
  1430 			}
  1431 		}
  1432 #endif
  1433 	} else
  1434 #endif /* MODPLUG_MUSIC || MOD_MUSIC */
  1435 	{
  1436 		Mix_SetError("Unrecognized music format");
  1437 		music->error=1;
  1438 	}
  1439 	if (music->error) {
  1440 		free(music);
  1441 		music=NULL;
  1442 	}
  1443 	return(music);
  1444 }