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