music.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 16 Oct 2001 18:18:05 +0000
changeset 125 60af9d35aba2
parent 106 ba6a6039e158
child 126 c1062993276e
permissions -rw-r--r--
Sam Lantinga - Tue Oct 16 11:17:12 PDT 2001
* The music file type is now determined by extension as well as magic
     1 /*
     2 	MIXERLIB:  An audio mixer library based on the SDL library
     3 	Copyright (C) 1997-1999  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 	5635-34 Springhouse Dr.
    21 	Pleasanton, CA 94588 (USA)
    22 	slouken@devolution.com
    23 */
    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 int samplesize;
    85 static Mix_Music * volatile music_playing = NULL;
    86 static int music_volume = MIX_MAX_VOLUME;
    87 static int music_swap8;
    88 static int music_swap16;
    89 
    90 struct _Mix_Music {
    91 	enum {
    92 		MUS_CMD,
    93 		MUS_WAV,
    94 		MUS_MOD,
    95 		MUS_MID,
    96 		MUS_OGG,
    97 		MUS_MP3
    98 	} type;
    99 	union {
   100 #ifdef CMD_MUSIC
   101 		MusicCMD *cmd;
   102 #endif
   103 #ifdef WAV_MUSIC
   104 		WAVStream *wave;
   105 #endif
   106 #ifdef MOD_MUSIC
   107 		UNIMOD *module;
   108 #endif
   109 #ifdef MID_MUSIC
   110 #ifdef USE_TIMIDITY_MIDI
   111 		MidiSong *midi;
   112 #endif
   113 #ifdef USE_NATIVE_MIDI
   114 		NativeMidiSong *nativemidi;
   115 #endif
   116 #endif
   117 #ifdef OGG_MUSIC
   118 		OGG_music *ogg;
   119 #endif
   120 #ifdef MP3_MUSIC
   121 		SMPEG *mp3;
   122 #endif
   123 	} data;
   124 	Mix_Fading fading;
   125 	int fade_volume;
   126 	int fade_step;
   127 	int fade_steps;
   128 	int error;
   129 };
   130 #ifdef MID_MUSIC
   131 #ifdef USE_TIMIDITY_MIDI
   132 static int timidity_ok;
   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") == 0)) ||
   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 			music->error = 1;
   444 		}
   445 	} else
   446 #endif
   447 #ifdef MID_MUSIC
   448 	/* MIDI files have the magic four bytes "MThd" */
   449 	if ( (ext && (MIX_string_equals(ext, "MID") == 0)) ||
   450 	     (ext && (MIX_string_equals(ext, "MIDI") == 0)) ||
   451 	     strcmp(magic, "MThd") == 0 ) {
   452 		music->type = MUS_MID;
   453 #ifdef USE_NATIVE_MIDI
   454   		if ( native_midi_ok ) {
   455   			music->data.nativemidi = native_midi_loadsong((char *)file);
   456 	  		if ( music->data.nativemidi == NULL ) {
   457 		  		Mix_SetError("%s", native_midi_error());
   458 			  	music->error = 1;
   459 			}
   460 	  	} MIDI_ELSE
   461 #endif
   462 #ifdef USE_TIMIDITY_MIDI
   463 		if ( timidity_ok ) {
   464 			music->data.midi = Timidity_LoadSong((char *)file);
   465 			if ( music->data.midi == NULL ) {
   466 				Mix_SetError("%s", Timidity_Error());
   467 				music->error = 1;
   468 			}
   469 		} else {
   470 			Mix_SetError("%s", Timidity_Error());
   471 			music->error = 1;
   472 		}
   473 #endif
   474 	} else
   475 #endif
   476 #ifdef OGG_MUSIC
   477 	/* Ogg Vorbis files have the magic four bytes "OggS" */
   478 	if ( (ext && (MIX_string_equals(ext, "OGG") == 0)) ||
   479 	     strcmp(magic, "OggS") == 0 ) {
   480 		music->type = MUS_OGG;
   481 		music->data.ogg = OGG_new(file);
   482 		if ( music->data.ogg == NULL ) {
   483 			music->error = 1;
   484 		}
   485 	} else
   486 #endif
   487 #ifdef MP3_MUSIC
   488 	if ( (ext && (MIX_string_equals(ext, "MPG") == 0)) ||
   489 	     (ext && (MIX_string_equals(ext, "MPEG") == 0)) ||
   490 	     magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
   491 		SMPEG_Info info;
   492 		music->type = MUS_MP3;
   493 		music->data.mp3 = SMPEG_new(file, &info, 0);
   494 		if(!info.has_audio){
   495 			Mix_SetError("MPEG file does not have any audio stream.");
   496 			music->error = 1;
   497 		}else{
   498 			SMPEG_actualSpec(music->data.mp3, &used_mixer);
   499 		}
   500 	} else
   501 #endif
   502 #ifdef MOD_MUSIC
   503 	if ( 1 ) {
   504 		music->type = MUS_MOD;
   505 		music->data.module = MikMod_LoadSong((char *)file, 64);
   506 		if ( music->data.module == NULL ) {
   507 			Mix_SetError("%s", MikMod_strerror(MikMod_errno));
   508 			music->error = 1;
   509 		}
   510 	} else
   511 #endif
   512 	{
   513 		Mix_SetError("Unrecognized music format");
   514 		music->error = 1;
   515 	}
   516 	if ( music->error ) {
   517 		free(music);
   518 		music = NULL;
   519 	}
   520 	return(music);
   521 }
   522 
   523 /* Free a music chunk previously loaded */
   524 void Mix_FreeMusic(Mix_Music *music)
   525 {
   526 	if ( music ) {
   527 		/* Caution: If music is playing, mixer will crash */
   528 		if ( music == music_playing && !music_stopped ) {
   529 			if ( music->fading == MIX_FADING_OUT ) {
   530 				/* Wait for the fade out to finish */
   531 				while ( music_playing && !music_stopped && (music_playing->fading == MIX_FADING_OUT) )
   532 					SDL_Delay(100);
   533 			} else {
   534 				Mix_HaltMusic(); /* Stop it immediately */
   535 			}
   536 		}
   537 		switch (music->type) {
   538 #ifdef CMD_MUSIC
   539 			case MUS_CMD:
   540 				MusicCMD_FreeSong(music->data.cmd);
   541 				break;
   542 #endif
   543 #ifdef WAV_MUSIC
   544 			case MUS_WAV:
   545 				WAVStream_FreeSong(music->data.wave);
   546 				break;
   547 #endif
   548 #ifdef MOD_MUSIC
   549 			case MUS_MOD:
   550 				MikMod_FreeSong(music->data.module);
   551 				break;
   552 #endif
   553 #ifdef MID_MUSIC
   554 			case MUS_MID:
   555 #ifdef USE_NATIVE_MIDI
   556   				if ( native_midi_ok ) {
   557 					native_midi_freesong(music->data.nativemidi);
   558 				} MIDI_ELSE
   559 #endif
   560 #ifdef USE_TIMIDITY_MIDI
   561 				if ( timidity_ok ) {
   562 					Timidity_FreeSong(music->data.midi);
   563 				}
   564 #endif
   565 				break;
   566 #endif
   567 #ifdef OGG_MUSIC
   568 			case MUS_OGG:
   569 				OGG_delete(music->data.ogg);
   570 				break;
   571 #endif
   572 #ifdef MP3_MUSIC
   573 			case MUS_MP3:
   574 				SMPEG_delete(music->data.mp3);
   575 				break;
   576 #endif
   577 			default:
   578 				/* Unknown music type?? */
   579 				break;
   580 		}
   581 		free(music);
   582 	}
   583 }
   584 
   585 static int lowlevel_play(Mix_Music *music)
   586 {
   587 	if(!music)
   588 		return(-1);
   589 
   590 	switch (music->type) {
   591 #ifdef CMD_MUSIC
   592 		case MUS_CMD:
   593 			MusicCMD_SetVolume(music_volume);
   594 			MusicCMD_Start(music->data.cmd);
   595 			break;
   596 #endif
   597 #ifdef WAV_MUSIC
   598 		case MUS_WAV:
   599 			WAVStream_SetVolume(music_volume);
   600 			WAVStream_Start(music->data.wave);
   601 			break;
   602 #endif
   603 #ifdef MOD_MUSIC
   604 		case MUS_MOD:
   605 			Player_SetVolume((SWORD)music_volume);
   606 			Player_Start(music->data.module);
   607 			Player_SetPosition(0);
   608 			break;
   609 #endif
   610 #ifdef MID_MUSIC
   611 		case MUS_MID:
   612 #ifdef USE_NATIVE_MIDI
   613   			if ( native_midi_ok ) {
   614 				native_midi_setvolume(music_volume);
   615 				native_midi_start(music->data.nativemidi);
   616 			} MIDI_ELSE
   617 #endif
   618 #ifdef USE_TIMIDITY_MIDI
   619 			if ( timidity_ok ) {
   620 				Timidity_SetVolume(music_volume);
   621 				Timidity_Start(music->data.midi);
   622 			}
   623 #endif
   624 			break;
   625 #endif
   626 #ifdef OGG_MUSIC
   627 		case MUS_OGG:
   628 			OGG_setvolume(music->data.ogg, music_volume);
   629 			OGG_play(music->data.ogg);
   630 			break;
   631 #endif
   632 #ifdef MP3_MUSIC
   633 		case MUS_MP3:
   634 			SMPEG_enableaudio(music->data.mp3,1);
   635 			SMPEG_enablevideo(music->data.mp3,0);
   636 			SMPEG_setvolume(music->data.mp3,(int)(((float)music_volume/(float)MIX_MAX_VOLUME)*100.0));
   637 			SMPEG_play(music->data.mp3);
   638 			break;
   639 #endif
   640 		default:
   641 			/* Unknown music type?? */
   642 			return(-1);
   643 	}
   644 	return(0);
   645 }
   646 
   647 /* Play a music chunk.  Returns 0, or -1 if there was an error.
   648 */
   649 int Mix_PlayMusic(Mix_Music *music, int loops)
   650 {
   651 	/* Don't play null pointers :-) */
   652 	if ( music == NULL ) {
   653 		return(-1);
   654 	}
   655 	/* If the current music is fading out, wait for the fade to complete */
   656 	while ( music_playing && !music_stopped && music_playing->fading==MIX_FADING_OUT ) {
   657 		SDL_Delay(100);
   658 	}
   659 
   660 	if ( lowlevel_play(music) < 0 ) {
   661 		return(-1);
   662 	}
   663 	music_active = 1;
   664 	music_stopped = 0;
   665 	music_loops = loops;
   666 	music_playing = music;
   667 	music_playing->fading = MIX_NO_FADING;
   668 	return(0);
   669 }
   670 
   671 /* Fade in a music over "ms" milliseconds */
   672 int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
   673 {
   674 	if ( music && music_volume > 0 ) { /* No need to fade if we can't hear it */
   675 		music->fade_volume = music_volume;
   676 		music_volume = 0;
   677 		if ( Mix_PlayMusic(music, loops) < 0 ) {
   678 			return(-1);
   679 		}
   680 		music_playing->fade_step = 0;
   681 		music_playing->fade_steps = ms/ms_per_step;
   682 		music_playing->fading = MIX_FADING_IN;
   683 	}
   684 	return(0);
   685 }
   686 
   687 /* Set the music volume */
   688 int Mix_VolumeMusic(int volume)
   689 {
   690 	int prev_volume;
   691 
   692 	prev_volume = music_volume;
   693 	if ( volume < 0 ) {
   694 		volume = 0;
   695 	}
   696 	if ( volume > SDL_MIX_MAXVOLUME ) {
   697 		volume = SDL_MIX_MAXVOLUME;
   698 	}
   699 	music_volume = volume;
   700 	if ( music_playing && !music_stopped ) {
   701 		switch (music_playing->type) {
   702 #ifdef CMD_MUSIC
   703 		case MUS_CMD:
   704 			MusicCMD_SetVolume(music_volume);
   705 			break;
   706 #endif
   707 #ifdef WAV_MUSIC
   708 		case MUS_WAV:
   709 			WAVStream_SetVolume(music_volume);
   710 			break;
   711 #endif
   712 #ifdef MOD_MUSIC
   713 		case MUS_MOD:
   714 			Player_SetVolume((SWORD)music_volume);
   715 			break;
   716 #endif
   717 #ifdef MID_MUSIC
   718 		case MUS_MID:
   719 #ifdef USE_NATIVE_MIDI
   720 			if ( native_midi_ok ) {
   721 				native_midi_setvolume(music_volume);
   722 			} MIDI_ELSE
   723 #endif
   724 #ifdef USE_TIMIDITY_MIDI
   725 			if ( timidity_ok ) {
   726 				Timidity_SetVolume(music_volume);
   727 			}
   728 #endif
   729 			break;
   730 #endif
   731 #ifdef OGG_MUSIC
   732 		case MUS_OGG:
   733 			OGG_setvolume(music_playing->data.ogg, music_volume);
   734 			break;
   735 #endif
   736 #ifdef MP3_MUSIC
   737 		case MUS_MP3:
   738 			SMPEG_setvolume(music_playing->data.mp3,(int)(((float)music_volume/(float)MIX_MAX_VOLUME)*100.0));
   739 			break;
   740 #endif
   741 		default:
   742 			/* Unknown music type?? */
   743 			break;
   744 		}
   745 	}
   746 	return(prev_volume);
   747 }
   748 
   749 static void lowlevel_halt(void)
   750 {
   751 	switch (music_playing->type) {
   752 #ifdef CMD_MUSIC
   753 	case MUS_CMD:
   754 		MusicCMD_Stop(music_playing->data.cmd);
   755 		break;
   756 #endif
   757 #ifdef WAV_MUSIC
   758 	case MUS_WAV:
   759 		WAVStream_Stop();
   760 		break;
   761 #endif
   762 #ifdef MOD_MUSIC
   763 	case MUS_MOD:
   764 		Player_Stop();
   765 		break;
   766 #endif
   767 #ifdef MID_MUSIC
   768 	case MUS_MID:
   769 #ifdef USE_NATIVE_MIDI
   770 		if ( native_midi_ok ) {
   771 			native_midi_stop();
   772 		} MIDI_ELSE
   773 #endif
   774 #ifdef USE_TIMIDITY_MIDI
   775 		if ( timidity_ok ) {
   776 			Timidity_Stop();
   777 		}
   778 #endif
   779 		break;
   780 #endif
   781 #ifdef OGG_MUSIC
   782 	case MUS_OGG:
   783 		OGG_stop(music_playing->data.ogg);
   784 		break;
   785 #endif
   786 #ifdef MP3_MUSIC
   787 	case MUS_MP3:
   788 		SMPEG_stop(music_playing->data.mp3);
   789 		break;
   790 #endif
   791 	default:
   792 		/* Unknown music type?? */
   793 		return;
   794 	}
   795 	if ( music_playing->fading != MIX_NO_FADING ) /* Restore volume */
   796 		music_volume = music_playing->fade_volume;
   797 	music_playing->fading = MIX_NO_FADING;
   798 	music_playing = NULL;
   799 	music_active = 0;
   800 	music_loops = 0;
   801 	music_stopped = 0;
   802 }
   803 
   804 /* Halt playing of music */
   805 int Mix_HaltMusic(void)
   806 {
   807 	if ( music_playing && !music_stopped ) {
   808 		/* Mark the music to be stopped from the sound thread */
   809 		music_stopped = 1;
   810 		/* Wait for it to be actually stopped */
   811 		while ( music_playing && music_active )
   812 			SDL_Delay(10);
   813 	}
   814 	return(0);
   815 }
   816 
   817 /* Progressively stop the music */
   818 int Mix_FadeOutMusic(int ms)
   819 {
   820 	if ( music_playing && !music_stopped &&
   821 	     (music_playing->fading == MIX_NO_FADING) ) {
   822 		if ( music_volume > 0 ) {
   823 			music_playing->fading = MIX_FADING_OUT;
   824 			music_playing->fade_volume = music_volume;
   825 			music_playing->fade_step = 0;
   826 			music_playing->fade_steps = ms/ms_per_step;
   827 			return(1);
   828 		}
   829 	}
   830 	return(0);
   831 }
   832 
   833 Mix_Fading Mix_FadingMusic(void)
   834 {
   835 	if( music_playing && !music_stopped )
   836 		return music_playing->fading;
   837 	return MIX_NO_FADING;
   838 }
   839 
   840 /* Pause/Resume the music stream */
   841 void Mix_PauseMusic(void)
   842 {
   843 	if ( music_playing && !music_stopped ) {
   844 		music_active = 0;
   845 	}
   846 }
   847 
   848 void Mix_ResumeMusic(void)
   849 {
   850 	if ( music_playing && !music_stopped ) {
   851 		music_active = 1;
   852 	}
   853 }
   854 
   855 void Mix_RewindMusic(void)
   856 {
   857 	if ( music_playing && !music_stopped ) {
   858 		switch ( music_playing->type ) {
   859 #ifdef MOD_MUSIC
   860 		case MUS_MOD:
   861 			Player_Start(music_playing->data.module);
   862 			Player_SetPosition(0);
   863 			break;
   864 #endif
   865 #ifdef MP3_MUSIC
   866 		case MUS_MP3:
   867 			SMPEG_rewind(music_playing->data.mp3);
   868 			break;
   869 #endif
   870 #ifdef MID_MUSIC
   871 		case MUS_MID:
   872 #ifdef USE_NATIVE_MIDI
   873 			if ( native_midi_ok ) {
   874 				native_midi_stop();
   875 			}
   876 #endif
   877 			break;
   878 #endif
   879 		default:
   880 			/* TODO: Implement this for other music backends */
   881 			break;
   882 		}
   883 	}
   884 }
   885 
   886 int Mix_PausedMusic(void)
   887 {
   888 	return (music_active == 0);
   889 }
   890 
   891 /* Check the status of the music */
   892 int Mix_PlayingMusic(void)
   893 {
   894 	if ( music_playing && ! music_stopped ) {
   895 		switch (music_playing->type) {
   896 #ifdef CMD_MUSIC
   897 			case MUS_CMD:
   898 				if (!MusicCMD_Active(music_playing->data.cmd)) {
   899 					return(0);
   900 				}
   901 				break;
   902 #endif
   903 #ifdef WAV_MUSIC
   904 			case MUS_WAV:
   905 				if ( ! WAVStream_Active() ) {
   906 					return(0);
   907 				}
   908 				break;
   909 #endif
   910 #ifdef MOD_MUSIC
   911 			case MUS_MOD:
   912 				if ( ! Player_Active() ) {
   913 					return(0);
   914 				}
   915 				break;
   916 #endif
   917 #ifdef MID_MUSIC
   918 			case MUS_MID:
   919 #ifdef USE_NATIVE_MIDI
   920 				if ( native_midi_ok ) {
   921 					if ( ! native_midi_active() )
   922 						return(0);
   923 				} MIDI_ELSE
   924 #endif
   925 #ifdef USE_TIMIDITY_MIDI
   926 				if ( timidity_ok ) {
   927 					if ( ! Timidity_Active() )
   928 						return(0);
   929 				}
   930 #endif
   931 				break;
   932 #endif
   933 #ifdef OGG_MUSIC
   934 			case MUS_OGG:
   935 				if ( ! OGG_playing(music_playing->data.ogg) ) {
   936 					return(0);
   937 				}
   938 				break;
   939 #endif
   940 #ifdef MP3_MUSIC
   941 			case MUS_MP3:
   942 				if ( SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
   943 					return(0);
   944 				break;
   945 #endif
   946 			default:
   947 				break;
   948 		}
   949 		return(1);
   950 	}
   951 	return(0);
   952 }
   953 
   954 /* Set the external music playback command */
   955 int Mix_SetMusicCMD(const char *command)
   956 {
   957 	Mix_HaltMusic();
   958 	if ( music_cmd ) {
   959 		free(music_cmd);
   960 		music_cmd = NULL;
   961 	}
   962 	if ( command ) {
   963 		music_cmd = (char *)malloc(strlen(command)+1);
   964 		if ( music_cmd == NULL ) {
   965 			return(-1);
   966 		}
   967 		strcpy(music_cmd, command);
   968 	}
   969 	return(0);
   970 }
   971 
   972 /* Uninitialize the music players */
   973 void close_music(void)
   974 {
   975 	Mix_HaltMusic();
   976 #ifdef CMD_MUSIC
   977 	Mix_SetMusicCMD(NULL);
   978 #endif
   979 #ifdef MOD_MUSIC
   980 	MikMod_Exit();
   981 	MikMod_UnregisterAllLoaders();
   982 	MikMod_UnregisterAllDrivers();
   983 #endif
   984 }
   985