mixer.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 01 May 2006 03:34:46 +0000
changeset 300 b7dde67b444c
parent 245 63b3650714de
child 337 673aa2fe1dbc
permissions -rw-r--r--
Fixed bug #119

if a Mixer has already been opened with paramter set A and I try to open
another one with parameter set B, Mix_OpenAudio will happily say "you
already opened audio with parameter set B, go ahead", which is wrong.

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