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