mixer.c
changeset 777 92882ef2ab81
parent 771 fa081ec2940e
child 782 e7d3a8f73e88
     1.1 --- a/mixer.c	Mon Oct 16 02:16:47 2017 -0700
     1.2 +++ b/mixer.c	Tue Oct 17 02:33:47 2017 -0700
     1.3 @@ -29,17 +29,9 @@
     1.4  
     1.5  #include "SDL_mixer.h"
     1.6  #include "mixer.h"
     1.7 +#include "music.h"
     1.8  #include "load_aiff.h"
     1.9  #include "load_voc.h"
    1.10 -#include "load_mp3.h"
    1.11 -#include "load_ogg.h"
    1.12 -#include "load_flac.h"
    1.13 -#include "dynamic_flac.h"
    1.14 -#include "dynamic_fluidsynth.h"
    1.15 -#include "dynamic_modplug.h"
    1.16 -#include "dynamic_mod.h"
    1.17 -#include "dynamic_mp3.h"
    1.18 -#include "dynamic_ogg.h"
    1.19  
    1.20  #define __MIX_INTERNAL_EFFECT__
    1.21  #include "effects_internal.h"
    1.22 @@ -48,9 +40,7 @@
    1.23  #define RIFF        0x46464952      /* "RIFF" */
    1.24  #define WAVE        0x45564157      /* "WAVE" */
    1.25  #define FORM        0x4d524f46      /* "FORM" */
    1.26 -#define OGGS        0x5367674f      /* "OggS" */
    1.27  #define CREA        0x61657243      /* "Crea" */
    1.28 -#define FLAC        0x43614C66      /* "fLaC" */
    1.29  
    1.30  static int audio_opened = 0;
    1.31  static SDL_AudioSpec mixer;
    1.32 @@ -95,13 +85,7 @@
    1.33  /* rcg07062001 callback to alert when channels are done playing. */
    1.34  static void (*channel_done_callback)(int channel) = NULL;
    1.35  
    1.36 -/* Music function declarations */
    1.37 -extern int open_music(SDL_AudioSpec *mixer);
    1.38 -extern void close_music(void);
    1.39 -
    1.40 -/* Support for user defined music functions, plus the default one */
    1.41 -extern int volatile music_active;
    1.42 -extern void music_mixer(void *udata, Uint8 *stream, int len);
    1.43 +/* Support for user defined music functions */
    1.44  static void (*mix_music)(void *udata, Uint8 *stream, int len) = music_mixer;
    1.45  static void *music_data = NULL;
    1.46  
    1.47 @@ -109,8 +93,6 @@
    1.48  static const char **chunk_decoders = NULL;
    1.49  static int num_decoders = 0;
    1.50  
    1.51 -/* Semicolon-separated SoundFont paths */
    1.52 -extern char* soundfont_paths;
    1.53  
    1.54  int Mix_GetNumChunkDecoders(void)
    1.55  {
    1.56 @@ -125,6 +107,17 @@
    1.57      return(chunk_decoders[index]);
    1.58  }
    1.59  
    1.60 +SDL_bool Mix_HasChunkDecoder(const char *name)
    1.61 +{
    1.62 +    int index;
    1.63 +    for (index = 0; index < num_decoders; ++index) {
    1.64 +        if (SDL_strcasecmp(name, chunk_decoders[index]) == 0) {
    1.65 +            return SDL_TRUE;
    1.66 +        }
    1.67 +    }
    1.68 +    return SDL_FALSE;
    1.69 +}
    1.70 +
    1.71  static void add_chunk_decoder(const char *decoder)
    1.72  {
    1.73      void *ptr = SDL_realloc((void *)chunk_decoders, (num_decoders + 1) * sizeof (const char *));
    1.74 @@ -143,115 +136,64 @@
    1.75      return(&linked_version);
    1.76  }
    1.77  
    1.78 -static int initialized = 0;
    1.79 -
    1.80  int Mix_Init(int flags)
    1.81  {
    1.82      int result = 0;
    1.83  
    1.84  #ifdef MIX_INIT_SOUNDFONT_PATHS
    1.85 -    if (!soundfont_paths)
    1.86 +    if (!soundfont_paths) {
    1.87          soundfont_paths = SDL_strdup(MIX_INIT_SOUNDFONT_PATHS);
    1.88 +    }
    1.89  #endif
    1.90  
    1.91 -    if (flags & MIX_INIT_FLUIDSYNTH) {
    1.92 -#ifdef USE_FLUIDSYNTH_MIDI
    1.93 -        if ((initialized & MIX_INIT_FLUIDSYNTH) || Mix_InitFluidSynth() == 0) {
    1.94 -            result |= MIX_INIT_FLUIDSYNTH;
    1.95 +    load_music();
    1.96 +
    1.97 +    if (flags & MIX_INIT_FLAC) {
    1.98 +        if (has_music(MUS_FLAC)) {
    1.99 +            result |= MIX_INIT_FLAC;
   1.100 +        } else {
   1.101 +            Mix_SetError("FLAC support not available");
   1.102          }
   1.103 -#else
   1.104 -        Mix_SetError("Mixer not built with FluidSynth support");
   1.105 -#endif
   1.106 -    }
   1.107 -    if (flags & MIX_INIT_FLAC) {
   1.108 -#ifdef FLAC_MUSIC
   1.109 -        if ((initialized & MIX_INIT_FLAC) || Mix_InitFLAC() == 0) {
   1.110 -            result |= MIX_INIT_FLAC;
   1.111 -        }
   1.112 -#else
   1.113 -        Mix_SetError("Mixer not built with FLAC support");
   1.114 -#endif
   1.115 -    }
   1.116 -    if (flags & MIX_INIT_MODPLUG) {
   1.117 -#ifdef MODPLUG_MUSIC
   1.118 -        if ((initialized & MIX_INIT_MODPLUG) || Mix_InitModPlug() == 0) {
   1.119 -            result |= MIX_INIT_MODPLUG;
   1.120 -        }
   1.121 -#else
   1.122 -        Mix_SetError("Mixer not built with MOD modplug support");
   1.123 -#endif
   1.124      }
   1.125      if (flags & MIX_INIT_MOD) {
   1.126 -#ifdef MOD_MUSIC
   1.127 -        if ((initialized & MIX_INIT_MOD) || Mix_InitMOD() == 0) {
   1.128 +        if (has_music(MUS_MOD)) {
   1.129              result |= MIX_INIT_MOD;
   1.130 +        } else {
   1.131 +            Mix_SetError("MOD support not available");
   1.132          }
   1.133 -#else
   1.134 -        Mix_SetError("Mixer not built with MOD mikmod support");
   1.135 -#endif
   1.136      }
   1.137      if (flags & MIX_INIT_MP3) {
   1.138 -#if defined(MP3_MUSIC) || defined(MP3_MPG_MUSIC)
   1.139 -        if ((initialized & MIX_INIT_MP3) || Mix_InitMP3() == 0) {
   1.140 +        if (has_music(MUS_MP3)) {
   1.141              result |= MIX_INIT_MP3;
   1.142 +        } else {
   1.143 +            Mix_SetError("MP3 support not available");
   1.144          }
   1.145 -#elif defined(MP3_MAD_MUSIC)
   1.146 -        result |= MIX_INIT_MP3;
   1.147 -#else
   1.148 -        Mix_SetError("Mixer not built with MP3 support");
   1.149 -#endif
   1.150      }
   1.151      if (flags & MIX_INIT_OGG) {
   1.152 -#ifdef OGG_MUSIC
   1.153 -        if ((initialized & MIX_INIT_OGG) || Mix_InitOgg() == 0) {
   1.154 +        if (has_music(MUS_OGG)) {
   1.155              result |= MIX_INIT_OGG;
   1.156 +        } else {
   1.157 +            Mix_SetError("OGG support not available");
   1.158          }
   1.159 -#else
   1.160 -        Mix_SetError("Mixer not built with Ogg Vorbis support");
   1.161 -#endif
   1.162      }
   1.163 -    initialized |= result;
   1.164 -
   1.165 +    if (flags & MIX_INIT_MID) {
   1.166 +        if (has_music(MUS_MID)) {
   1.167 +            result |= MIX_INIT_MID;
   1.168 +        } else {
   1.169 +            Mix_SetError("MIDI support not available");
   1.170 +        }
   1.171 +    }
   1.172      return (result);
   1.173  }
   1.174  
   1.175  void Mix_Quit()
   1.176  {
   1.177 -#ifdef USE_FLUIDSYNTH_MIDI
   1.178 -    if (initialized & MIX_INIT_FLUIDSYNTH) {
   1.179 -        Mix_QuitFluidSynth();
   1.180 -    }
   1.181 -#endif
   1.182 -#ifdef FLAC_MUSIC
   1.183 -    if (initialized & MIX_INIT_FLAC) {
   1.184 -        Mix_QuitFLAC();
   1.185 -    }
   1.186 -#endif
   1.187 -#ifdef MODPLUG_MUSIC
   1.188 -    if (initialized & MIX_INIT_MODPLUG) {
   1.189 -        Mix_QuitModPlug();
   1.190 -    }
   1.191 -#endif
   1.192 -#ifdef MOD_MUSIC
   1.193 -    if (initialized & MIX_INIT_MOD) {
   1.194 -        Mix_QuitMOD();
   1.195 -    }
   1.196 -#endif
   1.197 -#if defined(MP3_MUSIC) || defined(MP3_MPG_MUSIC)
   1.198 -    if (initialized & MIX_INIT_MP3) {
   1.199 -        Mix_QuitMP3();
   1.200 -    }
   1.201 -#endif
   1.202 -#ifdef OGG_MUSIC
   1.203 -    if (initialized & MIX_INIT_OGG) {
   1.204 -        Mix_QuitOgg();
   1.205 -    }
   1.206 -#endif
   1.207 +    unload_music();
   1.208 +
   1.209      if (soundfont_paths) {
   1.210          SDL_free(soundfont_paths);
   1.211          soundfont_paths = NULL;
   1.212      }
   1.213 -    initialized = 0;
   1.214  }
   1.215  
   1.216  static int _Mix_remove_all_effects(int channel, effect_info **e);
   1.217 @@ -308,7 +250,7 @@
   1.218  mix_channels(void *udata, Uint8 *stream, int len)
   1.219  {
   1.220      Uint8 *mix_input;
   1.221 -    int i, mixable, volume = SDL_MIX_MAXVOLUME;
   1.222 +    int i, mixable, volume = MIX_MAX_VOLUME;
   1.223      Uint32 sdl_ticks;
   1.224  
   1.225  #if SDL_VERSION_ATLEAST(1, 3, 0)
   1.226 @@ -317,26 +259,26 @@
   1.227  #endif
   1.228  
   1.229      /* Mix the music (must be done before the channels are added) */
   1.230 -    if ( music_active || (mix_music != music_mixer) ) {
   1.231 +    if (music_active || (mix_music != music_mixer)) {
   1.232          mix_music(music_data, stream, len);
   1.233      }
   1.234  
   1.235      /* Mix any playing channels... */
   1.236      sdl_ticks = SDL_GetTicks();
   1.237 -    for ( i=0; i<num_channels; ++i ) {
   1.238 -        if ( !mix_channel[i].paused ) {
   1.239 -            if ( mix_channel[i].expire > 0 && mix_channel[i].expire < sdl_ticks ) {
   1.240 +    for (i=0; i<num_channels; ++i) {
   1.241 +        if (!mix_channel[i].paused) {
   1.242 +            if (mix_channel[i].expire > 0 && mix_channel[i].expire < sdl_ticks) {
   1.243                  /* Expiration delay for that channel is reached */
   1.244                  mix_channel[i].playing = 0;
   1.245                  mix_channel[i].looping = 0;
   1.246                  mix_channel[i].fading = MIX_NO_FADING;
   1.247                  mix_channel[i].expire = 0;
   1.248                  _Mix_channel_done_playing(i);
   1.249 -            } else if ( mix_channel[i].fading != MIX_NO_FADING ) {
   1.250 +            } else if (mix_channel[i].fading != MIX_NO_FADING) {
   1.251                  Uint32 ticks = sdl_ticks - mix_channel[i].ticks_fade;
   1.252 -                if ( ticks >= mix_channel[i].fade_length ) {
   1.253 +                if (ticks >= mix_channel[i].fade_length) {
   1.254                      Mix_Volume(i, mix_channel[i].fade_volume_reset); /* Restore the volume */
   1.255 -                    if( mix_channel[i].fading == MIX_FADING_OUT ) {
   1.256 +                    if(mix_channel[i].fading == MIX_FADING_OUT) {
   1.257                          mix_channel[i].playing = 0;
   1.258                          mix_channel[i].looping = 0;
   1.259                          mix_channel[i].expire = 0;
   1.260 @@ -344,22 +286,22 @@
   1.261                      }
   1.262                      mix_channel[i].fading = MIX_NO_FADING;
   1.263                  } else {
   1.264 -                    if ( mix_channel[i].fading == MIX_FADING_OUT ) {
   1.265 +                    if (mix_channel[i].fading == MIX_FADING_OUT) {
   1.266                          Mix_Volume(i, (mix_channel[i].fade_volume * (mix_channel[i].fade_length-ticks))
   1.267 -                                   / mix_channel[i].fade_length );
   1.268 +                                   / mix_channel[i].fade_length);
   1.269                      } else {
   1.270 -                        Mix_Volume(i, (mix_channel[i].fade_volume * ticks) / mix_channel[i].fade_length );
   1.271 +                        Mix_Volume(i, (mix_channel[i].fade_volume * ticks) / mix_channel[i].fade_length);
   1.272                      }
   1.273                  }
   1.274              }
   1.275 -            if ( mix_channel[i].playing > 0 ) {
   1.276 +            if (mix_channel[i].playing > 0) {
   1.277                  int index = 0;
   1.278                  int remaining = len;
   1.279                  while (mix_channel[i].playing > 0 && index < len) {
   1.280                      remaining = len - index;
   1.281                      volume = (mix_channel[i].volume*mix_channel[i].chunk->volume) / MIX_MAX_VOLUME;
   1.282                      mixable = mix_channel[i].playing;
   1.283 -                    if ( mixable > remaining ) {
   1.284 +                    if (mixable > remaining) {
   1.285                          mixable = remaining;
   1.286                      }
   1.287  
   1.288 @@ -380,7 +322,7 @@
   1.289  
   1.290                  /* If looping the sample and we are at its end, make sure
   1.291                     we will still return a full buffer */
   1.292 -                while ( mix_channel[i].looping && index < len ) {
   1.293 +                while (mix_channel[i].looping && index < len) {
   1.294                      int alen = mix_channel[i].chunk->alen;
   1.295                      remaining = len - index;
   1.296                      if (remaining > alen) {
   1.297 @@ -399,7 +341,7 @@
   1.298                      mix_channel[i].playing = mix_channel[i].chunk->alen - remaining;
   1.299                      index += remaining;
   1.300                  }
   1.301 -                if ( ! mix_channel[i].playing && mix_channel[i].looping ) {
   1.302 +                if (! mix_channel[i].playing && mix_channel[i].looping) {
   1.303                      if (mix_channel[i].looping > 0) {
   1.304                          --mix_channel[i].looping;
   1.305                      }
   1.306 @@ -413,7 +355,7 @@
   1.307      /* rcg06122001 run posteffects... */
   1.308      Mix_DoEffects(MIX_CHANNEL_POST, stream, len);
   1.309  
   1.310 -    if ( mix_postmix ) {
   1.311 +    if (mix_postmix) {
   1.312          mix_postmix(mix_postmix_data, stream, len);
   1.313      }
   1.314  }
   1.315 @@ -430,7 +372,7 @@
   1.316  
   1.317  /* Open the mixer with a certain desired audio format */
   1.318  int Mix_OpenAudioDevice(int frequency, Uint16 format, int nchannels, int chunksize,
   1.319 -			const char* device, int allowed_changes)
   1.320 +                        const char* device, int allowed_changes)
   1.321  {
   1.322      int i;
   1.323      SDL_AudioSpec desired;
   1.324 @@ -445,12 +387,12 @@
   1.325      }
   1.326  
   1.327      /* If the mixer is already opened, increment open count */
   1.328 -    if ( audio_opened ) {
   1.329 -        if ( format == mixer.format && nchannels == mixer.channels ) {
   1.330 +    if (audio_opened) {
   1.331 +        if (format == mixer.format && nchannels == mixer.channels) {
   1.332              ++audio_opened;
   1.333              return(0);
   1.334          }
   1.335 -        while ( audio_opened ) {
   1.336 +        while (audio_opened) {
   1.337              Mix_CloseAudio();
   1.338          }
   1.339      }
   1.340 @@ -464,7 +406,7 @@
   1.341      desired.userdata = NULL;
   1.342  
   1.343      /* Accept nearly any audio format */
   1.344 -    if ( (audio_device = SDL_OpenAudioDevice(device, 0, &desired, &mixer, allowed_changes)) == 0 ) {
   1.345 +    if ((audio_device = SDL_OpenAudioDevice(device, 0, &desired, &mixer, allowed_changes)) == 0) {
   1.346          return(-1);
   1.347      }
   1.348  #if 0
   1.349 @@ -472,7 +414,8 @@
   1.350  #endif
   1.351  
   1.352      /* Initialize the music players */
   1.353 -    if ( open_music(&mixer) < 0 ) {
   1.354 +    load_music();
   1.355 +    if (open_music(&mixer) < 0) {
   1.356          SDL_CloseAudioDevice(audio_device);
   1.357          return(-1);
   1.358      }
   1.359 @@ -481,7 +424,7 @@
   1.360      mix_channel = (struct _Mix_Channel *) SDL_malloc(num_channels * sizeof(struct _Mix_Channel));
   1.361  
   1.362      /* Clear out the audio channels */
   1.363 -    for ( i=0; i<num_channels; ++i ) {
   1.364 +    for (i=0; i<num_channels; ++i) {
   1.365          mix_channel[i].chunk = NULL;
   1.366          mix_channel[i].playing = 0;
   1.367          mix_channel[i].looping = 0;
   1.368 @@ -498,19 +441,24 @@
   1.369  
   1.370      _Mix_InitEffects();
   1.371  
   1.372 -    /* This list is (currently) decided at build time. */
   1.373      add_chunk_decoder("WAVE");
   1.374      add_chunk_decoder("AIFF");
   1.375      add_chunk_decoder("VOC");
   1.376 -#ifdef OGG_MUSIC
   1.377 -    add_chunk_decoder("OGG");
   1.378 -#endif
   1.379 -#ifdef FLAC_MUSIC
   1.380 -    add_chunk_decoder("FLAC");
   1.381 -#endif
   1.382 -#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC) || defined(MP3_MPG_MUSIC)
   1.383 -    add_chunk_decoder("MP3");
   1.384 -#endif
   1.385 +    if (has_music(MUS_MOD)) {
   1.386 +        add_chunk_decoder("MOD");
   1.387 +    }
   1.388 +    if (has_music(MUS_MID)) {
   1.389 +        add_chunk_decoder("MID");
   1.390 +    }
   1.391 +    if (has_music(MUS_OGG)) {
   1.392 +        add_chunk_decoder("OGG");
   1.393 +    }
   1.394 +    if (has_music(MUS_MP3)) {
   1.395 +        add_chunk_decoder("MP3");
   1.396 +    }
   1.397 +    if (has_music(MUS_FLAC)) {
   1.398 +        add_chunk_decoder("FLAC");
   1.399 +    }
   1.400  
   1.401      audio_opened = 1;
   1.402      SDL_PauseAudioDevice(audio_device, 0);
   1.403 @@ -531,10 +479,10 @@
   1.404   */
   1.405  int Mix_AllocateChannels(int numchans)
   1.406  {
   1.407 -    if ( numchans<0 || numchans==num_channels )
   1.408 +    if (numchans<0 || numchans==num_channels)
   1.409          return(num_channels);
   1.410  
   1.411 -    if ( numchans < num_channels ) {
   1.412 +    if (numchans < num_channels) {
   1.413          /* Stop the affected channels */
   1.414          int i;
   1.415          for(i=numchans; i < num_channels; i++) {
   1.416 @@ -544,16 +492,16 @@
   1.417      }
   1.418      Mix_LockAudio();
   1.419      mix_channel = (struct _Mix_Channel *) SDL_realloc(mix_channel, numchans * sizeof(struct _Mix_Channel));
   1.420 -    if ( numchans > num_channels ) {
   1.421 +    if (numchans > num_channels) {
   1.422          /* Initialize the new channels */
   1.423          int i;
   1.424          for(i=num_channels; i < numchans; i++) {
   1.425              mix_channel[i].chunk = NULL;
   1.426              mix_channel[i].playing = 0;
   1.427              mix_channel[i].looping = 0;
   1.428 -            mix_channel[i].volume = SDL_MIX_MAXVOLUME;
   1.429 -            mix_channel[i].fade_volume = SDL_MIX_MAXVOLUME;
   1.430 -            mix_channel[i].fade_volume_reset = SDL_MIX_MAXVOLUME;
   1.431 +            mix_channel[i].volume = MIX_MAX_VOLUME;
   1.432 +            mix_channel[i].fade_volume = MIX_MAX_VOLUME;
   1.433 +            mix_channel[i].fade_volume_reset = MIX_MAX_VOLUME;
   1.434              mix_channel[i].fading = MIX_NO_FADING;
   1.435              mix_channel[i].tag = -1;
   1.436              mix_channel[i].expire = 0;
   1.437 @@ -569,57 +517,197 @@
   1.438  /* Return the actual mixer parameters */
   1.439  int Mix_QuerySpec(int *frequency, Uint16 *format, int *channels)
   1.440  {
   1.441 -    if ( audio_opened ) {
   1.442 -        if ( frequency ) {
   1.443 +    if (audio_opened) {
   1.444 +        if (frequency) {
   1.445              *frequency = mixer.freq;
   1.446          }
   1.447 -        if ( format ) {
   1.448 +        if (format) {
   1.449              *format = mixer.format;
   1.450          }
   1.451 -        if ( channels ) {
   1.452 +        if (channels) {
   1.453              *channels = mixer.channels;
   1.454          }
   1.455      }
   1.456      return(audio_opened);
   1.457  }
   1.458  
   1.459 -static int detect_mp3(Uint8 *magic)
   1.460 +typedef struct _MusicFragment
   1.461  {
   1.462 -    if ( strncmp((char *)magic, "ID3", 3) == 0 ) {
   1.463 -        return 1;
   1.464 +    Uint8 *data;
   1.465 +    int size;
   1.466 +    struct _MusicFragment *next;
   1.467 +} MusicFragment;
   1.468 +
   1.469 +static SDL_AudioSpec *Mix_LoadMusic_RW(Mix_MusicType music_type, SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
   1.470 +{
   1.471 +    int i;
   1.472 +    Mix_MusicInterface *interface = NULL;
   1.473 +    void *music = NULL;
   1.474 +    Sint64 start;
   1.475 +    SDL_bool playing;
   1.476 +    MusicFragment *first = NULL, *last = NULL, *fragment = NULL;
   1.477 +    int count = 0;
   1.478 +    int fragment_size;
   1.479 +    int original_volume;
   1.480 +
   1.481 +    *spec = mixer;
   1.482 +
   1.483 +    /* Use fragments sized on full audio frame boundaries - this'll do */
   1.484 +    fragment_size = spec->size;
   1.485 +
   1.486 +    start = SDL_RWtell(src);
   1.487 +    for (i = 0; i < get_num_music_interfaces(); ++i) {
   1.488 +        interface = get_music_interface(i);
   1.489 +        if (interface->type != music_type) {
   1.490 +            continue;
   1.491 +        }
   1.492 +        if (!interface->CreateFromRW || !interface->GetAudio) {
   1.493 +            continue;
   1.494 +        }
   1.495 +
   1.496 +        /* These music interfaces are not safe to use while music is playing */
   1.497 +        if (interface->api == MIX_MUSIC_CMD ||
   1.498 +             interface->api == MIX_MUSIC_MIKMOD ||
   1.499 +             interface->api == MIX_MUSIC_TIMIDITY ||
   1.500 +             interface->api == MIX_MUSIC_NATIVEMIDI) {
   1.501 +            continue;
   1.502 +        }
   1.503 +
   1.504 +        music = interface->CreateFromRW(src, freesrc);
   1.505 +        if (music) {
   1.506 +            /* The interface owns the data source now */
   1.507 +            freesrc = SDL_FALSE;
   1.508 +            break;
   1.509 +        }
   1.510 +
   1.511 +        if (interface->api == MIX_MUSIC_SMPEG) {
   1.512 +            /* Uh oh, if SMPEG couldn't create anything, it freed the src */
   1.513 +            freesrc = SDL_FALSE;
   1.514 +            break;
   1.515 +        }
   1.516 +
   1.517 +        /* Reset the stream for the next decoder */
   1.518 +        SDL_RWseek(src, start, RW_SEEK_SET);
   1.519      }
   1.520  
   1.521 -    /* Detection code lifted from SMPEG */
   1.522 -    if(((magic[0] & 0xff) != 0xff) || // No sync bits
   1.523 -       ((magic[1] & 0xf0) != 0xf0) || //
   1.524 -       ((magic[2] & 0xf0) == 0x00) || // Bitrate is 0
   1.525 -       ((magic[2] & 0xf0) == 0xf0) || // Bitrate is 15
   1.526 -       ((magic[2] & 0x0c) == 0x0c) || // Frequency is 3
   1.527 -       ((magic[1] & 0x06) == 0x00)) { // Layer is 4
   1.528 -        return(0);
   1.529 +    if (!music) {
   1.530 +        if (freesrc) {
   1.531 +            SDL_RWclose(src);
   1.532 +        }
   1.533 +        Mix_SetError("Unrecognized audio format");
   1.534 +        return NULL;
   1.535      }
   1.536 -    return 1;
   1.537 +
   1.538 +    Mix_LockAudio();
   1.539 +    original_volume = music_volume;
   1.540 + 
   1.541 +    if (interface->SetVolume) {
   1.542 +        interface->SetVolume(music, MIX_MAX_VOLUME);
   1.543 +    }
   1.544 +
   1.545 +    if (interface->Play) {
   1.546 +        interface->Play(music);
   1.547 +	}
   1.548 +    playing = SDL_TRUE;
   1.549 +
   1.550 +    while (playing) {
   1.551 +        int left;
   1.552 +
   1.553 +        fragment = (MusicFragment *)SDL_malloc(sizeof(*fragment));
   1.554 +        if (!fragment) {
   1.555 +            /* Uh oh, out of memory, let's return what we have */
   1.556 +            break;
   1.557 +        }
   1.558 +        fragment->data = (Uint8 *)SDL_malloc(fragment_size);
   1.559 +        if (!fragment->data) {
   1.560 +            /* Uh oh, out of memory, let's return what we have */
   1.561 +            SDL_free(fragment);
   1.562 +            break;
   1.563 +        }
   1.564 +        fragment->next = NULL;
   1.565 +
   1.566 +        left = interface->GetAudio(music, fragment->data, fragment_size);
   1.567 +        if (left > 0) {
   1.568 +            playing = SDL_FALSE;
   1.569 +        } else if (interface->IsPlaying) {
   1.570 +            playing = interface->IsPlaying(music);
   1.571 +        }
   1.572 +        fragment->size = (fragment_size - left);
   1.573 +
   1.574 +        if (!first) {
   1.575 +            first = fragment;
   1.576 +        }
   1.577 +        if (last) {
   1.578 +            last->next = fragment;
   1.579 +        }
   1.580 +        last = fragment;
   1.581 +        ++count;
   1.582 +    }
   1.583 +
   1.584 +    if (interface->Stop) {
   1.585 +        interface->Stop(music);
   1.586 +    }
   1.587 +
   1.588 +    if (interface->SetVolume) {
   1.589 +        interface->SetVolume(music, original_volume);
   1.590 +    }
   1.591 +
   1.592 +    if (music) {
   1.593 +        interface->Delete(music);
   1.594 +    }
   1.595 +
   1.596 +    Mix_UnlockAudio();
   1.597 +
   1.598 +    if (count > 0) {
   1.599 +        *audio_len = (count - 1) * fragment_size + fragment->size;
   1.600 +        *audio_buf = (Uint8 *)SDL_malloc(*audio_len);
   1.601 +        if (*audio_buf) {
   1.602 +            Uint8 *dst = *audio_buf;
   1.603 +            for (fragment = first; fragment; fragment = fragment->next) {
   1.604 +                SDL_memcpy(dst, fragment->data, fragment->size);
   1.605 +                dst += fragment->size;
   1.606 +            }
   1.607 +        } else {
   1.608 +            SDL_OutOfMemory();
   1.609 +            spec = NULL;
   1.610 +        }
   1.611 +    } else {
   1.612 +        Mix_SetError("No audio data");
   1.613 +        spec = NULL;
   1.614 +    }
   1.615 +
   1.616 +    while (first) {
   1.617 +        fragment = first;
   1.618 +        first = first->next;
   1.619 +        SDL_free(fragment->data);
   1.620 +        SDL_free(fragment);
   1.621 +    }
   1.622 +
   1.623 +    if (freesrc) {
   1.624 +        SDL_RWclose(src);
   1.625 +    }
   1.626 +    return spec;
   1.627  }
   1.628  
   1.629  /* Load a wave file */
   1.630  Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
   1.631  {
   1.632 -    Uint32 magic;
   1.633 +    Uint8 magic[4];
   1.634      Mix_Chunk *chunk;
   1.635      SDL_AudioSpec wavespec, *loaded;
   1.636      SDL_AudioCVT wavecvt;
   1.637      int samplesize;
   1.638  
   1.639      /* rcg06012001 Make sure src is valid */
   1.640 -    if ( ! src ) {
   1.641 +    if (!src) {
   1.642          SDL_SetError("Mix_LoadWAV_RW with NULL src");
   1.643          return(NULL);
   1.644      }
   1.645  
   1.646      /* Make sure audio has been opened */
   1.647 -    if ( ! audio_opened ) {
   1.648 +    if (!audio_opened) {
   1.649          SDL_SetError("Audio device hasn't been opened");
   1.650 -        if ( freesrc ) {
   1.651 +        if (freesrc) {
   1.652              SDL_RWclose(src);
   1.653          }
   1.654          return(NULL);
   1.655 @@ -627,64 +715,36 @@
   1.656  
   1.657      /* Allocate the chunk memory */
   1.658      chunk = (Mix_Chunk *)SDL_malloc(sizeof(Mix_Chunk));
   1.659 -    if ( chunk == NULL ) {
   1.660 +    if (chunk == NULL) {
   1.661          SDL_SetError("Out of memory");
   1.662 -        if ( freesrc ) {
   1.663 +        if (freesrc) {
   1.664              SDL_RWclose(src);
   1.665          }
   1.666          return(NULL);
   1.667      }
   1.668  
   1.669      /* Find out what kind of audio file this is */
   1.670 -    magic = SDL_ReadLE32(src);
   1.671 +    if (SDL_RWread(src, magic, 1, 4) != 4) {
   1.672 +        if (freesrc) {
   1.673 +            SDL_RWclose(src);
   1.674 +        }
   1.675 +        Mix_SetError("Couldn't read first 4 bytes of audio data");
   1.676 +        return NULL;
   1.677 +    }
   1.678      /* Seek backwards for compatibility with older loaders */
   1.679 -    SDL_RWseek(src, -(int)sizeof(Uint32), RW_SEEK_CUR);
   1.680 +    SDL_RWseek(src, -4, RW_SEEK_CUR);
   1.681  
   1.682 -    switch (magic) {
   1.683 -        case WAVE:
   1.684 -        case RIFF:
   1.685 -            loaded = SDL_LoadWAV_RW(src, freesrc, &wavespec,
   1.686 -                    (Uint8 **)&chunk->abuf, &chunk->alen);
   1.687 -            break;
   1.688 -        case FORM:
   1.689 -            loaded = Mix_LoadAIFF_RW(src, freesrc, &wavespec,
   1.690 -                    (Uint8 **)&chunk->abuf, &chunk->alen);
   1.691 -            break;
   1.692 -#ifdef OGG_MUSIC
   1.693 -        case OGGS:
   1.694 -            loaded = Mix_LoadOGG_RW(src, freesrc, &wavespec,
   1.695 -                    (Uint8 **)&chunk->abuf, &chunk->alen);
   1.696 -            break;
   1.697 -#endif
   1.698 -#ifdef FLAC_MUSIC
   1.699 -        case FLAC:
   1.700 -            loaded = Mix_LoadFLAC_RW(src, freesrc, &wavespec,
   1.701 -                    (Uint8 **)&chunk->abuf, &chunk->alen);
   1.702 -            break;
   1.703 -#endif
   1.704 -        case CREA:
   1.705 -            loaded = Mix_LoadVOC_RW(src, freesrc, &wavespec,
   1.706 -                    (Uint8 **)&chunk->abuf, &chunk->alen);
   1.707 -            break;
   1.708 -        default:
   1.709 -#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC) || defined(MP3_MPG_MUSIC)
   1.710 -			if (detect_mp3((Uint8*)&magic))
   1.711 -			{
   1.712 -				/* note: send a copy of the mixer spec */
   1.713 -				wavespec = mixer;
   1.714 -				loaded = Mix_LoadMP3_RW(src, freesrc, &wavespec,
   1.715 -						(Uint8 **)&chunk->abuf, &chunk->alen);
   1.716 -				break;
   1.717 -			}
   1.718 -#endif
   1.719 -            SDL_SetError("Unrecognized sound file type");
   1.720 -            if ( freesrc ) {
   1.721 -                SDL_RWclose(src);
   1.722 -            }
   1.723 -            loaded = NULL;
   1.724 -            break;
   1.725 +    if (SDL_memcmp(magic, "WAVE", 4) == 0 || SDL_memcmp(magic, "RIFF", 4) == 0) {
   1.726 +        loaded = SDL_LoadWAV_RW(src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
   1.727 +    } else if (SDL_memcmp(magic, "FORM", 4) == 0) {
   1.728 +        loaded = Mix_LoadAIFF_RW(src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
   1.729 +    } else if (SDL_memcmp(magic, "CREA", 4) == 0) {
   1.730 +        loaded = Mix_LoadVOC_RW(src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
   1.731 +    } else {
   1.732 +        Mix_MusicType music_type = detect_music_type_from_magic(magic);
   1.733 +        loaded = Mix_LoadMusic_RW(music_type, src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
   1.734      }
   1.735 -    if ( !loaded ) {
   1.736 +    if (!loaded) {
   1.737          /* The individual loaders have closed src if needed */
   1.738          SDL_free(chunk);
   1.739          return(NULL);
   1.740 @@ -696,12 +756,12 @@
   1.741  #endif
   1.742  
   1.743      /* Build the audio converter and create conversion buffers */
   1.744 -    if ( wavespec.format != mixer.format ||
   1.745 +    if (wavespec.format != mixer.format ||
   1.746           wavespec.channels != mixer.channels ||
   1.747 -         wavespec.freq != mixer.freq ) {
   1.748 -        if ( SDL_BuildAudioCVT(&wavecvt,
   1.749 +         wavespec.freq != mixer.freq) {
   1.750 +        if (SDL_BuildAudioCVT(&wavecvt,
   1.751                  wavespec.format, wavespec.channels, wavespec.freq,
   1.752 -                mixer.format, mixer.channels, mixer.freq) < 0 ) {
   1.753 +                mixer.format, mixer.channels, mixer.freq) < 0) {
   1.754              SDL_free(chunk->abuf);
   1.755              SDL_free(chunk);
   1.756              return(NULL);
   1.757 @@ -709,7 +769,7 @@
   1.758          samplesize = ((wavespec.format & 0xFF)/8)*wavespec.channels;
   1.759          wavecvt.len = chunk->alen & ~(samplesize-1);
   1.760          wavecvt.buf = (Uint8 *)SDL_calloc(1, wavecvt.len*wavecvt.len_mult);
   1.761 -        if ( wavecvt.buf == NULL ) {
   1.762 +        if (wavecvt.buf == NULL) {
   1.763              SDL_SetError("Out of memory");
   1.764              SDL_free(chunk->abuf);
   1.765              SDL_free(chunk);
   1.766 @@ -719,7 +779,7 @@
   1.767          SDL_free(chunk->abuf);
   1.768  
   1.769          /* Run the audio converter */
   1.770 -        if ( SDL_ConvertAudio(&wavecvt) < 0 ) {
   1.771 +        if (SDL_ConvertAudio(&wavecvt) < 0) {
   1.772              SDL_free(wavecvt.buf);
   1.773              SDL_free(chunk);
   1.774              return(NULL);
   1.775 @@ -742,14 +802,14 @@
   1.776      Uint8 magic[4];
   1.777  
   1.778      /* Make sure audio has been opened */
   1.779 -    if ( ! audio_opened ) {
   1.780 +    if (! audio_opened) {
   1.781          SDL_SetError("Audio device hasn't been opened");
   1.782          return(NULL);
   1.783      }
   1.784  
   1.785      /* Allocate the chunk memory */
   1.786      chunk = (Mix_Chunk *)SDL_calloc(1,sizeof(Mix_Chunk));
   1.787 -    if ( chunk == NULL ) {
   1.788 +    if (chunk == NULL) {
   1.789          SDL_SetError("Out of memory");
   1.790          return(NULL);
   1.791      }
   1.792 @@ -764,7 +824,7 @@
   1.793          mem += 4;
   1.794          chunk->abuf = mem;
   1.795          mem += chunk->alen;
   1.796 -    } while ( memcmp(magic, "data", 4) != 0 );
   1.797 +    } while (memcmp(magic, "data", 4) != 0);
   1.798      chunk->volume = MIX_MAX_VOLUME;
   1.799  
   1.800      return(chunk);
   1.801 @@ -776,14 +836,14 @@
   1.802      Mix_Chunk *chunk;
   1.803  
   1.804      /* Make sure audio has been opened */
   1.805 -    if ( ! audio_opened ) {
   1.806 +    if (! audio_opened) {
   1.807          SDL_SetError("Audio device hasn't been opened");
   1.808          return(NULL);
   1.809      }
   1.810  
   1.811      /* Allocate the chunk memory */
   1.812      chunk = (Mix_Chunk *)SDL_malloc(sizeof(Mix_Chunk));
   1.813 -    if ( chunk == NULL ) {
   1.814 +    if (chunk == NULL) {
   1.815          SDL_SetError("Out of memory");
   1.816          return(NULL);
   1.817      }
   1.818 @@ -803,12 +863,12 @@
   1.819      int i;
   1.820  
   1.821      /* Caution -- if the chunk is playing, the mixer will crash */
   1.822 -    if ( chunk ) {
   1.823 +    if (chunk) {
   1.824          /* Guarantee that this chunk isn't playing */
   1.825          Mix_LockAudio();
   1.826 -        if ( mix_channel ) {
   1.827 -            for ( i=0; i<num_channels; ++i ) {
   1.828 -                if ( chunk == mix_channel[i].chunk ) {
   1.829 +        if (mix_channel) {
   1.830 +            for (i=0; i<num_channels; ++i) {
   1.831 +                if (chunk == mix_channel[i].chunk) {
   1.832                      mix_channel[i].playing = 0;
   1.833                      mix_channel[i].looping = 0;
   1.834                  }
   1.835 @@ -816,7 +876,7 @@
   1.836          }
   1.837          Mix_UnlockAudio();
   1.838          /* Actually free the chunk */
   1.839 -        if ( chunk->allocated ) {
   1.840 +        if (chunk->allocated) {
   1.841              SDL_free(chunk->abuf);
   1.842          }
   1.843          SDL_free(chunk);
   1.844 @@ -843,7 +903,7 @@
   1.845                                                                  void *arg)
   1.846  {
   1.847      Mix_LockAudio();
   1.848 -    if ( mix_func != NULL ) {
   1.849 +    if (mix_func != NULL) {
   1.850          music_data = arg;
   1.851          mix_music = mix_func;
   1.852      } else {
   1.853 @@ -899,11 +959,11 @@
   1.854      int i;
   1.855  
   1.856      /* Don't play null pointers :-) */
   1.857 -    if ( chunk == NULL ) {
   1.858 +    if (chunk == NULL) {
   1.859          Mix_SetError("Tried to play a NULL chunk");
   1.860          return(-1);
   1.861      }
   1.862 -    if ( !checkchunkintegral(chunk)) {
   1.863 +    if (!checkchunkintegral(chunk)) {
   1.864          Mix_SetError("Tried to play a chunk with a bad frame");
   1.865          return(-1);
   1.866      }
   1.867 @@ -912,12 +972,12 @@
   1.868      Mix_LockAudio();
   1.869      {
   1.870          /* If which is -1, play on the first free channel */
   1.871 -        if ( which == -1 ) {
   1.872 -            for ( i=reserved_channels; i<num_channels; ++i ) {
   1.873 -                if ( mix_channel[i].playing <= 0 )
   1.874 +        if (which == -1) {
   1.875 +            for (i=reserved_channels; i<num_channels; ++i) {
   1.876 +                if (mix_channel[i].playing <= 0)
   1.877                      break;
   1.878              }
   1.879 -            if ( i == num_channels ) {
   1.880 +            if (i == num_channels) {
   1.881                  Mix_SetError("No free channels available");
   1.882                  which = -1;
   1.883              } else {
   1.884 @@ -926,7 +986,7 @@
   1.885          }
   1.886  
   1.887          /* Queue up the audio data for this channel */
   1.888 -        if ( which >= 0 && which < num_channels ) {
   1.889 +        if (which >= 0 && which < num_channels) {
   1.890              Uint32 sdl_ticks = SDL_GetTicks();
   1.891              if (Mix_Playing(which))
   1.892                  _Mix_channel_done_playing(which);
   1.893 @@ -951,12 +1011,12 @@
   1.894  {
   1.895      int status = 0;
   1.896  
   1.897 -    if ( which == -1 ) {
   1.898 +    if (which == -1) {
   1.899          int i;
   1.900 -        for ( i=0; i < num_channels; ++ i ) {
   1.901 +        for (i=0; i < num_channels; ++ i) {
   1.902              status += Mix_ExpireChannel(i, ticks);
   1.903          }
   1.904 -    } else if ( which < num_channels ) {
   1.905 +    } else if (which < num_channels) {
   1.906          Mix_LockAudio();
   1.907          mix_channel[which].expire = (ticks>0) ? (SDL_GetTicks() + ticks) : 0;
   1.908          Mix_UnlockAudio();
   1.909 @@ -971,10 +1031,10 @@
   1.910      int i;
   1.911  
   1.912      /* Don't play null pointers :-) */
   1.913 -    if ( chunk == NULL ) {
   1.914 +    if (chunk == NULL) {
   1.915          return(-1);
   1.916      }
   1.917 -    if ( !checkchunkintegral(chunk)) {
   1.918 +    if (!checkchunkintegral(chunk)) {
   1.919          Mix_SetError("Tried to play a chunk with a bad frame");
   1.920          return(-1);
   1.921      }
   1.922 @@ -983,12 +1043,12 @@
   1.923      Mix_LockAudio();
   1.924      {
   1.925          /* If which is -1, play on the first free channel */
   1.926 -        if ( which == -1 ) {
   1.927 -            for ( i=reserved_channels; i<num_channels; ++i ) {
   1.928 -                if ( mix_channel[i].playing <= 0 )
   1.929 +        if (which == -1) {
   1.930 +            for (i=reserved_channels; i<num_channels; ++i) {
   1.931 +                if (mix_channel[i].playing <= 0)
   1.932                      break;
   1.933              }
   1.934 -            if ( i == num_channels ) {
   1.935 +            if (i == num_channels) {
   1.936                  which = -1;
   1.937              } else {
   1.938                  which = i;
   1.939 @@ -996,7 +1056,7 @@
   1.940          }
   1.941  
   1.942          /* Queue up the audio data for this channel */
   1.943 -        if ( which >= 0 && which < num_channels ) {
   1.944 +        if (which >= 0 && which < num_channels) {
   1.945              Uint32 sdl_ticks = SDL_GetTicks();
   1.946              if (Mix_Playing(which))
   1.947                  _Mix_channel_done_playing(which);
   1.948 @@ -1026,16 +1086,16 @@
   1.949      int i;
   1.950      int prev_volume = 0;
   1.951  
   1.952 -    if ( which == -1 ) {
   1.953 -        for ( i=0; i<num_channels; ++i ) {
   1.954 +    if (which == -1) {
   1.955 +        for (i=0; i<num_channels; ++i) {
   1.956              prev_volume += Mix_Volume(i, volume);
   1.957          }
   1.958          prev_volume /= num_channels;
   1.959 -    } else if ( which < num_channels ) {
   1.960 +    } else if (which < num_channels) {
   1.961          prev_volume = mix_channel[which].volume;
   1.962 -        if ( volume >= 0 ) {
   1.963 -            if ( volume > SDL_MIX_MAXVOLUME ) {
   1.964 -                volume = SDL_MIX_MAXVOLUME;
   1.965 +        if (volume >= 0) {
   1.966 +            if (volume > MIX_MAX_VOLUME) {
   1.967 +                volume = MIX_MAX_VOLUME;
   1.968              }
   1.969              mix_channel[which].volume = volume;
   1.970          }
   1.971 @@ -1048,8 +1108,8 @@
   1.972      int prev_volume;
   1.973  
   1.974      prev_volume = chunk->volume;
   1.975 -    if ( volume >= 0 ) {
   1.976 -        if ( volume > MIX_MAX_VOLUME ) {
   1.977 +    if (volume >= 0) {
   1.978 +        if (volume > MIX_MAX_VOLUME) {
   1.979              volume = MIX_MAX_VOLUME;
   1.980          }
   1.981          chunk->volume = volume;
   1.982 @@ -1062,11 +1122,11 @@
   1.983  {
   1.984      int i;
   1.985  
   1.986 -    if ( which == -1 ) {
   1.987 -        for ( i=0; i<num_channels; ++i ) {
   1.988 +    if (which == -1) {
   1.989 +        for (i=0; i<num_channels; ++i) {
   1.990              Mix_HaltChannel(i);
   1.991          }
   1.992 -    } else if ( which < num_channels ) {
   1.993 +    } else if (which < num_channels) {
   1.994          Mix_LockAudio();
   1.995          if (mix_channel[which].playing) {
   1.996              _Mix_channel_done_playing(which);
   1.997 @@ -1087,8 +1147,8 @@
   1.998  {
   1.999      int i;
  1.1000  
  1.1001 -    for ( i=0; i<num_channels; ++i ) {
  1.1002 -        if( mix_channel[i].tag == tag ) {
  1.1003 +    for (i=0; i<num_channels; ++i) {
  1.1004 +        if(mix_channel[i].tag == tag) {
  1.1005              Mix_HaltChannel(i);
  1.1006          }
  1.1007      }
  1.1008 @@ -1101,18 +1161,18 @@
  1.1009      int status;
  1.1010  
  1.1011      status = 0;
  1.1012 -    if ( audio_opened ) {
  1.1013 -        if ( which == -1 ) {
  1.1014 +    if (audio_opened) {
  1.1015 +        if (which == -1) {
  1.1016              int i;
  1.1017  
  1.1018 -            for ( i=0; i<num_channels; ++i ) {
  1.1019 +            for (i=0; i<num_channels; ++i) {
  1.1020                  status += Mix_FadeOutChannel(i, ms);
  1.1021              }
  1.1022 -        } else if ( which < num_channels ) {
  1.1023 +        } else if (which < num_channels) {
  1.1024              Mix_LockAudio();
  1.1025 -            if ( mix_channel[which].playing &&
  1.1026 +            if (mix_channel[which].playing &&
  1.1027                  (mix_channel[which].volume > 0) &&
  1.1028 -                (mix_channel[which].fading != MIX_FADING_OUT) ) {
  1.1029 +                (mix_channel[which].fading != MIX_FADING_OUT)) {
  1.1030                  mix_channel[which].fade_volume = mix_channel[which].volume;
  1.1031                  mix_channel[which].fading = MIX_FADING_OUT;
  1.1032                  mix_channel[which].fade_length = (Uint32)ms;
  1.1033 @@ -1135,8 +1195,8 @@
  1.1034  {
  1.1035      int i;
  1.1036      int status = 0;
  1.1037 -    for ( i=0; i<num_channels; ++i ) {
  1.1038 -        if( mix_channel[i].tag == tag ) {
  1.1039 +    for (i=0; i<num_channels; ++i) {
  1.1040 +        if(mix_channel[i].tag == tag) {
  1.1041              status += Mix_FadeOutChannel(i,ms);
  1.1042          }
  1.1043      }
  1.1044 @@ -1145,7 +1205,7 @@
  1.1045  
  1.1046  Mix_Fading Mix_FadingChannel(int which)
  1.1047  {
  1.1048 -    if ( which < 0 || which >= num_channels ) {
  1.1049 +    if (which < 0 || which >= num_channels) {
  1.1050          return MIX_NO_FADING;
  1.1051      }
  1.1052      return mix_channel[which].fading;
  1.1053 @@ -1159,19 +1219,19 @@
  1.1054      int status;
  1.1055  
  1.1056      status = 0;
  1.1057 -    if ( which == -1 ) {
  1.1058 +    if (which == -1) {
  1.1059          int i;
  1.1060  
  1.1061 -        for ( i=0; i<num_channels; ++i ) {
  1.1062 +        for (i=0; i<num_channels; ++i) {
  1.1063              if ((mix_channel[i].playing > 0) ||
  1.1064                  mix_channel[i].looping)
  1.1065              {
  1.1066                  ++status;
  1.1067              }
  1.1068          }
  1.1069 -    } else if ( which < num_channels ) {
  1.1070 -        if ( (mix_channel[which].playing > 0) ||
  1.1071 -             mix_channel[which].looping )
  1.1072 +    } else if (which < num_channels) {
  1.1073 +        if ((mix_channel[which].playing > 0) ||
  1.1074 +             mix_channel[which].looping)
  1.1075          {
  1.1076              ++status;
  1.1077          }
  1.1078 @@ -1196,17 +1256,18 @@
  1.1079  {
  1.1080      int i;
  1.1081  
  1.1082 -    if ( audio_opened ) {
  1.1083 -        if ( audio_opened == 1 ) {
  1.1084 +    if (audio_opened) {
  1.1085 +        if (audio_opened == 1) {
  1.1086              for (i = 0; i < num_channels; i++) {
  1.1087                  Mix_UnregisterAllEffects(i);
  1.1088              }
  1.1089              Mix_UnregisterAllEffects(MIX_CHANNEL_POST);
  1.1090              close_music();
  1.1091 +            Mix_SetMusicCMD(NULL);
  1.1092              Mix_HaltChannel(-1);
  1.1093              _Mix_DeinitEffects();
  1.1094              SDL_CloseAudioDevice(audio_device);
  1.1095 -	    audio_device = 0;
  1.1096 +            audio_device = 0;
  1.1097              SDL_free(mix_channel);
  1.1098              mix_channel = NULL;
  1.1099  
  1.1100 @@ -1223,16 +1284,16 @@
  1.1101  void Mix_Pause(int which)
  1.1102  {
  1.1103      Uint32 sdl_ticks = SDL_GetTicks();
  1.1104 -    if ( which == -1 ) {
  1.1105 +    if (which == -1) {
  1.1106          int i;
  1.1107  
  1.1108 -        for ( i=0; i<num_channels; ++i ) {
  1.1109 -            if ( mix_channel[i].playing > 0 ) {
  1.1110 +        for (i=0; i<num_channels; ++i) {
  1.1111 +            if (mix_channel[i].playing > 0) {
  1.1112                  mix_channel[i].paused = sdl_ticks;
  1.1113              }
  1.1114          }
  1.1115 -    } else if ( which < num_channels ) {
  1.1116 -        if ( mix_channel[which].playing > 0 ) {
  1.1117 +    } else if (which < num_channels) {
  1.1118 +        if (mix_channel[which].playing > 0) {
  1.1119              mix_channel[which].paused = sdl_ticks;
  1.1120          }
  1.1121      }
  1.1122 @@ -1244,18 +1305,18 @@
  1.1123      Uint32 sdl_ticks = SDL_GetTicks();
  1.1124  
  1.1125      Mix_LockAudio();
  1.1126 -    if ( which == -1 ) {
  1.1127 +    if (which == -1) {
  1.1128          int i;
  1.1129  
  1.1130 -        for ( i=0; i<num_channels; ++i ) {
  1.1131 -            if ( mix_channel[i].playing > 0 ) {
  1.1132 +        for (i=0; i<num_channels; ++i) {
  1.1133 +            if (mix_channel[i].playing > 0) {
  1.1134                  if(mix_channel[i].expire > 0)
  1.1135                      mix_channel[i].expire += sdl_ticks - mix_channel[i].paused;
  1.1136                  mix_channel[i].paused = 0;
  1.1137              }
  1.1138          }
  1.1139 -    } else if ( which < num_channels ) {
  1.1140 -        if ( mix_channel[which].playing > 0 ) {
  1.1141 +    } else if (which < num_channels) {
  1.1142 +        if (mix_channel[which].playing > 0) {
  1.1143              if(mix_channel[which].expire > 0)
  1.1144                  mix_channel[which].expire += sdl_ticks - mix_channel[which].paused;
  1.1145              mix_channel[which].paused = 0;
  1.1146 @@ -1266,16 +1327,16 @@
  1.1147  
  1.1148  int Mix_Paused(int which)
  1.1149  {
  1.1150 -    if ( which < 0 ) {
  1.1151 +    if (which < 0) {
  1.1152          int status = 0;
  1.1153          int i;
  1.1154 -        for( i=0; i < num_channels; ++i ) {
  1.1155 -            if ( mix_channel[i].paused ) {
  1.1156 +        for(i=0; i < num_channels; ++i) {
  1.1157 +            if (mix_channel[i].paused) {
  1.1158                  ++ status;
  1.1159              }
  1.1160          }
  1.1161          return(status);
  1.1162 -    } else if ( which < num_channels ) {
  1.1163 +    } else if (which < num_channels) {
  1.1164          return(mix_channel[which].paused != 0);
  1.1165      } else {
  1.1166          return(0);
  1.1167 @@ -1285,7 +1346,7 @@
  1.1168  /* Change the group of a channel */
  1.1169  int Mix_GroupChannel(int which, int tag)
  1.1170  {
  1.1171 -    if ( which < 0 || which > num_channels )
  1.1172 +    if (which < 0 || which > num_channels)
  1.1173          return(0);
  1.1174  
  1.1175      Mix_LockAudio();
  1.1176 @@ -1298,7 +1359,7 @@
  1.1177  int Mix_GroupChannels(int from, int to, int tag)
  1.1178  {
  1.1179      int status = 0;
  1.1180 -    for( ; from <= to; ++ from ) {
  1.1181 +    for(; from <= to; ++ from) {
  1.1182          status += Mix_GroupChannel(from, tag);
  1.1183      }
  1.1184      return(status);
  1.1185 @@ -1308,9 +1369,9 @@
  1.1186  int Mix_GroupAvailable(int tag)
  1.1187  {
  1.1188      int i;
  1.1189 -    for( i=0; i < num_channels; i ++ ) {
  1.1190 -        if ( ((tag == -1) || (tag == mix_channel[i].tag)) &&
  1.1191 -                            (mix_channel[i].playing <= 0) )
  1.1192 +    for(i=0; i < num_channels; i ++) {
  1.1193 +        if (((tag == -1) || (tag == mix_channel[i].tag)) &&
  1.1194 +                            (mix_channel[i].playing <= 0))
  1.1195              return i;
  1.1196      }
  1.1197      return(-1);
  1.1198 @@ -1320,8 +1381,8 @@
  1.1199  {
  1.1200      int count = 0;
  1.1201      int i;
  1.1202 -    for( i=0; i < num_channels; i ++ ) {
  1.1203 -        if ( mix_channel[i].tag==tag || tag==-1 )
  1.1204 +    for(i=0; i < num_channels; i ++) {
  1.1205 +        if (mix_channel[i].tag==tag || tag==-1)
  1.1206              ++ count;
  1.1207      }
  1.1208      return(count);
  1.1209 @@ -1333,9 +1394,9 @@
  1.1210      int chan = -1;
  1.1211      Uint32 mintime = SDL_GetTicks();
  1.1212      int i;
  1.1213 -    for( i=0; i < num_channels; i ++ ) {
  1.1214 -        if ( (mix_channel[i].tag==tag || tag==-1) && mix_channel[i].playing > 0
  1.1215 -             && mix_channel[i].start_time <= mintime ) {
  1.1216 +    for(i=0; i < num_channels; i ++) {
  1.1217 +        if ((mix_channel[i].tag==tag || tag==-1) && mix_channel[i].playing > 0
  1.1218 +             && mix_channel[i].start_time <= mintime) {
  1.1219              mintime = mix_channel[i].start_time;
  1.1220              chan = i;
  1.1221          }
  1.1222 @@ -1349,9 +1410,9 @@
  1.1223      int chan = -1;
  1.1224      Uint32 maxtime = 0;
  1.1225      int i;
  1.1226 -    for( i=0; i < num_channels; i ++ ) {
  1.1227 -        if ( (mix_channel[i].tag==tag || tag==-1) && mix_channel[i].playing > 0
  1.1228 -             && mix_channel[i].start_time >= maxtime ) {
  1.1229 +    for(i=0; i < num_channels; i ++) {
  1.1230 +        if ((mix_channel[i].tag==tag || tag==-1) && mix_channel[i].playing > 0
  1.1231 +             && mix_channel[i].start_time >= maxtime) {
  1.1232              maxtime = mix_channel[i].start_time;
  1.1233              chan = i;
  1.1234          }
  1.1235 @@ -1555,15 +1616,16 @@
  1.1236      return(retval);
  1.1237  }
  1.1238  
  1.1239 -void Mix_LockAudio()
  1.1240 +void Mix_LockAudio(void)
  1.1241  {
  1.1242      SDL_LockAudioDevice(audio_device);
  1.1243  }
  1.1244  
  1.1245 -void Mix_UnlockAudio()
  1.1246 +void Mix_UnlockAudio(void)
  1.1247  {
  1.1248      SDL_UnlockAudioDevice(audio_device);
  1.1249  }
  1.1250  
  1.1251  /* end of mixer.c ... */
  1.1252  
  1.1253 +/* vi: set ts=4 sw=4 expandtab: */