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