mixer.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 11 Nov 1999 20:07:05 +0000
changeset 19 1e7588c1fb88
parent 17 d5638c74656f
child 21 2d26b0f75a05
permissions -rw-r--r--
The MPEG decoder callback needs to be called even when music is off.
slouken@0
     1
/*
slouken@0
     2
    MIXERLIB:  An audio mixer library based on the SDL library
slouken@0
     3
    Copyright (C) 1997-1999  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@0
    20
    5635-34 Springhouse Dr.
slouken@0
    21
    Pleasanton, CA 94588 (USA)
slouken@0
    22
    slouken@devolution.com
slouken@0
    23
*/
slouken@0
    24
megastep@7
    25
/* $Id$ */
megastep@7
    26
slouken@0
    27
#include <stdio.h>
slouken@0
    28
#include <stdlib.h>
slouken@0
    29
#include <string.h>
slouken@0
    30
slouken@0
    31
#include <SDL/SDL_mutex.h>
slouken@0
    32
#include <SDL/SDL_endian.h>
slouken@0
    33
slouken@0
    34
#include "mixer.h"
slouken@0
    35
slouken@0
    36
static int audio_opened = 0;
slouken@0
    37
slouken@0
    38
static SDL_AudioSpec mixer;
slouken@0
    39
static SDL_mutex *mixer_lock;
slouken@0
    40
static struct _Mix_Channel {
slouken@0
    41
	Mix_Chunk *chunk;
slouken@0
    42
	int playing;
slouken@0
    43
	int paused;
slouken@0
    44
	Uint8 *samples;
slouken@0
    45
	int volume;
slouken@0
    46
	int looping;
megastep@7
    47
	int tag;
megastep@7
    48
	int expire;
megastep@8
    49
	Uint32 start_time;
megastep@4
    50
	Mix_Fading fading;
megastep@4
    51
	int fade_volume;
megastep@4
    52
	int fade_length;
megastep@4
    53
	Uint32 ticks_fade;
megastep@7
    54
} *channel = NULL;
megastep@7
    55
static int num_channels;
slouken@17
    56
static int reserved_channels = 0;
slouken@0
    57
slouken@0
    58
slouken@0
    59
/* Support for user defined music functions, plus the default one */
slouken@0
    60
extern int music_active;
slouken@0
    61
extern void music_mixer(void *udata, Uint8 *stream, int len);
slouken@0
    62
static void (*mix_music)(void *udata, Uint8 *stream, int len) = music_mixer;
slouken@0
    63
void *music_data = NULL;
slouken@0
    64
slouken@0
    65
/* Mixing function */
slouken@0
    66
static void mix_channels(void *udata, Uint8 *stream, int len)
slouken@0
    67
{
slouken@0
    68
	int i, mixable, volume;
megastep@7
    69
	Uint32 sdl_ticks;
slouken@0
    70
slouken@15
    71
	/* Mix the music (must be done before the channels are added) */
slouken@19
    72
	if ( music_active || (mix_music != music_mixer) ) {
slouken@15
    73
		mix_music(music_data, stream, len);
slouken@15
    74
	}
slouken@15
    75
slouken@0
    76
	/* Grab the channels we need to mix */
slouken@0
    77
	SDL_mutexP(mixer_lock);
megastep@7
    78
	sdl_ticks = SDL_GetTicks();
megastep@7
    79
	for ( i=0; i<num_channels; ++i ) {
megastep@8
    80
		if( ! channel[i].paused ) {
megastep@8
    81
			if ( channel[i].expire > 0 && channel[i].expire < sdl_ticks ) {
megastep@8
    82
				/* Expiration delay for that channel is reached */
megastep@8
    83
				channel[i].playing = 0;
megastep@4
    84
				channel[i].fading = MIX_NO_FADING;
megastep@8
    85
				channel[i].expire = -1;
megastep@8
    86
			} else if ( channel[i].fading != MIX_NO_FADING ) {
megastep@8
    87
				Uint32 ticks = sdl_ticks - channel[i].ticks_fade;
megastep@8
    88
				if( ticks > channel[i].fade_length ) {
megastep@8
    89
					if( channel[i].fading == MIX_FADING_OUT ) {
megastep@8
    90
						channel[i].playing = 0;
megastep@8
    91
						channel[i].expire = -1;
megastep@8
    92
						Mix_Volume(i, channel[i].fading); /* Restore the volume */
megastep@8
    93
					}
megastep@8
    94
					channel[i].fading = MIX_NO_FADING;
megastep@4
    95
				} else {
megastep@8
    96
					if( channel[i].fading == MIX_FADING_OUT ) {
megastep@8
    97
						Mix_Volume(i, (channel[i].fade_volume * (channel[i].fade_length-ticks))
megastep@8
    98
								   / channel[i].fade_length );
megastep@8
    99
					} else {
megastep@8
   100
						Mix_Volume(i, (channel[i].fade_volume * ticks) / channel[i].fade_length );
megastep@8
   101
					}
megastep@4
   102
				}
megastep@4
   103
			}
megastep@8
   104
			if ( channel[i].playing > 0 ) {
megastep@8
   105
				mixable = channel[i].playing;
megastep@8
   106
				if ( mixable > len ) {
megastep@8
   107
					mixable = len;
megastep@8
   108
				}
megastep@8
   109
				volume = (channel[i].volume*channel[i].chunk->volume) /
megastep@8
   110
					MIX_MAX_VOLUME;
megastep@8
   111
				SDL_MixAudio(stream,channel[i].samples,mixable,volume);
megastep@8
   112
				channel[i].samples += mixable;
megastep@8
   113
				channel[i].playing -= mixable;
megastep@8
   114
				if ( ! channel[i].playing && channel[i].looping ) {
megastep@8
   115
					if ( --channel[i].looping ) {
megastep@8
   116
						channel[i].samples = channel[i].chunk->abuf;
megastep@8
   117
						channel[i].playing = channel[i].chunk->alen;
megastep@8
   118
					}
slouken@0
   119
				}
slouken@0
   120
			}
slouken@0
   121
		}
slouken@0
   122
	}
slouken@0
   123
	SDL_mutexV(mixer_lock);
slouken@0
   124
}
slouken@0
   125
