mixer.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 16 Jul 2002 17:39:48 +0000
changeset 188 23c83aaa7373
parent 184 2de3ca27a2f9
child 189 983b27be0ec8
permissions -rw-r--r--
Date: Mon, 08 Jul 2002 20:14:52 +0300
From: Martin_Storsj� <martin@martin.st>
Subject: Patch for SDL_mixer

Hi

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