mixer.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 01 Jun 2013 19:52:15 -0700
changeset 625 1d489d8ec2e0
parent 621 944412baab72
child 639 f8901a7ff3f1
permissions -rw-r--r--
SDL_LoadMUS_RW() now takes an argument telling whether or not the data source should be freed when done.
Fixes crashes cleaning up the data source for music when loading fails.
slouken@0
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@601
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@0
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@140
    22
/* $Id$ */
megastep@7
    23
slouken@0
    24
#include <stdio.h>
slouken@0
    25
#include <stdlib.h>
slouken@0
    26
#include <string.h>
slouken@0
    27
slouken@24
    28
#include "SDL_mutex.h"
slouken@24
    29
#include "SDL_endian.h"
slouken@29
    30
#include "SDL_timer.h"
slouken@0
    31
slouken@34
    32
#include "SDL_mixer.h"
slouken@111
    33
#include "load_aiff.h"
slouken@111
    34
#include "load_voc.h"
slouken@190
    35
#include "load_ogg.h"
slouken@382
    36
#include "load_flac.h"
slouken@470
    37
#include "dynamic_flac.h"
slouken@470
    38
#include "dynamic_mod.h"
slouken@470
    39
#include "dynamic_mp3.h"
slouken@470
    40
#include "dynamic_ogg.h"
slouken@111
    41
icculus@359
    42
#define __MIX_INTERNAL_EFFECT__
icculus@359
    43
#include "effects_internal.h"
icculus@359
    44
slouken@111
    45
/* Magic numbers for various audio file formats */
slouken@617
    46
#define RIFF        0x46464952      /* "RIFF" */
slouken@617
    47
#define WAVE        0x45564157      /* "WAVE" */
slouken@617
    48
#define FORM        0x4d524f46      /* "FORM" */
slouken@617
    49
#define OGGS        0x5367674f      /* "OggS" */
slouken@617
    50
#define CREA        0x61657243      /* "Crea" */
slouken@617
    51
#define FLAC        0x43614C66      /* "fLaC" */
slouken@0
    52
slouken@0
    53
static int audio_opened = 0;
slouken@0
    54
static SDL_AudioSpec mixer;
slouken@113
    55
slouken@113
    56
typedef struct _Mix_effectinfo
slouken@113
    57
{
slouken@617
    58
    Mix_EffectFunc_t callback;
slouken@617
    59
    Mix_EffectDone_t done_callback;
slouken@617
    60
    void *udata;
slouken@617
    61
    struct _Mix_effectinfo *next;
slouken@113
    62
} effect_info;
slouken@113
    63
slouken@0
    64
static struct _Mix_Channel {
slouken@617
    65
    Mix_Chunk *chunk;
slouken@617
    66
    int playing;
slouken@617
    67
    int paused;
slouken@617
    68
    Uint8 *samples;
slouken@617
    69
    int volume;
slouken@617
    70
    int looping;
slouken@617
    71
    int tag;
slouken@617
    72
    Uint32 expire;
slouken@617
    73
    Uint32 start_time;
slouken@617
    74
    Mix_Fading fading;
slouken@617
    75
    int fade_volume;
slouken@617
    76
    int fade_volume_reset;
slouken@617
    77
    Uint32 fade_length;
slouken@617
    78
    Uint32 ticks_fade;
slouken@617
    79
    effect_info *effects;
slouken@60
    80
} *mix_channel = NULL;
slouken@113
    81
slouken@113
    82
static effect_info *posteffects = NULL;
slouken@113
    83
megastep@7
    84
static int num_channels;
slouken@17
    85
static int reserved_channels = 0;
slouken@0
    86
slouken@0
    87
slouken@26
    88
/* Support for hooking into the mixer callback system */
slouken@26
    89
static void (*mix_postmix)(void *udata, Uint8 *stream, int len) = NULL;
slouken@29
    90
static void *mix_postmix_data = NULL;
slouken@26
    91
slouken@92
    92
/* rcg07062001 callback to alert when channels are done playing. */
slouken@92
    93
static void (*channel_done_callback)(int channel) = NULL;
slouken@92
    94
slouken@42
    95
/* Music function declarations */
slouken@42
    96
extern int open_music(SDL_AudioSpec *mixer);
slouken@42
    97
extern void close_music(void);
slouken@42
    98
slouken@0
    99
/* Support for user defined music functions, plus the default one */
slouken@42
   100
extern int volatile music_active;
slouken@0
   101
extern void music_mixer(void *udata, Uint8 *stream, int len);
slouken@0
   102
static void (*mix_music)(void *udata, Uint8 *stream, int len) = music_mixer;
slouken@42
   103
static void *music_data = NULL;
slouken@0
   104
icculus@390
   105
/* rcg06042009 report available decoders at runtime. */
icculus@390
   106
static const char **chunk_decoders = NULL;
icculus@390
   107
static int num_decoders = 0;
icculus@390
   108
chewi@506
   109
/* Semicolon-separated SoundFont paths */
chewi@506
   110
#ifdef MID_MUSIC
chewi@506
   111
extern char* soundfont_paths;
chewi@506
   112
#endif
chewi@506
   113
slouken@466
   114
int Mix_GetNumChunkDecoders(void)
icculus@390
   115
{
slouken@617
   116
    return(num_decoders);
icculus@390
   117
}
icculus@390
   118
icculus@390
   119
const char *Mix_GetChunkDecoder(int index)
icculus@390
   120
{
slouken@617
   121
    if ((index < 0) || (index >= num_decoders)) {
slouken@617
   122
        return NULL;
slouken@617
   123
    }
slouken@617
   124
    return(chunk_decoders[index]);
icculus@390
   125
}
icculus@390
   126
icculus@390
   127
static void add_chunk_decoder(const char *decoder)
icculus@390
   128
{
slouken@621
   129
    void *ptr = SDL_realloc((void *)chunk_decoders, (num_decoders + 1) * sizeof (const char **));
slouken@617
   130
    if (ptr == NULL) {
slouken@617
   131
        return;  /* oh well, go on without it. */
slouken@617
   132
    }
slouken@617
   133
    chunk_decoders = (const char **) ptr;
slouken@617
   134
    chunk_decoders[num_decoders++] = decoder;
icculus@390
   135
}
slouken@113
   136
slouken@113
   137
/* rcg06192001 get linked library's version. */
slouken@113
   138
const SDL_version *Mix_Linked_Version(void)
slouken@113
   139
{
slouken@617
   140
    static SDL_version linked_version;
slouken@617
   141
    SDL_MIXER_VERSION(&linked_version);
slouken@617
   142
    return(&linked_version);
slouken@113
   143
}
slouken@113
   144
slouken@470
   145
static int initialized = 0;
slouken@470
   146
slouken@470
   147
int Mix_Init(int flags)
slouken@470
   148
{
slouken@617
   149
    int result = 0;
slouken@470
   150
slouken@617
   151
    if (flags & MIX_INIT_FLUIDSYNTH) {
chewi@506
   152
#ifdef USE_FLUIDSYNTH_MIDI
slouken@617
   153
        if ((initialized & MIX_INIT_FLUIDSYNTH) || Mix_InitFluidSynth() == 0) {
slouken@617
   154
            result |= MIX_INIT_FLUIDSYNTH;
slouken@617
   155
        }
chewi@506
   156
#else
slouken@617
   157
        Mix_SetError("Mixer not built with FluidSynth support");
chewi@506
   158
#endif
slouken@617
   159
    }
slouken@617
   160
    if (flags & MIX_INIT_FLAC) {
slouken@472
   161
#ifdef FLAC_MUSIC
slouken@617
   162
        if ((initialized & MIX_INIT_FLAC) || Mix_InitFLAC() == 0) {
slouken@617
   163
            result |= MIX_INIT_FLAC;
slouken@617
   164
        }
slouken@472
   165
#else
slouken@617
   166
        Mix_SetError("Mixer not built with FLAC support");
slouken@472
   167
#endif
slouken@617
   168
    }
slouken@617
   169
    if (flags & MIX_INIT_MOD) {
slouken@472
   170
#ifdef MOD_MUSIC
slouken@617
   171
        if ((initialized & MIX_INIT_MOD) || Mix_InitMOD() == 0) {
slouken@617
   172
            result |= MIX_INIT_MOD;
slouken@617
   173
        }
slouken@472
   174
#else
slouken@617
   175
        Mix_SetError("Mixer not built with MOD support");
slouken@472
   176
#endif
slouken@617
   177
    }
slouken@617
   178
    if (flags & MIX_INIT_MP3) {
slouken@472
   179
#ifdef MP3_MUSIC
slouken@617
   180
        if ((initialized & MIX_INIT_MP3) || Mix_InitMP3() == 0) {
slouken@617
   181
            result |= MIX_INIT_MP3;
slouken@617
   182
        }
slouken@472
   183
#else
slouken@617
   184
        Mix_SetError("Mixer not built with MP3 support");
slouken@472
   185
#endif
slouken@617
   186
    }
slouken@617
   187
    if (flags & MIX_INIT_OGG) {
slouken@472
   188
#ifdef OGG_MUSIC
slouken@617
   189
        if ((initialized & MIX_INIT_OGG) || Mix_InitOgg() == 0) {
slouken@617
   190
            result |= MIX_INIT_OGG;
slouken@617
   191
        }
slouken@472
   192
#else
slouken@617
   193
        Mix_SetError("Mixer not built with Ogg Vorbis support");
slouken@472
   194
#endif
slouken@617
   195
    }
slouken@617
   196
    initialized |= result;
slouken@470
   197
slouken@617
   198
    return (result);
slouken@470
   199
}
slouken@470
   200
slouken@470
   201
