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