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