mixer.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 08 Jul 2007 05:56:26 +0000
changeset 345 2c24b688dc3b
parent 338 eb910f60045b
child 347 33d5f49ed91e
permissions -rw-r--r--
Update SDL_mixer for newer versions of SDL
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
slouken@111
    38
/* Magic numbers for various audio file formats */
slouken@117
    39
#define RIFF		0x46464952		/* "RIFF" */
slouken@111
    40
#define WAVE		0x45564157		/* "WAVE" */
slouken@111
    41
#define FORM		0x4d524f46		/* "FORM" */
slouken@190
    42
#define OGGS		0x5367674f		/* "OggS" */
icculus@191
    43
#define CREA	    	0x61657243		/* "Crea" */
slouken@0
    44
slouken@0
    45
static int audio_opened = 0;
slouken@0
    46
static SDL_AudioSpec mixer;
slouken@113
    47
slouken@113
    48
typedef struct _Mix_effectinfo
slouken@113
    49
{
slouken@113
    50
	Mix_EffectFunc_t callback;
slouken@113
    51
	Mix_EffectDone_t done_callback;
slouken@113
    52
	void *udata;
slouken@113
    53
	struct _Mix_effectinfo *next;
slouken@113
    54
} effect_info;
slouken@113
    55
slouken@0
    56
static struct _Mix_Channel {
slouken@0
    57
	Mix_Chunk *chunk;
slouken@0
    58
	int playing;
slouken@0
    59
	int paused;
slouken@0
    60
	Uint8 *samples;
slouken@0
    61
	int volume;
slouken@0
    62
	int looping;
megastep@7
    63
	int tag;
slouken@42
    64
	Uint32 expire;
megastep@8
    65
	Uint32 start_time;
megastep@4
    66
	Mix_Fading fading;
megastep@4
    67
	int fade_volume;
slouken@42
    68
	Uint32 fade_length;
megastep@4
    69
	Uint32 ticks_fade;
slouken@113
    70
	effect_info *effects;
slouken@60
    71
} *mix_channel = NULL;
slouken@113
    72
slouken@113
    73
static effect_info *posteffects = NULL;
slouken@113
    74
megastep@7
    75
static int num_channels;
slouken@17
    76
static int reserved_channels = 0;
slouken@0
    77
slouken@0
    78
slouken@26
    79
/* Support for hooking into the mixer callback system */
slouken@26
    80
static void (*mix_postmix)(void *udata, Uint8 *stream, int len) = NULL;
slouken@29
    81
static void *mix_postmix_data = NULL;
slouken@26
    82
slouken@92
    83
/* rcg07062001 callback to alert when channels are done playing. */
slouken@92
    84
static void (*channel_done_callback)(int channel) = NULL;
slouken@92
    85
slouken@42
    86
/* Music function declarations */
slouken@42
    87
extern int open_music(SDL_AudioSpec *mixer);
slouken@42
    88
extern void close_music(void);
slouken@42
    89
slouken@0
    90
/* Support for user defined music functions, plus the default one */
slouken@42
    91
extern int volatile music_active;
slouken@0
    92
extern void music_mixer(void *udata, Uint8 *stream, int len);
slouken@0
    93
static void (*mix_music)(void *udata, Uint8 *stream, int len) = music_mixer;
slouken@42
    94
static void *music_data = NULL;
slouken@0
    95
slouken@113
    96
slouken@113
    97
/* rcg06192001 get linked library's version. */
slouken@113
    98
const SDL_version *Mix_Linked_Version(void)
slouken@113
    99
{
slouken@189
   100
	static SDL_version linked_version;
slouken@227
   101
	SDL_MIXER_VERSION(&linked_version);
slouken@189
   102
	return(&linked_version);
slouken@113
   103
}
slouken@113
   104
slouken@164
   105
static int _Mix_remove_all_effects(int channel, effect_info **e);
slouken@113
   106
slouken@172
   107
/*
slouken@172
   108
 * rcg06122001 Cleanup effect callbacks.
slouken@172
   109
 *  MAKE SURE SDL_LockAudio() is called before this (or you're in the
slouken@172
   110
 *   audio callback).
slouken@172
   111
 */
slouken@172
   112
static void _Mix_channel_done_playing(int channel)
slouken@113
   113
{
slouken@113
   114
	if (channel_done_callback) {
slouken@113
   115
	    channel_done_callback(channel);
slouken@113
   116
	}
slouken@113
   117
slouken@164
   118
	/*
slouken@172
   119
	 * Call internal function directly, to avoid locking audio from
slouken@172
   120
	 *   inside audio callback.
slouken@172
   121
	 */
slouken@164
   122
	_Mix_remove_all_effects(channel, &mix_channel[channel].effects);
slouken@113
   123
}
slouken@113
   124
slouken@113
   125
slouken@113
   126
static void *Mix_DoEffects(int chan, void *snd, int len)
slouken@113
   127
{
slouken@113
   128
	int posteffect = (chan == MIX_CHANNEL_POST);
slouken@113
   129
	effect_info *e = ((posteffect) ? posteffects : mix_channel[chan].effects);
slouken@113
   130
	void *buf = snd;
slouken@113
   131
slouken@113
   132
	if (e != NULL) {    /* are there any registered effects? */
slouken@113
   133
		/* if this is the postmix, we can just overwrite the original. */
slouken@113
   134
		if (!posteffect) {
slouken@113
   135
			buf = malloc(len);
slouken@113
   136
			if (buf == NULL) {
slouken@113
   137
				return(snd);
slouken@113
   138
			}
slouken@113
   139
		    memcpy(buf, snd, len);
slouken@113
   140
		}
slouken@113
   141
slouken@113
   142
		for (; e != NULL; e = e->next) {
slouken@113
   143
			if (e->callback != NULL) {
slouken@113
   144
				e->callback(chan, buf, len, e->udata);
slouken@113
   145
			}
slouken@113
   146
		}
slouken@113
   147
	}
slouken@113
   148
slouken@113
   149
	/* be sure to free() the return value if != snd ... */
slouken@113
   150
	return(buf);
slouken@113
   151
}
slouken@113
   152
slouken@113
   153
slouken@0
   154
/* Mixing function */
slouken@0
   155
