mixer.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 17 Oct 2017 02:33:47 -0700
changeset 777 92882ef2ab81
parent 771 fa081ec2940e
child 782 e7d3a8f73e88
permissions -rw-r--r--
Rewrote music.c to support any number of decode libraries using a compiled-in plugin interface
Mix_LoadWAV_RW() can now load sound formats that were previously available only as music.

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