Fixed loading Opus audio as audio chunks
authorSam Lantinga <slouken@libsdl.org>
Fri, 04 Oct 2019 18:00:42 -0700
changeset 950d4f1ccbad974
parent 949 59d490dfd5c8
child 951 11e76f76d5ff
Fixed loading Opus audio as audio chunks
mixer.c
music.c
music.h
     1.1 --- a/mixer.c	Wed Sep 11 20:20:50 2019 +0300
     1.2 +++ b/mixer.c	Fri Oct 04 18:00:42 2019 -0700
     1.3 @@ -720,8 +720,7 @@
     1.4      } else if (SDL_memcmp(magic, "Crea", 4) == 0) {
     1.5          loaded = Mix_LoadVOC_RW(src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
     1.6      } else {
     1.7 -        Mix_MusicType music_type = detect_music_type_from_magic(magic);
     1.8 -        loaded = Mix_LoadMusic_RW(music_type, src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
     1.9 +        loaded = Mix_LoadMusic_RW(detect_music_type(src), src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
    1.10      }
    1.11      if (!loaded) {
    1.12          /* The individual loaders have closed src if needed */
     2.1 --- a/music.c	Wed Sep 11 20:20:50 2019 +0300
     2.2 +++ b/music.c	Fri Oct 04 18:00:42 2019 -0700
     2.3 @@ -409,10 +409,33 @@
     2.4      return SDL_FALSE;
     2.5  }
     2.6  
     2.7 -Mix_MusicType detect_music_type_from_magic(const Uint8 *magic)
     2.8 +Mix_MusicType detect_music_type(SDL_RWops *src)
     2.9  {
    2.10 +    Uint8 magic[12];
    2.11 +    Mix_MusicType t;
    2.12 +
    2.13 +    if (SDL_RWread(src, magic, 1, 12) != 12) {
    2.14 +        Mix_SetError("Couldn't read first 12 bytes of audio data");
    2.15 +        return MUS_NONE;
    2.16 +    }
    2.17 +    SDL_RWseek(src, -12, RW_SEEK_CUR);
    2.18 +
    2.19 +    /* WAVE files have the magic four bytes "RIFF"
    2.20 +       AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */
    2.21 +    if (((SDL_memcmp(magic, "RIFF", 4) == 0) && (SDL_memcmp((magic+8), "WAVE", 4) == 0)) ||
    2.22 +        (SDL_memcmp(magic, "FORM", 4) == 0)) {
    2.23 +        return MUS_WAV;
    2.24 +    }
    2.25 +
    2.26      /* Ogg Vorbis files have the magic four bytes "OggS" */
    2.27      if (SDL_memcmp(magic, "OggS", 4) == 0) {
    2.28 +        Sint64 pos = SDL_RWtell(src);
    2.29 +        SDL_RWseek(src, 28, RW_SEEK_CUR);
    2.30 +        SDL_RWread(src, magic, 1, 8);
    2.31 +        SDL_RWseek(src, pos, RW_SEEK_SET);
    2.32 +        if (SDL_memcmp(magic, "OpusHead", 8) == 0) {
    2.33 +            return MUS_OPUS;
    2.34 +        }
    2.35          return MUS_OGG;
    2.36      }
    2.37  
    2.38 @@ -439,36 +462,6 @@
    2.39      return MUS_MOD;
    2.40  }
    2.41  
    2.42 -static Mix_MusicType detect_music_type(SDL_RWops *src)
    2.43 -{
    2.44 -    Uint8 magic[12];
    2.45 -    Mix_MusicType t;
    2.46 -
    2.47 -    if (SDL_RWread(src, magic, 1, 12) != 12) {
    2.48 -        Mix_SetError("Couldn't read first 12 bytes of audio data");
    2.49 -        return MUS_NONE;
    2.50 -    }
    2.51 -    SDL_RWseek(src, -12, RW_SEEK_CUR);
    2.52 -
    2.53 -    /* WAVE files have the magic four bytes "RIFF"
    2.54 -       AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */
    2.55 -    if (((SDL_memcmp(magic, "RIFF", 4) == 0) && (SDL_memcmp((magic+8), "WAVE", 4) == 0)) ||
    2.56 -        (SDL_memcmp(magic, "FORM", 4) == 0)) {
    2.57 -        return MUS_WAV;
    2.58 -    }
    2.59 -    t = detect_music_type_from_magic(magic);
    2.60 -    if (t == MUS_OGG) {
    2.61 -        Sint64 pos = SDL_RWtell(src);
    2.62 -        SDL_RWseek(src, 28, RW_SEEK_CUR);
    2.63 -        SDL_RWread(src, magic, 1, 8);
    2.64 -        SDL_RWseek(src, pos, RW_SEEK_SET);
    2.65 -        if (SDL_memcmp(magic, "OpusHead", 8) == 0) {
    2.66 -            return MUS_OPUS;
    2.67 -        }
    2.68 -    }
    2.69 -    return t;
    2.70 -}
    2.71 -
    2.72  /* Load a music file */
    2.73  Mix_Music *Mix_LoadMUS(const char *file)
    2.74  {
     3.1 --- a/music.h	Wed Sep 11 20:20:50 2019 +0300
     3.2 +++ b/music.h	Fri Oct 04 18:00:42 2019 -0700
     3.3 @@ -105,7 +105,7 @@
     3.4  
     3.5  extern int get_num_music_interfaces(void);
     3.6  extern Mix_MusicInterface *get_music_interface(int index);
     3.7 -extern Mix_MusicType detect_music_type_from_magic(const Uint8 *magic);
     3.8 +extern Mix_MusicType detect_music_type(SDL_RWops *src);
     3.9  extern SDL_bool load_music_type(Mix_MusicType type);
    3.10  extern SDL_bool open_music_type(Mix_MusicType type);
    3.11  extern SDL_bool has_music(Mix_MusicType type);