mixer.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 24 May 2017 16:41:47 -0400
changeset 727 119df1a05eb7
parent 725 bdf7b8d20566
child 738 e6fa426a79dd
permissions -rw-r--r--
Initialize the SDL audio subsystem if necessary.

SDL_OpenAudio() did this for us (legacy 1.2 behavior), but
SDL_OpenAudioDevice() doesn't, leaving us to manage it.

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