Sam Lantinga - Tue Oct 16 11:17:12 PDT 2001
authorSam Lantinga <slouken@libsdl.org>
Tue, 16 Oct 2001 18:18:05 +0000
changeset 12560af9d35aba2
parent 124 2bed4c2d4ead
child 126 c1062993276e
Sam Lantinga - Tue Oct 16 11:17:12 PDT 2001
* The music file type is now determined by extension as well as magic
CHANGES
music.c
     1.1 --- a/CHANGES	Mon Oct 01 00:24:24 2001 +0000
     1.2 +++ b/CHANGES	Tue Oct 16 18:18:05 2001 +0000
     1.3 @@ -1,5 +1,7 @@
     1.4  
     1.5  1.2.1:
     1.6 +Sam Lantinga - Tue Oct 16 11:17:12 PDT 2001
     1.7 + * The music file type is now determined by extension as well as magic
     1.8  Ryan C. Gordon - Tue Sep 11 12:05:54 PDT 2001
     1.9   * Reworked playwave.c to make it more useful as a mixer testbed
    1.10   * Added a realtime sound effect API to SDL_mixer.h
     2.1 --- a/music.c	Mon Oct 01 00:24:24 2001 +0000
     2.2 +++ b/music.c	Tue Oct 16 18:18:05 2001 +0000
     2.3 @@ -24,6 +24,7 @@
     2.4  
     2.5  #include <stdlib.h>
     2.6  #include <string.h>
     2.7 +#include <ctype.h>
     2.8  #include "SDL_endian.h"
     2.9  #include "SDL_audio.h"
    2.10  #include "SDL_timer.h"
    2.11 @@ -375,10 +376,24 @@
    2.12  	return(0);
    2.13  }
    2.14  
    2.15 +/* Portable case-insensitive string compare function */
    2.16 +int MIX_string_equals(const char *str1, const char *str2)
    2.17 +{
    2.18 +	while ( *str1 && *str2 ) {
    2.19 +		if ( toupper((unsigned char)*str1) !=
    2.20 +		     toupper((unsigned char)*str2) )
    2.21 +			break;
    2.22 +		++str1;
    2.23 +		++str2;
    2.24 +	}
    2.25 +	return (!*str1 && !*str2);
    2.26 +}
    2.27 +
    2.28  /* Load a music file */
    2.29  Mix_Music *Mix_LoadMUS(const char *file)
    2.30  {
    2.31  	FILE *fp;
    2.32 +	char *ext;
    2.33  	Uint8 magic[5];
    2.34  	Mix_Music *music;
    2.35  
    2.36 @@ -394,6 +409,10 @@
    2.37  	magic[4] = '\0';
    2.38  	fclose(fp);
    2.39  
    2.40 +	/* Figure out the file extension, so we can determine the type */
    2.41 +	ext = strrchr(file, '.');
    2.42 +	if ( ext ) ++ext; /* skip the dot in the extension */
    2.43 +
    2.44  	/* Allocate memory for the music structure */
    2.45  	music = (Mix_Music *)malloc(sizeof(Mix_Music));
    2.46  	if ( music == NULL ) {
    2.47 @@ -415,7 +434,8 @@
    2.48  	/* WAVE files have the magic four bytes "RIFF"
    2.49  	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
    2.50  	 */
    2.51 -	if ( (strcmp((char *)magic, "RIFF") == 0) ||
    2.52 +	if ( (ext && (MIX_string_equals(ext, "WAV") == 0)) ||
    2.53 +	     (strcmp((char *)magic, "RIFF") == 0) ||
    2.54  	     (strcmp((char *)magic, "FORM") == 0) ) {
    2.55  		music->type = MUS_WAV;
    2.56  		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
    2.57 @@ -426,7 +446,9 @@
    2.58  #endif
    2.59  #ifdef MID_MUSIC
    2.60  	/* MIDI files have the magic four bytes "MThd" */
    2.61 -	if ( strcmp(magic, "MThd") == 0 ) {
    2.62 +	if ( (ext && (MIX_string_equals(ext, "MID") == 0)) ||
    2.63 +	     (ext && (MIX_string_equals(ext, "MIDI") == 0)) ||
    2.64 +	     strcmp(magic, "MThd") == 0 ) {
    2.65  		music->type = MUS_MID;
    2.66  #ifdef USE_NATIVE_MIDI
    2.67    		if ( native_midi_ok ) {
    2.68 @@ -453,7 +475,8 @@
    2.69  #endif
    2.70  #ifdef OGG_MUSIC
    2.71  	/* Ogg Vorbis files have the magic four bytes "OggS" */
    2.72 -	if ( strcmp(magic, "OggS") == 0 ) {
    2.73 +	if ( (ext && (MIX_string_equals(ext, "OGG") == 0)) ||
    2.74 +	     strcmp(magic, "OggS") == 0 ) {
    2.75  		music->type = MUS_OGG;
    2.76  		music->data.ogg = OGG_new(file);
    2.77  		if ( music->data.ogg == NULL ) {
    2.78 @@ -462,7 +485,9 @@
    2.79  	} else
    2.80  #endif
    2.81  #ifdef MP3_MUSIC
    2.82 -	if ( magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
    2.83 +	if ( (ext && (MIX_string_equals(ext, "MPG") == 0)) ||
    2.84 +	     (ext && (MIX_string_equals(ext, "MPEG") == 0)) ||
    2.85 +	     magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
    2.86  		SMPEG_Info info;
    2.87  		music->type = MUS_MP3;
    2.88  		music->data.mp3 = SMPEG_new(file, &info, 0);