slouken@0
   126
static void PrintFormat(char *title, SDL_AudioSpec *fmt)
slouken@0
   127
{
slouken@0
   128
	printf("%s: %d bit %s audio (%s) at %u Hz\n", title, (fmt->format&0xFF),
slouken@0
   129
			(fmt->format&0x8000) ? "signed" : "unsigned",
slouken@0
   130
			(fmt->channels > 1) ? "stereo" : "mono", fmt->freq);
slouken@0
   131
}
slouken@0
   132
slouken@0
   133
/* Open the mixer with a certain desired audio format */
slouken@0
   134
int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize)
slouken@0
   135
{
slouken@0
   136
	int i;
slouken@0
   137
	SDL_AudioSpec desired;
slouken@0
   138
slouken@0
   139
	/* If the mixer is already opened, increment open count */
slouken@0
   140
	if ( audio_opened ) {
slouken@0
   141
	    ++audio_opened;
slouken@0
   142
	    return(0);
slouken@0
   143
	}
slouken@0
   144
slouken@0
   145
	/* Set the desired format and frequency */
slouken@0
   146
	desired.freq = frequency;
slouken@0
   147
	desired.format = format;
slouken@0
   148
	desired.channels = channels;
slouken@0
   149
	desired.samples = chunksize;
slouken@0
   150
	desired.callback = mix_channels;
slouken@0
   151
	desired.userdata = NULL;
slouken@0
   152
slouken@0
   153
	/* Accept nearly any audio format */
slouken@0
   154
	if ( SDL_OpenAudio(&desired, &mixer) < 0 ) {
slouken@0
   155
		return(-1);
slouken@0
   156
	}
slouken@0
   157
#if 0
slouken@0
   158
	PrintFormat("Audio device", &mixer);
slouken@0
   159
#endif
slouken@0
   160
slouken@0
   161
	/* Create the channel lock mutex */
slouken@0
   162
	mixer_lock = SDL_CreateMutex();
slouken@0
   163
	if ( mixer_lock == NULL ) {
slouken@0
   164
		SDL_CloseAudio();
slouken@0
   165
		SDL_SetError("Unable to create mixer lock");
slouken@0
   166
		return(-1);
slouken@0
   167
	}
slouken@0
   168
slouken@0
   169
	/* Initialize the music players */
slouken@0
   170
	if ( open_music(&mixer) < 0 ) {
slouken@0
   171
		SDL_CloseAudio();
slouken@0
   172
		SDL_DestroyMutex(mixer_lock);
slouken@0
   173
		return(-1);
slouken@0
   174
	}
megastep@7
   175
megastep@7
   176
	num_channels = MIX_CHANNELS;
megastep@7
   177
	channel = (struct _Mix_Channel *) malloc(num_channels * sizeof(struct _Mix_Channel));
megastep@7
   178
slouken@0
   179
	/* Clear out the audio channels */
megastep@7
   180
	for ( i=0; i<num_channels; ++i ) {
slouken@0
   181
		channel[i].chunk = NULL;
slouken@0
   182
		channel[i].playing = 0;
slouken@0
   183
		channel[i].looping = 0;
slouken@0
   184
		channel[i].volume = SDL_MIX_MAXVOLUME;
megastep@7
   185
		channel[i].tag = -1;
megastep@7
   186
		channel[i].expire = -1;
slouken@0
   187
	}
slouken@0
   188
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   189
	audio_opened = 1;
slouken@0
   190
	SDL_PauseAudio(0);
slouken@0
   191
	return(0);
slouken@0
   192
}
slouken@0
   193
megastep@7
   194
/* Dynamically change the number of channels managed by the mixer.
megastep@7
   195
   If decreasing the number of channels, the upper channels are
megastep@7
   196
   stopped.
megastep@7
   197
 */
megastep@7
   198
