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