mixer.c
author Stephane Peter <megastep@lokigames.com>
Sat, 23 Oct 1999 03:31:21 +0000
changeset 4 5c4485704b30
parent 1 4ef9250a6471
child 7 e73da9dc4bdc
permissions -rw-r--r--
Added fading in/out of music and samples.
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
slouken@0
    25
#include <stdio.h>
slouken@0
    26
#include <stdlib.h>
slouken@0
    27
#include <string.h>
slouken@0
    28
slouken@0
    29
#include <SDL/SDL_mutex.h>
slouken@0
    30
#include <SDL/SDL_endian.h>
slouken@0
    31
slouken@0
    32
#include "mixer.h"
slouken@0
    33
slouken@0
    34
static int audio_opened = 0;
slouken@0
    35
slouken@0
    36
static SDL_AudioSpec mixer;
slouken@0
    37
static SDL_mutex *mixer_lock;
slouken@0
    38
static struct _Mix_Channel {
slouken@0
    39
	Mix_Chunk *chunk;
slouken@0
    40
	int playing;
slouken@0
    41
	int paused;
slouken@0
    42
	Uint8 *samples;
slouken@0
    43
	int volume;
slouken@0
    44
	int looping;
megastep@4
    45
	Mix_Fading fading;
megastep@4
    46
	int fade_volume;
megastep@4
    47
	int fade_length;
megastep@4
    48
	Uint32 ticks_fade;
slouken@0
    49
} channel[MIX_CHANNELS];
slouken@0
    50
slouken@0
    51
#define MUSIC_VOL	64		/* Music volume 0-64 */
slouken@0
    52
slouken@0
    53
/* Support for user defined music functions, plus the default one */
slouken@0
    54
extern int music_active;
slouken@0
    55
extern void music_mixer(void *udata, Uint8 *stream, int len);
slouken@0
    56
static void (*mix_music)(void *udata, Uint8 *stream, int len) = music_mixer;
slouken@0
    57
static int num_channels; /* Holds the number of channels actually mixed, for debugging */
slouken@0
    58
static int reserved_channels = 0;
slouken@0
    59
void *music_data = NULL;
slouken@0
    60
slouken@0
    61
/* Mixing function */
slouken@0
    62
static void mix_channels(void *udata, Uint8 *stream, int len)
slouken@0
    63
{
slouken@0
    64
	int i, mixable, volume;
slouken@0
    65
slouken@0
    66
	/* Grab the channels we need to mix */
slouken@0
    67
	SDL_mutexP(mixer_lock);
slouken@0
    68
	num_channels = 0;
slouken@0
    69
	for ( i=0; i<MIX_CHANNELS; ++i ) {
megastep@4
    70
		if ( channel[i].fading != MIX_NO_FADING ) {
megastep@4
    71
			Uint32 ticks = SDL_GetTicks() - channel[i].ticks_fade;
megastep@4
    72
			if( ticks > channel[i].fade_length ) {
megastep@4
    73
				if( channel[i].fading == MIX_FADING_OUT ) {
megastep@4
    74
					channel[i].playing = 0;
megastep@4
    75
					Mix_Volume(i, channel[i].fading); /* Restore the volume */
megastep@4
    76
				}
megastep@4
    77
				channel[i].fading = MIX_NO_FADING;
megastep@4
    78
			} else {
megastep@4
    79
				if( channel[i].fading == MIX_FADING_OUT ) {
megastep@4
    80
					Mix_Volume(i, (channel[i].fade_volume * (channel[i].fade_length-ticks))
megastep@4
    81
							   / channel[i].fade_length );
megastep@4
    82
				} else {
megastep@4
    83
					Mix_Volume(i, (channel[i].fade_volume * ticks) / channel[i].fade_length );
megastep@4
    84
				}
megastep@4
    85
			}
megastep@4
    86
		}
slouken@0
    87
		if ( channel[i].playing && !channel[i].paused ) {
slouken@0
    88
		    ++ num_channels;
slouken@0
    89
			mixable = channel[i].playing;
slouken@0
    90
			if ( mixable > len ) {
slouken@0
    91
				mixable = len;
slouken@0
    92
			}
slouken@0
    93
			volume = (channel[i].volume*channel[i].chunk->volume) /
slouken@0
    94
								MIX_MAX_VOLUME;
slouken@0
    95
			SDL_MixAudio(stream,channel[i].samples,mixable,volume);
slouken@0
    96
			channel[i].samples += mixable;
slouken@0
    97
			channel[i].playing -= mixable;
slouken@0
    98
			if ( ! channel[i].playing && channel[i].looping ) {
slouken@0
    99
				if ( --channel[i].looping ) {
slouken@0
   100
				    channel[i].samples = channel[i].chunk->abuf;
slouken@0
   101
				    channel[i].playing = channel[i].chunk->alen;
slouken@0
   102
				}
slouken@0
   103
			}
slouken@0
   104
		}
slouken@0
   105
	}
slouken@0
   106
	SDL_mutexV(mixer_lock);
slouken@0
   107
slouken@0
   108
	/* Mix the music */
slouken@0
   109
	if ( music_active ) {
slouken@0
   110
		mix_music(music_data, stream, len);
slouken@0
   111
	}
slouken@0
   112
}
slouken@0
   113
