music.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 15 Jul 2007 05:33:35 +0000
changeset 357 d6d0cfdbea65
parent 336 ab67d7115d3e
child 381 2064088ea781
permissions -rw-r--r--
Fixed bug #269

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