static void mix_channels(void *udata, Uint8 *stream, int len)
slouken@0
   156
{
slouken@113
   157
	Uint8 *mix_input;
slouken@0
   158
	int i, mixable, volume;
megastep@7
   159
	Uint32 sdl_ticks;
slouken@0
   160
slouken@345
   161
	/* Initialize the audio stream (needed for 1.2.12 and later)
slouken@345
   162
	memset(stream, 0, len);
slouken@345
   163
slouken@15
   164
	/* Mix the music (must be done before the channels are added) */
slouken@19
   165
	if ( music_active || (mix_music != music_mixer) ) {
slouken@15
   166
		mix_music(music_data, stream, len);
slouken@15
   167
	}
slouken@15
   168
slouken@172
   169
	/* Mix any playing channels... */
megastep@7
   170
	sdl_ticks = SDL_GetTicks();
megastep@7
   171
	for ( i=0; i<num_channels; ++i ) {
slouken@60
   172
		if( ! mix_channel[i].paused ) {
slouken@60
   173
			if ( mix_channel[i].expire > 0 && mix_channel[i].expire < sdl_ticks ) {
megastep@8
   174
				/* Expiration delay for that channel is reached */
slouken@60
   175
				mix_channel[i].playing = 0;
slouken@60
   176
				mix_channel[i].fading = MIX_NO_FADING;
slouken@60
   177
				mix_channel[i].expire = 0;
slouken@219
   178
				_Mix_channel_done_playing(i);
slouken@60
   179
			} else if ( mix_channel[i].fading != MIX_NO_FADING ) {
slouken@60
   180
				Uint32 ticks = sdl_ticks - mix_channel[i].ticks_fade;
slouken@60
   181
				if( ticks > mix_channel[i].fade_length ) {
slouken@60
   182
					if( mix_channel[i].fading == MIX_FADING_OUT ) {
slouken@60
   183
						mix_channel[i].playing = 0;
slouken@60
   184
						mix_channel[i].expire = 0;
icculus@184
   185
						Mix_Volume(i, mix_channel[i].fade_volume); /* Restore the volume */
slouken@219
   186
						_Mix_channel_done_playing(i);
megastep@8
   187
					}
slouken@60
   188
					mix_channel[i].fading = MIX_NO_FADING;
megastep@4
   189
				} else {
slouken@60
   190
					if( mix_channel[i].fading == MIX_FADING_OUT ) {
slouken@60
   191
						Mix_Volume(i, (mix_channel[i].fade_volume * (mix_channel[i].fade_length-ticks))
slouken@60
   192
								   / mix_channel[i].fade_length );
megastep@8
   193
					} else {
slouken@60
   194
						Mix_Volume(i, (mix_channel[i].fade_volume * ticks) / mix_channel[i].fade_length );
megastep@8
   195
					}
megastep@4
   196
				}
megastep@4
   197
			}
slouken@60
   198
			if ( mix_channel[i].playing > 0 ) {
slouken@188
   199
				int index = 0;
slouken@188
   200
				int remaining = len;
slouken@188
   201
				while (mix_channel[i].playing > 0 && index < len) {
slouken@188
   202
					remaining = len - index;
slouken@188
   203
					volume = (mix_channel[i].volume*mix_channel[i].chunk->volume) / MIX_MAX_VOLUME;
slouken@188
   204
					mixable = mix_channel[i].playing;
slouken@188
   205
					if ( mixable > remaining ) {
slouken@188
   206
						mixable = remaining;
slouken@188
   207
					}
slouken@188
   208
slouken@188
   209
					mix_input = Mix_DoEffects(i, mix_channel[i].samples, mixable);
slouken@188
   210
					SDL_MixAudio(stream+index,mix_input,mixable,volume);
slouken@188
   211
					if (mix_input != mix_channel[i].samples)
slouken@188
   212
						free(mix_input);
slouken@188
   213
slouken@188
   214
					mix_channel[i].samples += mixable;
slouken@188
   215
					mix_channel[i].playing -= mixable;
slouken@188
   216
					index += mixable;
slouken@188
   217
slouken@188
   218
					/* rcg06072001 Alert app if channel is done playing. */
slouken@188
   219
					if (!mix_channel[i].playing && !mix_channel[i].looping) {
slouken@188
   220
						_Mix_channel_done_playing(i);
slouken@188
   221
					}
slouken@45
   222
				}
slouken@113
   223
megastep@21
   224
				/* If looping the sample and we are at its end, make sure
megastep@21
   225
				   we will still return a full buffer */
slouken@188
   226
				while ( mix_channel[i].looping && index < len ) {
slouken@60
   227
					int alen = mix_channel[i].chunk->alen;
slouken@188
   228
					remaining = len - index;
slouken@45
   229
				    	if (remaining > alen) {
slouken@45
   230
						remaining = alen;
slouken@45
   231
				    	}
slouken@113
   232
slouken@113
   233
					mix_input = Mix_DoEffects(i, mix_channel[i].chunk->abuf, remaining);
slouken@188
   234
					SDL_MixAudio(stream+index, mix_input, remaining, volume);
slouken@113
   235
					if (mix_input != mix_channel[i].chunk->abuf)
slouken@113
   236
						free(mix_input);
slouken@113
   237
slouken@60
   238
					--mix_channel[i].looping;
slouken@60
   239
					mix_channel[i].samples = mix_channel[i].chunk->abuf + remaining;
slouken@60
   240
					mix_channel[i].playing = mix_channel[i].chunk->alen - remaining;
slouken@188
   241
					index += remaining;
slouken@45
   242
				}
slouken@60
   243
				if ( ! mix_channel[i].playing && mix_channel[i].looping ) {
slouken@60
   244
					if ( --mix_channel[i].looping ) {
slouken@60
   245
						mix_channel[i].samples = mix_channel[i].chunk->abuf;
slouken@60
   246
						mix_channel[i].playing = mix_channel[i].chunk->alen;
megastep@8
   247
					}
slouken@0
   248
				}
slouken@92
   249
slouken@0
   250
			}
slouken@0
   251
		}
slouken@0
   252
	}
slouken@113
   253
slouken@113
   254
	/* rcg06122001 run posteffects... */
slouken@113
   255
	Mix_DoEffects(MIX_CHANNEL_POST, stream, len);
slouken@113
   256
slouken@26
   257
	if ( mix_postmix ) {
slouken@26
   258
		mix_postmix(mix_postmix_data, stream, len);
slouken@26
   259
	}
slouken@0
   260
}
slouken@0
   261
