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