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