slouken@0
   262
static void PrintFormat(char *title, SDL_AudioSpec *fmt)
slouken@0
   263
{
slouken@0
   264
	printf("%s: %d bit %s audio (%s) at %u Hz\n", title, (fmt->format&0xFF),
slouken@0
   265
			(fmt->format&0x8000) ? "signed" : "unsigned",
slouken@245
   266
			(fmt->channels > 2) ? "surround" :
slouken@0
   267
			(fmt->channels > 1) ? "stereo" : "mono", fmt->freq);
slouken@0
   268
}
slouken@0
   269
slouken@113
   270
slouken@113
   271
void _Mix_InitEffects(void);
slouken@113
   272
slouken@0
   273
/* Open the mixer with a certain desired audio format */
slouken@60
   274
int Mix_OpenAudio(int frequency, Uint16 format, int nchannels, int chunksize)
slouken@0
   275
{
slouken@0
   276
	int i;
slouken@0
   277
	SDL_AudioSpec desired;
slouken@0
   278
slouken@0
   279
	/* If the mixer is already opened, increment open count */
slouken@0
   280
	if ( audio_opened ) {
slouken@300
   281
		if ( format == mixer.format && nchannels == mixer.channels ) {
slouken@300
   282
	    	++audio_opened;
slouken@300
   283
	    	return(0);
slouken@300
   284
		}
slouken@300
   285
		while ( audio_opened ) {
slouken@300
   286
			Mix_CloseAudio();
slouken@300
   287
		}
slouken@0
   288
	}
slouken@0
   289
slouken@0
   290
	/* Set the desired format and frequency */
slouken@0
   291
	desired.freq = frequency;
slouken@0
   292
	desired.format = format;
slouken@60
   293
	desired.channels = nchannels;
slouken@0
   294
	desired.samples = chunksize;
slouken@0
   295
	desired.callback = mix_channels;
slouken@0
   296
	desired.userdata = NULL;
slouken@0
   297
slouken@0
   298
	/* Accept nearly any audio format */
slouken@0
   299
	if ( SDL_OpenAudio(&desired, &mixer) < 0 ) {
slouken@0
   300
		return(-1);
slouken@0
   301
	}
slouken@0
   302
#if 0
slouken@0
   303
	PrintFormat("Audio device", &mixer);
slouken@0
   304
#endif
slouken@0
   305
slouken@0
   306
	/* Initialize the music players */
slouken@0
   307
	if ( open_music(&mixer) < 0 ) {
slouken@0
   308
		SDL_CloseAudio();
slouken@0
   309
		return(-1);
slouken@0
   310
	}
megastep@7
   311
megastep@7
   312
	num_channels = MIX_CHANNELS;
slouken@60
   313
	mix_channel = (struct _Mix_Channel *) malloc(num_channels * sizeof(struct _Mix_Channel));
megastep@7
   314
slouken@0
   315
	/* Clear out the audio channels */
megastep@7
   316
	for ( i=0; i<num_channels; ++i ) {
slouken@60
   317
		mix_channel[i].chunk = NULL;
slouken@60
   318
		mix_channel[i].playing = 0;
slouken@60
   319
		mix_channel[i].looping = 0;
slouken@60
   320
		mix_channel[i].volume = SDL_MIX_MAXVOLUME;
slouken@183
   321
		mix_channel[i].fade_volume = SDL_MIX_MAXVOLUME;
slouken@183
   322
		mix_channel[i].fading = MIX_NO_FADING;
slouken@60
   323
		mix_channel[i].tag = -1;
slouken@60
   324
		mix_channel[i].expire = 0;
slouken@113
   325
		mix_channel[i].effects = NULL;
slouken@198
   326
		mix_channel[i].paused = 0;
slouken@0
   327
	}
slouken@0
   328
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@113
   329
slouken@113
   330
	_Mix_InitEffects();
slouken@113
   331
slouken@0
   332
	audio_opened = 1;
slouken@0
   333
	SDL_PauseAudio(0);
slouken@0
   334
	return(0);
slouken@0
   335
}
slouken@0
   336
megastep@7
   337
/* Dynamically change the number of channels managed by the mixer.
megastep@7
   338
   If decreasing the number of channels, the upper channels are
megastep@7
   339
   stopped.
megastep@7
   340
 */
megastep@7
   341
int Mix_AllocateChannels(int numchans)
megastep@7
   342
{
megastep@7
   343
	if ( numchans<0 || numchans==num_channels )
megastep@7
   344
		return(num_channels);
megastep@7
   345
megastep@7
   346
	if ( numchans < num_channels ) {
megastep@7
   347
		/* Stop the affected channels */
megastep@7
   348
		int i;
megastep@7
   349
		for(i=numchans; i < num_channels; i++) {
megastep@7
   350
			Mix_HaltChannel(i);
megastep@7
   351
		}
megastep@7
   352
	}
slouken@172
   353
	SDL_LockAudio();
slouken@60
   354
	mix_channel = (struct _Mix_Channel *) realloc(mix_channel, numchans * sizeof(struct _Mix_Channel));
megastep@7
   355
	if ( numchans > num_channels ) {
megastep@7
   356
		/* Initialize the new channels */
megastep@7
   357
		int i;
megastep@7
   358
		for(i=num_channels; i < numchans; i++) {
slouken@60
   359
			mix_channel[i].chunk = NULL;
slouken@60
   360
			mix_channel[i].playing = 0;
slouken@60
   361
			mix_channel[i].looping = 0;
slouken@60
   362
			mix_channel[i].volume = SDL_MIX_MAXVOLUME;
slouken@183
   363
			mix_channel[i].fade_volume = SDL_MIX_MAXVOLUME;
slouken@183
   364
			mix_channel[i].fading = MIX_NO_FADING;
slouken@60
   365
			mix_channel[i].tag = -1;
slouken@60
   366
			mix_channel[i].expire = 0;
slouken@113
   367
			mix_channel[i].effects = NULL;
slouken@198
   368
			mix_channel[i].paused = 0;
megastep@7
   369
		}
megastep@7
   370
	}
megastep@7
   371
	num_channels = numchans;
slouken@172
   372
	SDL_UnlockAudio();
megastep@7
   373
	return(num_channels);
megastep@7
   374
}
megastep@7
   375