slouken@0
   114
static void PrintFormat(char *title, SDL_AudioSpec *fmt)
slouken@0
   115
{
slouken@0
   116
	printf("%s: %d bit %s audio (%s) at %u Hz\n", title, (fmt->format&0xFF),
slouken@0
   117
			(fmt->format&0x8000) ? "signed" : "unsigned",
slouken@0
   118
			(fmt->channels > 1) ? "stereo" : "mono", fmt->freq);
slouken@0
   119
}
slouken@0
   120
slouken@0
   121
/* Open the mixer with a certain desired audio format */
slouken@0
   122
int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize)
slouken@0
   123
{
slouken@0
   124
	int i;
slouken@0
   125
	SDL_AudioSpec desired;
slouken@0
   126
slouken@0
   127
	/* If the mixer is already opened, increment open count */
slouken@0
   128
	if ( audio_opened ) {
slouken@0
   129
	    ++audio_opened;
slouken@0
   130
	    return(0);
slouken@0
   131
	}
slouken@0
   132
slouken@0
   133
	/* Set the desired format and frequency */
slouken@0
   134
	desired.freq = frequency;
slouken@0
   135
	desired.format = format;
slouken@0
   136
	desired.channels = channels;
slouken@0
   137
	desired.samples = chunksize;
slouken@0
   138
	desired.callback = mix_channels;
slouken@0
   139
	desired.userdata = NULL;
slouken@0
   140
slouken@0
   141
	/* Accept nearly any audio format */
slouken@0
   142
	if ( SDL_OpenAudio(&desired, &mixer) < 0 ) {
slouken@0
   143
		return(-1);
slouken@0
   144
	}
slouken@0
   145
#if 0
slouken@0
   146
	PrintFormat("Audio device", &mixer);
slouken@0
   147
#endif
slouken@0
   148
slouken@0
   149
	/* Create the channel lock mutex */
slouken@0
   150
	mixer_lock = SDL_CreateMutex();
slouken@0
   151
	if ( mixer_lock == NULL ) {
slouken@0
   152
		SDL_CloseAudio();
slouken@0
   153
		SDL_SetError("Unable to create mixer lock");
slouken@0
   154
		return(-1);
slouken@0
   155
	}
slouken@0
   156
slouken@0
   157
	/* Initialize the music players */
slouken@0
   158
	if ( open_music(&mixer) < 0 ) {
slouken@0
   159
		SDL_CloseAudio();
slouken@0
   160
		SDL_DestroyMutex(mixer_lock);
slouken@0
   161
		return(-1);
slouken@0
   162
	}
slouken@0
   163
	
slouken@0
   164
	/* Clear out the audio channels */
slouken@0
   165
	for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   166
		channel[i].chunk = NULL;
slouken@0
   167
		channel[i].playing = 0;
slouken@0
   168
		channel[i].looping = 0;
slouken@0
   169
		channel[i].volume = SDL_MIX_MAXVOLUME;
slouken@0
   170
	}
slouken@0
   171
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   172
	audio_opened = 1;
slouken@0
   173
	SDL_PauseAudio(0);
slouken@0
   174
	return(0);
slouken@0
   175
}
slouken@0
   176
slouken@0
   177
/* Return the actual mixer parameters */
slouken@0
   178
