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