slouken@0
   376
/* Return the actual mixer parameters */
slouken@0
   377
int Mix_QuerySpec(int *frequency, Uint16 *format, int *channels)
slouken@0
   378
{
slouken@0
   379
	if ( audio_opened ) {
slouken@0
   380
		if ( frequency ) {
slouken@0
   381
			*frequency = mixer.freq;
slouken@0
   382
		}
slouken@0
   383
		if ( format ) {
slouken@0
   384
			*format = mixer.format;
slouken@0
   385
		}
slouken@79
   386
		if ( channels ) {
slouken@0
   387
			*channels = mixer.channels;
slouken@0
   388
		}
slouken@0
   389
	}
slouken@0
   390
	return(audio_opened);
slouken@0
   391
}
slouken@0
   392
slouken@91
   393
slouken@91
   394
/*
slouken@91
   395
 * !!! FIXME: Ideally, we want a Mix_LoadSample_RW(), which will handle the
slouken@91
   396
 *             generic setup, then call the correct file format loader.
slouken@91
   397
 */
slouken@91
   398
slouken@0
   399
/* Load a wave file */
slouken@0
   400
Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
slouken@0
   401
{
slouken@111
   402
	Uint32 magic;
slouken@0
   403
	Mix_Chunk *chunk;
slouken@111
   404
	SDL_AudioSpec wavespec, *loaded;
slouken@0
   405
	SDL_AudioCVT wavecvt;
slouken@41
   406
	int samplesize;
slouken@0
   407
slouken@91
   408
	/* rcg06012001 Make sure src is valid */
slouken@91
   409
	if ( ! src ) {
slouken@111
   410
		SDL_SetError("Mix_LoadWAV_RW with NULL src");
slouken@91
   411
		return(NULL);
slouken@91
   412
	}
slouken@91
   413
slouken@0
   414
	/* Make sure audio has been opened */
slouken@0
   415
	if ( ! audio_opened ) {
slouken@0
   416
		SDL_SetError("Audio device hasn't been opened");
slouken@89
   417
		if ( freesrc && src ) {
slouken@0
   418
			SDL_RWclose(src);
slouken@0
   419
		}
slouken@0
   420
		return(NULL);
slouken@0
   421
	}
slouken@0
   422
slouken@0
   423
	/* Allocate the chunk memory */
slouken@0
   424
	chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk));
slouken@0
   425
	if ( chunk == NULL ) {
slouken@0
   426
		SDL_SetError("Out of memory");
slouken@0
   427
		if ( freesrc ) {
slouken@0
   428
			SDL_RWclose(src);
slouken@0
   429
		}
slouken@0
   430
		return(NULL);
slouken@0
   431
	}
slouken@0
   432
slouken@111
   433
	/* Find out what kind of audio file this is */
slouken@111
   434
	magic = SDL_ReadLE32(src);
slouken@111
   435
	/* Seek backwards for compatibility with older loaders */
slouken@146
   436
	SDL_RWseek(src, -(int)sizeof(Uint32), SEEK_CUR);
slouken@111
   437
slouken@111
   438
	switch (magic) {
slouken@111
   439
		case WAVE:
slouken@117
   440
		case RIFF:
slouken@111
   441
			loaded = SDL_LoadWAV_RW(src, freesrc, &wavespec,
slouken@111
   442
					(Uint8 **)&chunk->abuf, &chunk->alen);
slouken@111
   443
			break;
slouken@111
   444
		case FORM:
slouken@111
   445
			loaded = Mix_LoadAIFF_RW(src, freesrc, &wavespec,
slouken@111
   446
					(Uint8 **)&chunk->abuf, &chunk->alen);
slouken@111
   447
			break;
slouken@190
   448
#ifdef OGG_MUSIC
slouken@190
   449
		case OGGS:
slouken@190
   450
			loaded = Mix_LoadOGG_RW(src, freesrc, &wavespec,
slouken@190
   451
					(Uint8 **)&chunk->abuf, &chunk->alen);
slouken@190
   452
			break;
slouken@190
   453
#endif
icculus@191
   454
		case CREA:
slouken@111
   455
			loaded = Mix_LoadVOC_RW(src, freesrc, &wavespec,
slouken@111
   456
					(Uint8 **)&chunk->abuf, &chunk->alen);
slouken@111
   457
			break;
icculus@191
   458
		default:
icculus@191
   459
			SDL_SetError("Unrecognized sound file type");
icculus@191
   460
			return(0);			
slouken@111
   461
	}
slouken@111
   462
	if ( !loaded ) {
slouken@0
   463
		free(chunk);
slouken@0
   464
		return(NULL);
slouken@0
   465
	}
slouken@91
   466
slouken@0
   467
#if 0
slouken@0
   468
	PrintFormat("Audio device", &mixer);
slouken@0
   469
	PrintFormat("-- Wave file", &wavespec);
slouken@0
   470
#endif
slouken@0
   471
slouken@0
   472
	/* Build the audio converter and create conversion buffers */
slouken@0
   473
	if ( SDL_BuildAudioCVT(&wavecvt,
slouken@0
   474
			wavespec.format, wavespec.channels, wavespec.freq,
slouken@0
   475
			mixer.format, mixer.channels, mixer.freq) < 0 ) {
slouken@0
   476
		SDL_FreeWAV(chunk->abuf);
slouken@0
   477
		free(chunk);
slouken@0
   478
		return(NULL);
slouken@0
   479
	}
slouken@41
   480
	samplesize = ((wavespec.format & 0xFF)/8)*wavespec.channels;
slouken@41
   481
	wavecvt.len = chunk->alen & ~(samplesize-1);
slouken@0
   482
	wavecvt.buf = (Uint8 *)malloc(wavecvt.len*wavecvt.len_mult);
