mixer.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 21 Oct 1999 18:02:08 +0000
changeset 0 4ce2db4db959
child 1 4ef9250a6471
permissions -rw-r--r--
Initial revision
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;
slouken@0
    45
} channel[MIX_CHANNELS];
slouken@0
    46
slouken@0
    47
#define MUSIC_VOL	64		/* Music volume 0-64 */
slouken@0
    48
slouken@0
    49
/* Support for user defined music functions, plus the default one */
slouken@0
    50
extern int music_active;
slouken@0
    51
extern void music_mixer(void *udata, Uint8 *stream, int len);
slouken@0
    52
static void (*mix_music)(void *udata, Uint8 *stream, int len) = music_mixer;
slouken@0
    53
static int num_channels; /* Holds the number of channels actually mixed, for debugging */
slouken@0
    54
static int reserved_channels = 0;
slouken@0
    55
void *music_data = NULL;
slouken@0
    56
slouken@0
    57
/* Mixing function */
slouken@0
    58
static void mix_channels(void *udata, Uint8 *stream, int len)
slouken@0
    59
{
slouken@0
    60
	int i, mixable, volume;
slouken@0
    61
slouken@0
    62
	/* Grab the channels we need to mix */
slouken@0
    63
	SDL_mutexP(mixer_lock);
slouken@0
    64
	num_channels = 0;
slouken@0
    65
	for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
    66
		if ( channel[i].playing && !channel[i].paused ) {
slouken@0
    67
		    ++ num_channels;
slouken@0
    68
			mixable = channel[i].playing;
slouken@0
    69
			if ( mixable > len ) {
slouken@0
    70
				mixable = len;
slouken@0
    71
			}
slouken@0
    72
			volume = (channel[i].volume*channel[i].chunk->volume) /
slouken@0
    73
								MIX_MAX_VOLUME;
slouken@0
    74
			SDL_MixAudio(stream,channel[i].samples,mixable,volume);
slouken@0
    75
			channel[i].samples += mixable;
slouken@0
    76
			channel[i].playing -= mixable;
slouken@0
    77
			if ( ! channel[i].playing && channel[i].looping ) {
slouken@0
    78
				if ( --channel[i].looping ) {
slouken@0
    79
				    channel[i].samples = channel[i].chunk->abuf;
slouken@0
    80
				    channel[i].playing = channel[i].chunk->alen;
slouken@0
    81
				}
slouken@0
    82
			}
slouken@0
    83
		}
slouken@0
    84
	}
slouken@0
    85
	SDL_mutexV(mixer_lock);
slouken@0
    86
slouken@0
    87
	/* Mix the music */
slouken@0
    88
	if ( music_active ) {
slouken@0
    89
		mix_music(music_data, stream, len);
slouken@0
    90
	}
slouken@0
    91
}
slouken@0
    92
slouken@0
    93
static void PrintFormat(char *title, SDL_AudioSpec *fmt)
slouken@0
    94
{
slouken@0
    95
	printf("%s: %d bit %s audio (%s) at %u Hz\n", title, (fmt->format&0xFF),
slouken@0
    96
			(fmt->format&0x8000) ? "signed" : "unsigned",
slouken@0
    97
			(fmt->channels > 1) ? "stereo" : "mono", fmt->freq);
slouken@0
    98
}
slouken@0
    99
slouken@0
   100
/* Open the mixer with a certain desired audio format */
slouken@0
   101
