From e958e24923e961b925897752b6dd82ef591b0ece Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 16 Oct 2001 18:18:05 +0000 Subject: [PATCH] Sam Lantinga - Tue Oct 16 11:17:12 PDT 2001 * The music file type is now determined by extension as well as magic --- CHANGES | 2 ++ music.c | 33 +++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 93181587..ce6654b5 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,7 @@ 1.2.1: +Sam Lantinga - Tue Oct 16 11:17:12 PDT 2001 + * The music file type is now determined by extension as well as magic Ryan C. Gordon - Tue Sep 11 12:05:54 PDT 2001 * Reworked playwave.c to make it more useful as a mixer testbed * Added a realtime sound effect API to SDL_mixer.h diff --git a/music.c b/music.c index 6d052ec3..3aa4e610 100644 --- a/music.c +++ b/music.c @@ -24,6 +24,7 @@ #include #include +#include #include "SDL_endian.h" #include "SDL_audio.h" #include "SDL_timer.h" @@ -375,10 +376,24 @@ int open_music(SDL_AudioSpec *mixer) return(0); } +/* Portable case-insensitive string compare function */ +int MIX_string_equals(const char *str1, const char *str2) +{ + while ( *str1 && *str2 ) { + if ( toupper((unsigned char)*str1) != + toupper((unsigned char)*str2) ) + break; + ++str1; + ++str2; + } + return (!*str1 && !*str2); +} + /* Load a music file */ Mix_Music *Mix_LoadMUS(const char *file) { FILE *fp; + char *ext; Uint8 magic[5]; Mix_Music *music; @@ -394,6 +409,10 @@ Mix_Music *Mix_LoadMUS(const char *file) magic[4] = '\0'; fclose(fp); + /* Figure out the file extension, so we can determine the type */ + ext = strrchr(file, '.'); + if ( ext ) ++ext; /* skip the dot in the extension */ + /* Allocate memory for the music structure */ music = (Mix_Music *)malloc(sizeof(Mix_Music)); if ( music == NULL ) { @@ -415,7 +434,8 @@ Mix_Music *Mix_LoadMUS(const char *file) /* WAVE files have the magic four bytes "RIFF" AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */ - if ( (strcmp((char *)magic, "RIFF") == 0) || + if ( (ext && (MIX_string_equals(ext, "WAV") == 0)) || + (strcmp((char *)magic, "RIFF") == 0) || (strcmp((char *)magic, "FORM") == 0) ) { music->type = MUS_WAV; music->data.wave = WAVStream_LoadSong(file, (char *)magic); @@ -426,7 +446,9 @@ Mix_Music *Mix_LoadMUS(const char *file) #endif #ifdef MID_MUSIC /* MIDI files have the magic four bytes "MThd" */ - if ( strcmp(magic, "MThd") == 0 ) { + if ( (ext && (MIX_string_equals(ext, "MID") == 0)) || + (ext && (MIX_string_equals(ext, "MIDI") == 0)) || + strcmp(magic, "MThd") == 0 ) { music->type = MUS_MID; #ifdef USE_NATIVE_MIDI if ( native_midi_ok ) { @@ -453,7 +475,8 @@ Mix_Music *Mix_LoadMUS(const char *file) #endif #ifdef OGG_MUSIC /* Ogg Vorbis files have the magic four bytes "OggS" */ - if ( strcmp(magic, "OggS") == 0 ) { + if ( (ext && (MIX_string_equals(ext, "OGG") == 0)) || + strcmp(magic, "OggS") == 0 ) { music->type = MUS_OGG; music->data.ogg = OGG_new(file); if ( music->data.ogg == NULL ) { @@ -462,7 +485,9 @@ Mix_Music *Mix_LoadMUS(const char *file) } else #endif #ifdef MP3_MUSIC - if ( magic[0]==0xFF && (magic[1]&0xF0)==0xF0) { + if ( (ext && (MIX_string_equals(ext, "MPG") == 0)) || + (ext && (MIX_string_equals(ext, "MPEG") == 0)) || + magic[0]==0xFF && (magic[1]&0xF0)==0xF0) { SMPEG_Info info; music->type = MUS_MP3; music->data.mp3 = SMPEG_new(file, &info, 0);