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