mixer.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 16 Oct 2017 02:14:18 -0700
changeset 771 fa081ec2940e
parent 766 1e215d1f9492
child 777 92882ef2ab81
permissions -rw-r--r--
mixer.c (mix_channels): fix misplaced SDLCALL so that it compiles.

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