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