void Mix_Quit()
slouken@470
   202
{
chewi@506
   203
#ifdef USE_FLUIDSYNTH_MIDI
slouken@617
   204
    if (initialized & MIX_INIT_FLUIDSYNTH) {
slouken@617
   205
        Mix_QuitFluidSynth();
slouken@617
   206
    }
chewi@506
   207
#endif
slouken@472
   208
#ifdef FLAC_MUSIC
slouken@617
   209
    if (initialized & MIX_INIT_FLAC) {
slouken@617
   210
        Mix_QuitFLAC();
slouken@617
   211
    }
slouken@472
   212
#endif
slouken@472
   213
#ifdef MOD_MUSIC
slouken@617
   214
    if (initialized & MIX_INIT_MOD) {
slouken@617
   215
        Mix_QuitMOD();
slouken@617
   216
    }
slouken@472
   217
#endif
slouken@472
   218
#ifdef MP3_MUSIC
slouken@617
   219
    if (initialized & MIX_INIT_MP3) {
slouken@617
   220
        Mix_QuitMP3();
slouken@617
   221
    }
slouken@472
   222
#endif
slouken@472
   223
#ifdef OGG_MUSIC
slouken@617
   224
    if (initialized & MIX_INIT_OGG) {
slouken@617
   225
        Mix_QuitOgg();
slouken@617
   226
    }
slouken@472
   227
#endif
chewi@506
   228
#ifdef MID_MUSIC
slouken@617
   229
    if (soundfont_paths) {
slouken@617
   230
        SDL_free(soundfont_paths);
slouken@617
   231
    }
chewi@506
   232
#endif
slouken@617
   233
    initialized = 0;
slouken@470
   234
}
slouken@470
   235
slouken@164
   236
static int _Mix_remove_all_effects(int channel, effect_info **e);
slouken@113
   237
slouken@172
   238
/*
slouken@172
   239
 * rcg06122001 Cleanup effect callbacks.
slouken@172
   240
 *  MAKE SURE SDL_LockAudio() is called before this (or you're in the
slouken@172
   241
 *   audio callback).
slouken@172
   242
 */
slouken@172
   243
static void _Mix_channel_done_playing(int channel)
slouken@113
   244
{
slouken@617
   245
    if (channel_done_callback) {
slouken@617
   246
        channel_done_callback(channel);
slouken@617
   247
    }
slouken@113
   248
slouken@617
   249
    /*
slouken@617
   250
     * Call internal function directly, to avoid locking audio from
slouken@617
   251
     *   inside audio callback.
slouken@617
   252
     */
slouken@617
   253
    _Mix_remove_all_effects(channel, &mix_channel[channel].effects);
slouken@113
   254
}
slouken@113
   255
slouken@113
   256
slouken@113
   257
static void *Mix_DoEffects(int chan, void *snd, int len)
slouken@113
   258
{
slouken@617
   259
    int posteffect = (chan == MIX_CHANNEL_POST);
slouken@617
   260
    effect_info *e = ((posteffect) ? posteffects : mix_channel[chan].effects);
slouken@617
   261
    void *buf = snd;
slouken@113
   262
slouken@617
   263
    if (e != NULL) {    /* are there any registered effects? */
slouken@617
   264
        /* if this is the postmix, we can just overwrite the original. */
slouken@617
   265
        if (!posteffect) {
slouken@617
   266
            buf = SDL_malloc(len);
slouken@617
   267
            if (buf == NULL) {
slouken@617
   268
                return(snd);
slouken@617
   269
            }
slouken@621
   270
            SDL_memcpy(buf, snd, len);
slouken@617
   271
        }
slouken@113
   272
slouken@617
   273
        for (; e != NULL; e = e->next) {
slouken@617
   274
            if (e->callback != NULL) {
slouken@617
   275
                e->callback(chan, buf, len, e->udata);
slouken@617
   276
            }
slouken@617
   277
        }
slouken@617
   278
    }
slouken@113
   279
slouken@617
   280
    /* be sure to SDL_free() the return value if != snd ... */
slouken@617
   281
    return(buf);
slouken@113
   282
}
slouken@113
   283
slouken@113
   284
slouken@0
   285
/* Mixing function */
slouken@0
   286
static void mix_channels(void *udata, Uint8 *stream, int len)
slouken@0
   287
{
slouken@617
   288
    Uint8 *mix_input;
slouken@617
   289
    int i, mixable, volume = SDL_MIX_MAXVOLUME;
slouken@617
   290
    Uint32 sdl_ticks;
slouken@0
   291
slouken@364
   292
#if SDL_VERSION_ATLEAST(1, 3, 0)
slouken@617
   293
    /* Need to initialize the stream in SDL 1.3+ */
slouken@621
   294
    SDL_memset(stream, mixer.silence, len);
slouken@364
   295
#endif
slouken@345
   296
slouken@617
   297
    /* Mix the music (must be done before the channels are added) */
slouken@617
   298
    if ( music_active || (mix_music != music_mixer) ) {
slouken@617
   299
        mix_music(music_data, stream, len);
slouken@617
   300
    }
slouken@15
   301
slouken@617
   302
    /* Mix any playing channels... */
slouken@617
   303
    sdl_ticks = SDL_GetTicks();
slouken@617
   304
    for ( i=0; i<num_channels; ++i ) {
slouken@617
   305
        if( ! mix_channel[i].paused ) {
slouken@617
   306
            if ( mix_channel[i].expire > 0 && mix_channel[i].expire < sdl_ticks ) {
slouken@617
   307
                /* Expiration delay for that channel is reached */
slouken@617
   308
                mix_channel[i].playing = 0;
slouken@617
   309
                mix_channel[i].looping = 0;
slouken@617
   310
                mix_channel[i].fading = MIX_NO_FADING;
slouken@617
   311
                mix_channel[i].expire = 0;
slouken@617
   312
                _Mix_channel_done_playing(i);
slouken@617
   313
            } else if ( mix_channel[i].fading != MIX_NO_FADING ) {
slouken@617
   314
                Uint32 ticks = sdl_ticks - mix_channel[i].ticks_fade;
slouken@617
   315
                if( ticks > mix_channel[i].fade_length ) {
slouken@617
   316
                    Mix_Volume(i, mix_channel[i].fade_volume_reset); /* Restore the volume */
slouken@617
   317
                    if( mix_channel[i].fading == MIX_FADING_OUT ) {
slouken@617
   318
                        mix_channel[i].playing = 0;
slouken@617
   319
                        mix_channel[i].looping = 0;
slouken@617
   320
                        mix_channel[i].expire = 0;
slouken@617
   321
                        _Mix_channel_done_playing(i);
slouken@617
   322
                    }
slouken@617
   323
                    mix_channel[i].fading = MIX_NO_FADING;
slouken@617
   324
                } else {
slouken@617
   325
                    if( mix_channel[i].fading == MIX_FADING_OUT ) {
slouken@617
   326
                        Mix_Volume(i, (mix_channel[i].fade_volume * (mix_channel[i].fade_length-ticks))
slouken@617
   327
                                   / mix_channel[i].fade_length );
slouken@617
   328
                    } else {
slouken@617
   329
                        Mix_Volume(i, (mix_channel[i].fade_volume * ticks) / mix_channel[i].fade_length );
slouken@617
   330
                    }
slouken@617
   331
                }
slouken@617
   332
            }
slouken@617
   333
            if ( mix_channel[i].playing > 0 ) {
slouken@617
   334
                int index = 0;
slouken@617
   335
                int remaining = len;
slouken@617
   336
                while (mix_channel[i].playing > 0 && index < len) {
slouken@617
   337
                    remaining = len - index;
slouken@617
   338
                    volume = (mix_channel[i].volume*mix_channel[i].chunk->volume) / MIX_MAX_VOLUME;
slouken@617
   339
                    mixable = mix_channel[i].playing;
slouken@617
   340
                    if ( mixable > remaining ) {
slouken@617
   341
                        mixable = remaining;
slouken@617
   342
                    }
slouken@188
   343
slouken@617
   344
                    mix_input = Mix_DoEffects(i, mix_channel[i].samples, mixable);
slouken@617
   345
                    SDL_MixAudio(stream+index,mix_input,mixable,volume);
slouken@617
   346
                    if (mix_input != mix_channel[i].samples)
slouken@617
   347
                        SDL_free(mix_input);
slouken@188
   348
slouken@617
   349
                    mix_channel[i].samples += mixable;
slouken@617
   350
                    mix_channel[i].playing -= mixable;
slouken@617
   351
                    index += mixable;
slouken@188
   352
slouken@617
   353
                    /* rcg06072001 Alert app if channel is done playing. */
slouken@617
   354
                    if (!mix_channel[i].playing && !mix_channel[i].looping) {
slouken@617
   355
                        _Mix_channel_done_playing(i);
slouken@617
   356
                    }
slouken@617
   357
                }
slouken@113
   358
slouken@617
   359
                /* If looping the sample and we are at its end, make sure
slouken@617
   360
                   we will still return a full buffer */
slouken@617
   361
                while ( mix_channel[i].looping && index < len ) {
slouken@617
   362
                    int alen = mix_channel[i].chunk->alen;
slouken@617
   363
                    remaining = len - index;
slouken@617
   364
                    if (remaining > alen) {
slouken@617
   365
                        remaining = alen;
slouken@617
   366
                    }
slouken@113
   367
slouken@617
   368
                    mix_input = Mix_DoEffects(i, mix_channel[i].chunk->abuf, remaining);
slouken@617
   369
                    SDL_MixAudio(stream+index, mix_input, remaining, volume);
slouken@617
   370
                    if (mix_input != mix_channel[i].chunk->abuf)
slouken@617
   371
                        SDL_free(mix_input);
slouken@113
   372
slouken@617
   373
                    --mix_channel[i].looping;
slouken@617
   374
                    mix_channel[i].samples = mix_channel[i].chunk->abuf + remaining;
slouken@617
   375
                    mix_channel[i].playing = mix_channel[i].chunk->alen - remaining;
slouken@617
   376
                    index += remaining;
slouken@617
   377
                }
slouken@617
   378
                if ( ! mix_channel[i].playing && mix_channel[i].looping ) {
slouken@617
   379
                    --mix_channel[i].looping;
slouken@617
   380
                    mix_channel[i].samples = mix_channel[i].chunk->abuf;
slouken@617
   381
                    mix_channel[i].playing = mix_channel[i].chunk->alen;
slouken@617
   382
                }
slouken@617
   383
            }
slouken@617
   384
        }
slouken@617
   385
    }
slouken@113
   386
slouken@617
   387
    /* rcg06122001 run posteffects... */
slouken@617
   388
    Mix_DoEffects(MIX_CHANNEL_POST, stream, len);
slouken@113
   389
slouken@617
   390
    if ( mix_postmix ) {
slouken@617
   391
        mix_postmix(mix_postmix_data, stream, len);
slouken@617
   392
    }
slouken@0
   393
}
slouken@0
   394