int Mix_AllocateChannels(int numchans)
megastep@7
   199
{
megastep@7
   200
	if ( numchans<0 || numchans==num_channels )
megastep@7
   201
		return(num_channels);
megastep@7
   202
megastep@7
   203
	if ( numchans < num_channels ) {
megastep@7
   204
		/* Stop the affected channels */
megastep@7
   205
		int i;
megastep@7
   206
		for(i=numchans; i < num_channels; i++) {
megastep@7
   207
			Mix_HaltChannel(i);
megastep@7
   208
		}
megastep@7
   209
	}
megastep@7
   210
	SDL_mutexP(mixer_lock);
megastep@7
   211
	channel = (struct _Mix_Channel *) realloc(channel, numchans * sizeof(struct _Mix_Channel));
megastep@7
   212
	if ( numchans > num_channels ) {
megastep@7
   213
		/* Initialize the new channels */
megastep@7
   214
		int i;
megastep@7
   215
		for(i=num_channels; i < numchans; i++) {
megastep@7
   216
			channel[i].chunk = NULL;
megastep@7
   217
			channel[i].playing = 0;
megastep@7
   218
			channel[i].looping = 0;
megastep@7
   219
			channel[i].volume = SDL_MIX_MAXVOLUME;
megastep@7
   220
			channel[i].tag = -1;
megastep@7
   221
			channel[i].expire = -1;
megastep@7
   222
		}
megastep@7
   223
	}
megastep@7
   224
	num_channels = numchans;
megastep@7
   225
	SDL_mutexV(mixer_lock);
megastep@7
   226
	return(num_channels);
megastep@7
   227
}
megastep@7
   228
slouken@0
   229
/* Return the actual mixer parameters */
slouken@0
   230
int Mix_QuerySpec(int *frequency, Uint16 *format, int *channels)
slouken@0
   231
{
slouken@0
   232
	if ( audio_opened ) {
slouken@0
   233
		if ( frequency ) {
slouken@0
   234
			*frequency = mixer.freq;
slouken@0
   235
		}
slouken@0
   236
		if ( format ) {
slouken@0
   237
			*format = mixer.format;
slouken@0
   238
		}
slouken@0
   239
		if ( channels ) {
slouken@0
   240
			*channels = mixer.channels;
slouken@0
   241
		}
slouken@0
   242
	}
slouken@0
   243
	return(audio_opened);
slouken@0
   244
}
slouken@0
   245
slouken@0
   246
/* Load a wave file */
slouken@0
   247
Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
slouken@0
   248
{
slouken@0
   249
	Mix_Chunk *chunk;
slouken@0
   250
	SDL_AudioSpec wavespec;
slouken@0
   251
	SDL_AudioCVT wavecvt;
slouken@0
   252
slouken@0
   253
	/* Make sure audio has been opened */
slouken@0
   254
	if ( ! audio_opened ) {
slouken@0
   255
		SDL_SetError("Audio device hasn't been opened");
slouken@0
   256
		if ( freesrc ) {
slouken@0
   257
			SDL_RWclose(src);
slouken@0
   258
		}
slouken@0
   259
		return(NULL);
slouken@0
   260
	}
slouken@0
   261
slouken@0
   262
	/* Allocate the chunk memory */
slouken@0
   263
	chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk));
slouken@0
   264
	if ( chunk == NULL ) {
slouken@0
   265
		SDL_SetError("Out of memory");
slouken@0
   266
		if ( freesrc ) {
slouken@0
   267
			SDL_RWclose(src);
slouken@0
   268
		}
slouken@0
   269
		return(NULL);
slouken@0
   270
	}
slouken@0
   271
slouken@0
   272
	/* Load the WAV file into the chunk */
slouken@0
   273
	if ( SDL_LoadWAV_RW(src, freesrc,
slouken@0
   274
		&wavespec, (Uint8 **)&chunk->abuf, &chunk->alen) == NULL ) {
slouken@0
   275
		free(chunk);
slouken@0
   276
		return(NULL);
slouken@0
   277
	}
slouken@0
   278
#if 0
slouken@0
   279
	PrintFormat("Audio device", &mixer);
slouken@0
   280
	PrintFormat("-- Wave file", &wavespec);
slouken@0
   281
#endif
slouken@0
   282
slouken@0
   283
	/* Build the audio converter and create conversion buffers */
slouken@0
   284
	if ( SDL_BuildAudioCVT(&wavecvt,
slouken@0
   285
			wavespec.format, wavespec.channels, wavespec.freq,
slouken@0
   286
			mixer.format, mixer.channels, mixer.freq) < 0 ) {
slouken@0
   287
		SDL_FreeWAV(chunk->abuf);
slouken@0
   288
		free(chunk);
slouken@0
   289
		return(NULL);
slouken@0
   290
	}
slouken@0
   291
	wavecvt.len = chunk->alen;
slouken@0
   292
	wavecvt.buf = (Uint8 *)malloc(wavecvt.len*wavecvt.len_mult);
slouken@0
   293
	if ( wavecvt.buf == NULL ) {
slouken@0
   294
		SDL_SetError("Out of memory");
slouken@0
   295
		SDL_FreeWAV(chunk->abuf);
slouken@0
   296
		free(chunk);
slouken@0
   297
		return(NULL);
slouken@0
   298
	}
slouken@0
   299
	memcpy(wavecvt.buf, chunk->abuf, chunk->alen);
slouken@0
   300
	SDL_FreeWAV(chunk->abuf);
slouken@0
   301
