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