From 52845d1c31c97a962e973d81fee98db560da262f Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 4 Oct 2019 18:00:42 -0700 Subject: [PATCH] Fixed loading Opus audio as audio chunks --- mixer.c | 3 +-- music.c | 55 ++++++++++++++++++++++++------------------------------- music.h | 2 +- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/mixer.c b/mixer.c index bec58994..a93a274f 100644 --- a/mixer.c +++ b/mixer.c @@ -720,8 +720,7 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc) } else if (SDL_memcmp(magic, "Crea", 4) == 0) { loaded = Mix_LoadVOC_RW(src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen); } else { - Mix_MusicType music_type = detect_music_type_from_magic(magic); - loaded = Mix_LoadMusic_RW(music_type, src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen); + loaded = Mix_LoadMusic_RW(detect_music_type(src), src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen); } if (!loaded) { /* The individual loaders have closed src if needed */ diff --git a/music.c b/music.c index d40436b1..ab03ded8 100644 --- a/music.c +++ b/music.c @@ -409,10 +409,33 @@ SDL_bool has_music(Mix_MusicType type) return SDL_FALSE; } -Mix_MusicType detect_music_type_from_magic(const Uint8 *magic) +Mix_MusicType detect_music_type(SDL_RWops *src) { + Uint8 magic[12]; + Mix_MusicType t; + + if (SDL_RWread(src, magic, 1, 12) != 12) { + Mix_SetError("Couldn't read first 12 bytes of audio data"); + return MUS_NONE; + } + SDL_RWseek(src, -12, RW_SEEK_CUR); + + /* WAVE files have the magic four bytes "RIFF" + AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */ + if (((SDL_memcmp(magic, "RIFF", 4) == 0) && (SDL_memcmp((magic+8), "WAVE", 4) == 0)) || + (SDL_memcmp(magic, "FORM", 4) == 0)) { + return MUS_WAV; + } + /* Ogg Vorbis files have the magic four bytes "OggS" */ if (SDL_memcmp(magic, "OggS", 4) == 0) { + Sint64 pos = SDL_RWtell(src); + SDL_RWseek(src, 28, RW_SEEK_CUR); + SDL_RWread(src, magic, 1, 8); + SDL_RWseek(src, pos, RW_SEEK_SET); + if (SDL_memcmp(magic, "OpusHead", 8) == 0) { + return MUS_OPUS; + } return MUS_OGG; } @@ -439,36 +462,6 @@ Mix_MusicType detect_music_type_from_magic(const Uint8 *magic) return MUS_MOD; } -static Mix_MusicType detect_music_type(SDL_RWops *src) -{ - Uint8 magic[12]; - Mix_MusicType t; - - if (SDL_RWread(src, magic, 1, 12) != 12) { - Mix_SetError("Couldn't read first 12 bytes of audio data"); - return MUS_NONE; - } - SDL_RWseek(src, -12, RW_SEEK_CUR); - - /* WAVE files have the magic four bytes "RIFF" - AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */ - if (((SDL_memcmp(magic, "RIFF", 4) == 0) && (SDL_memcmp((magic+8), "WAVE", 4) == 0)) || - (SDL_memcmp(magic, "FORM", 4) == 0)) { - return MUS_WAV; - } - t = detect_music_type_from_magic(magic); - if (t == MUS_OGG) { - Sint64 pos = SDL_RWtell(src); - SDL_RWseek(src, 28, RW_SEEK_CUR); - SDL_RWread(src, magic, 1, 8); - SDL_RWseek(src, pos, RW_SEEK_SET); - if (SDL_memcmp(magic, "OpusHead", 8) == 0) { - return MUS_OPUS; - } - } - return t; -} - /* Load a music file */ Mix_Music *Mix_LoadMUS(const char *file) { diff --git a/music.h b/music.h index d78e510a..b36fec03 100644 --- a/music.h +++ b/music.h @@ -105,7 +105,7 @@ typedef struct extern int get_num_music_interfaces(void); extern Mix_MusicInterface *get_music_interface(int index); -extern Mix_MusicType detect_music_type_from_magic(const Uint8 *magic); +extern Mix_MusicType detect_music_type(SDL_RWops *src); extern SDL_bool load_music_type(Mix_MusicType type); extern SDL_bool open_music_type(Mix_MusicType type); extern SDL_bool has_music(Mix_MusicType type);