slouken@373
   395
#if 0
slouken@0
   396
static void PrintFormat(char *title, SDL_AudioSpec *fmt)
slouken@0
   397
{
slouken@617
   398
    printf("%s: %d bit %s audio (%s) at %u Hz\n", title, (fmt->format&0xFF),
slouken@617
   399
            (fmt->format&0x8000) ? "signed" : "unsigned",
slouken@617
   400
            (fmt->channels > 2) ? "surround" :
slouken@617
   401
            (fmt->channels > 1) ? "stereo" : "mono", fmt->freq);
slouken@0
   402
}
slouken@373
   403
#endif
slouken@0
   404
slouken@113
   405
slouken@0
   406
/* Open the mixer with a certain desired audio format */
slouken@60
   407
int Mix_OpenAudio(int frequency, Uint16 format, int nchannels, int chunksize)
slouken@0
   408
{
slouken@617
   409
    int i;
slouken@617
   410
    SDL_AudioSpec desired;
slouken@0
   411
slouken@617
   412
    /* If the mixer is already opened, increment open count */
slouken@617
   413
    if ( audio_opened ) {
slouken@617
   414
        if ( format == mixer.format && nchannels == mixer.channels ) {
slouken@617
   415
            ++audio_opened;
slouken@617
   416
            return(0);
slouken@617
   417
        }
slouken@617
   418
        while ( audio_opened ) {
slouken@617
   419
            Mix_CloseAudio();
slouken@617
   420
        }
slouken@617
   421
    }
slouken@0
   422
slouken@617
   423
    /* Set the desired format and frequency */
slouken@617
   424
    desired.freq = frequency;
slouken@617
   425
    desired.format = format;
slouken@617
   426
    desired.channels = nchannels;
slouken@617
   427
    desired.samples = chunksize;
slouken@617
   428
    desired.callback = mix_channels;
slouken@617
   429
    desired.userdata = NULL;
slouken@0
   430
slouken@617
   431
    /* Accept nearly any audio format */
slouken@617
   432
    if ( SDL_OpenAudio(&desired, &mixer) < 0 ) {
slouken@617
   433
        return(-1);
slouken@617
   434
    }
slouken@0
   435
#if 0
slouken@617
   436
    PrintFormat("Audio device", &mixer);
slouken@0
   437
#endif
slouken@0
   438
slouken@617
   439
    /* Initialize the music players */
slouken@617
   440
    if ( open_music(&mixer) < 0 ) {
slouken@617
   441
        SDL_CloseAudio();
slouken@617
   442
        return(-1);
slouken@617
   443
    }
megastep@7
   444
slouken@617
   445
    num_channels = MIX_CHANNELS;
slouken@617
   446
    mix_channel = (struct _Mix_Channel *) SDL_malloc(num_channels * sizeof(struct _Mix_Channel));
megastep@7
   447
slouken@617
   448
    /* Clear out the audio channels */
slouken@617
   449
    for ( i=0; i<num_channels; ++i ) {
slouken@617
   450
        mix_channel[i].chunk = NULL;
slouken@617
   451
        mix_channel[i].playing = 0;
slouken@617
   452
        mix_channel[i].looping = 0;
slouken@617
   453
        mix_channel[i].volume = SDL_MIX_MAXVOLUME;
slouken@617
   454
        mix_channel[i].fade_volume = SDL_MIX_MAXVOLUME;
slouken@617
   455
        mix_channel[i].fade_volume_reset = SDL_MIX_MAXVOLUME;
slouken@617
   456
        mix_channel[i].fading = MIX_NO_FADING;
slouken@617
   457
        mix_channel[i].tag = -1;
slouken@617
   458
        mix_channel[i].expire = 0;
slouken@617
   459
        mix_channel[i].effects = NULL;
slouken@617
   460
        mix_channel[i].paused = 0;
slouken@617
   461
    }
slouken@617
   462
    Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@113
   463
slouken@617
   464
    _Mix_InitEffects();
slouken@113
   465
slouken@617
   466
    /* This list is (currently) decided at build time. */
slouken@617
   467
    add_chunk_decoder("WAVE");
slouken@617
   468
    add_chunk_decoder("AIFF");
slouken@617
   469
    add_chunk_decoder("VOC");
icculus@390
   470
#ifdef OGG_MUSIC
slouken@617
   471
    add_chunk_decoder("OGG");
icculus@390
   472
#endif
icculus@390
   473
#ifdef FLAC_MUSIC
slouken@617
   474
    add_chunk_decoder("FLAC");
icculus@390
   475
#endif
icculus@390
   476
slouken@617
   477
    audio_opened = 1;
slouken@617
   478
    SDL_PauseAudio(0);
slouken@617
   479
    return(0);
slouken@0
   480
}
slouken@0
   481
megastep@7
   482
/* Dynamically change the number of channels managed by the mixer.
megastep@7
   483
   If decreasing the number of channels, the upper channels are
megastep@7
   484
   stopped.
megastep@7
   485
 */
megastep@7
   486
int Mix_AllocateChannels(int numchans)
megastep@7
   487
{
slouken@617
   488
    if ( numchans<0 || numchans==num_channels )
slouken@617
   489
        return(num_channels);
megastep@7
   490
slouken@617
   491
    if ( numchans < num_channels ) {
slouken@617
   492
        /* Stop the affected channels */
slouken@617
   493
        int i;
slouken@617
   494
        for(i=numchans; i < num_channels; i++) {
slouken@617
   495
            Mix_UnregisterAllEffects(i);
slouken@617
   496
            Mix_HaltChannel(i);
slouken@617
   497
        }
slouken@617
   498
    }
slouken@617
   499
    SDL_LockAudio();
slouken@617
   500
    mix_channel = (struct _Mix_Channel *) SDL_realloc(mix_channel, numchans * sizeof(struct _Mix_Channel));
slouken@617
   501
    if ( numchans > num_channels ) {
slouken@617
   502
        /* Initialize the new channels */
slouken@617
   503
        int i;
slouken@617
   504
        for(i=num_channels; i < numchans; i++) {
slouken@617
   505
            mix_channel[i].chunk = NULL;
slouken@617
   506
            mix_channel[i].playing = 0;
slouken@617
   507
            mix_channel[i].looping = 0;
slouken@617
   508
            mix_channel[i].volume = SDL_MIX_MAXVOLUME;
slouken@617
   509
            mix_channel[i].fade_volume = SDL_MIX_MAXVOLUME;
slouken@617
   510
            mix_channel[i].fade_volume_reset = SDL_MIX_MAXVOLUME;
slouken@617
   511
            mix_channel[i].fading = MIX_NO_FADING;
slouken@617
   512
            mix_channel[i].tag = -1;
slouken@617
   513
            mix_channel[i].expire = 0;
slouken@617
   514
            mix_channel[i].effects = NULL;
slouken@617
   515
            mix_channel[i].paused = 0;
slouken@617
   516
        }
slouken@617
   517
    }
slouken@617
   518
    num_channels = numchans;
slouken@617
   519
    SDL_UnlockAudio();
slouken@617
   520
    return(num_channels);
megastep@7
   521
}
megastep@7
   522
slouken@0
   523
/* Return the actual mixer parameters */
slouken@0
   524
int Mix_QuerySpec(int *frequency, Uint16 *format, int *channels)
slouken@0
   525
{
slouken@617
   526
    if ( audio_opened ) {
slouken@617
   527
        if ( frequency ) {
slouken@617
   528
            *frequency = mixer.freq;
slouken@617
   529
        }
slouken@617
   530
        if ( format ) {
slouken@617
   531
            *format = mixer.format;
slouken@617
   532
        }
slouken@617
   533
        if ( channels ) {
slouken@617
   534
            *channels = mixer.channels;
slouken@617
   535
        }
slouken@617
   536
    }
slouken@617
   537
    return(audio_opened);
slouken@0
   538
}
slouken@0
   539
slouken@91
   540
slouken@91
   541
/*
slouken@91
   542
 * !!! FIXME: Ideally, we want a Mix_LoadSample_RW(), which will handle the
slouken@91
   543
 *             generic setup, then call the correct file format loader.
slouken@91
   544
 */
slouken@91
   545
slouken@0
   546