slouken@0
   483
	if ( wavecvt.buf == NULL ) {
slouken@0
   484
		SDL_SetError("Out of memory");
slouken@0
   485
		SDL_FreeWAV(chunk->abuf);
slouken@0
   486
		free(chunk);
slouken@0
   487
		return(NULL);
slouken@0
   488
	}
slouken@0
   489
	memcpy(wavecvt.buf, chunk->abuf, chunk->alen);
slouken@0
   490
	SDL_FreeWAV(chunk->abuf);
slouken@0
   491
slouken@0
   492
	/* Run the audio converter */
slouken@0
   493
	if ( SDL_ConvertAudio(&wavecvt) < 0 ) {
slouken@0
   494
		free(wavecvt.buf);
slouken@0
   495
		free(chunk);
slouken@0
   496
		return(NULL);
slouken@0
   497
	}
slouken@1
   498
	chunk->allocated = 1;
slouken@0
   499
	chunk->abuf = wavecvt.buf;
slouken@0
   500
	chunk->alen = wavecvt.len_cvt;
slouken@0
   501
	chunk->volume = MIX_MAX_VOLUME;
slouken@0
   502
	return(chunk);
slouken@0
   503
}
slouken@0
   504
slouken@1
   505
/* Load a wave file of the mixer format from a memory buffer */
slouken@1
   506
Mix_Chunk *Mix_QuickLoad_WAV(Uint8 *mem)
slouken@1
   507
{
slouken@1
   508
	Mix_Chunk *chunk;
slouken@1
   509
	Uint8 magic[4];
slouken@1
   510
slouken@1
   511
	/* Make sure audio has been opened */
slouken@1
   512
	if ( ! audio_opened ) {
slouken@1
   513
		SDL_SetError("Audio device hasn't been opened");
slouken@1
   514
		return(NULL);
slouken@1
   515
	}
slouken@1
   516
slouken@1
   517
	/* Allocate the chunk memory */
patmandin@338
   518
	chunk = (Mix_Chunk *)calloc(1,sizeof(Mix_Chunk));
slouken@1
   519
	if ( chunk == NULL ) {
slouken@1
   520
		SDL_SetError("Out of memory");
slouken@1
   521
		return(NULL);
slouken@1
   522
	}
slouken@1
   523
slouken@1
   524
	/* Essentially just skip to the audio data (no error checking - fast) */
slouken@1
   525
	chunk->allocated = 0;
slouken@1
   526
	mem += 12; /* WAV header */
slouken@1
   527
	do {
slouken@1
   528
		memcpy(magic, mem, 4);
slouken@1
   529
		mem += 4;
slouken@1
   530
		chunk->alen = ((mem[3]<<24)|(mem[2]<<16)|(mem[1]<<8)|(mem[0]));
slouken@1
   531
		mem += 4;
slouken@1
   532
		chunk->abuf = mem;
slouken@1
   533
		mem += chunk->alen;
slouken@1
   534
	} while ( memcmp(magic, "data", 4) != 0 );
slouken@1
   535
	chunk->volume = MIX_MAX_VOLUME;
slouken@1
   536
slouken@1
   537
	return(chunk);
slouken@1
   538
}
slouken@1
   539
slouken@174
   540
/* Load raw audio data of the mixer format from a memory buffer */
slouken@174
   541
Mix_Chunk *Mix_QuickLoad_RAW(Uint8 *mem, Uint32 len)
slouken@174
   542
{
slouken@174
   543
	Mix_Chunk *chunk;
slouken@174
   544
slouken@174
   545
	/* Make sure audio has been opened */
slouken@174
   546
	if ( ! audio_opened ) {
slouken@174
   547
		SDL_SetError("Audio device hasn't been opened");
slouken@174
   548
		return(NULL);
slouken@174
   549
	}
slouken@174
   550
slouken@174
   551
	/* Allocate the chunk memory */
slouken@174
   552
	chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk));
slouken@174
   553
	if ( chunk == NULL ) {
slouken@174
   554
		SDL_SetError("Out of memory");
slouken@174
   555
		return(NULL);
slouken@174
   556
	}
slouken@174
   557
slouken@174
   558
	/* Essentially just point at the audio data (no error checking - fast) */
slouken@174
   559
	chunk->allocated = 0;
slouken@174
   560
	chunk->alen = len;
slouken@174
   561
	chunk->abuf = mem;
slouken@174
   562
	chunk->volume = MIX_MAX_VOLUME;
slouken@174
   563
slouken@174
   564
	return(chunk);
slouken@174
   565
}
slouken@174
   566
slouken@0
   567
/* Free an audio chunk previously loaded */
slouken@0
   568
void Mix_FreeChunk(Mix_Chunk *chunk)
slouken@0
   569
{
slouken@0
   570
	int i;
slouken@0
   571
slouken@0
   572
	/* Caution -- if the chunk is playing, the mixer will crash */
slouken@0
   573
	if ( chunk ) {
slouken@0
   574
		/* Guarantee that this chunk isn't playing */
slouken@172
   575
		SDL_LockAudio();
slouken@60
   576
		if ( mix_channel ) {
slouken@60
   577
			for ( i=0; i<num_channels; ++i ) {
slouken@60
   578
				if ( chunk == mix_channel[i].chunk ) {
slouken@60
   579
					mix_channel[i].playing = 0;
slouken@60
   580
				}
slouken@0
   581
			}
slouken@0
   582
		}
slouken@172
   583
		SDL_UnlockAudio();
slouken@0
   584
		/* Actually free the chunk */
slouken@1
   585
		if ( chunk->allocated ) {
slouken@1
   586
			free(chunk->abuf);
slouken@1
   587
		}
slouken@0
   588
		free(chunk);
slouken@0
   589
	}
slouken@0
   590
}
slouken@0
   591
slouken@26
   592
/* Set a function that is called after all mixing is performed.
slouken@26
   593
   This can be used to provide real-time visual display of the audio stream
slouken@26
   594
   or add a custom mixer filter for the stream data.
slouken@26
   595
*/
slouken@26
   596
void Mix_SetPostMix(void (*mix_func)
slouken@26
   597
                    (void *udata, Uint8 *stream, int len), void *arg)
