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