/* Load a wave file */
slouken@0
   547
Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
slouken@0
   548
{
slouken@617
   549
    Uint32 magic;
slouken@617
   550
    Mix_Chunk *chunk;
slouken@617
   551
    SDL_AudioSpec wavespec, *loaded;
slouken@617
   552
    SDL_AudioCVT wavecvt;
slouken@617
   553
    int samplesize;
slouken@0
   554
slouken@617
   555
    /* rcg06012001 Make sure src is valid */
slouken@617
   556
    if ( ! src ) {
slouken@617
   557
        SDL_SetError("Mix_LoadWAV_RW with NULL src");
slouken@617
   558
        return(NULL);
slouken@617
   559
    }
slouken@91
   560
slouken@617
   561
    /* Make sure audio has been opened */
slouken@617
   562
    if ( ! audio_opened ) {
slouken@617
   563
        SDL_SetError("Audio device hasn't been opened");
slouken@625
   564
        if ( freesrc ) {
slouken@617
   565
            SDL_RWclose(src);
slouken@617
   566
        }
slouken@617
   567
        return(NULL);
slouken@617
   568
    }
slouken@0
   569
slouken@617
   570
    /* Allocate the chunk memory */
slouken@617
   571
    chunk = (Mix_Chunk *)SDL_malloc(sizeof(Mix_Chunk));
slouken@617
   572
    if ( chunk == NULL ) {
slouken@617
   573
        SDL_SetError("Out of memory");
slouken@617
   574
        if ( freesrc ) {
slouken@617
   575
            SDL_RWclose(src);
slouken@617
   576
        }
slouken@617
   577
        return(NULL);
slouken@617
   578
    }
slouken@0
   579
slouken@617
   580
    /* Find out what kind of audio file this is */
slouken@617
   581
    magic = SDL_ReadLE32(src);
slouken@617
   582
    /* Seek backwards for compatibility with older loaders */
slouken@617
   583
    SDL_RWseek(src, -(int)sizeof(Uint32), RW_SEEK_CUR);
slouken@111
   584
slouken@617
   585
    switch (magic) {
slouken@617
   586
        case WAVE:
slouken@617
   587
        case RIFF:
slouken@617
   588
            loaded = SDL_LoadWAV_RW(src, freesrc, &wavespec,
slouken@617
   589
                    (Uint8 **)&chunk->abuf, &chunk->alen);
slouken@617
   590
            break;
slouken@617
   591
        case FORM:
slouken@617
   592
            loaded = Mix_LoadAIFF_RW(src, freesrc, &wavespec,
slouken@617
   593
                    (Uint8 **)&chunk->abuf, &chunk->alen);
slouken@617
   594
            break;
slouken@190
   595
#ifdef OGG_MUSIC
slouken@617
   596
        case OGGS:
slouken@617
   597
            loaded = Mix_LoadOGG_RW(src, freesrc, &wavespec,
slouken@617
   598
                    (Uint8 **)&chunk->abuf, &chunk->alen);
slouken@617
   599
            break;
slouken@190
   600
#endif
slouken@382
   601
#ifdef FLAC_MUSIC
slouken@617
   602
        case FLAC:
slouken@617
   603
            loaded = Mix_LoadFLAC_RW(src, freesrc, &wavespec,
slouken@617
   604
                    (Uint8 **)&chunk->abuf, &chunk->alen);
slouken@617
   605
            break;
slouken@382
   606
#endif
slouken@617
   607
        case CREA:
slouken@617
   608
            loaded = Mix_LoadVOC_RW(src, freesrc, &wavespec,
slouken@617
   609
                    (Uint8 **)&chunk->abuf, &chunk->alen);
slouken@617
   610
            break;
slouken@617
   611
        default:
slouken@617
   612
            SDL_SetError("Unrecognized sound file type");
slouken@617
   613
            if ( freesrc ) {
slouken@617
   614
                SDL_RWclose(src);
slouken@617
   615
            }
slouken@617
   616
            loaded = NULL;
slouken@617
   617
            break;
slouken@617
   618
    }
slouken@617
   619
    if ( !loaded ) {
slouken@617
   620
        /* The individual loaders have closed src if needed */
slouken@617
   621
        SDL_free(chunk);
slouken@617
   622
        return(NULL);
slouken@617
   623
    }
slouken@91
   624
slouken@0
   625
#if 0
slouken@617
   626
    PrintFormat("Audio device", &mixer);
slouken@617
   627
    PrintFormat("-- Wave file", &wavespec);
slouken@0
   628
#endif
slouken@0
   629
slouken@617
   630
    /* Build the audio converter and create conversion buffers */
slouken@617
   631
    if ( wavespec.format != mixer.format ||
slouken@617
   632
         wavespec.channels != mixer.channels ||
slouken@617
   633
         wavespec.freq != mixer.freq ) {
slouken@617
   634
        if ( SDL_BuildAudioCVT(&wavecvt,
slouken@617
   635
                wavespec.format, wavespec.channels, wavespec.freq,
slouken@617
   636
                mixer.format, mixer.channels, mixer.freq) < 0 ) {
slouken@617
   637
            SDL_free(chunk->abuf);
slouken@617
   638
            SDL_free(chunk);
slouken@617
   639
            return(NULL);
slouken@617
   640
        }
slouken@617
   641
        samplesize = ((wavespec.format & 0xFF)/8)*wavespec.channels;
slouken@617
   642
        wavecvt.len = chunk->alen & ~(samplesize-1);
slouken@617
   643
        wavecvt.buf = (Uint8 *)SDL_calloc(1, wavecvt.len*wavecvt.len_mult);
slouken@617
   644
        if ( wavecvt.buf == NULL ) {
slouken@617
   645
            SDL_SetError("Out of memory");
slouken@617
   646
            SDL_free(chunk->abuf);
slouken@617
   647
            SDL_free(chunk);
slouken@617
   648
            return(NULL);
slouken@617
   649
        }
slouken@621
   650
        SDL_memcpy(wavecvt.buf, chunk->abuf, chunk->alen);
slouken@617
   651
        SDL_free(chunk->abuf);
slouken@560
   652
slouken@617
   653
        /* Run the audio converter */
slouken@617
   654
        if ( SDL_ConvertAudio(&wavecvt) < 0 ) {
slouken@617
   655
            SDL_free(wavecvt.buf);
slouken@617
   656
            SDL_free(chunk);
slouken@617
   657
            return(NULL);
slouken@617
   658
        }
slouken@562
   659
slouken@617
   660
        chunk->abuf = wavecvt.buf;
slouken@617
   661
        chunk->alen = wavecvt.len_cvt;
slouken@617
   662
    }
slouken@0
   663
slouken@617
   664
    chunk->allocated = 1;
slouken@617
   665
    chunk->volume = MIX_MAX_VOLUME;
icculus@516
   666
slouken@617
   667
    return(chunk);
slouken@0
   668
}
slouken@0
   669
slouken@1
   670
/* Load a wave file of the mixer format from a memory buffer */
slouken@1
   671
Mix_Chunk *Mix_QuickLoad_WAV(Uint8 *mem)
slouken@1
   672
{
slouken@617
   673
    Mix_Chunk *chunk;
slouken@617
   674
    Uint8 magic[4];
slouken@1
   675
slouken@617
   676
    /* Make sure audio has been opened */
slouken@617
   677
    if ( ! audio_opened ) {
slouken@617
   678
        SDL_SetError("Audio device hasn't been opened");
slouken@617
   679
        return(NULL);
slouken@617
   680
    }
slouken@1
   681
slouken@617
   682
    /* Allocate the chunk memory */
slouken@617
   683
    chunk = (Mix_Chunk *)SDL_calloc(1,sizeof(Mix_Chunk));
slouken@617
   684
    if ( chunk == NULL ) {
slouken@617
   685
        SDL_SetError("Out of memory");
slouken@617
   686
        return(NULL);
slouken@617
   687
    }
slouken@1
   688
slouken@617
   689
    /* Essentially just skip to the audio data (no error checking - fast) */
slouken@617
   690
    chunk->allocated = 0;
slouken@617
   691
    mem += 12; /* WAV header */
slouken@617
   692
    do {
slouken@621
   693
        SDL_memcpy(magic, mem, 4);
slouken@617
   694
        mem += 4;
slouken@617
   695
        chunk->alen = ((mem[3]<<24)|(mem[2]<<16)|(mem[1]<<8)|(mem[0]));
slouken@617
   696
        mem += 4;
slouken@617
   697
        chunk->abuf = mem;
slouken@617
   698
        mem += chunk->alen;
slouken@617
   699
    } while ( memcmp(magic, "data", 4) != 0 );
slouken@617
   700
    chunk->volume = MIX_MAX_VOLUME;
slouken@1
   701
slouken@617
   702
    return(chunk);
slouken@1
   703
}
slouken@1
   704
slouken@174
   705
/* Load raw audio data of the mixer format from a memory buffer */
slouken@174
   706
Mix_Chunk *Mix_QuickLoad_RAW(Uint8 *mem, Uint32 len)
slouken@174
   707
{
slouken@617
   708
    Mix_Chunk *chunk;
slouken@174
   709
slouken@617
   710
    /* Make sure audio has been opened */
slouken@617
   711
    if ( ! audio_opened ) {
slouken@617
   712
        SDL_SetError("Audio device hasn't been opened");
slouken@617
   713
        return(NULL);
slouken@617
   714
    }
slouken@174
   715
slouken@617
   716
    /* Allocate the chunk memory */
slouken@617
   717
    chunk = (Mix_Chunk *)SDL_malloc(sizeof(Mix_Chunk));
slouken@617
   718
    if ( chunk == NULL ) {
slouken@617
   719
        SDL_SetError("Out of memory");
slouken@617
   720
        return(NULL);
slouken@617
   721
    }
slouken@174
   722
slouken@617
   723
    /* Essentially just point at the audio data (no error checking - fast) */
slouken@617
   724
    chunk->allocated = 0;
slouken@617
   725
    chunk->alen = len;
slouken@617
   726
    chunk->abuf = mem;
slouken@617
   727
    chunk->volume = MIX_MAX_VOLUME;
slouken@174
   728
slouken@617
   729
    return(chunk);
slouken@174
   730
}
slouken@174
   731
