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