mixer.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 23 Aug 2014 10:57:26 -0700
changeset 696 9599bb0ff844
parent 694 59ef72d7586d
child 710 1aca2b6d570f
permissions -rw-r--r--
Fixed bug 2690 - Floating point exception in Mix_Volume()

Francisco de la Peña

This happens rarely under uncertain circunstances, as it doesn't crash always and does it randomly. Fortunately, I've got a backtrace when running a GDB session. Might be a divide by zero issue in SDL or SDL_mixer.

Program received signal SIGFPE, Arithmetic exception.
0x00000038e8c08a7d in mix_channels (udata=<optimized out>, stream=0x24e11b0 "", len=2048) at mixer.c:345
345 Mix_Volume(i, (mix_channel[i].fade_volume * ticks) / mix_channel[i].fade_length );
(gdb) bt f
#0 0x00000038e8c08a7d in mix_channels (udata=<optimized out>, stream=0x24e11b0 "", len=2048) at mixer.c:345
ticks = 0
mix_input = <optimized out>
i = 0
mixable = <optimized out>
volume = <optimized out>
sdl_ticks = 129373

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