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