slouken@0
   732
/* Free an audio chunk previously loaded */
slouken@0
   733
void Mix_FreeChunk(Mix_Chunk *chunk)
slouken@0
   734
{
slouken@617
   735
    int i;
slouken@0
   736
slouken@617
   737
    /* Caution -- if the chunk is playing, the mixer will crash */
slouken@617
   738
    if ( chunk ) {
slouken@617
   739
        /* Guarantee that this chunk isn't playing */
slouken@617
   740
        SDL_LockAudio();
slouken@617
   741
        if ( mix_channel ) {
slouken@617
   742
            for ( i=0; i<num_channels; ++i ) {
slouken@617
   743
                if ( chunk == mix_channel[i].chunk ) {
slouken@617
   744
                    mix_channel[i].playing = 0;
slouken@617
   745
                    mix_channel[i].looping = 0;
slouken@617
   746
                }
slouken@617
   747
            }
slouken@617
   748
        }
slouken@617
   749
        SDL_UnlockAudio();
slouken@617
   750
        /* Actually free the chunk */
slouken@617
   751
        if ( chunk->allocated ) {
slouken@617
   752
            SDL_free(chunk->abuf);
slouken@617
   753
        }
slouken@617
   754
        SDL_free(chunk);
slouken@617
   755
    }
slouken@0
   756
}
slouken@0
   757
slouken@26
   758
/* Set a function that is called after all mixing is performed.
slouken@26
   759
   This can be used to provide real-time visual display of the audio stream
slouken@26
   760
   or add a custom mixer filter for the stream data.
slouken@26
   761
*/
slouken@26
   762
void Mix_SetPostMix(void (*mix_func)
slouken@26
   763
                    (void *udata, Uint8 *stream, int len), void *arg)
slouken@26
   764
{
slouken@617
   765
    SDL_LockAudio();
slouken@617
   766
    mix_postmix_data = arg;
slouken@617
   767
    mix_postmix = mix_func;
slouken@617
   768
    SDL_UnlockAudio();
slouken@26
   769
}
slouken@26
   770
slouken@0
   771
/* Add your own music player or mixer function.
slouken@0
   772
   If 'mix_func' is NULL, the default music player is re-enabled.
slouken@0
   773
 */
slouken@0
   774
void Mix_HookMusic(void (*mix_func)(void *udata, Uint8 *stream, int len),
slouken@0
   775
                                                                void *arg)
slouken@0
   776
{
slouken@617
   777
    SDL_LockAudio();
slouken@617
   778
    if ( mix_func != NULL ) {
slouken@617
   779
        music_data = arg;
slouken@617
   780
        mix_music = mix_func;
slouken@617
   781
    } else {
slouken@617
   782
        music_data = NULL;
slouken@617
   783
        mix_music = music_mixer;
slouken@617
   784
    }
slouken@617
   785
    SDL_UnlockAudio();
slouken@0
   786
}
slouken@0
   787
slouken@0
   788
void *Mix_GetMusicHookData(void)
slouken@0
   789
{
slouken@617
   790
    return(music_data);
slouken@0
   791
}
slouken@0
   792
slouken@92
   793
void Mix_ChannelFinished(void (*channel_finished)(int channel))
slouken@92
   794
{
slouken@617
   795
    SDL_LockAudio();
slouken@617
   796
    channel_done_callback = channel_finished;
slouken@617
   797
    SDL_UnlockAudio();
slouken@92
   798
}
slouken@92
   799
slouken@92
   800
slouken@0
   801
/* Reserve the first channels (0 -> n-1) for the application, i.e. don't allocate
slouken@0
   802
   them dynamically to the next sample if requested with a -1 value below.
slouken@0
   803
   Returns the number of reserved channels.
slouken@0
   804
 */
slouken@0
   805
int Mix_ReserveChannels(int num)
slouken@0
   806
{
slouken@617
   807
    if (num > num_channels)
slouken@617
   808
        num = num_channels;
slouken@617
   809
    reserved_channels = num;
slouken@617
   810
    return num;
slouken@0
   811
}
slouken@0
   812
slouken@245
   813
static int checkchunkintegral(Mix_Chunk *chunk)
slouken@245
   814
{
slouken@617
   815
    int frame_width = 1;
slouken@245
   816
slouken@617
   817
    if ((mixer.format & 0xFF) == 16) frame_width = 2;
slouken@617
   818
    frame_width *= mixer.channels;
slouken@617
   819
    while (chunk->alen % frame_width) chunk->alen--;
slouken@617
   820
    return chunk->alen;
slouken@245
   821
}
slouken@245
   822
slouken@0
   823
/* Play an audio chunk on a specific channel.
slouken@0
   824
   If the specified channel is -1, play on the first free channel.
megastep@7
   825
   'ticks' is the number of milliseconds at most to play the sample, or -1
megastep@7
   826
   if there is no limit.
slouken@0
   827
   Returns which channel was used to play the sound.
slouken@0
   828
*/
megastep@7
   829
int Mix_PlayChannelTimed(int which, Mix_Chunk *chunk, int loops, int ticks)
slouken@0
   830
{
slouken@617
   831
    int i;
slouken@0
   832
slouken@617
   833
    /* Don't play null pointers :-) */
slouken@617
   834
    if ( chunk == NULL ) {
slouken@617
   835
        Mix_SetError("Tried to play a NULL chunk");
slouken@617
   836
        return(-1);
slouken@617
   837
    }
slouken@617
   838
    if ( !checkchunkintegral(chunk)) {
slouken@617
   839
        Mix_SetError("Tried to play a chunk with a bad frame");
slouken@617
   840
        return(-1);
slouken@617
   841
    }
slouken@0
   842
slouken@617
   843
    /* Lock the mixer while modifying the playing channels */
slouken@617
   844
    SDL_LockAudio();
slouken@617
   845
    {
slouken@617
   846
        /* If which is -1, play on the first free channel */
slouken@617
   847
        if ( which == -1 ) {
slouken@617
   848
            for ( i=reserved_channels; i<num_channels; ++i ) {
slouken@617
   849
                if ( mix_channel[i].playing <= 0 )
slouken@617
   850
                    break;
slouken@617
   851
            }
slouken@617
   852
            if ( i == num_channels ) {
slouken@617
   853
                Mix_SetError("No free channels available");
slouken@617
   854
                which = -1;
slouken@617
   855
            } else {
slouken@617
   856
                which = i;
slouken@617
   857
            }
slouken@617
   858
        }
slouken@0
   859
slouken@617
   860
        /* Queue up the audio data for this channel */
slouken@617
   861
        if ( which >= 0 && which < num_channels ) {
slouken@617
   862
            Uint32 sdl_ticks = SDL_GetTicks();
slouken@617
   863
            if (Mix_Playing(which))
slouken@617
   864
                _Mix_channel_done_playing(which);
slouken@617
   865
            mix_channel[which].samples = chunk->abuf;
slouken@617
   866
            mix_channel[which].playing = chunk->alen;
slouken@617
   867
            mix_channel[which].looping = loops;
slouken@617
   868
            mix_channel[which].chunk = chunk;
slouken@617
   869
            mix_channel[which].paused = 0;
slouken@617
   870
            mix_channel[which].fading = MIX_NO_FADING;
slouken@617
   871
            mix_channel[which].start_time = sdl_ticks;
slouken@617
   872
            mix_channel[which].expire = (ticks>0) ? (sdl_ticks + ticks) : 0;
slouken@617
   873
        }
slouken@617
   874
    }
slouken@617
   875
    SDL_UnlockAudio();
megastep@4
   876
slouken@617
   877
    /* Return the channel on which the sound is being played */
slouken@617
   878
    return(which);
megastep@4
   879
}
megastep@4
   880
megastep@7
   881
/* Change the expiration delay for a channel */
megastep@7
   882
int Mix_ExpireChannel(int which, int ticks)
megastep@7
   883
{
slouken@617
   884
    int status = 0;
megastep@7
   885
slouken@617
   886
    if ( which == -1 ) {
slouken@617
   887
        int i;
slouken@617
   888
        for ( i=0; i < num_channels; ++ i ) {
slouken@617
   889
            status += Mix_ExpireChannel(i, ticks);
slouken@617
   890
        }
slouken@617
   891
    } else if ( which < num_channels ) {
slouken@617
   892
        SDL_LockAudio();
slouken@617
   893
        mix_channel[which].expire = (ticks>0) ? (SDL_GetTicks() + ticks) : 0;
slouken@617
   894
        SDL_UnlockAudio();
slouken@617
   895
        ++ status;
slouken@617
   896
    }
slouken@617
   897
    return(status);
megastep@7
   898
}
megastep@7
   899
megastep@4
   900
/* Fade in a sound on a channel, over ms milliseconds */
megastep@7
   901
