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