int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize)
slouken@0
   102
{
slouken@0
   103
	int i;
slouken@0
   104
	SDL_AudioSpec desired;
slouken@0
   105
slouken@0
   106
	/* If the mixer is already opened, increment open count */
slouken@0
   107
	if ( audio_opened ) {
slouken@0
   108
	    ++audio_opened;
slouken@0
   109
	    return(0);
slouken@0
   110
	}
slouken@0
   111
slouken@0
   112
	/* Set the desired format and frequency */
slouken@0
   113
	desired.freq = frequency;
slouken@0
   114
	desired.format = format;
slouken@0
   115
	desired.channels = channels;
slouken@0
   116
	desired.samples = chunksize;
slouken@0
   117
	desired.callback = mix_channels;
slouken@0
   118
	desired.userdata = NULL;
slouken@0
   119
slouken@0
   120
	/* Accept nearly any audio format */
slouken@0
   121
	if ( SDL_OpenAudio(&desired, &mixer) < 0 ) {
slouken@0
   122
		return(-1);
slouken@0
   123
	}
slouken@0
   124
#if 0
slouken@0
   125
	PrintFormat("Audio device", &mixer);
slouken@0
   126
#endif
slouken@0
   127
slouken@0
   128
	/* Create the channel lock mutex */
slouken@0
   129
	mixer_lock = SDL_CreateMutex();
slouken@0
   130
	if ( mixer_lock == NULL ) {
slouken@0
   131
		SDL_CloseAudio();
slouken@0
   132
		SDL_SetError("Unable to create mixer lock");
slouken@0
   133
		return(-1);
slouken@0
   134
	}
slouken@0
   135
slouken@0
   136
	/* Initialize the music players */
slouken@0
   137
	if ( open_music(&mixer) < 0 ) {
slouken@0
   138
		SDL_CloseAudio();
slouken@0
   139
		SDL_DestroyMutex(mixer_lock);
slouken@0
   140
		return(-1);
slouken@0
   141
	}
slouken@0
   142
	
slouken@0
   143
	/* Clear out the audio channels */
slouken@0
   144
	for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   145
		channel[i].chunk = NULL;
slouken@0
   146
		channel[i].playing = 0;
slouken@0
   147
		channel[i].looping = 0;
slouken@0
   148
		channel[i].volume = SDL_MIX_MAXVOLUME;
slouken@0
   149
	}
slouken@0
   150
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   151
	audio_opened = 1;
slouken@0
   152
	SDL_PauseAudio(0);
slouken@0
   153
	return(0);
slouken@0
   154
}
slouken@0
   155
slouken@0
   156
/* Return the actual mixer parameters */
slouken@0
   157
int Mix_QuerySpec(int *frequency, Uint16 *format, int *channels)
slouken@0
   158
{
slouken@0
   159
	if ( audio_opened ) {
slouken@0
   160
		if ( frequency ) {
slouken@0
   161
			*frequency = mixer.freq;
slouken@0
   162
		}
slouken@0
   163
		if ( format ) {
slouken@0
   164
			*format = mixer.format;
slouken@0
   165
		}
slouken@0
   166
		if ( channels ) {
slouken@0
   167
			*channels = mixer.channels;
slouken@0
   168
		}
slouken@0
   169
	}
slouken@0
   170
	return(audio_opened);
slouken@0
   171
}
slouken@0
   172
slouken@0
   173
/* Load a wave file */
slouken@0
   174
Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
slouken@0
   175
{
slouken@0
   176
	Mix_Chunk *chunk;
slouken@0
   177
	SDL_AudioSpec wavespec;
slouken@0
   178
	SDL_AudioCVT wavecvt;
slouken@0
   179
slouken@0
   180
	/* Make sure audio has been opened */
slouken@0
   181
	if ( ! audio_opened ) {
slouken@0
   182
		SDL_SetError("Audio device hasn't been opened");
slouken@0
   183
		if ( freesrc ) {
slouken@0
   184
			SDL_RWclose(src);
slouken@0
   185
		}
slouken@0
   186
		return(NULL);
slouken@0
   187
	}
slouken@0
   188
slouken@0
   189
	/* Allocate the chunk memory */
slouken@0
   190
	chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk));
slouken@0
   191
	if ( chunk == NULL ) {
slouken@0
   192
		SDL_SetError("Out of memory");
slouken@0
   193
		if ( freesrc ) {
slouken@0
   194
			SDL_RWclose(src);
slouken@0
   195
		}
slouken@0
   196
		return(NULL);
slouken@0
   197
	}
slouken@0
   198
slouken@0
   199
	/* Load the WAV file into the chunk */
slouken@0
   200
	if ( SDL_LoadWAV_RW(src, freesrc,
slouken@0
   201
		&wavespec, (Uint8 **)&chunk->abuf, &chunk->alen) == NULL ) {
slouken@0
   202
		free(chunk);
slouken@0
   203
		return(NULL);
slouken@0
   204
	}
slouken@0
   205
