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