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