mixer.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 15 Jul 2007 15:43:02 +0000
changeset 359 83eaf245b7e7
parent 356 dae4b083e3ef
child 360 61cddad42a8a
permissions -rw-r--r--
On shutdown, deallocate global memory we might have allocated for effects.

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