int Mix_QuerySpec(int *frequency, Uint16 *format, int *channels)
slouken@0
   179
{
slouken@0
   180
	if ( audio_opened ) {
slouken@0
   181
		if ( frequency ) {
slouken@0
   182
			*frequency = mixer.freq;
slouken@0
   183
		}
slouken@0
   184
		if ( format ) {
slouken@0
   185
			*format = mixer.format;
slouken@0
   186
		}
slouken@0
   187
		if ( channels ) {
slouken@0
   188
			*channels = mixer.channels;
slouken@0
   189
		}
slouken@0
   190
	}
slouken@0
   191
	return(audio_opened);
slouken@0
   192
}
slouken@0
   193
slouken@0
   194
/* Load a wave file */
slouken@0
   195
Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
slouken@0
   196
{
slouken@0
   197
	Mix_Chunk *chunk;
slouken@0
   198
	SDL_AudioSpec wavespec;
slouken@0
   199
	SDL_AudioCVT wavecvt;
slouken@0
   200
slouken@0
   201
	/* Make sure audio has been opened */
slouken@0
   202
	if ( ! audio_opened ) {
slouken@0
   203
		SDL_SetError("Audio device hasn't been opened");
slouken@0
   204
		if ( freesrc ) {
slouken@0
   205
			SDL_RWclose(src);
slouken@0
   206
		}
slouken@0
   207
		return(NULL);
slouken@0
   208
	}
slouken@0
   209
slouken@0
   210
	/* Allocate the chunk memory */
slouken@0
   211
	chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk));
slouken@0
   212
	if ( chunk == NULL ) {
slouken@0
   213
		SDL_SetError("Out of memory");
slouken@0
   214
		if ( freesrc ) {
slouken@0
   215
			SDL_RWclose(src);
slouken@0
   216
		}
slouken@0
   217
		return(NULL);
slouken@0
   218
	}
slouken@0
   219
slouken@0
   220
	/* Load the WAV file into the chunk */
slouken@0
   221
	if ( SDL_LoadWAV_RW(src, freesrc,
slouken@0
   222
		&wavespec, (Uint8 **)&chunk->abuf, &chunk->alen) == NULL ) {
slouken@0
   223
		free(chunk);
slouken@0
   224
		return(NULL);
slouken@0
   225
	}
slouken@0
   226
#if 0
slouken@0
   227
	PrintFormat("Audio device", &mixer);
slouken@0
   228
	PrintFormat("-- Wave file", &wavespec);
slouken@0
   229
#endif
slouken@0
   230
slouken@0
   231
	/* Build the audio converter and create conversion buffers */
slouken@0
   232
	if ( SDL_BuildAudioCVT(&wavecvt,
slouken@0
   233
			wavespec.format, wavespec.channels, wavespec.freq,
slouken@0
   234
			mixer.format, mixer.channels, mixer.freq) < 0 ) {
slouken@0
   235
		SDL_FreeWAV(chunk->abuf);
slouken@0
   236
		free(chunk);
slouken@0
   237
		return(NULL);
slouken@0
   238
	}
slouken@0
   239
	wavecvt.len = chunk->alen;
slouken@0
   240
	wavecvt.buf = (Uint8 *)malloc(wavecvt.len*wavecvt.len_mult);
slouken@0
   241
	if ( wavecvt.buf == NULL ) {
slouken@0
   242
		SDL_SetError("Out of memory");
slouken@0
   243
		SDL_FreeWAV(chunk->abuf);
slouken@0
   244
		free(chunk);
slouken@0
   245
		return(NULL);
slouken@0
   246
	}
slouken@0
   247
	memcpy(wavecvt.buf, chunk->abuf, chunk->alen);
slouken@0
   248
	SDL_FreeWAV(chunk->abuf);
slouken@0
   249
slouken@0
   250
	/* Run the audio converter */
slouken@0
   251
	if ( SDL_ConvertAudio(&wavecvt) < 0 ) {
slouken@0
   252
		free(wavecvt.buf);
slouken@0
   253
		free(chunk);
slouken@0
   254
		return(NULL);
slouken@0
   255
	}
slouken@1
   256
	chunk->allocated = 1;
slouken@0
   257
	chunk->abuf = wavecvt.buf;
