mixer.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 11 Oct 2017 18:36:53 -0400
changeset 738 e6fa426a79dd
parent 727 119df1a05eb7
child 757 420f3b37dc95
permissions -rw-r--r--
Don't allow device format changes in Mix_OpenAudio().

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