slouken@0
   302
	/* Run the audio converter */
slouken@0
   303
	if ( SDL_ConvertAudio(&wavecvt) < 0 ) {
slouken@0
   304
		free(wavecvt.buf);
slouken@0
   305
		free(chunk);
slouken@0
   306
		return(NULL);
slouken@0
   307
	}
slouken@1
   308
	chunk->allocated = 1;
slouken@0
   309
	chunk->abuf = wavecvt.buf;
slouken@0
   310
	chunk->alen = wavecvt.len_cvt;
slouken@0
   311
	chunk->volume = MIX_MAX_VOLUME;
slouken@0
   312
	return(chunk);
slouken@0
   313
}
slouken@0
   314
slouken@1
   315
/* Load a wave file of the mixer format from a memory buffer */
slouken@1
   316
Mix_Chunk *Mix_QuickLoad_WAV(Uint8 *mem)
slouken@1
   317
{
slouken@1
   318
	Mix_Chunk *chunk;
slouken@1
   319
	Uint8 magic[4];
slouken@1
   320
slouken@1
   321
	/* Make sure audio has been opened */
slouken@1
   322
	if ( ! audio_opened ) {
slouken@1
   323
		SDL_SetError("Audio device hasn't been opened");
slouken@1
   324
		return(NULL);
slouken@1
   325
	}
slouken@1
   326
slouken@1
   327
	/* Allocate the chunk memory */
slouken@1
   328
	chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk));
slouken@1
   329
	if ( chunk == NULL ) {
slouken@1
   330
		SDL_SetError("Out of memory");
slouken@1
   331
		return(NULL);
slouken@1
   332
	}
slouken@1
   333
slouken@1
   334
	/* Essentially just skip to the audio data (no error checking - fast) */
slouken@1
   335
	chunk->allocated = 0;
slouken@1
   336
	mem += 12; /* WAV header */
slouken@1
   337
	do {
slouken@1
   338
		memcpy(magic, mem, 4);
slouken@1
   339
		mem += 4;
slouken@1
   340
		chunk->alen = ((mem[3]<<24)|(mem[2]<<16)|(mem[1]<<8)|(mem[0]));
slouken@1
   341
		mem += 4;
slouken@1
   342
		chunk->abuf = mem;
slouken@1
   343
		mem += chunk->alen;
slouken@1
   344
	} while ( memcmp(magic, "data", 4) != 0 );
slouken@1
   345
	chunk->volume = MIX_MAX_VOLUME;
slouken@1
   346
slouken@1
   347
	return(chunk);
slouken@1
   348
}
slouken@1
   349
slouken@0
   350
/* Free an audio chunk previously loaded */
slouken@0
   351
void Mix_FreeChunk(Mix_Chunk *chunk)
slouken@0
   352
{
slouken@0
   353
	int i;
slouken@0
   354
slouken@0
   355
	/* Caution -- if the chunk is playing, the mixer will crash */
slouken@0
   356
	if ( chunk ) {
slouken@0
   357
		/* Guarantee that this chunk isn't playing */
slouken@0
   358
		SDL_mutexP(mixer_lock);
megastep@7
   359
		for ( i=0; i<num_channels; ++i ) {
slouken@0
   360
			if ( chunk == channel[i].chunk ) {
slouken@0
   361
				channel[i].playing = 0;
slouken@0
   362
			}
slouken@0
   363
		}
slouken@0
   364
		SDL_mutexV(mixer_lock);
slouken@0
   365
slouken@0
   366
		/* Actually free the chunk */
slouken@1
   367
		if ( chunk->allocated ) {
slouken@1
   368
			free(chunk->abuf);
slouken@1
   369
		}
slouken@0
   370
		free(chunk);
slouken@0
   371
	}
slouken@0
   372
}
slouken@0
   373
slouken@0
   374
/* Add your own music player or mixer function.
slouken@0
   375
   If 'mix_func' is NULL, the default music player is re-enabled.
slouken@0
   376
 */
slouken@0
   377
void Mix_HookMusic(void (*mix_func)(void *udata, Uint8 *stream, int len),
slouken@0
   378
                                                                void *arg)
slouken@0
   379
{
slouken@0
   380
	SDL_LockAudio();
slouken@0
   381
	if ( mix_func != NULL ) {
slouken@0
   382
		music_data = arg;
slouken@0
   383
		mix_music = mix_func;
slouken@0
   384
	} else {
slouken@0
   385
		music_data = NULL;
slouken@0
   386
		mix_music = music_mixer;
slouken@0
   387
	}
slouken@0
   388
	SDL_UnlockAudio();
slouken@0
   389
}
slouken@0
   390
slouken@0
   391
void *Mix_GetMusicHookData(void)
slouken@0
   392
{
slouken@0
   393
	return(music_data);
slouken@0
   394
}
slouken@0
   395
slouken@0
   396
/* Reserve the first channels (0 -> n-1) for the application, i.e. don't allocate
slouken@0
   397
   them dynamically to the next sample if requested with a -1 value below.
slouken@0
   398
   Returns the number of reserved channels.
slouken@0
   399
 */
slouken@0
   400
