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