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