int Mix_ReserveChannels(int num)
slouken@0
   401
{
megastep@7
   402
	if (num > num_channels)
megastep@7
   403
		num = num_channels;
slouken@0
   404
	reserved_channels = num;
slouken@0
   405
	return num;
slouken@0
   406
}
slouken@0
   407
slouken@0
   408
/* Play an audio chunk on a specific channel.
slouken@0
   409
   If the specified channel is -1, play on the first free channel.
megastep@7
   410
   'ticks' is the number of milliseconds at most to play the sample, or -1
megastep@7
   411
   if there is no limit.
slouken@0
   412
   Returns which channel was used to play the sound.
slouken@0
   413
*/
megastep@7
   414
int Mix_PlayChannelTimed(int which, Mix_Chunk *chunk, int loops, int ticks)
slouken@0
   415
{
slouken@0
   416
	int i;
slouken@0
   417
slouken@0
   418
	/* Don't play null pointers :-) */
slouken@0
   419
	if ( chunk == NULL ) {
slouken@0
   420
		return(-1);
slouken@0
   421
	}
slouken@0
   422
slouken@0
   423
	/* Lock the mixer while modifying the playing channels */
slouken@0
   424
	SDL_mutexP(mixer_lock);
slouken@0
   425
	{
slouken@0
   426
		/* If which is -1, play on the first free channel */
slouken@0
   427
		if ( which == -1 ) {
megastep@7
   428
			for ( i=reserved_channels; i<num_channels; ++i ) {
slouken@0
   429
				if ( channel[i].playing <= 0 )
slouken@0
   430
					break;
slouken@0
   431
			}
megastep@7
   432
			if ( i == num_channels ) {
slouken@0
   433
				which = -1;
slouken@0
   434
			} else {
slouken@0
   435
				which = i;
slouken@0
   436
			}
slouken@0
   437
		}
slouken@0
   438
slouken@0
   439
		/* Queue up the audio data for this channel */
slouken@0
   440
		if ( which >= 0 ) {
megastep@8
   441
			Uint32 sdl_ticks = SDL_GetTicks();
slouken@0
   442
			channel[which].samples = chunk->abuf;
slouken@0
   443
			channel[which].playing = chunk->alen;
slouken@0
   444
			channel[which].looping = loops;
slouken@0
   445
			channel[which].chunk = chunk;
slouken@0
   446
			channel[which].paused = 0;
megastep@4
   447
			channel[which].fading = MIX_NO_FADING;
megastep@8
   448
			channel[which].start_time = sdl_ticks;
megastep@8
   449
			channel[which].expire = (ticks>0) ? (sdl_ticks + ticks) : -1;
megastep@4
   450
		}
megastep@4
   451
	}
megastep@4
   452
	SDL_mutexV(mixer_lock);
megastep@4
   453
megastep@4
   454
	/* Return the channel on which the sound is being played */
megastep@4
   455
	return(which);
megastep@4
   456
}
megastep@4
   457
megastep@7
   458
/* Change the expiration delay for a channel */
megastep@7
   459
int Mix_ExpireChannel(int which, int ticks)
megastep@7
   460
{
megastep@7
   461
	int status = 0;
megastep@7
   462
megastep@7
   463
	if ( which == -1 ) {
megastep@7
   464
		int i;
megastep@7
   465
		for ( i=0; i < num_channels; ++ i ) {
megastep@7
   466
			status += Mix_ExpireChannel(i, ticks);
megastep@7
   467
		}
megastep@7
   468
	} else if ( which < num_channels ) {
megastep@7
   469
		SDL_mutexP(mixer_lock);
megastep@7
   470
		channel[which].expire = (ticks>0) ? (SDL_GetTicks() + ticks) : -1;		
megastep@7
   471
		SDL_mutexV(mixer_lock);
megastep@7
   472
		++ status;
megastep@7
   473
	}
megastep@7
   474
	return(status);
megastep@7
   475
}
megastep@7
   476
megastep@4
   477
/* Fade in a sound on a channel, over ms milliseconds */
megastep@7
   478