#if 0
slouken@0
   206
	PrintFormat("Audio device", &mixer);
slouken@0
   207
	PrintFormat("-- Wave file", &wavespec);
slouken@0
   208
#endif
slouken@0
   209
slouken@0
   210
	/* Build the audio converter and create conversion buffers */
slouken@0
   211
	if ( SDL_BuildAudioCVT(&wavecvt,
slouken@0
   212
			wavespec.format, wavespec.channels, wavespec.freq,
slouken@0
   213
			mixer.format, mixer.channels, mixer.freq) < 0 ) {
slouken@0
   214
		SDL_FreeWAV(chunk->abuf);
slouken@0
   215
		free(chunk);
slouken@0
   216
		return(NULL);
slouken@0
   217
	}
slouken@0
   218
	wavecvt.len = chunk->alen;
slouken@0
   219
	wavecvt.buf = (Uint8 *)malloc(wavecvt.len*wavecvt.len_mult);
slouken@0
   220
	if ( wavecvt.buf == NULL ) {
slouken@0
   221
		SDL_SetError("Out of memory");
slouken@0
   222
		SDL_FreeWAV(chunk->abuf);
slouken@0
   223
		free(chunk);
slouken@0
   224
		return(NULL);
slouken@0
   225
	}
slouken@0
   226
	memcpy(wavecvt.buf, chunk->abuf, chunk->alen);
slouken@0
   227
	SDL_FreeWAV(chunk->abuf);
slouken@0
   228
slouken@0
   229
	/* Run the audio converter */
slouken@0
   230
	if ( SDL_ConvertAudio(&wavecvt) < 0 ) {
slouken@0
   231
		free(wavecvt.buf);
slouken@0
   232
		free(chunk);
slouken@0
   233
		return(NULL);
slouken@0
   234
	}
slouken@0
   235
	chunk->abuf = wavecvt.buf;
slouken@0
   236
	chunk->alen = wavecvt.len_cvt;
slouken@0
   237
	chunk->volume = MIX_MAX_VOLUME;
slouken@0
   238
	return(chunk);
slouken@0
   239
}
slouken@0
   240
slouken@0
   241
/* Free an audio chunk previously loaded */
slouken@0
   242
void Mix_FreeChunk(Mix_Chunk *chunk)
slouken@0
   243
{
slouken@0
   244
	int i;
slouken@0
   245
slouken@0
   246
	/* Caution -- if the chunk is playing, the mixer will crash */
slouken@0
   247
	if ( chunk ) {
slouken@0
   248
		/* Guarantee that this chunk isn't playing */
slouken@0
   249
		SDL_mutexP(mixer_lock);
slouken@0
   250
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   251
			if ( chunk == channel[i].chunk ) {
slouken@0
   252
				channel[i].playing = 0;
slouken@0
   253
			}
slouken@0
   254
		}
slouken@0
   255
		SDL_mutexV(mixer_lock);
slouken@0
   256
slouken@0
   257
		/* Actually free the chunk */
slouken@0
   258
		free(chunk->abuf);
slouken@0
   259
		free(chunk);
slouken@0
   260
	}
slouken@0
   261
}
slouken@0
   262
slouken@0
   263
/* Add your own music player or mixer function.
slouken@0
   264
   If 'mix_func' is NULL, the default music player is re-enabled.
slouken@0
   265
 */
slouken@0
   266
void Mix_HookMusic(void (*mix_func)(void *udata, Uint8 *stream, int len),
slouken@0
   267
                                                                void *arg)
slouken@0
   268
{
slouken@0
   269
	SDL_LockAudio();
slouken@0
   270
	if ( mix_func != NULL ) {
slouken@0
   271
		music_data = arg;
slouken@0
   272
		mix_music = mix_func;
slouken@0
   273
	} else {
slouken@0
   274
		music_data = NULL;
slouken@0
   275
		mix_music = music_mixer;
slouken@0
   276
	}
slouken@0
   277
	SDL_UnlockAudio();
slouken@0
   278
}
slouken@0
   279
slouken@0
   280
void *Mix_GetMusicHookData(void)
slouken@0
   281
{
slouken@0
   282
	return(music_data);
slouken@0
   283
}
slouken@0
   284
slouken@0
   285
