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