slouken@26
   598
{
slouken@26
   599
	SDL_LockAudio();
slouken@26
   600
	mix_postmix_data = arg;
slouken@26
   601
	mix_postmix = mix_func;
slouken@26
   602
	SDL_UnlockAudio();
slouken@26
   603
}
slouken@26
   604
slouken@0
   605
/* Add your own music player or mixer function.
slouken@0
   606
   If 'mix_func' is NULL, the default music player is re-enabled.
slouken@0
   607
 */
slouken@0
   608
void Mix_HookMusic(void (*mix_func)(void *udata, Uint8 *stream, int len),
slouken@0
   609
                                                                void *arg)
slouken@0
   610
{
slouken@0
   611
	SDL_LockAudio();
slouken@0
   612
	if ( mix_func != NULL ) {
slouken@0
   613
		music_data = arg;
slouken@0
   614
		mix_music = mix_func;
slouken@0
   615
	} else {
slouken@0
   616
		music_data = NULL;
slouken@0
   617
		mix_music = music_mixer;
slouken@0
   618
	}
slouken@0
   619
	SDL_UnlockAudio();
slouken@0
   620
}
slouken@0
   621
slouken@0
   622
void *Mix_GetMusicHookData(void)
slouken@0
   623
{
slouken@0
   624
	return(music_data);
slouken@0
   625
}
slouken@0
   626
slouken@92
   627
void Mix_ChannelFinished(void (*channel_finished)(int channel))
slouken@92
   628
{
slouken@172
   629
	SDL_LockAudio();
slouken@172
   630
	channel_done_callback = channel_finished;
slouken@172
   631
	SDL_UnlockAudio();
slouken@92
   632
}
slouken@92
   633
slouken@92
   634
slouken@0
   635
/* Reserve the first channels (0 -> n-1) for the application, i.e. don't allocate
slouken@0
   636
   them dynamically to the next sample if requested with a -1 value below.
slouken@0
   637
   Returns the number of reserved channels.
slouken@0
   638
 */
slouken@0
   639
int Mix_ReserveChannels(int num)
slouken@0
   640
{
megastep@7
   641
	if (num > num_channels)
megastep@7
   642
		num = num_channels;
slouken@0
   643
	reserved_channels = num;
slouken@0
   644
	return num;
slouken@0
   645
}
slouken@0
   646
slouken@245
   647
static int checkchunkintegral(Mix_Chunk *chunk)
slouken@245
   648
{
slouken@245
   649
	int frame_width = 1;
slouken@245
   650
slouken@245
   651
	if ((mixer.format & 0xFF) == 16) frame_width = 2;
slouken@245
   652
	frame_width *= mixer.channels;
slouken@245
   653
	while (chunk->alen % frame_width) chunk->alen--;
slouken@245
   654
	return chunk->alen;
slouken@245
   655
}
slouken@245
   656
slouken@0
   657
/* Play an audio chunk on a specific channel.
slouken@0
   658
   If the specified channel is -1, play on the first free channel.
megastep@7
   659
   'ticks' is the number of milliseconds at most to play the sample, or -1
megastep@7
   660
   if there is no limit.
slouken@0
   661
   Returns which channel was used to play the sound.
slouken@0
   662
*/
megastep@7
   663
int Mix_PlayChannelTimed(int which, Mix_Chunk *chunk, int loops, int ticks)
slouken@0
   664
{
slouken@0
   665
	int i;
slouken@0
   666
slouken@0
   667
	/* Don't play null pointers :-) */
slouken@0
   668
	if ( chunk == NULL ) {
slouken@178
   669
		Mix_SetError("Tried to play a NULL chunk");
slouken@0
   670
		return(-1);
slouken@0
   671
	}
slouken@245
   672
	if ( !checkchunkintegral(chunk)) {
slouken@245
   673
		Mix_SetError("Tried to play a chunk with a bad frame");
slouken@245
   674
		return(-1);
slouken@245
   675
	}
slouken@0
   676
slouken@0
   677
	/* Lock the mixer while modifying the playing channels */
slouken@172
   678
	SDL_LockAudio();
slouken@0
   679
	{
slouken@0
   680
		/* If which is -1, play on the first free channel */
slouken@0
   681
		if ( which == -1 ) {
megastep@7
   682
			for ( i=reserved_channels; i<num_channels; ++i ) {
slouken@60
   683
				if ( mix_channel[i].playing <= 0 )
slouken@0
   684
					break;
slouken@0
   685
			}
megastep@7
   686
			if ( i == num_channels ) {
slouken@178
   687
				Mix_SetError("No free channels available");
slouken@0
   688
				which = -1;
slouken@0
   689
			} else {
slouken@0
   690
				which = i;
slouken@0
   691
			}
slouken@0
   692
		}
slouken@0
   693
slouken@0
   694
		/* Queue up the audio data for this channel */
slouken@0
   695
		if ( which >= 0 ) {
slouken@158
   696
			Uint32 sdl_ticks = SDL_GetTicks();
slouken@157
   697
			if (Mix_Playing(which))
slouken@172
   698
				_Mix_channel_done_playing(which);
slouken@60
   699
			mix_channel[which].samples = chunk->abuf;
slouken@60
   700
			mix_channel[which].playing = chunk->alen;
slouken@60
   701
			mix_channel[which].looping = loops;
slouken@60
   702
			mix_channel[which].chunk = chunk;
slouken@60
   703
			mix_channel[which].paused = 0;
slouken@60
   704
			mix_channel[which].fading = MIX_NO_FADING;
slouken@60
   705
			mix_channel[which].start_time = sdl_ticks;
slouken@60
   706
			mix_channel[which].expire = (ticks>0) ? (sdl_ticks + ticks) : 0;
megastep@4
   707
		}
megastep@4
   708
	}
slouken@172
   709
	SDL_UnlockAudio();
megastep@4
   710
megastep@4
   711
	/* Return the channel on which the sound is being played */
megastep@4
   712
	return(which);
megastep@4
   713
}
megastep@4
   714
megastep@7
   715
/* Change the expiration delay for a channel */
megastep@7
   716