/* Reserve the first channels (0 -> n-1) for the application, i.e. don't allocate
slouken@0
   286
   them dynamically to the next sample if requested with a -1 value below.
slouken@0
   287
   Returns the number of reserved channels.
slouken@0
   288
 */
slouken@0
   289
int Mix_ReserveChannels(int num)
slouken@0
   290
{
slouken@0
   291
	if (num > MIX_CHANNELS)
slouken@0
   292
		num = MIX_CHANNELS;
slouken@0
   293
	reserved_channels = num;
slouken@0
   294
	return num;
slouken@0
   295
}
slouken@0
   296
slouken@0
   297
/* Play an audio chunk on a specific channel.
slouken@0
   298
   If the specified channel is -1, play on the first free channel.
slouken@0
   299
   Returns which channel was used to play the sound.
slouken@0
   300
*/
slouken@0
   301
int Mix_PlayChannel(int which, Mix_Chunk *chunk, int loops)
slouken@0
   302
{
slouken@0
   303
	int i;
slouken@0
   304
slouken@0
   305
	/* Don't play null pointers :-) */
slouken@0
   306
	if ( chunk == NULL ) {
slouken@0
   307
		return(-1);
slouken@0
   308
	}
slouken@0
   309
slouken@0
   310
	/* Lock the mixer while modifying the playing channels */
slouken@0
   311
	SDL_mutexP(mixer_lock);
slouken@0
   312
	{
slouken@0
   313
		/* If which is -1, play on the first free channel */
slouken@0
   314
		if ( which == -1 ) {
slouken@0
   315
			for ( i=reserved_channels; i<MIX_CHANNELS; ++i ) {
slouken@0
   316
				if ( channel[i].playing <= 0 )
slouken@0
   317
					break;
slouken@0
   318
			}
slouken@0
   319
			if ( i == MIX_CHANNELS ) {
slouken@0
   320
				which = -1;
slouken@0
   321
			} else {
slouken@0
   322
				which = i;
slouken@0
   323
			}
slouken@0
   324
		}
slouken@0
   325
slouken@0
   326
		/* Queue up the audio data for this channel */
slouken@0
   327
		if ( which >= 0 ) {
slouken@0
   328
			channel[which].samples = chunk->abuf;
slouken@0
   329
			channel[which].playing = chunk->alen;
slouken@0
   330
			channel[which].looping = loops;
slouken@0
   331
			channel[which].chunk = chunk;
slouken@0
   332
			channel[which].paused = 0;
slouken@0
   333
		}
slouken@0
   334
	}
slouken@0
   335
	SDL_mutexV(mixer_lock);
slouken@0
   336
slouken@0
   337
	/* Return the channel on which the sound is being played */
slouken@0
   338
	return(which);
slouken@0
   339
}
slouken@0
   340
slouken@0
   341
/* Set volume of a particular channel */
slouken@0
   342
int Mix_Volume(int which, int volume)
slouken@0
   343
{
slouken@0
   344
	int i;
slouken@0
   345
	int prev_volume;
slouken@0
   346
slouken@0
   347
	if ( which == -1 ) {
slouken@0
   348
		prev_volume = 0;
slouken@0
   349
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   350
			prev_volume += Mix_Volume(i, volume);
slouken@0
   351
		}
slouken@0
   352
		prev_volume /= MIX_CHANNELS;
slouken@0
   353
	} else {
slouken@0
   354
		prev_volume = channel[which].volume;
slouken@0
   355
		if ( volume >= 0 ) {
slouken@0
   356
			if ( volume > SDL_MIX_MAXVOLUME ) {
slouken@0
   357
				volume = SDL_MIX_MAXVOLUME;
slouken@0
   358
			}
slouken@0
   359
			channel[which].volume = volume;
slouken@0
   360
		}
slouken@0
   361
	}
slouken@0
   362
	return(prev_volume);
slouken@0
   363
}
slouken@0
   364
/* Set volume of a particular chunk */
slouken@0
   365
