mixer.c
changeset 718 fb0562cc1559
parent 711 f40c5ac95b12
child 725 bdf7b8d20566
     1.1 --- a/mixer.c	Tue Jan 12 05:56:16 2016 -0800
     1.2 +++ b/mixer.c	Fri Jan 29 12:44:13 2016 -0800
     1.3 @@ -30,6 +30,7 @@
     1.4  #include "SDL_timer.h"
     1.5  
     1.6  #include "SDL_mixer.h"
     1.7 +#include "mixer.h"
     1.8  #include "load_aiff.h"
     1.9  #include "load_voc.h"
    1.10  #include "load_mp3.h"
    1.11 @@ -55,6 +56,7 @@
    1.12  
    1.13  static int audio_opened = 0;
    1.14  static SDL_AudioSpec mixer;
    1.15 +static SDL_AudioDeviceID audio_device;
    1.16  
    1.17  typedef struct _Mix_effectinfo
    1.18  {
    1.19 @@ -257,7 +259,7 @@
    1.20  
    1.21  /*
    1.22   * rcg06122001 Cleanup effect callbacks.
    1.23 - *  MAKE SURE SDL_LockAudio() is called before this (or you're in the
    1.24 + *  MAKE SURE Mix_LockAudio() is called before this (or you're in the
    1.25   *   audio callback).
    1.26   */
    1.27  static void _Mix_channel_done_playing(int channel)
    1.28 @@ -362,7 +364,7 @@
    1.29                      }
    1.30  
    1.31                      mix_input = Mix_DoEffects(i, mix_channel[i].samples, mixable);
    1.32 -                    SDL_MixAudio(stream+index,mix_input,mixable,volume);
    1.33 +                    SDL_MixAudioFormat(stream+index,mix_input,mixer.format,mixable,volume);
    1.34                      if (mix_input != mix_channel[i].samples)
    1.35                          SDL_free(mix_input);
    1.36  
    1.37 @@ -386,7 +388,7 @@
    1.38                      }
    1.39  
    1.40                      mix_input = Mix_DoEffects(i, mix_channel[i].chunk->abuf, remaining);
    1.41 -                    SDL_MixAudio(stream+index, mix_input, remaining, volume);
    1.42 +                    SDL_MixAudioFormat(stream+index, mix_input, mixer.format, remaining, volume);
    1.43                      if (mix_input != mix_channel[i].chunk->abuf)
    1.44                          SDL_free(mix_input);
    1.45  
    1.46 @@ -426,9 +428,9 @@
    1.47  }
    1.48  #endif
    1.49  
    1.50 -
    1.51  /* Open the mixer with a certain desired audio format */
    1.52 -int Mix_OpenAudio(int frequency, Uint16 format, int nchannels, int chunksize)
    1.53 +int Mix_OpenAudioDevice(int frequency, Uint16 format, int nchannels, int chunksize,
    1.54 +			const char* device, int allowed_changes)
    1.55  {
    1.56      int i;
    1.57      SDL_AudioSpec desired;
    1.58 @@ -453,7 +455,7 @@
    1.59      desired.userdata = NULL;
    1.60  
    1.61      /* Accept nearly any audio format */
    1.62 -    if ( SDL_OpenAudio(&desired, &mixer) < 0 ) {
    1.63 +    if ( (audio_device = SDL_OpenAudioDevice(device, 0, &desired, &mixer, allowed_changes)) == 0 ) {
    1.64          return(-1);
    1.65      }
    1.66  #if 0
    1.67 @@ -462,7 +464,7 @@
    1.68  
    1.69      /* Initialize the music players */
    1.70      if ( open_music(&mixer) < 0 ) {
    1.71 -        SDL_CloseAudio();
    1.72 +        SDL_CloseAudioDevice(audio_device);
    1.73          return(-1);
    1.74      }
    1.75  
    1.76 @@ -502,10 +504,17 @@
    1.77  #endif
    1.78  
    1.79      audio_opened = 1;
    1.80 -    SDL_PauseAudio(0);
    1.81 +    SDL_PauseAudioDevice(audio_device, 0);
    1.82      return(0);
    1.83  }
    1.84  
    1.85 +/* Open the mixer with a certain desired audio format */
    1.86 +int Mix_OpenAudio(int frequency, Uint16 format, int nchannels, int chunksize)
    1.87 +{
    1.88 +    return Mix_OpenAudioDevice(frequency, format, nchannels, chunksize,
    1.89 +			       NULL, SDL_AUDIO_ALLOW_ANY_CHANGE);
    1.90 +}
    1.91 +
    1.92  /* Dynamically change the number of channels managed by the mixer.
    1.93     If decreasing the number of channels, the upper channels are
    1.94     stopped.
    1.95 @@ -523,7 +532,7 @@
    1.96              Mix_HaltChannel(i);
    1.97          }
    1.98      }
    1.99 -    SDL_LockAudio();
   1.100 +    Mix_LockAudio();
   1.101      mix_channel = (struct _Mix_Channel *) SDL_realloc(mix_channel, numchans * sizeof(struct _Mix_Channel));
   1.102      if ( numchans > num_channels ) {
   1.103          /* Initialize the new channels */
   1.104 @@ -543,7 +552,7 @@
   1.105          }
   1.106      }
   1.107      num_channels = numchans;
   1.108 -    SDL_UnlockAudio();
   1.109 +    Mix_UnlockAudio();
   1.110      return(num_channels);
   1.111  }
   1.112  
   1.113 @@ -786,7 +795,7 @@
   1.114      /* Caution -- if the chunk is playing, the mixer will crash */
   1.115      if ( chunk ) {
   1.116          /* Guarantee that this chunk isn't playing */
   1.117 -        SDL_LockAudio();
   1.118 +        Mix_LockAudio();
   1.119          if ( mix_channel ) {
   1.120              for ( i=0; i<num_channels; ++i ) {
   1.121                  if ( chunk == mix_channel[i].chunk ) {
   1.122 @@ -795,7 +804,7 @@
   1.123                  }
   1.124              }
   1.125          }
   1.126 -        SDL_UnlockAudio();
   1.127 +        Mix_UnlockAudio();
   1.128          /* Actually free the chunk */
   1.129          if ( chunk->allocated ) {
   1.130              SDL_free(chunk->abuf);
   1.131 @@ -811,10 +820,10 @@
   1.132  void Mix_SetPostMix(void (*mix_func)
   1.133                      (void *udata, Uint8 *stream, int len), void *arg)
   1.134  {
   1.135 -    SDL_LockAudio();
   1.136 +    Mix_LockAudio();
   1.137      mix_postmix_data = arg;
   1.138      mix_postmix = mix_func;
   1.139 -    SDL_UnlockAudio();
   1.140 +    Mix_UnlockAudio();
   1.141  }
   1.142  
   1.143  /* Add your own music player or mixer function.
   1.144 @@ -823,7 +832,7 @@
   1.145  void Mix_HookMusic(void (*mix_func)(void *udata, Uint8 *stream, int len),
   1.146                                                                  void *arg)
   1.147  {
   1.148 -    SDL_LockAudio();
   1.149 +    Mix_LockAudio();
   1.150      if ( mix_func != NULL ) {
   1.151          music_data = arg;
   1.152          mix_music = mix_func;
   1.153 @@ -831,7 +840,7 @@
   1.154          music_data = NULL;
   1.155          mix_music = music_mixer;
   1.156      }
   1.157 -    SDL_UnlockAudio();
   1.158 +    Mix_UnlockAudio();
   1.159  }
   1.160  
   1.161  void *Mix_GetMusicHookData(void)
   1.162 @@ -841,9 +850,9 @@
   1.163  
   1.164  void Mix_ChannelFinished(void (*channel_finished)(int channel))
   1.165  {
   1.166 -    SDL_LockAudio();
   1.167 +    Mix_LockAudio();
   1.168      channel_done_callback = channel_finished;
   1.169 -    SDL_UnlockAudio();
   1.170 +    Mix_UnlockAudio();
   1.171  }
   1.172  
   1.173  
   1.174 @@ -890,7 +899,7 @@
   1.175      }
   1.176  
   1.177      /* Lock the mixer while modifying the playing channels */
   1.178 -    SDL_LockAudio();
   1.179 +    Mix_LockAudio();
   1.180      {
   1.181          /* If which is -1, play on the first free channel */
   1.182          if ( which == -1 ) {
   1.183 @@ -921,7 +930,7 @@
   1.184              mix_channel[which].expire = (ticks>0) ? (sdl_ticks + ticks) : 0;
   1.185          }
   1.186      }
   1.187 -    SDL_UnlockAudio();
   1.188 +    Mix_UnlockAudio();
   1.189  
   1.190      /* Return the channel on which the sound is being played */
   1.191      return(which);
   1.192 @@ -938,9 +947,9 @@
   1.193              status += Mix_ExpireChannel(i, ticks);
   1.194          }
   1.195      } else if ( which < num_channels ) {
   1.196 -        SDL_LockAudio();
   1.197 +        Mix_LockAudio();
   1.198          mix_channel[which].expire = (ticks>0) ? (SDL_GetTicks() + ticks) : 0;
   1.199 -        SDL_UnlockAudio();
   1.200 +        Mix_UnlockAudio();
   1.201          ++ status;
   1.202      }
   1.203      return(status);
   1.204 @@ -961,7 +970,7 @@
   1.205      }
   1.206  
   1.207      /* Lock the mixer while modifying the playing channels */
   1.208 -    SDL_LockAudio();
   1.209 +    Mix_LockAudio();
   1.210      {
   1.211          /* If which is -1, play on the first free channel */
   1.212          if ( which == -1 ) {
   1.213 @@ -995,7 +1004,7 @@
   1.214              mix_channel[which].expire = (ticks > 0) ? (sdl_ticks+ticks) : 0;
   1.215          }
   1.216      }
   1.217 -    SDL_UnlockAudio();
   1.218 +    Mix_UnlockAudio();
   1.219  
   1.220      /* Return the channel on which the sound is being played */
   1.221      return(which);
   1.222 @@ -1048,7 +1057,7 @@
   1.223              Mix_HaltChannel(i);
   1.224          }
   1.225      } else if ( which < num_channels ) {
   1.226 -        SDL_LockAudio();
   1.227 +        Mix_LockAudio();
   1.228          if (mix_channel[which].playing) {
   1.229              _Mix_channel_done_playing(which);
   1.230              mix_channel[which].playing = 0;
   1.231 @@ -1058,7 +1067,7 @@
   1.232          if(mix_channel[which].fading != MIX_NO_FADING) /* Restore volume */
   1.233              mix_channel[which].volume = mix_channel[which].fade_volume_reset;
   1.234          mix_channel[which].fading = MIX_NO_FADING;
   1.235 -        SDL_UnlockAudio();
   1.236 +        Mix_UnlockAudio();
   1.237      }
   1.238      return(0);
   1.239  }
   1.240 @@ -1090,7 +1099,7 @@
   1.241                  status += Mix_FadeOutChannel(i, ms);
   1.242              }
   1.243          } else if ( which < num_channels ) {
   1.244 -            SDL_LockAudio();
   1.245 +            Mix_LockAudio();
   1.246              if ( mix_channel[which].playing &&
   1.247                  (mix_channel[which].volume > 0) &&
   1.248                  (mix_channel[which].fading != MIX_FADING_OUT) ) {
   1.249 @@ -1105,7 +1114,7 @@
   1.250                  }
   1.251                  ++status;
   1.252              }
   1.253 -            SDL_UnlockAudio();
   1.254 +            Mix_UnlockAudio();
   1.255          }
   1.256      }
   1.257      return(status);
   1.258 @@ -1186,7 +1195,8 @@
   1.259              close_music();
   1.260              Mix_HaltChannel(-1);
   1.261              _Mix_DeinitEffects();
   1.262 -            SDL_CloseAudio();
   1.263 +            SDL_CloseAudioDevice(audio_device);
   1.264 +	    audio_device = 0;
   1.265              SDL_free(mix_channel);
   1.266              mix_channel = NULL;
   1.267  
   1.268 @@ -1223,7 +1233,7 @@
   1.269  {
   1.270      Uint32 sdl_ticks = SDL_GetTicks();
   1.271  
   1.272 -    SDL_LockAudio();
   1.273 +    Mix_LockAudio();
   1.274      if ( which == -1 ) {
   1.275          int i;
   1.276  
   1.277 @@ -1241,7 +1251,7 @@
   1.278              mix_channel[which].paused = 0;
   1.279          }
   1.280      }
   1.281 -    SDL_UnlockAudio();
   1.282 +    Mix_UnlockAudio();
   1.283  }
   1.284  
   1.285  int Mix_Paused(int which)
   1.286 @@ -1268,9 +1278,9 @@
   1.287      if ( which < 0 || which > num_channels )
   1.288          return(0);
   1.289  
   1.290 -    SDL_LockAudio();
   1.291 +    Mix_LockAudio();
   1.292      mix_channel[which].tag = tag;
   1.293 -    SDL_UnlockAudio();
   1.294 +    Mix_UnlockAudio();
   1.295      return(1);
   1.296  }
   1.297  
   1.298 @@ -1347,7 +1357,7 @@
   1.299   *  as Mix_SetPanning().
   1.300   */
   1.301  
   1.302 -/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
   1.303 +/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
   1.304  static int _Mix_register_effect(effect_info **e, Mix_EffectFunc_t f,
   1.305                  Mix_EffectDone_t d, void *arg)
   1.306  {
   1.307 @@ -1392,7 +1402,7 @@
   1.308  }
   1.309  
   1.310  
   1.311 -/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
   1.312 +/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
   1.313  static int _Mix_remove_effect(int channel, effect_info **e, Mix_EffectFunc_t f)
   1.314  {
   1.315      effect_info *cur;
   1.316 @@ -1427,7 +1437,7 @@
   1.317  }
   1.318  
   1.319  
   1.320 -/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
   1.321 +/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
   1.322  static int _Mix_remove_all_effects(int channel, effect_info **e)
   1.323  {
   1.324      effect_info *cur;
   1.325 @@ -1451,7 +1461,7 @@
   1.326  }
   1.327  
   1.328  
   1.329 -/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
   1.330 +/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
   1.331  int _Mix_RegisterEffect_locked(int channel, Mix_EffectFunc_t f,
   1.332              Mix_EffectDone_t d, void *arg)
   1.333  {
   1.334 @@ -1474,14 +1484,14 @@
   1.335              Mix_EffectDone_t d, void *arg)
   1.336  {
   1.337      int retval;
   1.338 -    SDL_LockAudio();
   1.339 +    Mix_LockAudio();
   1.340      retval = _Mix_RegisterEffect_locked(channel, f, d, arg);
   1.341 -    SDL_UnlockAudio();
   1.342 +    Mix_UnlockAudio();
   1.343      return retval;
   1.344  }
   1.345  
   1.346  
   1.347 -/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
   1.348 +/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
   1.349  int _Mix_UnregisterEffect_locked(int channel, Mix_EffectFunc_t f)
   1.350  {
   1.351      effect_info **e = NULL;
   1.352 @@ -1502,13 +1512,13 @@
   1.353  int Mix_UnregisterEffect(int channel, Mix_EffectFunc_t f)
   1.354  {
   1.355      int retval;
   1.356 -    SDL_LockAudio();
   1.357 +    Mix_LockAudio();
   1.358      retval = _Mix_UnregisterEffect_locked(channel, f);
   1.359 -    SDL_UnlockAudio();
   1.360 +    Mix_UnlockAudio();
   1.361      return(retval);
   1.362  }
   1.363  
   1.364 -/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
   1.365 +/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
   1.366  int _Mix_UnregisterAllEffects_locked(int channel)
   1.367  {
   1.368      effect_info **e = NULL;
   1.369 @@ -1529,11 +1539,21 @@
   1.370  int Mix_UnregisterAllEffects(int channel)
   1.371  {
   1.372      int retval;
   1.373 -    SDL_LockAudio();
   1.374 +    Mix_LockAudio();
   1.375      retval = _Mix_UnregisterAllEffects_locked(channel);
   1.376 -    SDL_UnlockAudio();
   1.377 +    Mix_UnlockAudio();
   1.378      return(retval);
   1.379  }
   1.380  
   1.381 +void Mix_LockAudio()
   1.382 +{
   1.383 +    SDL_LockAudioDevice(audio_device);
   1.384 +}
   1.385 +
   1.386 +void Mix_UnlockAudio()
   1.387 +{
   1.388 +    SDL_UnlockAudioDevice(audio_device);
   1.389 +}
   1.390 +
   1.391  /* end of mixer.c ... */
   1.392