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