music.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Feb 2003 05:07:12 +0000
changeset 213 0cab09308739
parent 196 db9f7643b026
child 226 3691a375f2e6
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL_mixer:  An audio mixer library based on the SDL library
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  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 /* The music command hack is UNIX specific */
    35 #ifndef unix
    36 #undef CMD_MUSIC
    37 #endif
    38 
    39 #ifdef CMD_MUSIC
    40 #include "music_cmd.h"
    41 #endif
    42 #ifdef WAV_MUSIC
    43 #include "wavestream.h"
    44 #endif
    45 #ifdef MOD_MUSIC
    46 #  include "mikmod.h"
    47 #  if defined(LIBMIKMOD_VERSION)                /* libmikmod 3.1.8 */
    48 #    define UNIMOD			MODULE
    49 #    define MikMod_Init()		MikMod_Init(NULL)
    50 #    define MikMod_LoadSong(a,b)	Player_Load(a,b,0)
    51 #    define MikMod_FreeSong		Player_Free
    52      extern int MikMod_errno;
    53 #  else                                        /* old MikMod 3.0.3 */
    54 #    define MikMod_strerror(x)		_mm_errmsg[x])
    55 #    define MikMod_errno		_mm_errno
    56 #  endif
    57 #endif
    58 #ifdef MID_MUSIC
    59 #  ifdef USE_TIMIDITY_MIDI
    60 #    include "timidity.h"
    61 #  endif
    62 #  ifdef USE_NATIVE_MIDI
    63 #    include "native_midi.h"
    64 #  endif
    65 #  if defined(USE_TIMIDITY_MIDI) && defined(USE_NATIVE_MIDI)
    66 #    define MIDI_ELSE	else
    67 #  else
    68 #    define MIDI_ELSE
    69 #  endif
    70 #endif
    71 #ifdef OGG_MUSIC
    72 #include "music_ogg.h"
    73 #endif
    74 #ifdef MP3_MUSIC
    75 #include "smpeg.h"
    76 
    77 static SDL_AudioSpec used_mixer;
    78 #endif
    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 static int music_swap8;
    87 static int music_swap16;
    88 
    89 struct _Mix_Music {
    90 	Mix_MusicType type;
    91 	union {
    92 #ifdef CMD_MUSIC
    93 		MusicCMD *cmd;
    94 #endif
    95 #ifdef WAV_MUSIC
    96 		WAVStream *wave;
    97 #endif
    98 #ifdef MOD_MUSIC
    99 		UNIMOD *module;
   100 #endif
   101 #ifdef MID_MUSIC
   102 #ifdef USE_TIMIDITY_MIDI
   103 		MidiSong *midi;
   104 #endif
   105 #ifdef USE_NATIVE_MIDI
   106 		NativeMidiSong *nativemidi;
   107 #endif
   108 #endif
   109 #ifdef OGG_MUSIC
   110 		OGG_music *ogg;
   111 #endif
   112 #ifdef MP3_MUSIC
   113 		SMPEG *mp3;
   114 #endif
   115 	} data;
   116 	Mix_Fading fading;
   117 	int fade_step;
   118 	int fade_steps;
   119 	int error;
   120 };
   121 #ifdef MID_MUSIC
   122 #ifdef USE_TIMIDITY_MIDI
   123 static int timidity_ok;
   124 static int samplesize;
   125 #endif
   126 #ifdef USE_NATIVE_MIDI
   127 static int native_midi_ok;
   128 #endif
   129 #endif
   130 
   131 /* Used to calculate fading steps */
   132 static int ms_per_step;
   133 
   134 /* Local low-level functions prototypes */
   135 static void music_internal_volume(int volume);
   136 static int  music_internal_play(Mix_Music *music, double position);
   137 static int  music_internal_position(double position);
   138 static int  music_internal_playing();
   139 static void music_internal_halt(void);
   140 
   141 
   142 /* Support for hooking when the music has finished */
   143 static void (*music_finished_hook)(void) = NULL;
   144 
   145 void Mix_HookMusicFinished(void (*music_finished)(void))
   146 {
   147 	SDL_LockAudio();
   148 	music_finished_hook = music_finished;
   149 	SDL_UnlockAudio();
   150 }
   151 
   152 
   153 /* Mixing function */
   154 void music_mixer(void *udata, Uint8 *stream, int len)
   155 {
   156 	if ( music_playing && music_active ) {
   157 		/* Handle fading */
   158 		if ( music_playing->fading != MIX_NO_FADING ) {
   159 			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
   160 				int volume;
   161 				int fade_step = music_playing->fade_step;
   162 				int fade_steps = music_playing->fade_steps;
   163 
   164 				if ( music_playing->fading == MIX_FADING_OUT ) {
   165 					volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
   166 				} else { /* Fading in */
   167 					volume = (music_volume * fade_step) / fade_steps;
   168 				}
   169 				music_internal_volume(volume);
   170 			} else {
   171 				if ( music_playing->fading == MIX_FADING_OUT ) {
   172 					music_internal_halt();
   173 					if ( music_finished_hook ) {
   174 						music_finished_hook();
   175 					}
   176 					return;
   177 				}
   178 				music_playing->fading = MIX_NO_FADING;
   179 			}
   180 		}
   181 		/* Restart music if it has to loop */
   182 		if ( !music_internal_playing() ) {
   183 			/* Restart music if it has to loop at a high level */
   184 			if ( music_loops && --music_loops ) {
   185 				Mix_Fading current_fade = music_playing->fading;
   186 				music_internal_play(music_playing, 0.0);
   187 				music_playing->fading = current_fade;
   188 			} else {
   189 				music_internal_halt();
   190 				if ( music_finished_hook ) {
   191 					music_finished_hook();
   192 				}
   193 				return;
   194 			}
   195 		}
   196 		switch (music_playing->type) {
   197 #ifdef CMD_MUSIC
   198 			case MUS_CMD:
   199 				/* The playing is done externally */
   200 				break;
   201 #endif
   202 #ifdef WAV_MUSIC
   203 			case MUS_WAV:
   204 				WAVStream_PlaySome(stream, len);
   205 				break;
   206 #endif
   207 #ifdef MOD_MUSIC
   208 			case MUS_MOD:
   209 				VC_WriteBytes((SBYTE *)stream, len);
   210 				if ( music_swap8 ) {
   211 					Uint8 *dst;
   212 					int i;
   213 
   214 					dst = stream;
   215 					for ( i=len; i; --i ) {
   216 						*dst++ ^= 0x80;
   217 					}
   218 				} else
   219 				if ( music_swap16 ) {
   220 					Uint8 *dst, tmp;
   221 					int i;
   222 
   223 					dst = stream;
   224 					for ( i=(len/2); i; --i ) {
   225 						tmp = dst[0];
   226 						dst[0] = dst[1];
   227 						dst[1] = tmp;
   228 						dst += 2;
   229 					}
   230 				}
   231 				break;
   232 #endif
   233 #ifdef MID_MUSIC
   234 #ifdef USE_TIMIDITY_MIDI
   235 			case MUS_MID:
   236 				if ( timidity_ok ) {
   237 					int samples = len / samplesize;
   238   					Timidity_PlaySome(stream, samples);
   239 				}
   240 				break;
   241 #endif
   242 #endif
   243 #ifdef OGG_MUSIC
   244 			case MUS_OGG:
   245 				OGG_playAudio(music_playing->data.ogg, stream, len);
   246 				break;
   247 #endif
   248 #ifdef MP3_MUSIC
   249 			case MUS_MP3:
   250 				SMPEG_playAudio(music_playing->data.mp3, stream, len);
   251 				break;
   252 #endif
   253 			default:
   254 				/* Unknown music type?? */
   255 				break;
   256 		}
   257 	}
   258 }
   259 
   260 /* Initialize the music players with a certain desired audio format */
   261 int open_music(SDL_AudioSpec *mixer)
   262 {
   263 	int music_error;
   264 
   265 	music_error = 0;
   266 #ifdef WAV_MUSIC
   267 	if ( WAVStream_Init(mixer) < 0 ) {
   268 		++music_error;
   269 	}
   270 #endif
   271 #ifdef MOD_MUSIC
   272 	/* Set the MikMod music format */
   273 	music_swap8 = 0;
   274 	music_swap16 = 0;
   275 	switch (mixer->format) {
   276 
   277 		case AUDIO_U8:
   278 		case AUDIO_S8: {
   279 			if ( mixer->format == AUDIO_S8 ) {
   280 				music_swap8 = 1;
   281 			}
   282 			md_mode = 0;
   283 		}
   284 		break;
   285 
   286 		case AUDIO_S16LSB:
   287 		case AUDIO_S16MSB: {
   288 			/* See if we need to correct MikMod mixing */
   289 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   290 			if ( mixer->format == AUDIO_S16MSB ) {
   291 #else
   292 			if ( mixer->format == AUDIO_S16LSB ) {
   293 #endif
   294 				music_swap16 = 1;
   295 			}
   296 			md_mode = DMODE_16BITS;
   297 		}
   298 		break;
   299 
   300 		default: {
   301 			Mix_SetError("Unknown hardware audio format");
   302 			++music_error;
   303 		}
   304 	}
   305 	if ( mixer->channels > 1 ) {
   306 		if ( mixer->channels > 2 ) {
   307 			Mix_SetError("Hardware uses more channels than mixer");
   308 			++music_error;
   309 		}
   310 		md_mode |= DMODE_STEREO;
   311 	}
   312 	md_mixfreq	 = mixer->freq;
   313 	md_device	  = 0;
   314 	md_volume	  = 96;
   315 	md_musicvolume = 128;
   316 	md_sndfxvolume = 128;
   317 	md_pansep	  = 128;
   318 	md_reverb	  = 0;
   319 	MikMod_RegisterAllLoaders();
   320 	MikMod_RegisterAllDrivers();
   321 	if ( MikMod_Init() ) {
   322 		Mix_SetError("%s", MikMod_strerror(MikMod_errno));
   323 		++music_error;
   324 	}
   325 #endif
   326 #ifdef MID_MUSIC
   327 #ifdef USE_TIMIDITY_MIDI
   328 	samplesize = mixer->size / mixer->samples;
   329 	if ( Timidity_Init(mixer->freq, mixer->format,
   330 	                    mixer->channels, mixer->samples) == 0 ) {
   331 		timidity_ok = 1;
   332 	} else {
   333 		timidity_ok = 0;
   334 	}
   335 #endif
   336 #ifdef USE_NATIVE_MIDI
   337 #ifdef USE_TIMIDITY_MIDI
   338 	native_midi_ok = !timidity_ok;
   339 	if ( native_midi_ok )
   340 #endif
   341 		native_midi_ok = native_midi_detect();
   342 #endif
   343 #endif
   344 #ifdef OGG_MUSIC
   345 	if ( OGG_init(mixer) < 0 ) {
   346 		++music_error;
   347 	}
   348 #endif
   349 #ifdef MP3_MUSIC
   350 	/* Keep a copy of the mixer */
   351 	used_mixer = *mixer;
   352 #endif
   353 	music_playing = NULL;
   354 	music_stopped = 0;
   355 	if ( music_error ) {
   356 		return(-1);
   357 	}
   358 	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
   359 
   360 	/* Calculate the number of ms for each callback */
   361 	ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
   362 
   363 	return(0);
   364 }
   365 
   366 /* Portable case-insensitive string compare function */
   367 int MIX_string_equals(const char *str1, const char *str2)
   368 {
   369 	while ( *str1 && *str2 ) {
   370 		if ( toupper((unsigned char)*str1) !=
   371 		     toupper((unsigned char)*str2) )
   372 			break;
   373 		++str1;
   374 		++str2;
   375 	}
   376 	return (!*str1 && !*str2);
   377 }
   378 
   379 /* Load a music file */
   380 Mix_Music *Mix_LoadMUS(const char *file)
   381 {
   382 	FILE *fp;
   383 	char *ext;
   384 	Uint8 magic[5];
   385 	Mix_Music *music;
   386 
   387 	/* Figure out what kind of file this is */
   388 	fp = fopen(file, "rb");
   389 	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
   390 		if ( fp != NULL ) {
   391 			fclose(fp);
   392 		}
   393 		Mix_SetError("Couldn't read from '%s'", file);
   394 		return(NULL);
   395 	}
   396 	magic[4] = '\0';
   397 	fclose(fp);
   398 
   399 	/* Figure out the file extension, so we can determine the type */
   400 	ext = strrchr(file, '.');
   401 	if ( ext ) ++ext; /* skip the dot in the extension */
   402 
   403 	/* Allocate memory for the music structure */
   404 	music = (Mix_Music *)malloc(sizeof(Mix_Music));
   405 	if ( music == NULL ) {
   406 		Mix_SetError("Out of memory");
   407 		return(NULL);
   408 	}
   409 	music->error = 0;
   410 
   411 #ifdef CMD_MUSIC
   412 	if ( music_cmd ) {
   413 		music->type = MUS_CMD;
   414 		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
   415 		if ( music->data.cmd == NULL ) {
   416 			music->error = 1;
   417 		}
   418 	} else
   419 #endif
   420 #ifdef WAV_MUSIC
   421 	/* WAVE files have the magic four bytes "RIFF"
   422 	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
   423 	 */
   424 	if ( (ext && MIX_string_equals(ext, "WAV")) ||
   425 	     (strcmp((char *)magic, "RIFF") == 0) ||
   426 	     (strcmp((char *)magic, "FORM") == 0) ) {
   427 		music->type = MUS_WAV;
   428 		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
   429 		if ( music->data.wave == NULL ) {
   430 		  	Mix_SetError("Unable to load WAV file");
   431 			music->error = 1;
   432 		}
   433 	} else
   434 #endif
   435 #ifdef MID_MUSIC
   436 	/* MIDI files have the magic four bytes "MThd" */
   437 	if ( (ext && MIX_string_equals(ext, "MID")) ||
   438 	     (ext && MIX_string_equals(ext, "MIDI")) ||
   439 	     strcmp((char *)magic, "MThd") == 0 ) {
   440 		music->type = MUS_MID;
   441 #ifdef USE_NATIVE_MIDI
   442   		if ( native_midi_ok ) {
   443   			music->data.nativemidi = native_midi_loadsong((char *)file);
   444 	  		if ( music->data.nativemidi == NULL ) {
   445 		  		Mix_SetError("%s", native_midi_error());
   446 			  	music->error = 1;
   447 			}
   448 	  	} MIDI_ELSE
   449 #endif
   450 #ifdef USE_TIMIDITY_MIDI
   451 		if ( timidity_ok ) {
   452 			music->data.midi = Timidity_LoadSong((char *)file);
   453 			if ( music->data.midi == NULL ) {
   454 				Mix_SetError("%s", Timidity_Error());
   455 				music->error = 1;
   456 			}
   457 		} else {
   458 			Mix_SetError("%s", Timidity_Error());
   459 			music->error = 1;
   460 		}
   461 #endif
   462 	} else
   463 #endif
   464 #ifdef OGG_MUSIC
   465 	/* Ogg Vorbis files have the magic four bytes "OggS" */
   466 	if ( (ext && MIX_string_equals(ext, "OGG")) ||
   467 	     strcmp((char *)magic, "OggS") == 0 ) {
   468 		music->type = MUS_OGG;
   469 		music->data.ogg = OGG_new(file);
   470 		if ( music->data.ogg == NULL ) {
   471 			music->error = 1;
   472 		}
   473 	} else
   474 #endif
   475 #ifdef MP3_MUSIC
   476 	if ( (ext && MIX_string_equals(ext, "MPG")) ||
   477 	     (ext && MIX_string_equals(ext, "MP3")) ||
   478 	     (ext && MIX_string_equals(ext, "MPEG")) ||
   479 	     magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
   480 		SMPEG_Info info;
   481 		music->type = MUS_MP3;
   482 		music->data.mp3 = SMPEG_new(file, &info, 0);
   483 		if(!info.has_audio){
   484 			Mix_SetError("MPEG file does not have any audio stream.");
   485 			music->error = 1;
   486 		}else{
   487 			SMPEG_actualSpec(music->data.mp3, &used_mixer);
   488 		}
   489 	} else
   490 #endif
   491 #ifdef MOD_MUSIC
   492 	if ( 1 ) {
   493 		music->type = MUS_MOD;
   494 		music->data.module = MikMod_LoadSong((char *)file, 64);
   495 		if ( music->data.module == NULL ) {
   496 			Mix_SetError("%s", MikMod_strerror(MikMod_errno));
   497 			music->error = 1;
   498 		} else {
   499 			/* Stop implicit looping, fade out and other flags. */
   500 			music->data.module->extspd  = 1;
   501 			music->data.module->panflag = 1;
   502 			music->data.module->wrap    = 0;
   503 			music->data.module->loop    = 0;
   504 #if 0 /* Don't set fade out by default - unfortunately there's no real way
   505          to query the status of the song or set trigger actions.  Hum. */
   506 			music->data.module->fadeout = 1;
   507 #endif
   508 		}
   509 	} else
   510 #endif
   511 	{
   512 		Mix_SetError("Unrecognized music format");
   513 		music->error = 1;
   514 	}
   515 	if ( music->error ) {
   516 		free(music);
   517 		music = NULL;
   518 	}
   519 	return(music);
   520 }
   521 
   522 /* Free a music chunk previously loaded */
   523 void Mix_FreeMusic(Mix_Music *music)
   524 {
   525 	if ( music ) {
   526 		/* Stop the music if it's currently playing */
   527 		SDL_LockAudio();
   528 		if ( music == music_playing ) {
   529 			/* Wait for any fade out to finish */
   530 			while ( music->fading == MIX_FADING_OUT ) {
   531 				SDL_UnlockAudio();
   532 				SDL_Delay(100);
   533 				SDL_LockAudio();
   534 			}
   535 			if ( music == music_playing ) {
   536 				music_internal_halt();
   537 			}
   538 		}
   539 		SDL_UnlockAudio();
   540 		switch (music->type) {
   541 #ifdef CMD_MUSIC
   542 			case MUS_CMD:
   543 				MusicCMD_FreeSong(music->data.cmd);
   544 				break;
   545 #endif
   546 #ifdef WAV_MUSIC
   547 			case MUS_WAV:
   548 				WAVStream_FreeSong(music->data.wave);
   549 				break;
   550 #endif
   551 #ifdef MOD_MUSIC
   552 			case MUS_MOD:
   553 				MikMod_FreeSong(music->data.module);
   554 				break;
   555 #endif
   556 #ifdef MID_MUSIC
   557 			case MUS_MID:
   558 #ifdef USE_NATIVE_MIDI
   559   				if ( native_midi_ok ) {
   560 					native_midi_freesong(music->data.nativemidi);
   561 				} MIDI_ELSE
   562 #endif
   563 #ifdef USE_TIMIDITY_MIDI
   564 				if ( timidity_ok ) {
   565 					Timidity_FreeSong(music->data.midi);
   566 				}
   567 #endif
   568 				break;
   569 #endif
   570 #ifdef OGG_MUSIC
   571 			case MUS_OGG:
   572 				OGG_delete(music->data.ogg);
   573 				break;
   574 #endif
   575 #ifdef MP3_MUSIC
   576 			case MUS_MP3:
   577 				SMPEG_delete(music->data.mp3);
   578 				break;
   579 #endif
   580 			default:
   581 				/* Unknown music type?? */
   582 				break;
   583 		}
   584 		free(music);
   585 	}
   586 }
   587 
   588 /* Find out the music format of a mixer music, or the currently playing
   589    music, if 'music' is NULL.
   590 */
   591 Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
   592 {
   593 	Mix_MusicType type = MUS_NONE;
   594 
   595 	if ( music ) {
   596 		type = music->type;
   597 	} else {
   598 		SDL_LockAudio();
   599 		if ( music_playing ) {
   600 			type = music_playing->type;
   601 		}
   602 		SDL_UnlockAudio();
   603 	}
   604 	return(type);
   605 }
   606 
   607 /* Play a music chunk.  Returns 0, or -1 if there was an error.
   608  */
   609 static int music_internal_play(Mix_Music *music, double position)
   610 {
   611 	int retval = 0;
   612 
   613 	/* Note the music we're playing */
   614 	if ( music_playing ) {
   615 		music_internal_halt();
   616 	}
   617 	music_playing = music;
   618 
   619 	/* Set the initial volume */
   620 	if ( music->fading == MIX_FADING_IN ) {
   621 		music_internal_volume(0);
   622 	} else {
   623 		music_internal_volume(music_volume);
   624 	}
   625 
   626 	/* Set up for playback */
   627 	switch (music->type) {
   628 #ifdef CMD_MUSIC
   629 	    case MUS_CMD:
   630 		MusicCMD_Start(music->data.cmd);
   631 		break;
   632 #endif
   633 #ifdef WAV_MUSIC
   634 	    case MUS_WAV:
   635 		WAVStream_Start(music->data.wave);
   636 		break;
   637 #endif
   638 #ifdef MOD_MUSIC
   639 	    case MUS_MOD:
   640 		Player_Start(music->data.module);
   641 		break;
   642 #endif
   643 #ifdef MID_MUSIC
   644 	    case MUS_MID:
   645 #ifdef USE_NATIVE_MIDI
   646 		if ( native_midi_ok ) {
   647 			native_midi_start(music->data.nativemidi);
   648 		} MIDI_ELSE
   649 #endif
   650 #ifdef USE_TIMIDITY_MIDI
   651 		if ( timidity_ok ) {
   652 			Timidity_Start(music->data.midi);
   653 		}
   654 #endif
   655 		break;
   656 #endif
   657 #ifdef OGG_MUSIC
   658 	    case MUS_OGG:
   659 		OGG_play(music->data.ogg);
   660 		break;
   661 #endif
   662 #ifdef MP3_MUSIC
   663 	    case MUS_MP3:
   664 		SMPEG_enableaudio(music->data.mp3,1);
   665 		SMPEG_enablevideo(music->data.mp3,0);
   666 		SMPEG_play(music_playing->data.mp3);
   667 		break;
   668 #endif
   669 	    default:
   670 		Mix_SetError("Can't play unknown music type");
   671 		retval = -1;
   672 		break;
   673 	}
   674 
   675 	/* Set the playback position, note any errors if an offset is used */
   676 	if ( retval == 0 ) {
   677 		if ( position > 0.0 ) {
   678 			if ( music_internal_position(position) < 0 ) {
   679 				Mix_SetError("Position not implemented for music type");
   680 				retval = -1;
   681 			}
   682 		} else {
   683 			music_internal_position(0.0);
   684 		}
   685 	}
   686 
   687 	/* If the setup failed, we're not playing any music anymore */
   688 	if ( retval < 0 ) {
   689 		music_playing = NULL;
   690 	}
   691 	return(retval);
   692 }
   693 int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
   694 {
   695 	int retval;
   696 
   697 	/* Don't play null pointers :-) */
   698 	if ( music == NULL ) {
   699 		Mix_SetError("music parameter was NULL");
   700 		return(-1);
   701 	}
   702 
   703 	/* Setup the data */
   704 	if ( ms ) {
   705 		music->fading = MIX_FADING_IN;
   706 	} else {
   707 		music->fading = MIX_NO_FADING;
   708 	}
   709 	music->fade_step = 0;
   710 	music->fade_steps = ms/ms_per_step;
   711 
   712 	/* Play the puppy */
   713 	SDL_LockAudio();
   714 	/* If the current music is fading out, wait for the fade to complete */
   715 	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
   716 		SDL_UnlockAudio();
   717 		SDL_Delay(100);
   718 		SDL_LockAudio();
   719 	}
   720 	music_active = 1;
   721 	music_loops = loops;
   722 	retval = music_internal_play(music, position);
   723 	SDL_UnlockAudio();
   724 
   725 	return(retval);
   726 }
   727 int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
   728 {
   729 	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
   730 }
   731 int Mix_PlayMusic(Mix_Music *music, int loops)
   732 {
   733 	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
   734 }
   735 
   736 /* Set the playing music position */
   737 int music_internal_position(double position)
   738 {
   739 	int retval = 0;
   740 
   741 	switch (music_playing->type) {
   742 #ifdef MOD_MUSIC
   743 	    case MUS_MOD:
   744 		Player_SetPosition((UWORD)position);
   745 		break;
   746 #endif
   747 #ifdef OGG_MUSIC
   748 	    case MUS_OGG:
   749 		OGG_jump_to_time(music_playing->data.ogg, position);
   750 		break;
   751 #endif
   752 #ifdef MP3_MUSIC
   753 	    case MUS_MP3:
   754 		if ( position > 0.0 ) {
   755 			SMPEG_skip(music_playing->data.mp3, position);
   756 		} else {
   757 			SMPEG_rewind(music_playing->data.mp3);
   758 			SMPEG_play(music_playing->data.mp3);
   759 		}
   760 		break;
   761 #endif
   762 	    default:
   763 		/* TODO: Implement this for other music backends */
   764 		retval = -1;
   765 		break;
   766 	}
   767 	return(retval);
   768 }
   769 int Mix_SetMusicPosition(double position)
   770 {
   771 	int retval;
   772 
   773 	SDL_LockAudio();
   774 	if ( music_playing ) {
   775 		retval = music_internal_position(position);
   776 		if ( retval < 0 ) {
   777 			Mix_SetError("Position not implemented for music type");
   778 		}
   779 	} else {
   780 		Mix_SetError("Music isn't playing");
   781 		retval = -1;
   782 	}
   783 	SDL_UnlockAudio();
   784 
   785 	return(retval);
   786 }
   787 
   788 /* Set the music volume */
   789 static void music_internal_volume(int volume)
   790 {
   791 	switch (music_playing->type) {
   792 #ifdef CMD_MUSIC
   793 	    case MUS_CMD:
   794 		MusicCMD_SetVolume(volume);
   795 		break;
   796 #endif
   797 #ifdef WAV_MUSIC
   798 	    case MUS_WAV:
   799 		WAVStream_SetVolume(volume);
   800 		break;
   801 #endif
   802 #ifdef MOD_MUSIC
   803 	    case MUS_MOD:
   804 		Player_SetVolume((SWORD)volume);
   805 		break;
   806 #endif
   807 #ifdef MID_MUSIC
   808 	    case MUS_MID:
   809 #ifdef USE_NATIVE_MIDI
   810 		if ( native_midi_ok ) {
   811 			native_midi_setvolume(volume);
   812 		} MIDI_ELSE
   813 #endif
   814 #ifdef USE_TIMIDITY_MIDI
   815 		if ( timidity_ok ) {
   816 			Timidity_SetVolume(volume);
   817 		}
   818 #endif
   819 		break;
   820 #endif
   821 #ifdef OGG_MUSIC
   822 	    case MUS_OGG:
   823 		OGG_setvolume(music_playing->data.ogg, volume);
   824 		break;
   825 #endif
   826 #ifdef MP3_MUSIC
   827 	    case MUS_MP3:
   828 		SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
   829 		break;
   830 #endif
   831 	    default:
   832 		/* Unknown music type?? */
   833 		break;
   834 	}
   835 }
   836 int Mix_VolumeMusic(int volume)
   837 {
   838 	int prev_volume;
   839 
   840 	prev_volume = music_volume;
   841 	if ( volume < 0 ) {
   842 		return prev_volume;
   843 	}
   844 	if ( volume > SDL_MIX_MAXVOLUME ) {
   845 		volume = SDL_MIX_MAXVOLUME;
   846 	}
   847 	music_volume = volume;
   848 	SDL_LockAudio();
   849 	if ( music_playing ) {
   850 		music_internal_volume(music_volume);
   851 	}
   852 	SDL_UnlockAudio();
   853 	return(prev_volume);
   854 }
   855 
   856 /* Halt playing of music */
   857 static void music_internal_halt(void)
   858 {
   859 	switch (music_playing->type) {
   860 #ifdef CMD_MUSIC
   861 	    case MUS_CMD:
   862 		MusicCMD_Stop(music_playing->data.cmd);
   863 		break;
   864 #endif
   865 #ifdef WAV_MUSIC
   866 	    case MUS_WAV:
   867 		WAVStream_Stop();
   868 		break;
   869 #endif
   870 #ifdef MOD_MUSIC
   871 	    case MUS_MOD:
   872 		Player_Stop();
   873 		break;
   874 #endif
   875 #ifdef MID_MUSIC
   876 	    case MUS_MID:
   877 #ifdef USE_NATIVE_MIDI
   878 		if ( native_midi_ok ) {
   879 			native_midi_stop();
   880 		} MIDI_ELSE
   881 #endif
   882 #ifdef USE_TIMIDITY_MIDI
   883 		if ( timidity_ok ) {
   884 			Timidity_Stop();
   885 		}
   886 #endif
   887 		break;
   888 #endif
   889 #ifdef OGG_MUSIC
   890 	    case MUS_OGG:
   891 		OGG_stop(music_playing->data.ogg);
   892 		break;
   893 #endif
   894 #ifdef MP3_MUSIC
   895 	    case MUS_MP3:
   896 		SMPEG_stop(music_playing->data.mp3);
   897 		break;
   898 #endif
   899 	    default:
   900 		/* Unknown music type?? */
   901 		return;
   902 	}
   903 	music_playing->fading = MIX_NO_FADING;
   904 	music_playing = NULL;
   905 }
   906 int Mix_HaltMusic(void)
   907 {
   908 	SDL_LockAudio();
   909 	if ( music_playing ) {
   910 		music_internal_halt();
   911 	}
   912 	SDL_UnlockAudio();
   913 
   914 	return(0);
   915 }
   916 
   917 /* Progressively stop the music */
   918 int Mix_FadeOutMusic(int ms)
   919 {
   920 	int retval = 0;
   921 
   922 	SDL_LockAudio();
   923 	if ( music_playing && (music_playing->fading == MIX_NO_FADING) ) {
   924 		music_playing->fading = MIX_FADING_OUT;
   925 		music_playing->fade_step = 0;
   926 		music_playing->fade_steps = ms/ms_per_step;
   927 		retval = 1;
   928 	}
   929 	SDL_UnlockAudio();
   930 
   931 	return(retval);
   932 }
   933 
   934 Mix_Fading Mix_FadingMusic(void)
   935 {
   936 	Mix_Fading fading = MIX_NO_FADING;
   937 
   938 	SDL_LockAudio();
   939 	if ( music_playing ) {
   940 		fading = music_playing->fading;
   941 	}
   942 	SDL_UnlockAudio();
   943 
   944 	return(fading);
   945 }
   946 
   947 /* Pause/Resume the music stream */
   948 void Mix_PauseMusic(void)
   949 {
   950 	music_active = 0;
   951 }
   952 
   953 void Mix_ResumeMusic(void)
   954 {
   955 	music_active = 1;
   956 }
   957 
   958 void Mix_RewindMusic(void)
   959 {
   960 	Mix_SetMusicPosition(0.0);
   961 }
   962 
   963 int Mix_PausedMusic(void)
   964 {
   965 	return (music_active == 0);
   966 }
   967 
   968 /* Check the status of the music */
   969 static int music_internal_playing()
   970 {
   971 	int playing = 1;
   972 	switch (music_playing->type) {
   973 #ifdef CMD_MUSIC
   974 	    case MUS_CMD:
   975 		if (!MusicCMD_Active(music_playing->data.cmd)) {
   976 			playing = 0;
   977 		}
   978 		break;
   979 #endif
   980 #ifdef WAV_MUSIC
   981 	    case MUS_WAV:
   982 		if ( ! WAVStream_Active() ) {
   983 			playing = 0;
   984 		}
   985 		break;
   986 #endif
   987 #ifdef MOD_MUSIC
   988 	    case MUS_MOD:
   989 		if ( ! Player_Active() ) {
   990 			playing = 0;
   991 		}
   992 		break;
   993 #endif
   994 #ifdef MID_MUSIC
   995 	    case MUS_MID:
   996 #ifdef USE_NATIVE_MIDI
   997 		if ( native_midi_ok ) {
   998 			if ( ! native_midi_active() )
   999 				playing = 0;
  1000 		} MIDI_ELSE
  1001 #endif
  1002 #ifdef USE_TIMIDITY_MIDI
  1003 		if ( timidity_ok ) {
  1004 			if ( ! Timidity_Active() )
  1005 				playing = 0;
  1006 		}
  1007 #endif
  1008 		break;
  1009 #endif
  1010 #ifdef OGG_MUSIC
  1011 	    case MUS_OGG:
  1012 		if ( ! OGG_playing(music_playing->data.ogg) ) {
  1013 			playing = 0;
  1014 		}
  1015 		break;
  1016 #endif
  1017 #ifdef MP3_MUSIC
  1018 	    case MUS_MP3:
  1019 		if ( SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
  1020 			playing = 0;
  1021 		break;
  1022 #endif
  1023 	    default:
  1024 		playing = 0;
  1025 		break;
  1026 	}
  1027 	return(playing);
  1028 }
  1029 int Mix_PlayingMusic(void)
  1030 {
  1031 	int playing = 0;
  1032 
  1033 	SDL_LockAudio();
  1034 	if ( music_playing ) {
  1035 		playing = music_internal_playing();
  1036 	}
  1037 	SDL_UnlockAudio();
  1038 
  1039 	return(playing);
  1040 }
  1041 
  1042 /* Set the external music playback command */
  1043 int Mix_SetMusicCMD(const char *command)
  1044 {
  1045 	Mix_HaltMusic();
  1046 	if ( music_cmd ) {
  1047 		free(music_cmd);
  1048 		music_cmd = NULL;
  1049 	}
  1050 	if ( command ) {
  1051 		music_cmd = (char *)malloc(strlen(command)+1);
  1052 		if ( music_cmd == NULL ) {
  1053 			return(-1);
  1054 		}
  1055 		strcpy(music_cmd, command);
  1056 	}
  1057 	return(0);
  1058 }
  1059 
  1060 int Mix_SetSynchroValue(int i)
  1061 {
  1062 	if ( music_playing && ! music_stopped ) {
  1063 		switch (music_playing->type) {
  1064 #ifdef MOD_MUSIC
  1065 		    case MUS_MOD:
  1066 			if ( ! Player_Active() ) {
  1067 				return(-1);
  1068 			}
  1069 			Player_SetSynchroValue(i);
  1070 			return 0;
  1071 			break;
  1072 #endif
  1073 		    default:
  1074 			return(-1);
  1075 			break;
  1076 		}
  1077 		return(-1);
  1078 	}
  1079 	return(-1);
  1080 }
  1081 
  1082 int Mix_GetSynchroValue(void)
  1083 {
  1084 	if ( music_playing && ! music_stopped ) {
  1085 		switch (music_playing->type) {
  1086 #ifdef MOD_MUSIC
  1087 		    case MUS_MOD:
  1088 			if ( ! Player_Active() ) {
  1089 				return(-1);
  1090 			}
  1091 			return Player_GetSynchroValue();
  1092 			break;
  1093 #endif
  1094 		    default:
  1095 			return(-1);
  1096 			break;
  1097 		}
  1098 		return(-1);
  1099 	}
  1100 	return(-1);
  1101 }
  1102 
  1103 
  1104 /* Uninitialize the music players */
  1105 void close_music(void)
  1106 {
  1107 	Mix_HaltMusic();
  1108 #ifdef CMD_MUSIC
  1109 	Mix_SetMusicCMD(NULL);
  1110 #endif
  1111 #ifdef MOD_MUSIC
  1112 	MikMod_Exit();
  1113 	MikMod_UnregisterAllLoaders();
  1114 	MikMod_UnregisterAllDrivers();
  1115 #endif
  1116 }
  1117