music.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 21 Aug 2005 20:54:52 +0000
changeset 276 00ff1db6f904
parent 269 1c015da5a712
child 277 7d1be40dca4a
permissions -rw-r--r--
Date: Fri, 22 Jul 2005 23:12:49 +1000
From: Ian Munsie
Subject: URGENT: SDL_mixer-1.2.6 bug fix (or two)

bug info: When SDL_mixer attempted to initialise libmikmod, it used
all drivers embedded in the libmikmod. Problem was, that the sound
card drivers were already in use by SDL, so libmikmod would hang
waiting for them to become free on many systems. As SDL_mixer uses a
hook to transfer the final stream from libmikmod into SDL_mixer's
mixer, there is no reason that libmikmod should be using a card driver
at all, the nosound driver still does all the internal processing, and
will still call the hook fine.

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