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