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