slouken@0
   258
	chunk->alen = wavecvt.len_cvt;
slouken@0
   259
	chunk->volume = MIX_MAX_VOLUME;
slouken@0
   260
	return(chunk);
slouken@0
   261
}
slouken@0
   262
slouken@1
   263
/* Load a wave file of the mixer format from a memory buffer */
slouken@1
   264
Mix_Chunk *Mix_QuickLoad_WAV(Uint8 *mem)
slouken@1
   265
{
slouken@1
   266
	Mix_Chunk *chunk;
slouken@1
   267
	Uint8 magic[4];
slouken@1
   268
slouken@1
   269
	/* Make sure audio has been opened */
slouken@1
   270
	if ( ! audio_opened ) {
slouken@1
   271
		SDL_SetError("Audio device hasn't been opened");
slouken@1
   272
		return(NULL);
slouken@1
   273
	}
slouken@1
   274
slouken@1
   275
	/* Allocate the chunk memory */
slouken@1
   276
	chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk));
slouken@1
   277
	if ( chunk == NULL ) {
slouken@1
   278
		SDL_SetError("Out of memory");
slouken@1
   279
		return(NULL);
slouken@1
   280
	}
slouken@1
   281
slouken@1
   282
	/* Essentially just skip to the audio data (no error checking - fast) */
slouken@1
   283
	chunk->allocated = 0;
slouken@1
   284
	mem += 12; /* WAV header */
slouken@1
   285
	do {
slouken@1
   286
		memcpy(magic, mem, 4);
slouken@1
   287
		mem += 4;
slouken@1
   288
		chunk->alen = ((mem[3]<<24)|(mem[2]<<16)|(mem[1]<<8)|(mem[0]));
slouken@1
   289
		mem += 4;
slouken@1
   290
		chunk->abuf = mem;
slouken@1
   291
		mem += chunk->alen;
slouken@1
   292
	} while ( memcmp(magic, "data", 4) != 0 );
slouken@1
   293
	chunk->volume = MIX_MAX_VOLUME;
slouken@1
   294
slouken@1
   295
	return(chunk);
slouken@1
   296
}
slouken@1
   297
slouken@0
   298
/* Free an audio chunk previously loaded */
slouken@0
   299
void Mix_FreeChunk(Mix_Chunk *chunk)
slouken@0
   300
{
slouken@0
   301
	int i;
slouken@0
   302
slouken@0
   303
	/* Caution -- if the chunk is playing, the mixer will crash */
slouken@0
   304
	if ( chunk ) {
slouken@0
   305
		/* Guarantee that this chunk isn't playing */
slouken@0
   306
		SDL_mutexP(mixer_lock);
slouken@0
   307
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   308
			if ( chunk == channel[i].chunk ) {
slouken@0
   309
				channel[i].playing = 0;
slouken@0
   310
			}
slouken@0
   311
		}
slouken@0
   312
		SDL_mutexV(mixer_lock);
slouken@0
   313
slouken@0
   314
		/* Actually free the chunk */
slouken@1
   315
		if ( chunk->allocated ) {
slouken@1
   316
			free(chunk->abuf);
slouken@1
   317
		}
slouken@0
   318
		free(chunk);
slouken@0
   319
	}
slouken@0
   320
}
slouken@0
   321
slouken@0
   322
/* Add your own music player or mixer function.
slouken@0
   323
   If 'mix_func' is NULL, the default music player is re-enabled.
slouken@0
   324
 */
slouken@0
   325
void Mix_HookMusic(void (*mix_func)(void *udata, Uint8 *stream, int len),
slouken@0
   326
                                                                void *arg)
slouken@0
   327
{
slouken@0
   328
	SDL_LockAudio();
slouken@0
   329
	if ( mix_func != NULL ) {
slouken@0
   330
		music_data = arg;
slouken@0
   331
		mix_music = mix_func;
slouken@0
   332
	} else {
slouken@0
   333
		music_data = NULL;
slouken@0
   334
		mix_music = music_mixer;
slouken@0
   335
	}
slouken@0
   336
	SDL_UnlockAudio();
slouken@0
   337
}
slouken@0
   338
slouken@0
   339
void *Mix_GetMusicHookData(void)
slouken@0
   340
{
slouken@0
   341
	return(music_data);
slouken@0
   342
}
slouken@0
   343
