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