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