mixer.c
author Ozkan Sezer
Sat, 14 Dec 2019 18:14:10 +0300
branchSDL-1.2
changeset 1081 119300487b73
parent 943 b57842dc6349
child 1136 33a3817d1066
permissions -rw-r--r--
backport default/2.0 commits 6c6adcc and 1e215d1 for fluidsynth support:

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