int Mix_FadeInChannelTimed(int which, Mix_Chunk *chunk, int loops, int ms, int ticks)
megastep@4
   479
{
megastep@4
   480
	int i;
megastep@4
   481
megastep@4
   482
	/* Don't play null pointers :-) */
megastep@4
   483
	if ( chunk == NULL ) {
megastep@4
   484
		return(-1);
megastep@4
   485
	}
megastep@4
   486
megastep@4
   487
	/* Lock the mixer while modifying the playing channels */
megastep@4
   488
	SDL_mutexP(mixer_lock);
megastep@4
   489
	{
megastep@4
   490
		/* If which is -1, play on the first free channel */
megastep@4
   491
		if ( which == -1 ) {
megastep@7
   492
			for ( i=reserved_channels; i<num_channels; ++i ) {
megastep@4
   493
				if ( channel[i].playing <= 0 )
megastep@4
   494
					break;
megastep@4
   495
			}
megastep@7
   496
			if ( i == num_channels ) {
megastep@4
   497
				which = -1;
megastep@4
   498
			} else {
megastep@4
   499
				which = i;
megastep@4
   500
			}
megastep@4
   501
		}
megastep@4
   502
megastep@4
   503
		/* Queue up the audio data for this channel */
megastep@4
   504
		if ( which >= 0 ) {
megastep@8
   505
			Uint32 sdl_ticks = SDL_GetTicks();
megastep@4
   506
			channel[which].samples = chunk->abuf;
megastep@4
   507
			channel[which].playing = chunk->alen;
megastep@4
   508
			channel[which].looping = loops;
megastep@4
   509
			channel[which].chunk = chunk;
megastep@4
   510
			channel[which].paused = 0;
megastep@4
   511
			channel[which].fading = MIX_FADING_IN;
megastep@4
   512
			channel[which].fade_volume = channel[which].volume;
megastep@4
   513
			channel[which].volume = 0;
megastep@4
   514
			channel[which].fade_length = ms;
megastep@8
   515
			channel[which].start_time = channel[which].ticks_fade = sdl_ticks;
megastep@8
   516
			channel[which].expire = (ticks > 0) ? (sdl_ticks+ticks) : -1;
slouken@0
   517
		}
slouken@0
   518
	}
slouken@0
   519
	SDL_mutexV(mixer_lock);
slouken@0
   520
slouken@0
   521
	/* Return the channel on which the sound is being played */
slouken@0
   522
	return(which);
slouken@0
   523
}
slouken@0
   524
slouken@0
   525
/* Set volume of a particular channel */
slouken@0
   526
int Mix_Volume(int which, int volume)
slouken@0
   527
{
slouken@0
   528
	int i;
slouken@0
   529
	int prev_volume;
slouken@0
   530
slouken@0
   531
	if ( which == -1 ) {
slouken@0
   532
		prev_volume = 0;
megastep@7
   533
		for ( i=0; i<num_channels; ++i ) {
slouken@0
   534
			prev_volume += Mix_Volume(i, volume);
slouken@0
   535
		}
megastep@7
   536
		prev_volume /= num_channels;
slouken@0
   537
	} else {
slouken@0
   538
		prev_volume = channel[which].volume;
megastep@4
   539
		if ( volume < 0 ) {
megastep@4
   540
			volume = 0;
slouken@0
   541
		}
megastep@4
   542
		if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
   543
			volume = SDL_MIX_MAXVOLUME;
megastep@4
   544
		}
megastep@4
   545
		channel[which].volume = volume;
slouken@0
   546
	}
slouken@0
   547
	return(prev_volume);
slouken@0
   548
}
slouken@0
   549
/* Set volume of a particular chunk */
slouken@0
   550
int Mix_VolumeChunk(Mix_Chunk *chunk, int volume)
slouken@0
   551
{
slouken@0
   552
	int prev_volume;
slouken@0
   553
slouken@0
   554
	prev_volume = chunk->volume;
slouken@0
   555
	if ( volume >= 0 ) {
slouken@0
   556
		if ( volume > MIX_MAX_VOLUME ) {
slouken@0
   557
			volume = MIX_MAX_VOLUME;
slouken@0
   558
		}
slouken@0
   559
		chunk->volume = volume;
slouken@0
   560
	}
slouken@0
   561
	return(prev_volume);
slouken@0
   562
}
slouken@0
   563
slouken@0
   564
/* Halt playing of a particular channel */
slouken@0
   565
int Mix_HaltChannel(int which)
slouken@0
   566
{
slouken@0
   567
	int i;
slouken@0
   568
slouken@0
   569
	if ( which == -1 ) {
megastep@7
   570
		for ( i=0; i<num_channels; ++i ) {
slouken@0
   571
			Mix_HaltChannel(i);
slouken@0
   572
		}
slouken@0
   573
	} else {
slouken@0
   574
		SDL_mutexP(mixer_lock);
slouken@0
   575
		channel[which].playing = 0;
megastep@7
   576
		channel[which].expire = -1;
megastep@4
   577
		if(channel[which].fading != MIX_NO_FADING) /* Restore volume */
megastep@4
   578
			channel[which].volume = channel[which].fade_volume;
megastep@4
   579
		channel[which].fading = MIX_NO_FADING;
slouken@0
   580
		SDL_mutexV(mixer_lock);
slouken@0
   581
	}
slouken@0
   582
	return(0);
slouken@0
   583
}
slouken@0
   584
megastep@7
   585
/* Halt playing of a particular group of channels */
megastep@7
   586
int Mix_HaltGroup(int tag)
megastep@7
   587
{
megastep@7
   588
	int i;
megastep@7
   589
megastep@7
   590
	for ( i=0; i<num_channels; ++i ) {
megastep@7
   591
		if( channel[i].tag == tag ) {
megastep@7
   592
			Mix_HaltChannel(i);
megastep@7
   593
		}
megastep@7
   594
	}
megastep@7
   595
	return(0);
megastep@7
   596
}
megastep@7
   597
megastep@4
   598
/* Fade out a channel and then stop it automatically */
megastep@4
   599