slouken@0
   344
/* Reserve the first channels (0 -> n-1) for the application, i.e. don't allocate
slouken@0
   345
   them dynamically to the next sample if requested with a -1 value below.
slouken@0
   346
   Returns the number of reserved channels.
slouken@0
   347
 */
slouken@0
   348
int Mix_ReserveChannels(int num)
slouken@0
   349
{
slouken@0
   350
	if (num > MIX_CHANNELS)
slouken@0
   351
		num = MIX_CHANNELS;
slouken@0
   352
	reserved_channels = num;
slouken@0
   353
	return num;
slouken@0
   354
}
slouken@0
   355
slouken@0
   356
/* Play an audio chunk on a specific channel.
slouken@0
   357
   If the specified channel is -1, play on the first free channel.
slouken@0
   358
   Returns which channel was used to play the sound.
slouken@0
   359
*/
slouken@0
   360
int Mix_PlayChannel(int which, Mix_Chunk *chunk, int loops)
slouken@0
   361
{
slouken@0
   362
	int i;
slouken@0
   363
slouken@0
   364
	/* Don't play null pointers :-) */
slouken@0
   365
	if ( chunk == NULL ) {
slouken@0
   366
		return(-1);
slouken@0
   367
	}
slouken@0
   368
slouken@0
   369
	/* Lock the mixer while modifying the playing channels */
slouken@0
   370
	SDL_mutexP(mixer_lock);
slouken@0
   371
	{
slouken@0
   372
		/* If which is -1, play on the first free channel */
slouken@0
   373
		if ( which == -1 ) {
slouken@0
   374
			for ( i=reserved_channels; i<MIX_CHANNELS; ++i ) {
slouken@0
   375
				if ( channel[i].playing <= 0 )
slouken@0
   376
					break;
slouken@0
   377
			}
slouken@0
   378
			if ( i == MIX_CHANNELS ) {
slouken@0
   379
				which = -1;
slouken@0
   380
			} else {
slouken@0
   381
				which = i;
slouken@0
   382
			}
slouken@0
   383
		}
slouken@0
   384
slouken@0
   385
		/* Queue up the audio data for this channel */
slouken@0
   386
		if ( which >= 0 ) {
slouken@0
   387
			channel[which].samples = chunk->abuf;
slouken@0
   388
			channel[which].playing = chunk->alen;
slouken@0
   389
			channel[which].looping = loops;
slouken@0
   390
			channel[which].chunk = chunk;
slouken@0
   391
			channel[which].paused = 0;
megastep@4
   392
			channel[which].fading = MIX_NO_FADING;
megastep@4
   393
		}
megastep@4
   394
	}
megastep@4
   395
	SDL_mutexV(mixer_lock);
megastep@4
   396
megastep@4
   397
	/* Return the channel on which the sound is being played */
megastep@4
   398
	return(which);
megastep@4
   399
}
megastep@4
   400
megastep@4
   401
/* Fade in a sound on a channel, over ms milliseconds */
megastep@4
   402
int Mix_FadeInChannel(int which, Mix_Chunk *chunk, int loops, int ms)
megastep@4
   403
{
megastep@4
   404
	int i;
megastep@4
   405
megastep@4
   406
	/* Don't play null pointers :-) */
megastep@4
   407
	if ( chunk == NULL ) {
megastep@4
   408
		return(-1);
megastep@4
   409
	}
megastep@4
   410
megastep@4
   411
	/* Lock the mixer while modifying the playing channels */
megastep@4
   412
	SDL_mutexP(mixer_lock);
megastep@4
   413
	{
megastep@4
   414
		/* If which is -1, play on the first free channel */
megastep@4
   415
		if ( which == -1 ) {
megastep@4
   416
			for ( i=reserved_channels; i<MIX_CHANNELS; ++i ) {
megastep@4
   417
				if ( channel[i].playing <= 0 )
megastep@4
   418
					break;
megastep@4
   419
			}
megastep@4
   420
			if ( i == MIX_CHANNELS ) {
megastep@4
   421
				which = -1;
megastep@4
   422
			} else {
megastep@4
   423
				which = i;
megastep@4
   424
			}
megastep@4
   425
		}
megastep@4
   426
megastep@4
   427
		/* Queue up the audio data for this channel */
megastep@4
   428
		if ( which >= 0 ) {
megastep@4
   429
			channel[which].samples = chunk->abuf;
megastep@4
   430
			channel[which].playing = chunk->alen;
megastep@4
   431
			channel[which].looping = loops;
megastep@4
   432
			channel[which].chunk = chunk;
megastep@4
   433
			channel[which].paused = 0;
megastep@4
   434
			channel[which].fading = MIX_FADING_IN;
megastep@4
   435
			channel[which].fade_volume = channel[which].volume;
megastep@4
   436
			channel[which].volume = 0;
megastep@4
   437
			channel[which].fade_length = ms;
megastep@4
   438
			channel[which].ticks_fade = SDL_GetTicks();
slouken@0
   439
		}
slouken@0
   440
	}
slouken@0
   441
	SDL_mutexV(mixer_lock);
slouken@0
   442
slouken@0
   443
	/* Return the channel on which the sound is being played */
slouken@0
   444
	return(which);
slouken@0
   445
}
slouken@0
   446
