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