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