slouken@0
   447
/* Set volume of a particular channel */
slouken@0
   448
int Mix_Volume(int which, int volume)
slouken@0
   449
{
slouken@0
   450
	int i;
slouken@0
   451
	int prev_volume;
slouken@0
   452
slouken@0
   453
	if ( which == -1 ) {
slouken@0
   454
		prev_volume = 0;
slouken@0
   455
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   456
			prev_volume += Mix_Volume(i, volume);
slouken@0
   457
		}
slouken@0
   458
		prev_volume /= MIX_CHANNELS;
slouken@0
   459
	} else {
slouken@0
   460
		prev_volume = channel[which].volume;
megastep@4
   461
		if ( volume < 0 ) {
megastep@4
   462
			volume = 0;
slouken@0
   463
		}
megastep@4
   464
		if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
   465
			volume = SDL_MIX_MAXVOLUME;
megastep@4
   466
		}
megastep@4
   467
		channel[which].volume = volume;
slouken@0
   468
	}
slouken@0
   469
	return(prev_volume);
slouken@0
   470
}
slouken@0
   471
/* Set volume of a particular chunk */
slouken@0
   472
int Mix_VolumeChunk(Mix_Chunk *chunk, int volume)
slouken@0
   473
{
slouken@0
   474
	int prev_volume;
slouken@0
   475
slouken@0
   476
	prev_volume = chunk->volume;
slouken@0
   477
	if ( volume >= 0 ) {
slouken@0
   478
		if ( volume > MIX_MAX_VOLUME ) {
slouken@0
   479
			volume = MIX_MAX_VOLUME;
slouken@0
   480
		}
slouken@0
   481
		chunk->volume = volume;
slouken@0
   482
	}
slouken@0
   483
	return(prev_volume);
slouken@0
   484
}
slouken@0
   485
slouken@0
   486
/* Halt playing of a particular channel */
slouken@0
   487
int Mix_HaltChannel(int which)
slouken@0
   488
{
slouken@0
   489
	int i;
slouken@0
   490
slouken@0
   491
	if ( which == -1 ) {
slouken@0
   492
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   493
			Mix_HaltChannel(i);
slouken@0
   494
		}
slouken@0
   495
	} else {
slouken@0
   496
		SDL_mutexP(mixer_lock);
slouken@0
   497
		channel[which].playing = 0;
megastep@4
   498
		if(channel[which].fading != MIX_NO_FADING) /* Restore volume */
megastep@4
   499
			channel[which].volume = channel[which].fade_volume;
megastep@4
   500
		channel[which].fading = MIX_NO_FADING;
slouken@0
   501
		SDL_mutexV(mixer_lock);
slouken@0
   502
	}
slouken@0
   503
	return(0);
slouken@0
   504
}
slouken@0
   505
megastep@4
   506
/* Fade out a channel and then stop it automatically */
megastep@4
   507
