Skip to content

Commit

Permalink
Fixed loading Opus audio as audio chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 5, 2019
1 parent d25cbd3 commit 52845d1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
3 changes: 1 addition & 2 deletions mixer.c
Expand Up @@ -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 */
Expand Down
55 changes: 24 additions & 31 deletions music.c
Expand Up @@ -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;
}

Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion music.h
Expand Up @@ -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);
Expand Down

0 comments on commit 52845d1

Please sign in to comment.