int Mix_ExpireChannel(int which, int ticks)
megastep@7
   717
{
megastep@7
   718
	int status = 0;
megastep@7
   719
megastep@7
   720
	if ( which == -1 ) {
megastep@7
   721
		int i;
megastep@7
   722
		for ( i=0; i < num_channels; ++ i ) {
megastep@7
   723
			status += Mix_ExpireChannel(i, ticks);
megastep@7
   724
		}
megastep@7
   725
	} else if ( which < num_channels ) {
slouken@172
   726
		SDL_LockAudio();
slouken@60
   727
		mix_channel[which].expire = (ticks>0) ? (SDL_GetTicks() + ticks) : 0;
slouken@172
   728
		SDL_UnlockAudio();
megastep@7
   729
		++ status;
megastep@7
   730
	}
megastep@7
   731
	return(status);
megastep@7
   732
}
megastep@7
   733
megastep@4
   734
/* Fade in a sound on a channel, over ms milliseconds */
megastep@7
   735
int Mix_FadeInChannelTimed(int which, Mix_Chunk *chunk, int loops, int ms, int ticks)
megastep@4
   736
{
megastep@4
   737
	int i;
megastep@4
   738
megastep@4
   739
	/* Don't play null pointers :-) */
megastep@4
   740
	if ( chunk == NULL ) {
megastep@4
   741
		return(-1);
megastep@4
   742
	}
slouken@245
   743
	if ( !checkchunkintegral(chunk)) {
slouken@245
   744
		Mix_SetError("Tried to play a chunk with a bad frame");
slouken@245
   745
		return(-1);
slouken@245
   746
	}
megastep@4
   747
megastep@4
   748
	/* Lock the mixer while modifying the playing channels */
slouken@172
   749
	SDL_LockAudio();
megastep@4
   750
	{
megastep@4
   751
		/* If which is -1, play on the first free channel */
megastep@4
   752
		if ( which == -1 ) {
megastep@7
   753
			for ( i=reserved_channels; i<num_channels; ++i ) {
slouken@60
   754
				if ( mix_channel[i].playing <= 0 )
megastep@4
   755
					break;
megastep@4
   756
			}
megastep@7
   757
			if ( i == num_channels ) {
megastep@4
   758
				which = -1;
megastep@4
   759
			} else {
megastep@4
   760
				which = i;
megastep@4
   761
			}
megastep@4
   762
		}
megastep@4
   763
megastep@4
   764
		/* Queue up the audio data for this channel */
megastep@4
   765
		if ( which >= 0 ) {
slouken@158
   766
			Uint32 sdl_ticks = SDL_GetTicks();
slouken@157
   767
			if (Mix_Playing(which))
slouken@172
   768
				_Mix_channel_done_playing(which);
slouken@60
   769
			mix_channel[which].samples = chunk->abuf;
slouken@60
   770
			mix_channel[which].playing = chunk->alen;
slouken@60
   771
			mix_channel[which].looping = loops;
slouken@60
   772
			mix_channel[which].chunk = chunk;
slouken@60
   773
			mix_channel[which].paused = 0;
slouken@60
   774
			mix_channel[which].fading = MIX_FADING_IN;
slouken@60
   775
			mix_channel[which].fade_volume = mix_channel[which].volume;
slouken@60
   776
			mix_channel[which].volume = 0;
slouken@60
   777
			mix_channel[which].fade_length = (Uint32)ms;
slouken@60
   778
			mix_channel[which].start_time = mix_channel[which].ticks_fade = sdl_ticks;
slouken@60
   779
			mix_channel[which].expire = (ticks > 0) ? (sdl_ticks+ticks) : 0;
slouken@0
   780
		}
slouken@0
   781
	}
slouken@172
   782
	SDL_UnlockAudio();
slouken@0
   783
slouken@0
   784
	/* Return the channel on which the sound is being played */
slouken@0
   785
	return(which);
slouken@0
   786
}
slouken@0
   787
slouken@0
   788
/* Set volume of a particular channel */
slouken@0
   789
int Mix_Volume(int which, int volume)
slouken@0
   790
{
slouken@0
   791
	int i;
slouken@0
   792
	int prev_volume;
slouken@0
   793
slouken@0
   794
	if ( which == -1 ) {
slouken@0
   795
		prev_volume = 0;
megastep@7
   796
		for ( i=0; i<num_channels; ++i ) {
slouken@0
   797
			prev_volume += Mix_Volume(i, volume);
slouken@0
   798
		}
megastep@7
   799
		prev_volume /= num_channels;
slouken@0
   800
	} else {
slouken@60
   801
		prev_volume = mix_channel[which].volume;
slouken@163
   802
		if ( volume >= 0 ) {
slouken@163
   803
			if ( volume > SDL_MIX_MAXVOLUME ) {
slouken@163
   804
				volume = SDL_MIX_MAXVOLUME;
slouken@163
   805
			}
slouken@163
   806
			mix_channel[which].volume = volume;
slouken@0
   807
		}
slouken@0
   808
	}
slouken@0
   809
	return(prev_volume);
slouken@0
   810
}
slouken@0
   811
/* Set volume of a particular chunk */
slouken@0
   812
int Mix_VolumeChunk(Mix_Chunk *chunk, int volume)
slouken@0
   813
{
slouken@0
   814
	int prev_volume;
slouken@0
   815
slouken@0
   816
	prev_volume = chunk->volume;
slouken@0
   817
	if ( volume >= 0 ) {
slouken@0
   818
		if ( volume > MIX_MAX_VOLUME ) {
slouken@0
   819
			volume = MIX_MAX_VOLUME;
slouken@0
   820
		}
slouken@0
   821
		chunk->volume = volume;
slouken@0
   822
	}
slouken@0
   823
	return(prev_volume);
slouken@0
   824
}
slouken@0
   825
slouken@0
   826
/* Halt playing of a particular channel */
slouken@0
   827