int Mix_FadeOutChannel(int which, int ms)
megastep@4
   508
{
megastep@4
   509
	int status;
megastep@4
   510
megastep@4
   511
	status = 0;
megastep@4
   512
	if ( which == -1 ) {
megastep@4
   513
		int i;
megastep@4
   514
megastep@4
   515
		for ( i=0; i<MIX_CHANNELS; ++i ) {
megastep@4
   516
			status += Mix_FadeOutChannel(i,ms);
megastep@4
   517
		}
megastep@4
   518
	} else {
megastep@4
   519
		SDL_mutexP(mixer_lock);
megastep@4
   520
		if ( channel[which].playing && channel[which].volume>0 && 
megastep@4
   521
			 channel[which].fading==MIX_NO_FADING ) {
megastep@4
   522
megastep@4
   523
			channel[which].fading = MIX_FADING_OUT;
megastep@4
   524
			channel[which].fade_volume = channel[which].volume;
megastep@4
   525
			channel[which].fade_length = ms;
megastep@4
   526
			channel[which].ticks_fade = SDL_GetTicks();
megastep@4
   527
			++ status;
megastep@4
   528
		}
megastep@4
   529
		SDL_mutexV(mixer_lock);
megastep@4
   530
	}
megastep@4
   531
	return(status);
megastep@4
   532
}
megastep@4
   533
megastep@4
   534
Mix_Fading Mix_FadingChannel(int which)
megastep@4
   535
{
megastep@4
   536
	return channel[which].fading;
megastep@4
   537
}
megastep@4
   538
slouken@0
   539
/* Check the status of a specific channel.
slouken@0
   540
   If the specified channel is -1, check all channels.
slouken@0
   541
*/
slouken@0
   542
int Mix_Playing(int which)
slouken@0
   543
{
slouken@0
   544
	int status;
slouken@0
   545
slouken@0
   546
	status = 0;
slouken@0
   547
	if ( which == -1 ) {
slouken@0
   548
		int i;
slouken@0
   549
slouken@0
   550
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   551
			if ( channel[i].playing > 0 ) {
slouken@0
   552
				++status;
slouken@0
   553
			}
slouken@0
   554
		}
slouken@0
   555
	} else {
slouken@0
   556
		if ( channel[which].playing > 0 ) {
slouken@0
   557
			++status;
slouken@0
   558
		}
slouken@0
   559
	}
slouken@0
   560
	return(status);
slouken@0
   561
}
slouken@0
   562
slouken@0
   563
/* Close the mixer, halting all playing audio */
slouken@0
   564
void Mix_CloseAudio(void)
slouken@0
   565
{
slouken@0
   566
	if ( audio_opened ) {
slouken@0
   567
		if ( audio_opened == 1 ) {
slouken@0
   568
			close_music();
slouken@0
   569
			Mix_HaltChannel(-1);
slouken@0
   570
			SDL_CloseAudio();
slouken@0
   571
			SDL_DestroyMutex(mixer_lock);
slouken@0
   572
		}
slouken@0
   573
		--audio_opened;
slouken@0
   574
	}
slouken@0
   575
}
slouken@0
   576
slouken@0
   577
/* Pause a particular channel (or all) */
slouken@0
   578
void Mix_Pause(int which)
slouken@0
   579
{
slouken@0
   580
	if ( which == -1 ) {
slouken@0
   581
		int i;
slouken@0
   582
slouken@0
   583
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   584
			if ( channel[i].playing > 0 ) {
slouken@0
   585
				channel[i].paused = 1;
slouken@0
   586
			}
slouken@0
   587
		}
slouken@0
   588
	} else {
slouken@0
   589
		if ( channel[which].playing > 0 ) {
slouken@0
   590
			channel[which].paused = 1;
slouken@0
   591
		}
slouken@0
   592
	}
slouken@0
   593
}
slouken@0
   594
slouken@0
   595
/* Resume a paused channel */
slouken@0
   596
void Mix_Resume(int which)
slouken@0
   597
{
slouken@0
   598
	if ( which == -1 ) {
slouken@0
   599
		int i;
slouken@0
   600
slouken@0
   601
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   602
			if ( channel[i].playing > 0 ) {
slouken@0
   603
				channel[i].paused = 0;
slouken@0
   604
			}
slouken@0
   605
		}
slouken@0
   606
	} else {
slouken@0
   607
		if ( channel[which].playing > 0 ) {
slouken@0
   608
			channel[which].paused = 0;
slouken@0
   609
		}
slouken@0
   610
	}
slouken@0
   611
}