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