music.c
changeset 542 3de4970b36d4
parent 541 323b314739ef
child 547 a9a5f18ea123
     1.1 --- a/music.c	Tue Jan 03 16:46:05 2012 -0800
     1.2 +++ b/music.c	Wed Jan 04 00:16:03 2012 -0500
     1.3 @@ -438,100 +438,271 @@
     1.4  	return (!*str1 && !*str2);
     1.5  }
     1.6  
     1.7 +static int detect_mp3(Uint8 *magic)
     1.8 +{
     1.9 +	if ( strncmp((char *)magic, "ID3", 3) == 0 ) {
    1.10 +		return 1;
    1.11 +	}
    1.12 +
    1.13 +	/* Detection code lifted from SMPEG */
    1.14 +	if(((magic[0] & 0xff) != 0xff) || // No sync bits
    1.15 +	   ((magic[1] & 0xf0) != 0xf0) || //
    1.16 +	   ((magic[2] & 0xf0) == 0x00) || // Bitrate is 0
    1.17 +	   ((magic[2] & 0xf0) == 0xf0) || // Bitrate is 15
    1.18 +	   ((magic[2] & 0x0c) == 0x0c) || // Frequency is 3
    1.19 +	   ((magic[1] & 0x06) == 0x00)) { // Layer is 4
    1.20 +		return(0);
    1.21 +	}
    1.22 +	return 1;
    1.23 +}
    1.24 +
    1.25 +/* MUS_MOD can't be auto-detected. If no other format was detected, MOD is
    1.26 + * assumed and MUS_MOD will be returned, meaning that the format might not
    1.27 + * actually be MOD-based.
    1.28 + *
    1.29 + * Returns MUS_NONE in case of errors. */
    1.30 +static Mix_MusicType detect_music_type(SDL_RWops *rw)
    1.31 +{
    1.32 +	Uint8 magic[5];
    1.33 +	Uint8 moremagic[9];
    1.34 +
    1.35 +	int start = SDL_RWtell(rw);
    1.36 +	if (SDL_RWread(rw, magic, 1, 4) != 4 || SDL_RWread(rw, moremagic, 1, 8) != 8 ) {
    1.37 +		Mix_SetError("Couldn't read from RWops");
    1.38 +		return MUS_NONE;
    1.39 +	}
    1.40 +	SDL_RWseek(rw, start, RW_SEEK_SET);
    1.41 +	magic[4]='\0';
    1.42 +	moremagic[8] = '\0';
    1.43 +
    1.44 +	/* WAVE files have the magic four bytes "RIFF"
    1.45 +	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */
    1.46 +	if (((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
    1.47 +		(strcmp((char *)magic, "FORM") == 0)) {
    1.48 +		return MUS_WAV;
    1.49 +	}
    1.50 +
    1.51 +	/* Ogg Vorbis files have the magic four bytes "OggS" */
    1.52 +	if (strcmp((char *)magic, "OggS") == 0) {
    1.53 +		return MUS_OGG;
    1.54 +	}
    1.55 +
    1.56 +	/* FLAC files have the magic four bytes "fLaC" */
    1.57 +	if (strcmp((char *)magic, "fLaC") == 0) {
    1.58 +		return MUS_FLAC;
    1.59 +	}
    1.60 +
    1.61 +	/* MIDI files have the magic four bytes "MThd" */
    1.62 +	if (strcmp((char *)magic, "MThd") == 0) {
    1.63 +		return MUS_MID;
    1.64 +	}
    1.65 +
    1.66 +	if (detect_mp3(magic)) {
    1.67 +		return MUS_MP3;
    1.68 +	}
    1.69 +
    1.70 +	/* Assume MOD format.
    1.71 +	 *
    1.72 +	 * Apparently there is no way to check if the file is really a MOD,
    1.73 +	 * or there are too many formats supported by MikMod/ModPlug, or
    1.74 +	 * MikMod/ModPlug does this check by itself. */
    1.75 +	return MUS_MOD;
    1.76 +}
    1.77 +
    1.78  /* Load a music file */
    1.79  Mix_Music *Mix_LoadMUS(const char *file)
    1.80  {
    1.81 -	FILE *fp;
    1.82 -	char *ext;
    1.83 -	Uint8 magic[5], moremagic[9];
    1.84 +	SDL_RWops *rw;
    1.85 +	Mix_Music *music;
    1.86 +	Mix_MusicType type;
    1.87 +	char *ext = strrchr(file, '.');
    1.88 +
    1.89 +#ifdef CMD_MUSIC
    1.90 +	if ( music_cmd ) {
    1.91 +		/* Allocate memory for the music structure */
    1.92 +		music = (Mix_Music *)malloc(sizeof(Mix_Music));
    1.93 +		if ( music == NULL ) {
    1.94 +			Mix_SetError("Out of memory");
    1.95 +			return(NULL);
    1.96 +		}
    1.97 +		music->error = 0;
    1.98 +		music->type = MUS_CMD;
    1.99 +		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
   1.100 +		if ( music->data.cmd == NULL ) {
   1.101 +			free(music);
   1.102 +			music == NULL;
   1.103 +		}
   1.104 +		return music;
   1.105 +	}
   1.106 +#endif
   1.107 +
   1.108 +	rw = SDL_RWFromFile(file, "rb");
   1.109 +	if ( rw == NULL ) {
   1.110 +		Mix_SetError("Couldn't open '%s'", file);
   1.111 +		return NULL;
   1.112 +	}
   1.113 +
   1.114 +	/* Use the extension as a first guess on the file type */
   1.115 +	type = MUS_NONE;
   1.116 +	ext = strrchr(file, '.');
   1.117 +	/* No need to guard these with #ifdef *_MUSIC stuff,
   1.118 +	 * since we simply call Mix_LoadMUSType_RW() later */
   1.119 +	if ( ext ) {
   1.120 +		++ext; /* skip the dot in the extension */
   1.121 +		if ( MIX_string_equals(ext, "WAV") ) {
   1.122 +			type = MUS_WAV;
   1.123 +		} else if ( MIX_string_equals(ext, "MID") ||
   1.124 +		            MIX_string_equals(ext, "MIDI") ||
   1.125 +		            MIX_string_equals(ext, "KAR") ) {
   1.126 +			type = MUS_MID;
   1.127 +		} else if ( MIX_string_equals(ext, "OGG") ) {
   1.128 +			type = MUS_OGG;
   1.129 +		} else if ( MIX_string_equals(ext, "FLAC") ) {
   1.130 +			type = MUS_FLAC;
   1.131 +		} else 	if ( MIX_string_equals(ext, "MPG") ||
   1.132 +		             MIX_string_equals(ext, "MPEG") ||
   1.133 +		             MIX_string_equals(ext, "MP3") ||
   1.134 +		             MIX_string_equals(ext, "MAD") ) {
   1.135 +			type = MUS_MP3;
   1.136 +		}
   1.137 +	}
   1.138 +	if ( type == MUS_NONE ) {
   1.139 +		type = detect_music_type(rw);
   1.140 +	}
   1.141 +
   1.142 +	/* We need to know if a specific error occurs; if not, we'll set a
   1.143 +	 * generic one, so we clear the current one. */
   1.144 +	Mix_SetError("");
   1.145 +	music = Mix_LoadMUSType_RW(rw, type, SDL_TRUE);
   1.146 +	if ( music == NULL && Mix_GetError()[0] == '\0' ) {
   1.147 +		SDL_FreeRW(rw);
   1.148 +		Mix_SetError("Couldn't open '%s'", file);
   1.149 +	}
   1.150 +	return music;
   1.151 +}
   1.152 +
   1.153 +Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
   1.154 +{
   1.155 +	return Mix_LoadMUSType_RW(rw, MUS_NONE, SDL_FALSE);
   1.156 +}
   1.157 +
   1.158 +Mix_Music *Mix_LoadMUSType_RW(SDL_RWops *rw, Mix_MusicType type, int freesrc)
   1.159 +{
   1.160  	Mix_Music *music;
   1.161  
   1.162 -	/* Figure out what kind of file this is */
   1.163 -	fp = fopen(file, "rb");
   1.164 -	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
   1.165 -		if ( fp != NULL ) {
   1.166 -			fclose(fp);
   1.167 +	if (!rw) {
   1.168 +		Mix_SetError("RWops pointer is NULL");
   1.169 +		return NULL;
   1.170 +	}
   1.171 +
   1.172 +	/* If the caller wants auto-detection, figure out what kind of file
   1.173 +	 * this is. */
   1.174 +	if (type == MUS_NONE) {
   1.175 +		if ((type = detect_music_type(rw)) == MUS_NONE) {
   1.176 +			/* Don't call Mix_SetError() here since detect_music_type()
   1.177 +			 * does that. */
   1.178 +			return NULL;
   1.179  		}
   1.180 -		Mix_SetError("Couldn't read from '%s'", file);
   1.181 -		return(NULL);
   1.182  	}
   1.183 -	if (!fread(moremagic, 8, 1, fp)) {
   1.184 -		Mix_SetError("Couldn't read from '%s'", file);
   1.185 -		return(NULL);
   1.186 -	}
   1.187 -	magic[4] = '\0';
   1.188 -	moremagic[8] = '\0';
   1.189 -	fclose(fp);
   1.190 -
   1.191 -	/* Figure out the file extension, so we can determine the type */
   1.192 -	ext = strrchr(file, '.');
   1.193 -	if ( ext ) ++ext; /* skip the dot in the extension */
   1.194  
   1.195  	/* Allocate memory for the music structure */
   1.196  	music = (Mix_Music *)malloc(sizeof(Mix_Music));
   1.197 -	if ( music == NULL ) {
   1.198 +	if (music == NULL ) {
   1.199  		Mix_SetError("Out of memory");
   1.200 -		return(NULL);
   1.201 +		return NULL;
   1.202  	}
   1.203  	music->error = 0;
   1.204  
   1.205 -#ifdef CMD_MUSIC
   1.206 -	if ( music_cmd ) {
   1.207 -		music->type = MUS_CMD;
   1.208 -		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
   1.209 -		if ( music->data.cmd == NULL ) {
   1.210 +	switch (type) {
   1.211 +#ifdef WAV_MUSIC
   1.212 +	case MUS_WAV:
   1.213 +		/* The WAVE loader needs the first 4 bytes of the header */
   1.214 +		{
   1.215 +			Uint8 magic[5];
   1.216 +			int start = SDL_RWtell(rw);
   1.217 +			if (SDL_RWread(rw, magic, 1, 4) != 4) {
   1.218 +				Mix_SetError("Couldn't read from RWops");
   1.219 +				return MUS_NONE;
   1.220 +			}
   1.221 +			SDL_RWseek(rw, start, RW_SEEK_SET);
   1.222 +			magic[4] = '\0';
   1.223 +			music->type = MUS_WAV;
   1.224 +			music->data.wave = WAVStream_LoadSong_RW(rw, (char *)magic, freesrc);
   1.225 +		}
   1.226 +		if (music->data.wave == NULL) {
   1.227  			music->error = 1;
   1.228  		}
   1.229 -	} else
   1.230 +		break;
   1.231  #endif
   1.232 -#ifdef WAV_MUSIC
   1.233 -	/* WAVE files have the magic four bytes "RIFF"
   1.234 -	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
   1.235 -	 */
   1.236 -	if ( (ext && MIX_string_equals(ext, "WAV")) ||
   1.237 -	     ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
   1.238 -	     (strcmp((char *)magic, "FORM") == 0) ) {
   1.239 -		music->type = MUS_WAV;
   1.240 -		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
   1.241 -		if ( music->data.wave == NULL ) {
   1.242 -		  	Mix_SetError("Unable to load WAV file");
   1.243 +#ifdef OGG_MUSIC
   1.244 +	case MUS_OGG:
   1.245 +		music->type = MUS_OGG;
   1.246 +		music->data.ogg = OGG_new_RW(rw, freesrc);
   1.247 +		if ( music->data.ogg == NULL ) {
   1.248  			music->error = 1;
   1.249  		}
   1.250 -	} else
   1.251 +		break;
   1.252 +#endif
   1.253 +#ifdef FLAC_MUSIC
   1.254 +	case MUS_FLAC:
   1.255 +		music->type = MUS_FLAC;
   1.256 +		music->data.flac = FLAC_new_RW(rw, freesrc);
   1.257 +		if ( music->data.flac == NULL ) {
   1.258 +			music->error = 1;
   1.259 +		}
   1.260 +		break;
   1.261 +#endif
   1.262 +#ifdef MP3_MUSIC
   1.263 +	case MUS_MP3:
   1.264 +		if ( Mix_Init(MIX_INIT_MP3) ) {
   1.265 +			SMPEG_Info info;
   1.266 +			music->type = MUS_MP3;
   1.267 +			music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
   1.268 +			if ( !info.has_audio ) {
   1.269 +				Mix_SetError("MPEG file does not have any audio stream.");
   1.270 +				music->error = 1;
   1.271 +			} else {
   1.272 +				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
   1.273 +			}
   1.274 +		} else {
   1.275 +			music->error = 1;
   1.276 +		}
   1.277 +		break;
   1.278 +#elif defined(MP3_MAD_MUSIC)
   1.279 +	case MUS_MP3:
   1.280 +		music->type = MUS_MP3_MAD;
   1.281 +		music->data.mp3_mad = mad_openFileRW(rw, &used_mixer, freesrc);
   1.282 +		if (music->data.mp3_mad == 0) {
   1.283 +			Mix_SetError("Could not initialize MPEG stream.");
   1.284 +			music->error = 1;
   1.285 +		}
   1.286 +		break;
   1.287  #endif
   1.288  #ifdef MID_MUSIC
   1.289 -	/* MIDI files have the magic four bytes "MThd" */
   1.290 -	if ( (ext && MIX_string_equals(ext, "MID")) ||
   1.291 -	     (ext && MIX_string_equals(ext, "MIDI")) ||
   1.292 -	     strcmp((char *)magic, "MThd") == 0  ||
   1.293 -	     ( strcmp((char *)magic, "RIFF") == 0  &&
   1.294 -	  	strcmp((char *)(moremagic+4), "RMID") == 0 ) ) {
   1.295 +	case MUS_MID:
   1.296  		music->type = MUS_MID;
   1.297  #ifdef USE_NATIVE_MIDI
   1.298 -  		if ( native_midi_ok ) {
   1.299 -/*printf("Native MIDI\n");*/
   1.300 -  			music->data.nativemidi = native_midi_loadsong(file);
   1.301 +		if ( native_midi_ok ) {
   1.302 +			music->data.nativemidi = native_midi_loadsong_RW(rw, freesrc);
   1.303  	  		if ( music->data.nativemidi == NULL ) {
   1.304  		  		Mix_SetError("%s", native_midi_error());
   1.305  			  	music->error = 1;
   1.306  			}
   1.307 -			goto skip;
   1.308 -	  	}
   1.309 +			break;
   1.310 +		}
   1.311  #endif
   1.312  #ifdef USE_FLUIDSYNTH_MIDI
   1.313  		if ( fluidsynth_ok ) {
   1.314 -/*printf("FluidSynth MIDI\n");*/
   1.315 -			music->data.fluidsynthmidi = fluidsynth_loadsong(file);
   1.316 +			music->data.fluidsynthmidi = fluidsynth_loadsong_RW(rw, freesrc);
   1.317  			if ( music->data.fluidsynthmidi == NULL ) {
   1.318  				music->error = 1;
   1.319  			}
   1.320 -			goto skip;
   1.321 +			break;
   1.322  		}
   1.323  #endif
   1.324  #ifdef USE_TIMIDITY_MIDI
   1.325  		if ( timidity_ok ) {
   1.326 -/*printf("Timidity MIDI\n");*/
   1.327 -			music->data.midi = Timidity_LoadSong(file);
   1.328 +			music->data.midi = Timidity_LoadSong_RW(rw, freesrc);
   1.329  			if ( music->data.midi == NULL ) {
   1.330  				Mix_SetError("%s", Timidity_Error());
   1.331  				music->error = 1;
   1.332 @@ -541,92 +712,41 @@
   1.333  			music->error = 1;
   1.334  		}
   1.335  #endif
   1.336 -	} else
   1.337 +		break;
   1.338  #endif
   1.339 -#ifdef OGG_MUSIC
   1.340 -	/* Ogg Vorbis files have the magic four bytes "OggS" */
   1.341 -	if ( (ext && MIX_string_equals(ext, "OGG")) ||
   1.342 -	     strcmp((char *)magic, "OggS") == 0 ) {
   1.343 -		music->type = MUS_OGG;
   1.344 -		music->data.ogg = OGG_new(file);
   1.345 -		if ( music->data.ogg == NULL ) {
   1.346 -			music->error = 1;
   1.347 +#if defined(MODPLUG_MUSIC) || defined(MOD_MUSIC)
   1.348 +	case MUS_MOD:
   1.349 +		music->error = 1;
   1.350 +#ifdef MODPLUG_MUSIC
   1.351 +		if ( music->error ) {
   1.352 +			music->type = MUS_MODPLUG;
   1.353 +			music->data.modplug = modplug_new_RW(rw, freesrc);
   1.354 +			if ( music->data.modplug ) {
   1.355 +				music->error = 0;
   1.356 +			}
   1.357  		}
   1.358 -	} else
   1.359 -#endif
   1.360 -#ifdef FLAC_MUSIC
   1.361 -	/* FLAC files have the magic four bytes "fLaC" */
   1.362 -	if ( (ext && MIX_string_equals(ext, "FLAC")) ||
   1.363 -		 strcmp((char *)magic, "fLaC") == 0 ) {
   1.364 -		music->type = MUS_FLAC;
   1.365 -		music->data.flac = FLAC_new(file);
   1.366 -		if ( music->data.flac == NULL ) {
   1.367 -			music->error = 1;
   1.368 -		}
   1.369 -	} else
   1.370 -#endif
   1.371 -#ifdef MP3_MUSIC
   1.372 -	if ( (ext && MIX_string_equals(ext, "MPG")) ||
   1.373 -	     (ext && MIX_string_equals(ext, "MP3")) ||
   1.374 -	     (ext && MIX_string_equals(ext, "MPEG")) ) {
   1.375 -		if ( Mix_Init(MIX_INIT_MP3) ) {
   1.376 -			SMPEG_Info info;
   1.377 -			music->type = MUS_MP3;
   1.378 -			music->data.mp3 = smpeg.SMPEG_new(file, &info, 0);
   1.379 -			if ( !info.has_audio ) {
   1.380 -				Mix_SetError("MPEG file does not have any audio stream.");
   1.381 -				music->error = 1;
   1.382 -			} else {
   1.383 -				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
   1.384 -			}
   1.385 -		} else {
   1.386 -			music->error = 1;
   1.387 -		}
   1.388 -	} else
   1.389 -#endif
   1.390 -#ifdef MP3_MAD_MUSIC
   1.391 -	if ( (ext && MIX_string_equals(ext, "MPG")) ||
   1.392 -	     (ext && MIX_string_equals(ext, "MP3")) ||
   1.393 -	     (ext && MIX_string_equals(ext, "MPEG")) ||
   1.394 -	     (ext && MIX_string_equals(ext, "MAD")) ||
   1.395 -	     /* Don't check the magic. Lots of bogus but usable MP3s out there...
   1.396 -	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ||
   1.397 -	     (strncmp((char *)magic, "ID3", 3) == 0)*/ ) {
   1.398 -		music->type = MUS_MP3_MAD;
   1.399 -		music->data.mp3_mad = mad_openFile(file, &used_mixer);
   1.400 -		if (music->data.mp3_mad == 0) {
   1.401 -		    Mix_SetError("Could not initialize MPEG stream.");
   1.402 -			music->error = 1;
   1.403 -		}
   1.404 -	} else
   1.405 -#endif
   1.406 -#ifdef MODPLUG_MUSIC
   1.407 -	if ( 1 ) {
   1.408 -		music->type = MUS_MODPLUG;
   1.409 -		music->data.modplug = modplug_new(file);
   1.410 -		if ( music->data.modplug == NULL ) {
   1.411 -			music->error = 1;
   1.412 -		}
   1.413 -	} else
   1.414  #endif
   1.415  #ifdef MOD_MUSIC
   1.416 -	if ( 1 ) {
   1.417 -		music->type = MUS_MOD;
   1.418 -		music->data.module = MOD_new(file);
   1.419 -		if ( music->data.module == NULL ) {
   1.420 -			music->error = 1;
   1.421 +		if ( music->error ) {
   1.422 +			music->type = MUS_MOD;
   1.423 +			music->data.module = MOD_new_RW(rw, freesrc);
   1.424 +			if ( music->data.module ) {
   1.425 +				music->error = 0;
   1.426 +			}
   1.427  		}
   1.428 -	} else
   1.429  #endif
   1.430 -	{
   1.431 +		break;
   1.432 +#endif
   1.433 +
   1.434 +	default:
   1.435  		Mix_SetError("Unrecognized music format");
   1.436 -		music->error = 1;
   1.437 -	}
   1.438 +		music->error=1;
   1.439 +	} /* switch (want) */
   1.440  
   1.441 -skip:
   1.442 -	if ( music->error ) {
   1.443 +
   1.444 +	if (music->error) {
   1.445  		free(music);
   1.446 -		music = NULL;
   1.447 +		music=NULL;
   1.448  	}
   1.449  	return(music);
   1.450  }
   1.451 @@ -1414,175 +1534,6 @@
   1.452  	ms_per_step = 0;
   1.453  }
   1.454  
   1.455 -Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
   1.456 -{
   1.457 -	Uint8 magic[5];	  /*Apparently there is no way to check if the file is really a MOD,*/
   1.458 -	/*		    or there are too many formats supported by MikMod or MikMod does */
   1.459 -	/*		    this check by itself. If someone implements other formats (e.g. MP3) */
   1.460 -	/*		    the check can be uncommented */
   1.461 -	Uint8 moremagic[9];
   1.462 -	Mix_Music *music;
   1.463 -	int start;
   1.464 -	int freerw = 0;
   1.465 -
   1.466 -	if (!rw) {
   1.467 -		Mix_SetError("RWops pointer is NULL");
   1.468 -		return NULL;
   1.469 -	}
   1.470 -
   1.471 -	/* Figure out what kind of file this is */
   1.472 -	start = SDL_RWtell(rw);
   1.473 -	if ( SDL_RWread(rw,magic,1,4) != 4 ||
   1.474 -	     SDL_RWread(rw,moremagic,1,8) != 8 ) {
   1.475 -		Mix_SetError("Couldn't read from RWops");
   1.476 -		return NULL;
   1.477 -	}
   1.478 -	SDL_RWseek(rw, start, RW_SEEK_SET);
   1.479 -	magic[4]='\0';
   1.480 -	moremagic[8] = '\0';
   1.481 -
   1.482 -	/* Allocate memory for the music structure */
   1.483 -	music=(Mix_Music *)malloc(sizeof(Mix_Music));
   1.484 -	if (music==NULL ) {
   1.485 -		Mix_SetError("Out of memory");
   1.486 -		return(NULL);
   1.487 -	}
   1.488 -	music->error = 0;
   1.489 -
   1.490 -#ifdef WAV_MUSIC
   1.491 -	/* WAVE files have the magic four bytes "RIFF"
   1.492 -	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
   1.493 -	 */
   1.494 -	if ( ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
   1.495 -	     (strcmp((char *)magic, "FORM") == 0) ) {
   1.496 -		music->type = MUS_WAV;
   1.497 -		music->data.wave = WAVStream_LoadSong_RW(rw, (char *)magic, freerw);
   1.498 -		if ( music->data.wave == NULL ) {
   1.499 -			music->error = 1;
   1.500 -		}
   1.501 -
   1.502 -	} else
   1.503 -#endif
   1.504 -#ifdef OGG_MUSIC
   1.505 -	/* Ogg Vorbis files have the magic four bytes "OggS" */
   1.506 -	if ( strcmp((char *)magic, "OggS") == 0 ) {
   1.507 -		music->type = MUS_OGG;
   1.508 -		music->data.ogg = OGG_new_RW(rw, freerw);
   1.509 -		if ( music->data.ogg == NULL ) {
   1.510 -			music->error = 1;
   1.511 -		}
   1.512 -	} else
   1.513 -#endif
   1.514 -#ifdef FLAC_MUSIC
   1.515 -	/* FLAC files have the magic four bytes "fLaC" */
   1.516 -	if ( strcmp((char *)magic, "fLaC") == 0 ) {
   1.517 -		music->type = MUS_FLAC;
   1.518 -		music->data.flac = FLAC_new_RW(rw, freerw);
   1.519 -		if ( music->data.flac == NULL ) {
   1.520 -			music->error = 1;
   1.521 -		}
   1.522 -	} else
   1.523 -#endif
   1.524 -#ifdef MP3_MUSIC
   1.525 -	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
   1.526 -		if ( Mix_Init(MIX_INIT_MP3) ) {
   1.527 -			SMPEG_Info info;
   1.528 -			music->type = MUS_MP3;
   1.529 -			music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
   1.530 -			if ( !info.has_audio ) {
   1.531 -				Mix_SetError("MPEG file does not have any audio stream.");
   1.532 -				music->error = 1;
   1.533 -			} else {
   1.534 -				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
   1.535 -			}
   1.536 -		} else {
   1.537 -			music->error = 1;
   1.538 -		}
   1.539 -	} else
   1.540 -#endif
   1.541 -#ifdef MP3_MAD_MUSIC
   1.542 -	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
   1.543 -		music->type = MUS_MP3_MAD;
   1.544 -		music->data.mp3_mad = mad_openFileRW(rw, &used_mixer, freerw);
   1.545 -		if (music->data.mp3_mad == 0) {
   1.546 -			Mix_SetError("Could not initialize MPEG stream.");
   1.547 -			music->error = 1;
   1.548 -		}
   1.549 -	} else
   1.550 -#endif
   1.551 -#ifdef MID_MUSIC
   1.552 -	/* MIDI files have the magic four bytes "MThd" */
   1.553 -	if ( strcmp((char *)magic, "MThd") == 0 ) {
   1.554 -		music->type = MUS_MID;
   1.555 -#ifdef USE_NATIVE_MIDI
   1.556 -		if ( native_midi_ok ) {
   1.557 -			music->data.nativemidi = native_midi_loadsong_RW(rw, freerw);
   1.558 -	  		if ( music->data.nativemidi == NULL ) {
   1.559 -		  		Mix_SetError("%s", native_midi_error());
   1.560 -			  	music->error = 1;
   1.561 -			}
   1.562 -			goto skip;
   1.563 -		}
   1.564 -#endif
   1.565 -#ifdef USE_FLUIDSYNTH_MIDI
   1.566 -		if ( fluidsynth_ok ) {
   1.567 -			music->data.fluidsynthmidi = fluidsynth_loadsong_RW(rw, freerw);
   1.568 -			if ( music->data.fluidsynthmidi == NULL ) {
   1.569 -				music->error = 1;
   1.570 -			}
   1.571 -			goto skip;
   1.572 -		}
   1.573 -#endif
   1.574 -#ifdef USE_TIMIDITY_MIDI
   1.575 -		if ( timidity_ok ) {
   1.576 -			music->data.midi = Timidity_LoadSong_RW(rw, freerw);
   1.577 -			if ( music->data.midi == NULL ) {
   1.578 -				Mix_SetError("%s", Timidity_Error());
   1.579 -				music->error = 1;
   1.580 -			}
   1.581 -		} else {
   1.582 -			Mix_SetError("%s", Timidity_Error());
   1.583 -			music->error = 1;
   1.584 -		}
   1.585 -#endif
   1.586 -	} else
   1.587 -#endif
   1.588 -#if defined(MODPLUG_MUSIC) || defined(MOD_MUSIC)
   1.589 -	if (1) {
   1.590 -		music->error = 1;
   1.591 -#ifdef MODPLUG_MUSIC
   1.592 -		if ( music->error ) {
   1.593 -			music->type = MUS_MODPLUG;
   1.594 -			music->data.modplug = modplug_new_RW(rw, freerw);
   1.595 -			if ( music->data.modplug ) {
   1.596 -				music->error = 0;
   1.597 -			}
   1.598 -		}
   1.599 -#endif
   1.600 -#ifdef MOD_MUSIC
   1.601 -		if ( music->error ) {
   1.602 -			music->type = MUS_MOD;
   1.603 -			music->data.module = MOD_new_RW(rw, freerw);
   1.604 -			if ( music->data.module ) {
   1.605 -				music->error = 0;
   1.606 -			}
   1.607 -		}
   1.608 -#endif
   1.609 -	} else
   1.610 -#endif /* MODPLUG_MUSIC || MOD_MUSIC */
   1.611 -	{
   1.612 -		Mix_SetError("Unrecognized music format");
   1.613 -		music->error=1;
   1.614 -	}
   1.615 -
   1.616 -skip:
   1.617 -	if (music->error) {
   1.618 -		free(music);
   1.619 -		music=NULL;
   1.620 -	}
   1.621 -	return(music);
   1.622 -}
   1.623 -
   1.624  int Mix_SetSoundFonts(const char *paths)
   1.625  {
   1.626  #ifdef MID_MUSIC