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