int Mix_FadeOutChannel(int which, int ms)
megastep@4
   600
{
megastep@4
   601
	int status;
megastep@4
   602
megastep@4
   603
	status = 0;
megastep@4
   604
	if ( which == -1 ) {
megastep@4
   605
		int i;
megastep@4
   606
megastep@7
   607
		for ( i=0; i<num_channels; ++i ) {
megastep@4
   608
			status += Mix_FadeOutChannel(i,ms);
megastep@4
   609
		}
megastep@4
   610
	} else {
megastep@4
   611
		SDL_mutexP(mixer_lock);
megastep@4
   612
		if ( channel[which].playing && channel[which].volume>0 && 
megastep@4
   613
			 channel[which].fading==MIX_NO_FADING ) {
megastep@4
   614
megastep@4
   615
			channel[which].fading = MIX_FADING_OUT;
megastep@4
   616
			channel[which].fade_volume = channel[which].volume;
megastep@4
   617
			channel[which].fade_length = ms;
megastep@4
   618
			channel[which].ticks_fade = SDL_GetTicks();
megastep@4
   619
			++ status;
megastep@4
   620
		}
megastep@4
   621
		SDL_mutexV(mixer_lock);
megastep@4
   622
	}
megastep@4
   623
	return(status);
megastep@4
   624
}
megastep@4
   625
megastep@7
   626
/* Halt playing of a particular group of channels */
megastep@7
   627
int Mix_FadeOutGroup(int tag, int ms)
megastep@7
   628
{
megastep@7
   629
	int i;
megastep@7
   630
	int status = 0;
megastep@7
   631
	for ( i=0; i<num_channels; ++i ) {
megastep@7
   632
		if( channel[i].tag == tag ) {
megastep@7
   633
			status += Mix_FadeOutChannel(i,ms);
megastep@7
   634
		}
megastep@7
   635
	}
megastep@7
   636
	return(status);
megastep@7
   637
}
megastep@7
   638
megastep@4
   639
Mix_Fading Mix_FadingChannel(int which)
megastep@4
   640
{
megastep@4
   641
	return channel[which].fading;
megastep@4
   642
}
megastep@4
   643
slouken@0
   644
/* Check the status of a specific channel.
slouken@0
   645
   If the specified channel is -1, check all channels.
slouken@0
   646
*/
slouken@0
   647
int Mix_Playing(int which)
slouken@0
   648
{
slouken@0
   649
	int status;
slouken@0
   650
slouken@0
   651
	status = 0;
slouken@0
   652
	if ( which == -1 ) {
slouken@0
   653
		int i;
slouken@0
   654
megastep@7
   655
		for ( i=0; i<num_channels; ++i ) {
slouken@0
   656
			if ( channel[i].playing > 0 ) {
slouken@0
   657
				++status;
slouken@0
   658
			}
slouken@0
   659
		}
slouken@0
   660
	} else {
slouken@0
   661
		if ( channel[which].playing > 0 ) {
slouken@0
   662
			++status;
slouken@0
   663
		}
slouken@0
   664
	}
slouken@0
   665
	return(status);
slouken@0
   666
}
slouken@0
   667
slouken@0
   668
/* Close the mixer, halting all playing audio */
slouken@0
   669
void Mix_CloseAudio(void)
slouken@0
   670
{
slouken@0
   671
	if ( audio_opened ) {
slouken@0
   672
		if ( audio_opened == 1 ) {
slouken@0
   673
			close_music();
slouken@0
   674
			Mix_HaltChannel(-1);
slouken@0
   675
			SDL_CloseAudio();
slouken@0
   676
			SDL_DestroyMutex(mixer_lock);
megastep@8
   677
			free(channel);
megastep@7
   678
			channel = NULL;
slouken@0
   679
		}
slouken@0
   680
		--audio_opened;
slouken@0
   681
	}
slouken@0
   682
}
slouken@0
   683
slouken@0
   684
/* Pause a particular channel (or all) */
slouken@0
   685
void Mix_Pause(int which)
slouken@0
   686
{
megastep@8
   687
	Uint32 sdl_ticks = SDL_GetTicks();
slouken@0
   688
	if ( which == -1 ) {
slouken@0
   689
		int i;
slouken@0
   690
megastep@7
   691
		for ( i=0; i<num_channels; ++i ) {
slouken@0
   692
			if ( channel[i].playing > 0 ) {
megastep@8
   693
				channel[i].paused = sdl_ticks;
slouken@0
   694
			}
slouken@0
   695
		}
slouken@0
   696
	} else {
slouken@0
   697
		if ( channel[which].playing > 0 ) {
megastep@8
   698
			channel[which].paused = sdl_ticks;
slouken@0
   699
		}
slouken@0
   700
	}
slouken@0
   701
}
slouken@0
   702
slouken@0
   703
/* Resume a paused channel */
slouken@0
   704