int Mix_FadeInChannelTimed(int which, Mix_Chunk *chunk, int loops, int ms, int ticks)
megastep@4
   902
{
slouken@617
   903
    int i;
megastep@4
   904
slouken@617
   905
    /* Don't play null pointers :-) */
slouken@617
   906
    if ( chunk == NULL ) {
slouken@617
   907
        return(-1);
slouken@617
   908
    }
slouken@617
   909
    if ( !checkchunkintegral(chunk)) {
slouken@617
   910
        Mix_SetError("Tried to play a chunk with a bad frame");
slouken@617
   911
        return(-1);
slouken@617
   912
    }
megastep@4
   913
slouken@617
   914
    /* Lock the mixer while modifying the playing channels */
slouken@617
   915
    SDL_LockAudio();
slouken@617
   916
    {
slouken@617
   917
        /* If which is -1, play on the first free channel */
slouken@617
   918
        if ( which == -1 ) {
slouken@617
   919
            for ( i=reserved_channels; i<num_channels; ++i ) {
slouken@617
   920
                if ( mix_channel[i].playing <= 0 )
slouken@617
   921
                    break;
slouken@617
   922
            }
slouken@617
   923
            if ( i == num_channels ) {
slouken@617
   924
                which = -1;
slouken@617
   925
            } else {
slouken@617
   926
                which = i;
slouken@617
   927
            }
slouken@617
   928
        }
megastep@4
   929
slouken@617
   930
        /* Queue up the audio data for this channel */
slouken@617
   931
        if ( which >= 0 && which < num_channels ) {
slouken@617
   932
            Uint32 sdl_ticks = SDL_GetTicks();
slouken@617
   933
            if (Mix_Playing(which))
slouken@617
   934
                _Mix_channel_done_playing(which);
slouken@617
   935
            mix_channel[which].samples = chunk->abuf;
slouken@617
   936
            mix_channel[which].playing = chunk->alen;
slouken@617
   937
            mix_channel[which].looping = loops;
slouken@617
   938
            mix_channel[which].chunk = chunk;
slouken@617
   939
            mix_channel[which].paused = 0;
slouken@617
   940
            mix_channel[which].fading = MIX_FADING_IN;
slouken@617
   941
            mix_channel[which].fade_volume = mix_channel[which].volume;
slouken@617
   942
            mix_channel[which].fade_volume_reset = mix_channel[which].volume;
slouken@617
   943
            mix_channel[which].volume = 0;
slouken@617
   944
            mix_channel[which].fade_length = (Uint32)ms;
slouken@617
   945
            mix_channel[which].start_time = mix_channel[which].ticks_fade = sdl_ticks;
slouken@617
   946
            mix_channel[which].expire = (ticks > 0) ? (sdl_ticks+ticks) : 0;
slouken@617
   947
        }
slouken@617
   948
    }
slouken@617
   949
    SDL_UnlockAudio();
slouken@0
   950
slouken@617
   951
    /* Return the channel on which the sound is being played */
slouken@617
   952
    return(which);
slouken@0
   953
}
slouken@0
   954
slouken@0
   955
/* Set volume of a particular channel */
slouken@0
   956
int Mix_Volume(int which, int volume)
slouken@0
   957
{
slouken@617
   958
    int i;
slouken@617
   959
    int prev_volume = 0;
slouken@0
   960
slouken@617
   961
    if ( which == -1 ) {
slouken@617
   962
        for ( i=0; i<num_channels; ++i ) {
slouken@617
   963
            prev_volume += Mix_Volume(i, volume);
slouken@617
   964
        }
slouken@617
   965
        prev_volume /= num_channels;
slouken@617
   966
    } else if ( which < num_channels ) {
slouken@617
   967
        prev_volume = mix_channel[which].volume;
slouken@617
   968
        if ( volume >= 0 ) {
slouken@617
   969
            if ( volume > SDL_MIX_MAXVOLUME ) {
slouken@617
   970
                volume = SDL_MIX_MAXVOLUME;
slouken@617
   971
            }
slouken@617
   972
            mix_channel[which].volume = volume;
slouken@617
   973
        }
slouken@617
   974
    }
slouken@617
   975
    return(prev_volume);
slouken@0
   976
}
slouken@0
   977
/* Set volume of a particular chunk */
slouken@0
   978
int Mix_VolumeChunk(Mix_Chunk *chunk, int volume)
slouken@0
   979
{
slouken@617
   980
    int prev_volume;
slouken@0
   981
slouken@617
   982
    prev_volume = chunk->volume;
slouken@617
   983
    if ( volume >= 0 ) {
slouken@617
   984
        if ( volume > MIX_MAX_VOLUME ) {
slouken@617
   985
            volume = MIX_MAX_VOLUME;
slouken@617
   986
        }
slouken@617
   987
        chunk->volume = volume;
slouken@617
   988
    }
slouken@617
   989
    return(prev_volume);
slouken@0
   990
}
slouken@0
   991
slouken@0
   992
/* Halt playing of a particular channel */
slouken@0
   993
int Mix_HaltChannel(int which)
slouken@0
   994
{
slouken@617
   995
    int i;
slouken@0
   996
slouken@617
   997
    if ( which == -1 ) {
slouken@617
   998
        for ( i=0; i<num_channels; ++i ) {
slouken@617
   999
            Mix_HaltChannel(i);
slouken@617
  1000
        }
slouken@617
  1001
    } else if ( which < num_channels ) {
slouken@617
  1002
        SDL_LockAudio();
slouken@617
  1003
        if (mix_channel[which].playing) {
slouken@617
  1004
            _Mix_channel_done_playing(which);
slouken@617
  1005
            mix_channel[which].playing = 0;
slouken@617
  1006
            mix_channel[which].looping = 0;
slouken@617
  1007
        }
slouken@617
  1008
        mix_channel[which].expire = 0;
slouken@617
  1009
        if(mix_channel[which].fading != MIX_NO_FADING) /* Restore volume */
slouken@617
  1010
            mix_channel[which].volume = mix_channel[which].fade_volume_reset;
slouken@617
  1011
        mix_channel[which].fading = MIX_NO_FADING;
slouken@617
  1012
        SDL_UnlockAudio();
slouken@617
  1013
    }
slouken@617
  1014
    return(0);
slouken@0
  1015
}
slouken@0
  1016
megastep@7
  1017
/* Halt playing of a particular group of channels */
megastep@7
  1018
int Mix_HaltGroup(int tag)
megastep@7
  1019
{
slouken@617
  1020
    int i;
megastep@7
  1021
slouken@617
  1022
    for ( i=0; i<num_channels; ++i ) {
slouken@617
  1023
        if( mix_channel[i].tag == tag ) {
slouken@617
  1024
            Mix_HaltChannel(i);
slouken@617
  1025
        }
slouken@617
  1026
    }
slouken@617
  1027
    return(0);
megastep@7
  1028
}
megastep@7
  1029
megastep@4
  1030
/* Fade out a channel and then stop it automatically */
megastep@4
  1031
int Mix_FadeOutChannel(int which, int ms)
megastep@4
  1032
{
slouken@617
  1033
    int status;
megastep@4
  1034
slouken@617
  1035
    status = 0;
slouken@617
  1036
    if ( audio_opened ) {
slouken@617
  1037
        if ( which == -1 ) {
slouken@617
  1038
            int i;
megastep@4
  1039
slouken@617
  1040
            for ( i=0; i<num_channels; ++i ) {
slouken@617
  1041
                status += Mix_FadeOutChannel(i, ms);
slouken@617
  1042
            }
slouken@617
  1043
        } else if ( which < num_channels ) {
slouken@617
  1044
            SDL_LockAudio();
slouken@617
  1045
            if ( mix_channel[which].playing &&
slouken@617
  1046
                (mix_channel[which].volume > 0) &&
slouken@617
  1047
                (mix_channel[which].fading != MIX_FADING_OUT) ) {
slouken@617
  1048
                mix_channel[which].fade_volume = mix_channel[which].volume;
slouken@617
  1049
                mix_channel[which].fading = MIX_FADING_OUT;
slouken@617
  1050
                mix_channel[which].fade_length = ms;
slouken@617
  1051
                mix_channel[which].ticks_fade = SDL_GetTicks();
icculus@443
  1052
slouken@617
  1053
                /* only change fade_volume_reset if we're not fading. */
slouken@617
  1054
                if (mix_channel[which].fading == MIX_NO_FADING) {
slouken@617
  1055
                    mix_channel[which].fade_volume_reset = mix_channel[which].volume;
slouken@617
  1056
                }
slouken@617
  1057
                ++status;
slouken@617
  1058
            }
slouken@617
  1059
            SDL_UnlockAudio();
slouken@617
  1060
        }
slouken@617
  1061
    }
slouken@617
  1062
    return(status);
megastep@4
  1063
}
megastep@4
  1064
megastep@7
  1065
/* Halt playing of a particular group of channels */
megastep@7
  1066
int Mix_FadeOutGroup(int tag, int ms)
megastep@7
  1067
{
slouken@617
  1068
    int i;
slouken@617
  1069
    int status = 0;
slouken@617
  1070
    for ( i=0; i<num_channels; ++i ) {
slouken@617
  1071
        if( mix_channel[i].tag == tag ) {
slouken@617
  1072
            status += Mix_FadeOutChannel(i,ms);
slouken@617
  1073
        }
slouken@617
  1074
    }
slouken@617
  1075
    return(status);
megastep@7
  1076
}
megastep@7
  1077
megastep@4
  1078
Mix_Fading Mix_FadingChannel(int which)
megastep@4
  1079
{
slouken@617
  1080
    if ( which < 0 || which >= num_channels ) {
slouken@617
  1081
        return MIX_NO_FADING;
slouken@617
  1082
    }
slouken@617
  1083
    return mix_channel[which].fading;
megastep@4
  1084
}
megastep@4
  1085
slouken@0
  1086
/* Check the status of a specific channel.
slouken@113
  1087
   If the specified mix_channel is -1, check all mix channels.
slouken@0
  1088
*/
slouken@0
  1089
