music.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 30 Nov 2001 16:33:05 +0000
changeset 133 2f15d225fe94
parent 129 782927d8df26
child 135 b9cc87fba5ac
permissions -rw-r--r--
*** empty log message ***
     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((char *)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((char *)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 		} else {
   510 			/* Stop implicit looping, fade out and other flags. */
   511 			music->data.module->extspd  = 1;
   512 			music->data.module->panflag = 1;
   513 			music->data.module->wrap    = 0;
   514 			music->data.module->loop    = 0;
   515 #if 0 /* Don't set fade out by default - unfortunately there's no real way
   516          to query the status of the song or set trigger actions.  Hum. */
   517 			music->data.module->fadeout = 1;
   518 #endif
   519 		}
   520 	} else
   521 #endif
   522 	{
   523 		Mix_SetError("Unrecognized music format");
   524 		music->error = 1;
   525 	}
   526 	if ( music->error ) {
   527 		free(music);
   528 		music = NULL;
   529 	}
   530 	return(music);
   531 }
   532 
   533 /* Free a music chunk previously loaded */
   534 void Mix_FreeMusic(Mix_Music *music)
   535 {
   536 	if ( music ) {
   537 		/* Caution: If music is playing, mixer will crash */
   538 		if ( music == music_playing && !music_stopped ) {
   539 			if ( music->fading == MIX_FADING_OUT ) {
   540 				/* Wait for the fade out to finish */
   541 				while ( music_playing && !music_stopped && (music_playing->fading == MIX_FADING_OUT) )
   542 					SDL_Delay(100);
   543 			} else {
   544 				Mix_HaltMusic(); /* Stop it immediately */
   545 			}
   546 		}
   547 		switch (music->type) {
   548 #ifdef CMD_MUSIC
   549 			case MUS_CMD:
   550 				MusicCMD_FreeSong(music->data.cmd);
   551 				break;
   552 #endif
   553 #ifdef WAV_MUSIC
   554 			case MUS_WAV:
   555 				WAVStream_FreeSong(music->data.wave);
   556 				break;
   557 #endif
   558 #ifdef MOD_MUSIC
   559 			case MUS_MOD:
   560 				MikMod_FreeSong(music->data.module);
   561 				break;
   562 #endif
   563 #ifdef MID_MUSIC
   564 			case MUS_MID:
   565 #ifdef USE_NATIVE_MIDI
   566   				if ( native_midi_ok ) {
   567 					native_midi_freesong(music->data.nativemidi);
   568 				} MIDI_ELSE
   569 #endif
   570 #ifdef USE_TIMIDITY_MIDI
   571 				if ( timidity_ok ) {
   572 					Timidity_FreeSong(music->data.midi);
   573 				}
   574 #endif
   575 				break;
   576 #endif
   577 #ifdef OGG_MUSIC
   578 			case MUS_OGG:
   579 				OGG_delete(music->data.ogg);
   580 				break;
   581 #endif
   582 #ifdef MP3_MUSIC
   583 			case MUS_MP3:
   584 				SMPEG_delete(music->data.mp3);
   585 				break;
   586 #endif
   587 			default:
   588 				/* Unknown music type?? */
   589 				break;
   590 		}
   591 		free(music);
   592 	}
   593 }
   594 
   595 static int lowlevel_play(Mix_Music *music)
   596 {
   597 	if(!music)
   598 		return(-1);
   599 
   600 	switch (music->type) {
   601 #ifdef CMD_MUSIC
   602 		case MUS_CMD:
   603 			MusicCMD_SetVolume(music_volume);
   604 			MusicCMD_Start(music->data.cmd);
   605 			break;
   606 #endif
   607 #ifdef WAV_MUSIC
   608 		case MUS_WAV:
   609 			WAVStream_SetVolume(music_volume);
   610 			WAVStream_Start(music->data.wave);
   611 			break;
   612 #endif
   613 #ifdef MOD_MUSIC
   614 		case MUS_MOD:
   615 			Player_SetVolume((SWORD)music_volume);
   616 			Player_Start(music->data.module);
   617 			Player_SetPosition(0);
   618 			break;
   619 #endif
   620 #ifdef MID_MUSIC
   621 		case MUS_MID:
   622 #ifdef USE_NATIVE_MIDI
   623   			if ( native_midi_ok ) {
   624 				native_midi_setvolume(music_volume);
   625 				native_midi_start(music->data.nativemidi);
   626 			} MIDI_ELSE
   627 #endif
   628 #ifdef USE_TIMIDITY_MIDI
   629 			if ( timidity_ok ) {
   630 				Timidity_SetVolume(music_volume);
   631 				Timidity_Start(music->data.midi);
   632 			}
   633 #endif
   634 			break;
   635 #endif
   636 #ifdef OGG_MUSIC
   637 		case MUS_OGG:
   638 			OGG_setvolume(music->data.ogg, music_volume);
   639 			OGG_play(music->data.ogg);
   640 			break;
   641 #endif
   642 #ifdef MP3_MUSIC
   643 		case MUS_MP3:
   644 			SMPEG_enableaudio(music->data.mp3,1);
   645 			SMPEG_enablevideo(music->data.mp3,0);
   646 			SMPEG_setvolume(music->data.mp3,(int)(((float)music_volume/(float)MIX_MAX_VOLUME)*100.0));
   647 			SMPEG_play(music->data.mp3);
   648 			break;
   649 #endif
   650 		default:
   651 			/* Unknown music type?? */
   652 			return(-1);
   653 	}
   654 	return(0);
   655 }
   656 
   657 /* Play a music chunk.  Returns 0, or -1 if there was an error.
   658 */
   659 int Mix_PlayMusic(Mix_Music *music, int loops)
   660 {
   661 	/* Don't play null pointers :-) */
   662 	if ( music == NULL ) {
   663 		return(-1);
   664 	}
   665 	/* If the current music is fading out, wait for the fade to complete */
   666 	while ( music_playing && !music_stopped && music_playing->fading==MIX_FADING_OUT ) {
   667 		SDL_Delay(100);
   668 	}
   669 
   670 	if ( lowlevel_play(music) < 0 ) {
   671 		return(-1);
   672 	}
   673 	music_active = 1;
   674 	music_stopped = 0;
   675 	music_loops = loops;
   676 	music_playing = music;
   677 	music_playing->fading = MIX_NO_FADING;
   678 	return(0);
   679 }
   680 
   681 /* Fade in a music over "ms" milliseconds */
   682 int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
   683 {
   684 	if ( music && music_volume > 0 ) { /* No need to fade if we can't hear it */
   685 		music->fade_volume = music_volume;
   686 		music_volume = 0;
   687 		if ( Mix_PlayMusic(music, loops) < 0 ) {
   688 			return(-1);
   689 		}
   690 		music_playing->fade_step = 0;
   691 		music_playing->fade_steps = ms/ms_per_step;
   692 		music_playing->fading = MIX_FADING_IN;
   693 	}
   694 	return(0);
   695 }
   696 
   697 /* Set the music volume */
   698 int Mix_VolumeMusic(int volume)
   699 {
   700 	int prev_volume;
   701 
   702 	prev_volume = music_volume;
   703 	if ( volume < 0 ) {
   704 		volume = 0;
   705 	}
   706 	if ( volume > SDL_MIX_MAXVOLUME ) {
   707 		volume = SDL_MIX_MAXVOLUME;
   708 	}
   709 	music_volume = volume;
   710 	if ( music_playing && !music_stopped ) {
   711 		switch (music_playing->type) {
   712 #ifdef CMD_MUSIC
   713 		case MUS_CMD:
   714 			MusicCMD_SetVolume(music_volume);
   715 			break;
   716 #endif
   717 #ifdef WAV_MUSIC
   718 		case MUS_WAV:
   719 			WAVStream_SetVolume(music_volume);
   720 			break;
   721 #endif
   722 #ifdef MOD_MUSIC
   723 		case MUS_MOD:
   724 			Player_SetVolume((SWORD)music_volume);
   725 			break;
   726 #endif
   727 #ifdef MID_MUSIC
   728 		case MUS_MID:
   729 #ifdef USE_NATIVE_MIDI
   730 			if ( native_midi_ok ) {
   731 				native_midi_setvolume(music_volume);
   732 			} MIDI_ELSE
   733 #endif
   734 #ifdef USE_TIMIDITY_MIDI
   735 			if ( timidity_ok ) {
   736 				Timidity_SetVolume(music_volume);
   737 			}
   738 #endif
   739 			break;
   740 #endif
   741 #ifdef OGG_MUSIC
   742 		case MUS_OGG:
   743 			OGG_setvolume(music_playing->data.ogg, music_volume);
   744 			break;
   745 #endif
   746 #ifdef MP3_MUSIC
   747 		case MUS_MP3:
   748 			SMPEG_setvolume(music_playing->data.mp3,(int)(((float)music_volume/(float)MIX_MAX_VOLUME)*100.0));
   749 			break;
   750 #endif
   751 		default:
   752 			/* Unknown music type?? */
   753 			break;
   754 		}
   755 	}
   756 	return(prev_volume);
   757 }
   758 
   759 static void lowlevel_halt(void)
   760 {
   761 	switch (music_playing->type) {
   762 #ifdef CMD_MUSIC
   763 	case MUS_CMD:
   764 		MusicCMD_Stop(music_playing->data.cmd);
   765 		break;
   766 #endif
   767 #ifdef WAV_MUSIC
   768 	case MUS_WAV:
   769 		WAVStream_Stop();
   770 		break;
   771 #endif
   772 #ifdef MOD_MUSIC
   773 	case MUS_MOD:
   774 		Player_Stop();
   775 		break;
   776 #endif
   777 #ifdef MID_MUSIC
   778 	case MUS_MID:
   779 #ifdef USE_NATIVE_MIDI
   780 		if ( native_midi_ok ) {
   781 			native_midi_stop();
   782 		} MIDI_ELSE
   783 #endif
   784 #ifdef USE_TIMIDITY_MIDI
   785 		if ( timidity_ok ) {
   786 			Timidity_Stop();
   787 		}
   788 #endif
   789 		break;
   790 #endif
   791 #ifdef OGG_MUSIC
   792 	case MUS_OGG:
   793 		OGG_stop(music_playing->data.ogg);
   794 		break;
   795 #endif
   796 #ifdef MP3_MUSIC
   797 	case MUS_MP3:
   798 		SMPEG_stop(music_playing->data.mp3);
   799 		break;
   800 #endif
   801 	default:
   802 		/* Unknown music type?? */
   803 		return;
   804 	}
   805 	if ( music_playing->fading != MIX_NO_FADING ) /* Restore volume */
   806 		music_volume = music_playing->fade_volume;
   807 	music_playing->fading = MIX_NO_FADING;
   808 	music_playing = NULL;
   809 	music_active = 0;
   810 	music_loops = 0;
   811 	music_stopped = 0;
   812 }
   813 
   814 /* Halt playing of music */
   815 int Mix_HaltMusic(void)
   816 {
   817 	if ( music_playing && !music_stopped ) {
   818 		/* Mark the music to be stopped from the sound thread */
   819 		music_stopped = 1;
   820 		/* Wait for it to be actually stopped */
   821 		while ( music_playing && music_active )
   822 			SDL_Delay(10);
   823 	}
   824 	return(0);
   825 }
   826 
   827 /* Progressively stop the music */
   828 int Mix_FadeOutMusic(int ms)
   829 {
   830 	if ( music_playing && !music_stopped &&
   831 	     (music_playing->fading == MIX_NO_FADING) ) {
   832 		if ( music_volume > 0 ) {
   833 			music_playing->fading = MIX_FADING_OUT;
   834 			music_playing->fade_volume = music_volume;
   835 			music_playing->fade_step = 0;
   836 			music_playing->fade_steps = ms/ms_per_step;
   837 			return(1);
   838 		}
   839 	}
   840 	return(0);
   841 }
   842 
   843 Mix_Fading Mix_FadingMusic(void)
   844 {
   845 	if( music_playing && !music_stopped )
   846 		return music_playing->fading;
   847 	return MIX_NO_FADING;
   848 }
   849 
   850 /* Pause/Resume the music stream */
   851 void Mix_PauseMusic(void)
   852 {
   853 	if ( music_playing && !music_stopped ) {
   854 		music_active = 0;
   855 	}
   856 }
   857 
   858 void Mix_ResumeMusic(void)
   859 {
   860 	if ( music_playing && !music_stopped ) {
   861 		music_active = 1;
   862 	}
   863 }
   864 
   865 void Mix_RewindMusic(void)
   866 {
   867 	if ( music_playing && !music_stopped ) {
   868 		switch ( music_playing->type ) {
   869 #ifdef MOD_MUSIC
   870 		case MUS_MOD:
   871 			Player_Start(music_playing->data.module);
   872 			Player_SetPosition(0);
   873 			break;
   874 #endif
   875 #ifdef MP3_MUSIC
   876 		case MUS_MP3:
   877 			SMPEG_rewind(music_playing->data.mp3);
   878 			break;
   879 #endif
   880 #ifdef MID_MUSIC
   881 		case MUS_MID:
   882 #ifdef USE_NATIVE_MIDI
   883 			if ( native_midi_ok ) {
   884 				native_midi_stop();
   885 			}
   886 #endif
   887 			break;
   888 #endif
   889 		default:
   890 			/* TODO: Implement this for other music backends */
   891 			break;
   892 		}
   893 	}
   894 }
   895 
   896 int Mix_PausedMusic(void)
   897 {
   898 	return (music_active == 0);
   899 }
   900 
   901 /* Check the status of the music */
   902 int Mix_PlayingMusic(void)
   903 {
   904 	if ( music_playing && ! music_stopped ) {
   905 		switch (music_playing->type) {
   906 #ifdef CMD_MUSIC
   907 			case MUS_CMD:
   908 				if (!MusicCMD_Active(music_playing->data.cmd)) {
   909 					return(0);
   910 				}
   911 				break;
   912 #endif
   913 #ifdef WAV_MUSIC
   914 			case MUS_WAV:
   915 				if ( ! WAVStream_Active() ) {
   916 					return(0);
   917 				}
   918 				break;
   919 #endif
   920 #ifdef MOD_MUSIC
   921 			case MUS_MOD:
   922 				if ( ! Player_Active() ) {
   923 					return(0);
   924 				}
   925 				break;
   926 #endif
   927 #ifdef MID_MUSIC
   928 			case MUS_MID:
   929 #ifdef USE_NATIVE_MIDI
   930 				if ( native_midi_ok ) {
   931 					if ( ! native_midi_active() )
   932 						return(0);
   933 				} MIDI_ELSE
   934 #endif
   935 #ifdef USE_TIMIDITY_MIDI
   936 				if ( timidity_ok ) {
   937 					if ( ! Timidity_Active() )
   938 						return(0);
   939 				}
   940 #endif
   941 				break;
   942 #endif
   943 #ifdef OGG_MUSIC
   944 			case MUS_OGG:
   945 				if ( ! OGG_playing(music_playing->data.ogg) ) {
   946 					return(0);
   947 				}
   948 				break;
   949 #endif
   950 #ifdef MP3_MUSIC
   951 			case MUS_MP3:
   952 				if ( SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
   953 					return(0);
   954 				break;
   955 #endif
   956 			default:
   957 				break;
   958 		}
   959 		return(1);
   960 	}
   961 	return(0);
   962 }
   963 
   964 /* Set the external music playback command */
   965 int Mix_SetMusicCMD(const char *command)
   966 {
   967 	Mix_HaltMusic();
   968 	if ( music_cmd ) {
   969 		free(music_cmd);
   970 		music_cmd = NULL;
   971 	}
   972 	if ( command ) {
   973 		music_cmd = (char *)malloc(strlen(command)+1);
   974 		if ( music_cmd == NULL ) {
   975 			return(-1);
   976 		}
   977 		strcpy(music_cmd, command);
   978 	}
   979 	return(0);
   980 }
   981 
   982 /* Uninitialize the music players */
   983 void close_music(void)
   984 {
   985 	Mix_HaltMusic();
   986 #ifdef CMD_MUSIC
   987 	Mix_SetMusicCMD(NULL);
   988 #endif
   989 #ifdef MOD_MUSIC
   990 	MikMod_Exit();
   991 	MikMod_UnregisterAllLoaders();
   992 	MikMod_UnregisterAllDrivers();
   993 #endif
   994 }
   995