mixer.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Oct 2009 05:29:55 +0000
changeset 443 d842a759e68a
parent 442 884a700fb3ff
child 444 41b7df493dad
permissions -rw-r--r--
Correctly reset channel volumes after a fade out interrupts a fade in.

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