int Mix_Playing(int which)
slouken@0
  1090
{
slouken@617
  1091
    int status;
slouken@0
  1092
slouken@617
  1093
    status = 0;
slouken@617
  1094
    if ( which == -1 ) {
slouken@617
  1095
        int i;
slouken@0
  1096
slouken@617
  1097
        for ( i=0; i<num_channels; ++i ) {
slouken@617
  1098
            if ((mix_channel[i].playing > 0) ||
slouken@617
  1099
                (mix_channel[i].looping > 0))
slouken@617
  1100
            {
slouken@617
  1101
                ++status;
slouken@617
  1102
            }
slouken@617
  1103
        }
slouken@617
  1104
    } else if ( which < num_channels ) {
slouken@617
  1105
        if ( (mix_channel[which].playing > 0) ||
slouken@617
  1106
             (mix_channel[which].looping > 0) )
slouken@617
  1107
        {
slouken@617
  1108
            ++status;
slouken@617
  1109
        }
slouken@617
  1110
    }
slouken@617
  1111
    return(status);
slouken@0
  1112
}
slouken@0
  1113
slouken@92
  1114
/* rcg06072001 Get the chunk associated with a channel. */
slouken@92
  1115
Mix_Chunk *Mix_GetChunk(int channel)
slouken@92
  1116
{
slouken@617
  1117
    Mix_Chunk *retval = NULL;
slouken@92
  1118
slouken@617
  1119
    if ((channel >= 0) && (channel < num_channels)) {
slouken@617
  1120
        retval = mix_channel[channel].chunk;
slouken@617
  1121
    }
slouken@92
  1122
slouken@617
  1123
    return(retval);
slouken@92
  1124
}
slouken@92
  1125
slouken@0
  1126
/* Close the mixer, halting all playing audio */
slouken@0
  1127
void Mix_CloseAudio(void)
slouken@0
  1128
{
slouken@617
  1129
    int i;
slouken@113
  1130
slouken@617
  1131
    if ( audio_opened ) {
slouken@617
  1132
        if ( audio_opened == 1 ) {
slouken@617
  1133
            for (i = 0; i < num_channels; i++) {
slouken@617
  1134
                Mix_UnregisterAllEffects(i);
slouken@617
  1135
            }
slouken@617
  1136
            Mix_UnregisterAllEffects(MIX_CHANNEL_POST);
slouken@617
  1137
            close_music();
slouken@617
  1138
            Mix_HaltChannel(-1);
slouken@617
  1139
            _Mix_DeinitEffects();
slouken@617
  1140
            SDL_CloseAudio();
slouken@617
  1141
            SDL_free(mix_channel);
slouken@617
  1142
            mix_channel = NULL;
icculus@390
  1143
slouken@617
  1144
            /* rcg06042009 report available decoders at runtime. */
slouken@621
  1145
            SDL_free((void *)chunk_decoders);
slouken@617
  1146
            chunk_decoders = NULL;
slouken@617
  1147
            num_decoders = 0;
slouken@617
  1148
        }
slouken@617
  1149
        --audio_opened;
slouken@617
  1150
    }
slouken@0
  1151
}
slouken@0
  1152
slouken@0
  1153
/* Pause a particular channel (or all) */
slouken@0
  1154
void Mix_Pause(int which)
slouken@0
  1155
{
slouken@617
  1156
    Uint32 sdl_ticks = SDL_GetTicks();
slouken@617
  1157
    if ( which == -1 ) {
slouken@617
  1158
        int i;
slouken@0
  1159
slouken@617
  1160
        for ( i=0; i<num_channels; ++i ) {
slouken@617
  1161
            if ( mix_channel[i].playing > 0 ) {
slouken@617
  1162
                mix_channel[i].paused = sdl_ticks;
slouken@617
  1163
            }
slouken@617
  1164
        }
slouken@617
  1165
    } else if ( which < num_channels ) {
slouken@617
  1166
        if ( mix_channel[which].playing > 0 ) {
slouken@617
  1167
            mix_channel[which].paused = sdl_ticks;
slouken@617
  1168
        }
slouken@617
  1169
    }
slouken@0
  1170
}
slouken@0
  1171
slouken@0
  1172
/* Resume a paused channel */
slouken@0
  1173
void Mix_Resume(int which)
slouken@0
  1174
{
slouken@617
  1175
    Uint32 sdl_ticks = SDL_GetTicks();
slouken@172
  1176
slouken@617
  1177
    SDL_LockAudio();
slouken@617
  1178
    if ( which == -1 ) {
slouken@617
  1179
        int i;
slouken@0
  1180
slouken@617
  1181
        for ( i=0; i<num_channels; ++i ) {
slouken@617
  1182
            if ( mix_channel[i].playing > 0 ) {
slouken@617
  1183
                if(mix_channel[i].expire > 0)
slouken@617
  1184
                    mix_channel[i].expire += sdl_ticks - mix_channel[i].paused;
slouken@617
  1185
                mix_channel[i].paused = 0;
slouken@617
  1186
            }
slouken@617
  1187
        }
slouken@617
  1188
    } else if ( which < num_channels ) {
slouken@617
  1189
        if ( mix_channel[which].playing > 0 ) {
slouken@617
  1190
            if(mix_channel[which].expire > 0)
slouken@617
  1191
                mix_channel[which].expire += sdl_ticks - mix_channel[which].paused;
slouken@617
  1192
            mix_channel[which].paused = 0;
slouken@617
  1193
        }
slouken@617
  1194
    }
slouken@617
  1195
    SDL_UnlockAudio();
slouken@0
  1196
}
megastep@7
  1197
megastep@13
  1198
int Mix_Paused(int which)
megastep@13
  1199
{
slouken@617
  1200
    if ( which < 0 ) {
slouken@617
  1201
        int status = 0;
slouken@617
  1202
        int i;
slouken@617
  1203
        for( i=0; i < num_channels; ++i ) {
slouken@617
  1204
            if ( mix_channel[i].paused ) {
slouken@617
  1205
                ++ status;
slouken@617
  1206
            }
slouken@617
  1207
        }
slouken@617
  1208
        return(status);
slouken@617
  1209
    } else if ( which < num_channels ) {
slouken@617
  1210
        return(mix_channel[which].paused != 0);
slouken@617
  1211
    } else {
slouken@617
  1212
        return(0);
slouken@617
  1213
    }
megastep@13
  1214
}
megastep@13
  1215
megastep@7
  1216
/* Change the group of a channel */
megastep@7
  1217
int Mix_GroupChannel(int which, int tag)
megastep@7
  1218
{
slouken@617
  1219
    if ( which < 0 || which > num_channels )
slouken@617
  1220
        return(0);
megastep@7
  1221
slouken@617
  1222
    SDL_LockAudio();
slouken@617
  1223
    mix_channel[which].tag = tag;
slouken@617
  1224
    SDL_UnlockAudio();
slouken@617
  1225
    return(1);
megastep@7
  1226
}
megastep@7
  1227
megastep@7
  1228
/* Assign several consecutive channels to a group */
megastep@7
  1229
int Mix_GroupChannels(int from, int to, int tag)
megastep@7
  1230
{
slouken@617
  1231
    int status = 0;
slouken@617
  1232
    for( ; from <= to; ++ from ) {
slouken@617
  1233
        status += Mix_GroupChannel(from, tag);
slouken@617
  1234
    }
slouken@617
  1235
    return(status);
megastep@7
  1236
}
megastep@7
  1237
megastep@7
  1238
/* Finds the first available channel in a group of channels */
megastep@7
  1239
int Mix_GroupAvailable(int tag)
megastep@7
  1240
{
slouken@617
  1241
    int i;
slouken@617
  1242
    for( i=0; i < num_channels; i ++ ) {
slouken@617
  1243
        if ( ((tag == -1) || (tag == mix_channel[i].tag)) &&
slouken@617
  1244
                            (mix_channel[i].playing <= 0) )
slouken@617
  1245
            return i;
slouken@617
  1246
    }
slouken@617
  1247
    return(-1);
megastep@7
  1248
}
megastep@7
  1249
megastep@7
  1250
int Mix_GroupCount(int tag)
megastep@7
  1251
{
slouken@617
  1252
    int count = 0;
slouken@617
  1253
    int i;
slouken@617
  1254
    for( i=0; i < num_channels; i ++ ) {
slouken@617
  1255
        if ( mix_channel[i].tag==tag || tag==-1 )
slouken@617
  1256
            ++ count;
slouken@617
  1257
    }
slouken@617
  1258
    return(count);
megastep@7
  1259
}
megastep@8
  1260
megastep@8
  1261
/* Finds the "oldest" sample playing in a group of channels */
megastep@8
  1262
int Mix_GroupOldest(int tag)
megastep@8
  1263
{
slouken@617
  1264
    int chan = -1;
slouken@617
  1265
    Uint32 mintime = SDL_GetTicks();
slouken@617
  1266
    int i;
slouken@617
  1267
    for( i=0; i < num_channels; i ++ ) {
slouken@617
  1268
        if ( (mix_channel[i].tag==tag || tag==-1) && mix_channel[i].playing > 0
slouken@617
  1269
             && mix_channel[i].start_time <= mintime ) {
slouken@617
  1270
            mintime = mix_channel[i].start_time;
slouken@617
  1271
            chan = i;
slouken@617
  1272
        }
slouken@617
  1273
    }
slouken@617
  1274
    return(chan);
megastep@8
  1275
}
megastep@8
  1276
megastep@8
  1277
/* Finds the "most recent" (i.e. last) sample playing in a group of channels */
megastep@8
  1278
int Mix_GroupNewer(int tag)
megastep@8
  1279
{
slouken@617
  1280
    int chan = -1;
slouken@617
  1281
    Uint32 maxtime = 0;
slouken@617
  1282
    int i;
slouken@617
  1283
    for( i=0; i < num_channels; i ++ ) {
slouken@617
  1284
        if ( (mix_channel[i].tag==tag || tag==-1) && mix_channel[i].playing > 0
slouken@617
  1285
             && mix_channel[i].start_time >= maxtime ) {
slouken@617
  1286
            maxtime = mix_channel[i].start_time;
slouken@617
  1287
            chan = i;
slouken@617
  1288
        }
slouken@617
  1289
    }
slouken@617
  1290
    return(chan);
megastep@8
  1291
}
slouken@113
  1292
