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