int Mix_HaltChannel(int which)
slouken@0
   828
{
slouken@0
   829
	int i;
slouken@0
   830
slouken@0
   831
	if ( which == -1 ) {
megastep@7
   832
		for ( i=0; i<num_channels; ++i ) {
slouken@0
   833
			Mix_HaltChannel(i);
slouken@0
   834
		}
slouken@0
   835
	} else {
slouken@172
   836
		SDL_LockAudio();
slouken@113
   837
		if (mix_channel[which].playing) {
slouken@172
   838
			_Mix_channel_done_playing(which);
slouken@60
   839
		mix_channel[which].playing = 0;
slouken@113
   840
		}
slouken@60
   841
		mix_channel[which].expire = 0;
slouken@60
   842
		if(mix_channel[which].fading != MIX_NO_FADING) /* Restore volume */
slouken@60
   843
			mix_channel[which].volume = mix_channel[which].fade_volume;
slouken@60
   844
		mix_channel[which].fading = MIX_NO_FADING;
slouken@172
   845
		SDL_UnlockAudio();
slouken@0
   846
	}
slouken@0
   847
	return(0);
slouken@0
   848
}
slouken@0
   849
megastep@7
   850
/* Halt playing of a particular group of channels */
megastep@7
   851
int Mix_HaltGroup(int tag)
megastep@7
   852
{
megastep@7
   853
	int i;
megastep@7
   854
megastep@7
   855
	for ( i=0; i<num_channels; ++i ) {
slouken@60
   856
		if( mix_channel[i].tag == tag ) {
megastep@7
   857
			Mix_HaltChannel(i);
megastep@7
   858
		}
megastep@7
   859
	}
megastep@7
   860
	return(0);
megastep@7
   861
}
megastep@7
   862
megastep@4
   863
/* Fade out a channel and then stop it automatically */
megastep@4
   864
int Mix_FadeOutChannel(int which, int ms)
megastep@4
   865
{
megastep@4
   866
	int status;
megastep@4
   867
megastep@4
   868
	status = 0;
slouken@89
   869
	if ( audio_opened ) {
slouken@89
   870
		if ( which == -1 ) {
slouken@89
   871
			int i;
megastep@4
   872
slouken@89
   873
			for ( i=0; i<num_channels; ++i ) {
slouken@89
   874
				status += Mix_FadeOutChannel(i, ms);
slouken@89
   875
			}
slouken@89
   876
		} else {
slouken@172
   877
			SDL_LockAudio();
slouken@89
   878
			if ( mix_channel[which].playing && 
slouken@89
   879
			    (mix_channel[which].volume > 0) &&
slouken@89
   880
			    (mix_channel[which].fading != MIX_FADING_OUT) ) {
slouken@89
   881
slouken@89
   882
				mix_channel[which].fading = MIX_FADING_OUT;
slouken@89
   883
				mix_channel[which].fade_volume = mix_channel[which].volume;
slouken@89
   884
				mix_channel[which].fade_length = ms;
slouken@89
   885
				mix_channel[which].ticks_fade = SDL_GetTicks();
slouken@89
   886
				++status;
slouken@89
   887
			}
slouken@172
   888
			SDL_UnlockAudio();
megastep@4
   889
		}
megastep@4
   890
	}
megastep@4
   891
	return(status);
megastep@4
   892
}
megastep@4
   893
megastep@7
   894
/* Halt playing of a particular group of channels */
megastep@7
   895
int Mix_FadeOutGroup(int tag, int ms)
megastep@7
   896
{
megastep@7
   897
	int i;
megastep@7
   898
	int status = 0;
megastep@7
   899
	for ( i=0; i<num_channels; ++i ) {
slouken@60
   900
		if( mix_channel[i].tag == tag ) {
megastep@7
   901
			status += Mix_FadeOutChannel(i,ms);
megastep@7
   902
		}
megastep@7
   903
	}
megastep@7
   904
	return(status);
megastep@7
   905
}
megastep@7
   906
megastep@4
   907
Mix_Fading Mix_FadingChannel(int which)
megastep@4
   908
{
slouken@60
   909
	return mix_channel[which].fading;
megastep@4
   910
}
megastep@4
   911
slouken@0
   912
/* Check the status of a specific channel.
slouken@113
   913
   If the specified mix_channel is -1, check all mix channels.
slouken@0
   914
*/
slouken@0
   915
int Mix_Playing(int which)
slouken@0
   916
{
slouken@0
   917
	int status;
slouken@0
   918
slouken@0
   919
	status = 0;
slouken@0
   920
	if ( which == -1 ) {
slouken@0
   921
		int i;
slouken@0
   922
megastep@7
   923
		for ( i=0; i<num_channels; ++i ) {
slouken@113
   924
			if ((mix_channel[i].playing > 0) ||
slouken@113
   925
				(mix_channel[i].looping > 0))
slouken@113
   926
			{
slouken@0
   927
				++status;
slouken@0
   928
			}
slouken@0
   929
		}
slouken@0
   930
	} else {
slouken@113
   931
		if ((mix_channel[which].playing > 0) ||
slouken@113
   932
			(mix_channel[which].looping > 0))
slouken@113
   933
		{
slouken@0
   934
			++status;
slouken@0
   935
		}
slouken@0
   936
	}
slouken@0
   937
	return(status);
slouken@0
   938
}
slouken@0
   939
slouken@92
   940
/* rcg06072001 Get the chunk associated with a channel. */
slouken@92
   941
Mix_Chunk *Mix_GetChunk(int channel)
slouken@92
   942
{
slouken@92
   943
	Mix_Chunk *retval = NULL;
slouken@92
   944
slouken@92
   945
	if ((channel >= 0) && (channel < num_channels)) {
slouken@92
   946
		retval = mix_channel[channel].chunk;
slouken@92
   947
	}
slouken@92
   948
slouken@92
   949
	return(retval);
slouken@92
   950
}
slouken@92
   951
slouken@0
   952
/* Close the mixer, halting all playing audio */
slouken@0
   953
void Mix_CloseAudio(void)
slouken@0
   954
{
slouken@113
   955
	int i;
slouken@113
   956
slouken@0
   957
	if ( audio_opened ) {
slouken@0
   958
		if ( audio_opened == 1 ) {
slouken@113
   959
			for (i = 0; i < num_channels; i++) {
slouken@113
   960
				Mix_UnregisterAllEffects(i);
slouken@113
   961
			}
slouken@113
   962
			Mix_UnregisterAllEffects(MIX_CHANNEL_POST);
slouken@0
   963
			close_music();
slouken@0
   964
			Mix_HaltChannel(-1);
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