void Mix_Resume(int which)
slouken@0
   705
{
megastep@8
   706
	Uint32 sdl_ticks = SDL_GetTicks();
slouken@0
   707
	if ( which == -1 ) {
slouken@0
   708
		int i;
slouken@0
   709
megastep@8
   710
		SDL_mutexP(mixer_lock);
megastep@7
   711
		for ( i=0; i<num_channels; ++i ) {
slouken@0
   712
			if ( channel[i].playing > 0 ) {
megastep@8
   713
				if(channel[i].expire > 0)
megastep@8
   714
					channel[i].expire += sdl_ticks - channel[i].paused;
slouken@0
   715
				channel[i].paused = 0;
slouken@0
   716
			}
slouken@0
   717
		}
megastep@8
   718
		SDL_mutexV(mixer_lock);
slouken@0
   719
	} else {
megastep@8
   720
		SDL_mutexP(mixer_lock);
slouken@0
   721
		if ( channel[which].playing > 0 ) {
megastep@8
   722
			if(channel[which].expire > 0)
megastep@8
   723
				channel[which].expire += sdl_ticks - channel[which].paused;
slouken@0
   724
			channel[which].paused = 0;
slouken@0
   725
		}
megastep@8
   726
		SDL_mutexV(mixer_lock);
slouken@0
   727
	}
slouken@0
   728
}
megastep@7
   729
megastep@13
   730
int Mix_Paused(int which)
megastep@13
   731
{
megastep@14
   732
	if ( which > num_channels )
megastep@13
   733
		return(0);
megastep@14
   734
	if ( which < 0 ) {
megastep@14
   735
		int status = 0;
megastep@14
   736
		int i;
megastep@14
   737
		for( i=0; i < num_channels; ++i ) {
megastep@14
   738
			if ( channel[i].paused ) {
megastep@14
   739
				++ status;
megastep@14
   740
			}
megastep@14
   741
		}
megastep@14
   742
		return(status);
megastep@14
   743
	} else {
megastep@14
   744
		return(channel[which].paused != 0);
megastep@14
   745
	}
megastep@13
   746
}
megastep@13
   747
megastep@7
   748
/* Change the group of a channel */
megastep@7
   749
int Mix_GroupChannel(int which, int tag)
megastep@7
   750
{
megastep@7
   751
	if ( which < 0 || which > num_channels )
megastep@7
   752
		return(0);
megastep@7
   753
megastep@7
   754
	SDL_mutexP(mixer_lock);
megastep@7
   755
	channel[which].tag = tag;
megastep@7
   756
	SDL_mutexV(mixer_lock);
megastep@7
   757
	return(1);
megastep@7
   758
}
megastep@7
   759
megastep@7
   760
/* Assign several consecutive channels to a group */
megastep@7
   761
int Mix_GroupChannels(int from, int to, int tag)
megastep@7
   762
{
megastep@7
   763
	int status = 0;
megastep@7
   764
	for( ; from <= to; ++ from ) {
megastep@7
   765
		status += Mix_GroupChannel(from, tag);
megastep@7
   766
	}
megastep@7
   767
	return(status);
megastep@7
   768
}
megastep@7
   769
megastep@7
   770
/* Finds the first available channel in a group of channels */
megastep@7
   771
int Mix_GroupAvailable(int tag)
megastep@7
   772
{
megastep@7
   773
	int i;
megastep@7
   774
	for( i=0; i < num_channels; i ++ ) {
megastep@7
   775
		if ( (channel[i].tag==tag || tag==-1) && channel[i].playing<=0 )
megastep@7
   776
			return i;
megastep@7
   777
	}
megastep@7
   778
	return(-1);
megastep@7
   779
}
megastep@7
   780
megastep@7
   781
int Mix_GroupCount(int tag)
megastep@7
   782
{
megastep@7
   783
	int count = 0;
megastep@7
   784
	int i;
megastep@7
   785
	for( i=0; i < num_channels; i ++ ) {
megastep@7
   786
		if ( channel[i].tag==tag || tag==-1 )
megastep@7
   787
			++ count;
megastep@7
   788
	}
megastep@7
   789
	return(count);
megastep@7
   790
}
megastep@8
   791
megastep@8
   792
/* Finds the "oldest" sample playing in a group of channels */
megastep@8
   793
int Mix_GroupOldest(int tag)
megastep@8
   794
{
megastep@8
   795
	int chan = -1;
megastep@8
   796
	Uint32 mintime = SDL_GetTicks();
megastep@8
   797
	int i;
megastep@8
   798
	for( i=0; i < num_channels; i ++ ) {
megastep@8
   799
		if ( (channel[i].tag==tag || tag==-1) && channel[i].playing > 0 
megastep@8
   800
			 && channel[i].start_time <= mintime ) {
megastep@8
   801
			mintime = channel[i].start_time;
megastep@8
   802
			chan = i;
megastep@8
   803
		}
megastep@8
   804
	}
megastep@8
   805
	return(chan);
megastep@8
   806
}
megastep@8
   807
megastep@8
   808
/* Finds the "most recent" (i.e. last) sample playing in a group of channels */
megastep@8
   809
int Mix_GroupNewer(int tag)
megastep@8
   810
{
megastep@8
   811
	int chan = -1;
megastep@8
   812
	Uint32 maxtime = 0;
megastep@8
   813
	int i;
megastep@8
   814
	for( i=0; i < num_channels; i ++ ) {
megastep@8
   815
		if ( (channel[i].tag==tag || tag==-1) && channel[i].playing > 0 
megastep@8
   816
			 && channel[i].start_time >= maxtime ) {
megastep@8
   817
			maxtime = channel[i].start_time;
megastep@8
   818
			chan = i;
megastep@8
   819
		}
megastep@8
   820
	}
megastep@8
   821
	return(chan);
megastep@8
   822
}