mixer.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 13 Oct 2013 20:03:03 -0700
changeset 665 8b25e64c7ac7
parent 662 fd5af79fac03
child 694 59ef72d7586d
permissions -rw-r--r--
Fixed bug 2142 - missing include of dynamic_fluidsynth.h in mixer.c

manuel.montezelo

I think that mixer is not including dynamic_fluidsynth.h in this block:

#include "SDL_mixer.h"
#include "load_aiff.h"
#include "load_voc.h"
#include "load_ogg.h"
#include "load_flac.h"
#include "dynamic_flac.h"
#include "dynamic_modplug.h"
#include "dynamic_mod.h"
#include "dynamic_mp3.h"
#include "dynamic_ogg.h"

So the compiler then generates these warnings (as spotted in [1]):

[1] http://qa.debian.org/bls/packages/l/libsdl2-mixer.html

W implicit-declaration mixer.c:154
W implicit-declaration mixer.c:215

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