slouken@113
  1293
slouken@113
  1294
slouken@113
  1295
/*
slouken@113
  1296
 * rcg06122001 The special effects exportable API.
slouken@113
  1297
 *  Please see effect_*.c for internally-implemented effects, such
slouken@113
  1298
 *  as Mix_SetPanning().
slouken@113
  1299
 */
slouken@113
  1300
slouken@164
  1301
/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
slouken@113
  1302
static int _Mix_register_effect(effect_info **e, Mix_EffectFunc_t f,
slouken@617
  1303
                Mix_EffectDone_t d, void *arg)
slouken@113
  1304
{
slouken@617
  1305
    effect_info *new_e;
slouken@113
  1306
slouken@617
  1307
    if (!e) {
slouken@617
  1308
        Mix_SetError("Internal error");
slouken@617
  1309
        return(0);
slouken@617
  1310
    }
slouken@113
  1311
slouken@617
  1312
    if (f == NULL) {
slouken@617
  1313
        Mix_SetError("NULL effect callback");
slouken@617
  1314
        return(0);
slouken@617
  1315
    }
slouken@113
  1316
slouken@617
  1317
    new_e = SDL_malloc(sizeof (effect_info));
slouken@617
  1318
    if (new_e == NULL) {
slouken@617
  1319
        Mix_SetError("Out of memory");
slouken@617
  1320
        return(0);
slouken@617
  1321
    }
slouken@113
  1322
slouken@617
  1323
    new_e->callback = f;
slouken@617
  1324
    new_e->done_callback = d;
slouken@617
  1325
    new_e->udata = arg;
slouken@617
  1326
    new_e->next = NULL;
slouken@113
  1327
slouken@617
  1328
    /* add new effect to end of linked list... */
slouken@617
  1329
    if (*e == NULL) {
slouken@617
  1330
        *e = new_e;
slouken@617
  1331
    } else {
slouken@617
  1332
        effect_info *cur = *e;
slouken@617
  1333
        while (1) {
slouken@617
  1334
            if (cur->next == NULL) {
slouken@617
  1335
                cur->next = new_e;
slouken@617
  1336
                break;
slouken@617
  1337
            }
slouken@617
  1338
            cur = cur->next;
slouken@617
  1339
        }
slouken@617
  1340
    }
slouken@113
  1341
slouken@617
  1342
    return(1);
slouken@113
  1343
}
slouken@113
  1344
slouken@113
  1345
slouken@164
  1346
/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
slouken@113
  1347
static int _Mix_remove_effect(int channel, effect_info **e, Mix_EffectFunc_t f)
slouken@113
  1348
{
slouken@617
  1349
    effect_info *cur;
slouken@617
  1350
    effect_info *prev = NULL;
slouken@617
  1351
    effect_info *next = NULL;
slouken@113
  1352
slouken@617
  1353
    if (!e) {
slouken@617
  1354
        Mix_SetError("Internal error");
slouken@617
  1355
        return(0);
slouken@617
  1356
    }
slouken@113
  1357
slouken@617
  1358
    for (cur = *e; cur != NULL; cur = cur->next) {
slouken@617
  1359
        if (cur->callback == f) {
slouken@617
  1360
            next = cur->next;
slouken@617
  1361
            if (cur->done_callback != NULL) {
slouken@617
  1362
                cur->done_callback(channel, cur->udata);
slouken@617
  1363
            }
slouken@617
  1364
            SDL_free(cur);
slouken@113
  1365
slouken@617
  1366
            if (prev == NULL) {   /* removing first item of list? */
slouken@617
  1367
                *e = next;
slouken@617
  1368
            } else {
slouken@617
  1369
                prev->next = next;
slouken@617
  1370
            }
slouken@617
  1371
            return(1);
slouken@617
  1372
        }
slouken@617
  1373
        prev = cur;
slouken@617
  1374
    }
slouken@113
  1375
slouken@617
  1376
    Mix_SetError("No such effect registered");
slouken@617
  1377
    return(0);
slouken@113
  1378
}
slouken@113
  1379
slouken@113
  1380
slouken@164
  1381
/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
slouken@113
  1382
static int _Mix_remove_all_effects(int channel, effect_info **e)
slouken@113
  1383
{
slouken@617
  1384
    effect_info *cur;
slouken@617
  1385
    effect_info *next;
slouken@113
  1386
slouken@617
  1387
    if (!e) {
slouken@617
  1388
        Mix_SetError("Internal error");
slouken@617
  1389
        return(0);
slouken@617
  1390
    }
slouken@113
  1391
slouken@617
  1392
    for (cur = *e; cur != NULL; cur = next) {
slouken@617
  1393
        next = cur->next;
slouken@617
  1394
        if (cur->done_callback != NULL) {
slouken@617
  1395
            cur->done_callback(channel, cur->udata);
slouken@617
  1396
        }
slouken@617
  1397
        SDL_free(cur);
slouken@617
  1398
    }
slouken@617
  1399
    *e = NULL;
slouken@113
  1400
slouken@617
  1401
    return(1);
slouken@113
  1402
}
slouken@113
  1403
slouken@113
  1404
icculus@442
  1405
/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
icculus@442
  1406
int _Mix_RegisterEffect_locked(int channel, Mix_EffectFunc_t f,
slouken@617
  1407
            Mix_EffectDone_t d, void *arg)
slouken@113
  1408
{
slouken@617
  1409
    effect_info **e = NULL;
slouken@113
  1410
slouken@617
  1411
    if (channel == MIX_CHANNEL_POST) {
slouken@617
  1412
        e = &posteffects;
slouken@617
  1413
    } else {
slouken@617
  1414
        if ((channel < 0) || (channel >= num_channels)) {
slouken@617
  1415
            Mix_SetError("Invalid channel number");
slouken@617
  1416
            return(0);
slouken@617
  1417
        }
slouken@617
  1418
        e = &mix_channel[channel].effects;
slouken@617
  1419
    }
slouken@113
  1420
slouken@617
  1421
    return _Mix_register_effect(e, f, d, arg);
slouken@113
  1422
}
slouken@113
  1423
icculus@442
  1424
int Mix_RegisterEffect(int channel, Mix_EffectFunc_t f,
slouken@617
  1425
            Mix_EffectDone_t d, void *arg)
icculus@442
  1426
{
icculus@442
  1427
    int retval;
slouken@617
  1428
    SDL_LockAudio();
slouken@617
  1429
    retval = _Mix_RegisterEffect_locked(channel, f, d, arg);
slouken@617
  1430
    SDL_UnlockAudio();
icculus@442
  1431
    return retval;
icculus@442
  1432
}
slouken@113
  1433
icculus@442
  1434
icculus@442
  1435
/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
icculus@442
  1436
int _Mix_UnregisterEffect_locked(int channel, Mix_EffectFunc_t f)
slouken@113
  1437
{
slouken@617
  1438
    effect_info **e = NULL;
slouken@113
  1439
slouken@617
  1440
    if (channel == MIX_CHANNEL_POST) {
slouken@617
  1441
        e = &posteffects;
slouken@617
  1442
    } else {
slouken@617
  1443
        if ((channel < 0) || (channel >= num_channels)) {
slouken@617
  1444
            Mix_SetError("Invalid channel number");
slouken@617
  1445
            return(0);
slouken@617
  1446
        }
slouken@617
  1447
        e = &mix_channel[channel].effects;
slouken@617
  1448
    }
slouken@113
  1449
slouken@617
  1450
    return _Mix_remove_effect(channel, e, f);
icculus@442
  1451
}
icculus@442
  1452
icculus@442
  1453
int Mix_UnregisterEffect(int channel, Mix_EffectFunc_t f)
icculus@442
  1454
{
slouken@617
  1455
    int retval;
slouken@617
  1456
    SDL_LockAudio();
slouken@617
  1457
    retval = _Mix_UnregisterEffect_locked(channel, f);
slouken@617
  1458
    SDL_UnlockAudio();
slouken@617
  1459
    return(retval);
slouken@164
  1460
}
slouken@164
  1461
icculus@442
  1462
/* MAKE SURE you hold the audio lock (SDL_LockAudio()) before calling this! */
icculus@442
  1463
int _Mix_UnregisterAllEffects_locked(int channel)
slouken@164
  1464
{
slouken@617
  1465
    effect_info **e = NULL;
slouken@164
  1466
slouken@617
  1467
    if (channel == MIX_CHANNEL_POST) {
slouken@617
  1468
        e = &posteffects;
slouken@617
  1469
    } else {
slouken@617
  1470
        if ((channel < 0) || (channel >= num_channels)) {
slouken@617
  1471
            Mix_SetError("Invalid channel number");
slouken@617
  1472
            return(0);
slouken@617
  1473
        }
slouken@617
  1474
        e = &mix_channel[channel].effects;
slouken@617
  1475
    }
slouken@164
  1476
slouken@617
  1477
    return _Mix_remove_all_effects(channel, e);
icculus@442
  1478
}
icculus@442
  1479
icculus@442
  1480
int Mix_UnregisterAllEffects(int channel)
icculus@442
  1481
{
slouken@617
  1482
    int retval;
slouken@617
  1483
    SDL_LockAudio();
slouken@617
  1484
    retval = _Mix_UnregisterAllEffects_locked(channel);
slouken@617
  1485
    SDL_UnlockAudio();
slouken@617
  1486
    return(retval);
slouken@113
  1487
}
slouken@113
  1488
slouken@113
  1489
/* end of mixer.c ... */
slouken@113
  1490