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