int Mix_VolumeChunk(Mix_Chunk *chunk, int volume)
slouken@0
   366
{
slouken@0
   367
	int prev_volume;
slouken@0
   368
slouken@0
   369
	prev_volume = chunk->volume;
slouken@0
   370
	if ( volume >= 0 ) {
slouken@0
   371
		if ( volume > MIX_MAX_VOLUME ) {
slouken@0
   372
			volume = MIX_MAX_VOLUME;
slouken@0
   373
		}
slouken@0
   374
		chunk->volume = volume;
slouken@0
   375
	}
slouken@0
   376
	return(prev_volume);
slouken@0
   377
}
slouken@0
   378
slouken@0
   379
/* Halt playing of a particular channel */
slouken@0
   380
int Mix_HaltChannel(int which)
slouken@0
   381
{
slouken@0
   382
	int i;
slouken@0
   383
slouken@0
   384
	if ( which == -1 ) {
slouken@0
   385
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   386
			Mix_HaltChannel(i);
slouken@0
   387
		}
slouken@0
   388
	} else {
slouken@0
   389
		SDL_mutexP(mixer_lock);
slouken@0
   390
		channel[which].playing = 0;
slouken@0
   391
		SDL_mutexV(mixer_lock);
slouken@0
   392
	}
slouken@0
   393
	return(0);
slouken@0
   394
}
slouken@0
   395
slouken@0
   396
/* Check the status of a specific channel.
slouken@0
   397
   If the specified channel is -1, check all channels.
slouken@0
   398
*/
slouken@0
   399
int Mix_Playing(int which)
slouken@0
   400
{
slouken@0
   401
	int status;
slouken@0
   402
slouken@0
   403
	status = 0;
slouken@0
   404
	if ( which == -1 ) {
slouken@0
   405
		int i;
slouken@0
   406
slouken@0
   407
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   408
			if ( channel[i].playing > 0 ) {
slouken@0
   409
				++status;
slouken@0
   410
			}
slouken@0
   411
		}
slouken@0
   412
	} else {
slouken@0
   413
		if ( channel[which].playing > 0 ) {
slouken@0
   414
			++status;
slouken@0
   415
		}
slouken@0
   416
	}
slouken@0
   417
	return(status);
slouken@0
   418
}
slouken@0
   419
slouken@0
   420
/* Close the mixer, halting all playing audio */
slouken@0
   421
void Mix_CloseAudio(void)
slouken@0
   422
{
slouken@0
   423
	if ( audio_opened ) {
slouken@0
   424
		if ( audio_opened == 1 ) {
slouken@0
   425
			close_music();
slouken@0
   426
			Mix_HaltChannel(-1);
slouken@0
   427
			SDL_CloseAudio();
slouken@0
   428
			SDL_DestroyMutex(mixer_lock);
slouken@0
   429
		}
slouken@0
   430
		--audio_opened;
slouken@0
   431
	}
slouken@0
   432
}
slouken@0
   433
slouken@0
   434
/* Pause a particular channel (or all) */
slouken@0
   435
void Mix_Pause(int which)
slouken@0
   436
{
slouken@0
   437
	if ( which == -1 ) {
slouken@0
   438
		int i;
slouken@0
   439
slouken@0
   440
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   441
			if ( channel[i].playing > 0 ) {
slouken@0
   442
				channel[i].paused = 1;
slouken@0
   443
			}
slouken@0
   444
		}
slouken@0
   445
	} else {
slouken@0
   446
		if ( channel[which].playing > 0 ) {
slouken@0
   447
			channel[which].paused = 1;
slouken@0
   448
		}
slouken@0
   449
	}
slouken@0
   450
}
slouken@0
   451
slouken@0
   452
/* Resume a paused channel */
slouken@0
   453
void Mix_Resume(int which)
slouken@0
   454
{
slouken@0
   455
	if ( which == -1 ) {
slouken@0
   456
		int i;
slouken@0
   457
slouken@0
   458
		for ( i=0; i<MIX_CHANNELS; ++i ) {
slouken@0
   459
			if ( channel[i].playing > 0 ) {
slouken@0
   460
				channel[i].paused = 0;
slouken@0
   461
			}
slouken@0
   462
		}
slouken@0
   463
	} else {
slouken@0
   464
		if ( channel[which].playing > 0 ) {
slouken@0
   465
			channel[which].paused = 0;
slouken@0
   466
		}
slouken@0
   467
	}
slouken@0
   468
}