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