music.c
author Stephane Peter <megastep@lokigames.com>
Tue, 26 Oct 1999 02:24:17 +0000
changeset 7 e73da9dc4bdc
parent 4 5c4485704b30
child 10 2fbd0414a1fc
permissions -rw-r--r--
*** empty log message ***
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@3
    25
#include <stdlib.h>
slouken@3
    26
#include <string.h>
slouken@0
    27
#include <SDL/SDL_endian.h>
slouken@0
    28
#include <SDL/SDL_audio.h>
slouken@0
    29
slouken@0
    30
#include "mixer.h"
slouken@0
    31
slouken@0
    32
/* The music command hack is UNIX specific */
slouken@0
    33
#ifndef unix
slouken@0
    34
#undef CMD_MUSIC
slouken@0
    35
#endif
slouken@0
    36
slouken@0
    37
#ifdef CMD_MUSIC
slouken@0
    38
#include "music_cmd.h"
slouken@0
    39
#endif
slouken@0
    40
#ifdef WAV_MUSIC
slouken@0
    41
#include "wavestream.h"
slouken@0
    42
#endif
slouken@0
    43
#ifdef MOD_MUSIC
slouken@0
    44
#include "mikmod.h"
slouken@0
    45
#endif
slouken@0
    46
#ifdef MID_MUSIC
slouken@0
    47
#include "timidity.h"
slouken@0
    48
#endif
slouken@0
    49
#ifdef MP3_MUSIC
slouken@0
    50
#include <smpeg/smpeg.h>
slouken@0
    51
slouken@0
    52
static SDL_AudioSpec used_mixer;
slouken@0
    53
#endif
slouken@0
    54
slouken@0
    55
int music_active = 1;
megastep@7
    56
static int music_stopped = 0;
slouken@0
    57
static int music_loops = 0;
slouken@0
    58
static char *music_cmd = NULL;
slouken@0
    59
static int samplesize;
slouken@0
    60
static Mix_Music *music_playing = 0;
slouken@0
    61
static int music_volume;
slouken@0
    62
static int music_swap8;
slouken@0
    63
static int music_swap16;
megastep@4
    64
slouken@0
    65
struct _Mix_Music {
slouken@0
    66
	enum {
slouken@0
    67
		MUS_CMD,
slouken@0
    68
		MUS_WAV,
slouken@0
    69
		MUS_MOD,
slouken@0
    70
		MUS_MID,
slouken@0
    71
		MUS_MP3
slouken@0
    72
	} type;
slouken@0
    73
	union {
slouken@0
    74
#ifdef CMD_MUSIC
slouken@0
    75
		MusicCMD *cmd;
slouken@0
    76
#endif
slouken@0
    77
#ifdef WAV_MUSIC
slouken@0
    78
		WAVStream *wave;
slouken@0
    79
#endif
slouken@0
    80
#ifdef MOD_MUSIC
slouken@0
    81
		UNIMOD *module;
slouken@0
    82
#endif
slouken@0
    83
#ifdef MID_MUSIC
slouken@0
    84
		MidiSong *midi;
slouken@0
    85
#endif
slouken@0
    86
#ifdef MP3_MUSIC
slouken@0
    87
		SMPEG *mp3;
slouken@0
    88
#endif
slouken@0
    89
	} data;
megastep@4
    90
	Mix_Fading fading;
megastep@4
    91
	int fade_volume;
megastep@4
    92
	int fade_length;
megastep@4
    93
	Uint32 ticks_fade;
slouken@0
    94
	int error;
slouken@0
    95
};
slouken@0
    96
static int timidity_ok;
slouken@0
    97
megastep@7
    98
static void lowlevel_halt(void);
megastep@7
    99
slouken@0
   100
/* Mixing function */
slouken@0
   101
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   102
{
slouken@0
   103
	int i;
slouken@0
   104
slouken@0
   105
	if ( music_playing ) {
megastep@7
   106
		if ( music_stopped ) {
megastep@7
   107
			/* To avoid concurrency problems and the use of mutexes,
megastep@7
   108
			   the music is always stopped from the sound thread */
megastep@7
   109
			lowlevel_halt(); /* This function sets music_playing to NULL */
megastep@7
   110
			return;
megastep@7
   111
		}	
megastep@7
   112
		if ( music_playing->fading != MIX_NO_FADING ) {
megastep@4
   113
			Uint32 ticks = SDL_GetTicks() - music_playing->ticks_fade;
megastep@4
   114
			if( ticks > music_playing->fade_length ) {
megastep@4
   115
				if ( music_playing->fading == MIX_FADING_OUT ) {
megastep@4
   116
					music_volume = music_playing->fade_volume;
megastep@4
   117
					music_playing->fading = MIX_NO_FADING;
megastep@7
   118
					lowlevel_halt();
megastep@4
   119
					return;
megastep@4
   120
				}
megastep@4
   121
				music_playing->fading = MIX_NO_FADING;
megastep@4
   122
			} else {
megastep@4
   123
				if ( music_playing->fading == MIX_FADING_OUT ) {
megastep@4
   124
					Mix_VolumeMusic((music_playing->fade_volume * (music_playing->fade_length-ticks)) 
megastep@4
   125
									/ music_playing->fade_length);
megastep@4
   126
				} else { /* Fading in */
megastep@4
   127
					Mix_VolumeMusic((music_playing->fade_volume * ticks) 
megastep@4
   128
									/ music_playing->fade_length);
megastep@4
   129
				}
megastep@4
   130
			}
megastep@4
   131
		}
slouken@0
   132
		switch (music_playing->type) {
slouken@0
   133
#ifdef CMD_MUSIC
slouken@0
   134
			case MUS_CMD:
slouken@0
   135
				/* The playing is done externally */
slouken@0
   136
				break;
slouken@0
   137
#endif
slouken@0
   138
#ifdef WAV_MUSIC
slouken@0
   139
			case MUS_WAV:
slouken@0
   140
				WAVStream_PlaySome(stream, len);
slouken@0
   141
				break;
slouken@0
   142
#endif
slouken@0
   143
#ifdef MOD_MUSIC
slouken@0
   144
			case MUS_MOD:
slouken@0
   145
				VC_WriteBytes((SBYTE *)stream, len);
slouken@0
   146
				if ( music_swap8 ) {
slouken@0
   147
					Uint8 *dst;
slouken@0
   148
slouken@0
   149
					dst = stream;
slouken@0
   150
					for ( i=len; i; --i ) {
slouken@0
   151
						*dst++ ^= 0x80;
slouken@0
   152
					}
slouken@0
   153
				} else
slouken@0
   154
				if ( music_swap16 ) {
slouken@0
   155
					Uint8 *dst, tmp;
slouken@0
   156
slouken@0
   157
					dst = stream;
slouken@0
   158
					for ( i=(len/2); i; --i ) {
slouken@0
   159
						tmp = dst[0];
slouken@0
   160
						dst[0] = dst[1];
slouken@0
   161
						dst[1] = tmp;
slouken@0
   162
						dst += 2;
slouken@0
   163
					}
slouken@0
   164
				}
slouken@0
   165
				break;
slouken@0
   166
#endif
slouken@0
   167
#ifdef MID_MUSIC
slouken@0
   168
			case MUS_MID:
slouken@0
   169
				Timidity_PlaySome(stream, len/samplesize);
slouken@0
   170
				break;
slouken@0
   171
#endif
slouken@0
   172
#ifdef MP3_MUSIC
slouken@0
   173
		    case MUS_MP3:
slouken@0
   174
			    SMPEG_playAudio(music_playing->data.mp3, stream, len);
slouken@0
   175
				break;
slouken@0
   176
#endif
slouken@0
   177
			default:
slouken@0
   178
				/* Unknown music type?? */
slouken@0
   179
				break;
slouken@0
   180
		}
slouken@0
   181
	}
slouken@0
   182
}
slouken@0
   183
slouken@0
   184
/* Initialize the music players with a certain desired audio format */
slouken@0
   185
int open_music(SDL_AudioSpec *mixer)
slouken@0
   186
{
slouken@0
   187
	int music_error;
slouken@0
   188
slouken@0
   189
	music_error = 0;
slouken@0
   190
#ifdef WAV_MUSIC
slouken@0
   191
	if ( WAVStream_Init(mixer) < 0 ) {
slouken@0
   192
		++music_error;
slouken@0
   193
	}
slouken@0
   194
#endif
slouken@0
   195
#ifdef MOD_MUSIC
slouken@0
   196
	/* Set the MikMod music format */
slouken@0
   197
	music_swap8 = 0;
slouken@0
   198
	music_swap16 = 0;
slouken@0
   199
	switch (mixer->format) {
slouken@0
   200
slouken@0
   201
		case AUDIO_U8:
slouken@0
   202
		case AUDIO_S8: {
slouken@0
   203
			if ( mixer->format == AUDIO_S8 ) {
slouken@0
   204
				music_swap8 = 1;
slouken@0
   205
			}
slouken@0
   206
			md_mode = 0;
slouken@0
   207
		}
slouken@0
   208
		break;
slouken@0
   209
slouken@0
   210
		case AUDIO_S16LSB:
slouken@0
   211
		case AUDIO_S16MSB: {
slouken@0
   212
			/* See if we need to correct MikMod mixing */
slouken@0
   213
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   214
			if ( mixer->format == AUDIO_S16MSB ) {
slouken@0
   215
#else
slouken@0
   216
			if ( mixer->format == AUDIO_S16LSB ) {
slouken@0
   217
#endif
slouken@0
   218
				music_swap16 = 1;
slouken@0
   219
			}
slouken@0
   220
			md_mode = DMODE_16BITS;
slouken@0
   221
		}
slouken@0
   222
		break;
slouken@0
   223
slouken@0
   224
		default: {
slouken@0
   225
			SDL_SetError("Unknown hardware audio format");
slouken@0
   226
			++music_error;
slouken@0
   227
		}
slouken@0
   228
	}
slouken@0
   229
	if ( mixer->channels > 1 ) {
slouken@0
   230
		if ( mixer->channels > 2 ) {
slouken@0
   231
			SDL_SetError("Hardware uses more channels than mixer");
slouken@0
   232
			++music_error;
slouken@0
   233
		}
slouken@0
   234
		md_mode |= DMODE_STEREO;
slouken@0
   235
	}
slouken@0
   236
	samplesize     = mixer->size/mixer->samples;
slouken@0
   237
	md_mixfreq     = mixer->freq;
slouken@0
   238
	md_device      = 0;
slouken@0
   239
	md_volume      = 96;
slouken@0
   240
	md_musicvolume = 128;
slouken@0
   241
	md_sndfxvolume = 128;
slouken@0
   242
	md_pansep      = 128;
slouken@0
   243
	md_reverb      = 0;
slouken@0
   244
	MikMod_RegisterAllLoaders();
slouken@0
   245
	MikMod_RegisterAllDrivers();
slouken@0
   246
	if ( MikMod_Init() ) {
slouken@0
   247
		SDL_SetError("%s", _mm_errmsg[_mm_errno]);
slouken@0
   248
		++music_error;
slouken@0
   249
	}
slouken@0
   250
#endif
slouken@0
   251
#ifdef MID_MUSIC
slouken@0
   252
	if ( Timidity_Init(mixer->freq,
slouken@0
   253
			mixer->format, mixer->channels, mixer->samples) == 0 ) {
slouken@0
   254
		timidity_ok = 1;
slouken@0
   255
	} else {
slouken@0
   256
		timidity_ok = 0;
slouken@0
   257
	}
slouken@0
   258
#endif
slouken@0
   259
#ifdef MP3_MUSIC
slouken@0
   260
	/* Keep a copy of the mixer */
slouken@0
   261
	used_mixer = *mixer;
slouken@0
   262
#endif
slouken@0
   263
	music_playing = 0;
megastep@7
   264
	music_stopped = 0;
megastep@4
   265
slouken@0
   266
	if ( music_error ) {
slouken@0
   267
		return(-1);
slouken@0
   268
	}
slouken@0
   269
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   270
slouken@0
   271
	return(0);
slouken@0
   272
}
slouken@0
   273
slouken@0
   274
/* Load a music file */
slouken@0
   275
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   276
{
slouken@0
   277
	FILE *fp;
slouken@0
   278
	unsigned char magic[5];
slouken@0
   279
	Mix_Music *music;
slouken@0
   280
slouken@0
   281
	/* Figure out what kind of file this is */
slouken@0
   282
	fp = fopen(file, "rb");
slouken@0
   283
	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
slouken@0
   284
		if ( fp != NULL ) {
slouken@0
   285
			fclose(fp);
slouken@0
   286
		}
slouken@0
   287
		SDL_SetError("Couldn't read from '%s'", file);
slouken@0
   288
		return(NULL);
slouken@0
   289
	}
slouken@0
   290
	magic[4] = '\0';
slouken@0
   291
	fclose(fp);
slouken@0
   292
slouken@0
   293
	/* Allocate memory for the music structure */
slouken@0
   294
	music = (Mix_Music *)malloc(sizeof(Mix_Music));
slouken@0
   295
	if ( music == NULL ) {
slouken@0
   296
		SDL_SetError("Out of memory");
slouken@0
   297
		return(NULL);
slouken@0
   298
	}
slouken@0
   299
	music->error = 0;
slouken@0
   300
slouken@0
   301
#ifdef CMD_MUSIC
slouken@0
   302
	if ( music_cmd ) {
slouken@0
   303
		music->type = MUS_CMD;
slouken@0
   304
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@0
   305
		if ( music->data.cmd == NULL ) {
slouken@0
   306
			music->error = 1;
slouken@0
   307
		}
slouken@0
   308
	} else
slouken@0
   309
#endif
slouken@0
   310
#ifdef WAV_MUSIC
slouken@0
   311
	/* WAVE files have the magic four bytes "RIFF"
slouken@0
   312
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@0
   313
	 */
slouken@0
   314
	if ( (strcmp(magic, "RIFF") == 0) || (strcmp(magic, "FORM") == 0) ) {
slouken@0
   315
		music->type = MUS_WAV;
slouken@0
   316
		music->data.wave = WAVStream_LoadSong(file, magic);
slouken@0
   317
		if ( music->data.wave == NULL ) {
slouken@0
   318
			music->error = 1;
slouken@0
   319
		}
slouken@0
   320
	} else
slouken@0
   321
#endif
slouken@0
   322
#ifdef MID_MUSIC
slouken@0
   323
	/* MIDI files have the magic four bytes "MThd" */
slouken@0
   324
	if ( strcmp(magic, "MThd") == 0 ) {
slouken@0
   325
		music->type = MUS_MID;
slouken@0
   326
		if ( timidity_ok ) {
slouken@0
   327
			music->data.midi = Timidity_LoadSong((char *)file);
slouken@0
   328
			if ( music->data.midi == NULL ) {
slouken@0
   329
				SDL_SetError("%s", Timidity_Error());
slouken@0
   330
				music->error = 1;
slouken@0
   331
			}
slouken@0
   332
		}
slouken@0
   333
		else {
slouken@0
   334
			SDL_SetError("%s", Timidity_Error());
slouken@0
   335
			music->error = 1;
slouken@0
   336
		}
slouken@0
   337
	} else
slouken@0
   338
#endif
slouken@0
   339
#ifdef MP3_MUSIC
slouken@0
   340
	if ( magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
slouken@0
   341
		SMPEG_Info info;
slouken@0
   342
		music->type = MUS_MP3;
slouken@0
   343
		music->data.mp3 = SMPEG_new(file, &info, 0);
slouken@0
   344
		if(!info.has_audio){
slouken@0
   345
			SDL_SetError("MPEG file does not have any audio stream.");
slouken@0
   346
			music->error = 1;
slouken@0
   347
		}else{
slouken@0
   348
			SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@0
   349
		}
slouken@0
   350
	} else
slouken@0
   351
#endif
slouken@0
   352
#ifdef MOD_MUSIC
slouken@0
   353
	if ( 1 ) {
slouken@0
   354
		music->type = MUS_MOD;
slouken@0
   355
		music->data.module = MikMod_LoadSong((char *)file, 64);
slouken@0
   356
		if ( music->data.module == NULL ) {
slouken@0
   357
			SDL_SetError("%s", _mm_errmsg[_mm_errno]);
slouken@0
   358
			music->error = 1;
slouken@0
   359
		}
slouken@0
   360
	} else
slouken@0
   361
#endif
slouken@0
   362
	{
slouken@0
   363
		SDL_SetError("Unrecognized music format");
slouken@0
   364
		music->error = 1;
slouken@0
   365
	}
slouken@0
   366
	if ( music->error ) {
slouken@0
   367
		free(music);
slouken@0
   368
		music = NULL;
slouken@0
   369
	}
slouken@0
   370
	return(music);
slouken@0
   371
}
slouken@0
   372
slouken@0
   373
/* Free a music chunk previously loaded */
slouken@0
   374
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   375
{
slouken@0
   376
	if ( music ) {
slouken@0
   377
		/* Caution: If music is playing, mixer will crash */
megastep@7
   378
		if ( music == music_playing && !music_stopped ) {
megastep@4
   379
			if ( music->fading == MIX_FADING_OUT ) {
megastep@4
   380
				/* Wait for the fade out to finish */
megastep@4
   381
				while(music->fading == MIX_FADING_OUT)
megastep@4
   382
					SDL_Delay(100);
megastep@4
   383
			} else {
megastep@4
   384
				Mix_HaltMusic(); /* Stop it immediately */
megastep@4
   385
			}
slouken@0
   386
		}
slouken@0
   387
		switch (music->type) {
slouken@0
   388
#ifdef CMD_MUSIC
slouken@0
   389
			case MUS_CMD:
slouken@0
   390
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   391
				break;
slouken@0
   392
#endif
slouken@0
   393
#ifdef WAV_MUSIC
slouken@0
   394
			case MUS_WAV:
slouken@0
   395
				WAVStream_FreeSong(music->data.wave);
slouken@0
   396
				break;
slouken@0
   397
#endif
slouken@0
   398
#ifdef MOD_MUSIC
slouken@0
   399
			case MUS_MOD:
slouken@0
   400
				MikMod_FreeSong(music->data.module);
slouken@0
   401
				break;
slouken@0
   402
#endif
slouken@0
   403
#ifdef MID_MUSIC
slouken@0
   404
			case MUS_MID:
slouken@0
   405
				Timidity_FreeSong(music->data.midi);
slouken@0
   406
				break;
slouken@0
   407
#endif
slouken@0
   408
#ifdef MP3_MUSIC
slouken@0
   409
		    case MUS_MP3:
slouken@0
   410
				SMPEG_delete(music->data.mp3);
slouken@0
   411
				break;
slouken@0
   412
#endif
slouken@0
   413
			default:
slouken@0
   414
				/* Unknown music type?? */
slouken@0
   415
				break;
slouken@0
   416
		}
slouken@0
   417
		free(music);
slouken@0
   418
	}
slouken@0
   419
}
slouken@0
   420
slouken@0
   421
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@0
   422
*/
slouken@0
   423
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@0
   424
{
slouken@0
   425
	/* Don't play null pointers :-) */
slouken@0
   426
	if ( music == NULL ) {
slouken@0
   427
		return(-1);
slouken@0
   428
	}
megastep@4
   429
	/* If the current music is fading out, wait for the fade to complete */
megastep@7
   430
	while ( music_playing && !music_stopped && music_playing->fading==MIX_FADING_OUT ) {
megastep@4
   431
		SDL_Delay(100);
megastep@4
   432
	}
slouken@0
   433
	switch (music->type) {
slouken@0
   434
#ifdef CMD_MUSIC
slouken@0
   435
		case MUS_CMD:
slouken@0
   436
			MusicCMD_SetVolume(music_volume);
slouken@0
   437
			MusicCMD_Start(music->data.cmd);
slouken@0
   438
			break;
slouken@0
   439
#endif
slouken@0
   440
#ifdef WAV_MUSIC
slouken@0
   441
		case MUS_WAV:
slouken@0
   442
			WAVStream_SetVolume(music_volume);
slouken@0
   443
			WAVStream_Start(music->data.wave);
slouken@0
   444
			break;
slouken@0
   445
#endif
slouken@0
   446
#ifdef MOD_MUSIC
slouken@0
   447
		case MUS_MOD:
slouken@0
   448
			Player_SetVolume(music_volume);
slouken@0
   449
			Player_Start(music->data.module);
slouken@0
   450
			Player_SetPosition(0);
slouken@0
   451
			break;
slouken@0
   452
#endif
slouken@0
   453
#ifdef MID_MUSIC
slouken@0
   454
		case MUS_MID:
slouken@0
   455
			Timidity_SetVolume(music_volume);
slouken@0
   456
			Timidity_Start(music->data.midi);
slouken@0
   457
			break;
slouken@0
   458
#endif
slouken@0
   459
#ifdef MP3_MUSIC
slouken@0
   460
	    case MUS_MP3:
slouken@0
   461
			SMPEG_enableaudio(music->data.mp3,1);
slouken@0
   462
			SMPEG_enablevideo(music->data.mp3,0);
slouken@0
   463
			SMPEG_setvolume(music->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
slouken@0
   464
			SMPEG_loop(music->data.mp3, loops);
slouken@0
   465
			SMPEG_play(music->data.mp3);
slouken@0
   466
			break;
slouken@0
   467
#endif
slouken@0
   468
		default:
slouken@0
   469
			/* Unknown music type?? */
slouken@0
   470
			return(-1);
slouken@0
   471
	}
slouken@0
   472
	music_active = 1;
megastep@7
   473
	music_stopped = 0;
slouken@0
   474
	music_playing = music;
megastep@4
   475
	music_playing->fading = MIX_NO_FADING;
megastep@4
   476
	return(0);
megastep@4
   477
}
megastep@4
   478
megastep@4
   479
/* Fade in a music over "ms" milliseconds */
megastep@4
   480
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
megastep@4
   481
{
megastep@4
   482
	if ( music && music_volume > 0 ) { /* No need to fade if we can't hear it */
megastep@4
   483
		music->fade_volume = music_volume;
megastep@4
   484
		music_volume = 0;
megastep@4
   485
		if(Mix_PlayMusic(music,loops)<0)
megastep@4
   486
			return(-1);
megastep@4
   487
		music_playing->fading = MIX_FADING_IN;
megastep@4
   488
		music_playing->fade_length = ms;
megastep@4
   489
		music_playing->ticks_fade = SDL_GetTicks();
megastep@4
   490
	}
slouken@0
   491
	return(0);
slouken@0
   492
}
slouken@0
   493
slouken@0
   494
/* Set the music volume */
slouken@0
   495
int Mix_VolumeMusic(int volume)
slouken@0
   496
{
slouken@0
   497
	int prev_volume;
slouken@0
   498
slouken@0
   499
	prev_volume = music_volume;
megastep@4
   500
	if ( volume < 0 ) {
megastep@4
   501
		volume = 0;
megastep@4
   502
	}
megastep@4
   503
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
   504
		volume = SDL_MIX_MAXVOLUME;
megastep@4
   505
	}
megastep@4
   506
	music_volume = volume;
megastep@7
   507
	if ( music_playing && !music_stopped ) {
megastep@4
   508
		switch (music_playing->type) {
slouken@0
   509
#ifdef CMD_MUSIC
megastep@4
   510
		case MUS_CMD:
megastep@4
   511
			MusicCMD_SetVolume(music_volume);
megastep@4
   512
			break;
slouken@0
   513
#endif
slouken@0
   514
#ifdef WAV_MUSIC
megastep@4
   515
		case MUS_WAV:
megastep@4
   516
			WAVStream_SetVolume(music_volume);
megastep@4
   517
			break;
slouken@0
   518
#endif
slouken@0
   519
#ifdef MOD_MUSIC
megastep@4
   520
		case MUS_MOD:
megastep@4
   521
			Player_SetVolume(music_volume);
megastep@4
   522
			break;
slouken@0
   523
#endif
slouken@0
   524
#ifdef MID_MUSIC
megastep@4
   525
		case MUS_MID:
megastep@4
   526
			Timidity_SetVolume(music_volume);
megastep@4
   527
			break;
slouken@0
   528
#endif
slouken@0
   529
#ifdef MP3_MUSIC
megastep@4
   530
		case MUS_MP3:
megastep@4
   531
			SMPEG_setvolume(music_playing->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
megastep@4
   532
			break;
slouken@0
   533
#endif
megastep@4
   534
		default:
megastep@4
   535
			/* Unknown music type?? */
megastep@4
   536
			break;
slouken@0
   537
		}
slouken@0
   538
	}
slouken@0
   539
	return(prev_volume);
slouken@0
   540
}
slouken@0
   541
megastep@7
   542
static void lowlevel_halt(void)
megastep@7
   543
{
megastep@7
   544
	switch (music_playing->type) {
megastep@7
   545
#ifdef CMD_MUSIC
megastep@7
   546
	case MUS_CMD:
megastep@7
   547
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
   548
		break;
megastep@7
   549
#endif
megastep@7
   550
#ifdef WAV_MUSIC
megastep@7
   551
	case MUS_WAV:
megastep@7
   552
		WAVStream_Stop();
megastep@7
   553
		break;
megastep@7
   554
#endif
megastep@7
   555
#ifdef MOD_MUSIC
megastep@7
   556
	case MUS_MOD:
megastep@7
   557
		Player_Stop();
megastep@7
   558
		break;
megastep@7
   559
#endif
megastep@7
   560
#ifdef MID_MUSIC
megastep@7
   561
	case MUS_MID:
megastep@7
   562
		Timidity_Stop();
megastep@7
   563
		break;
megastep@7
   564
#endif
megastep@7
   565
#ifdef MP3_MUSIC
megastep@7
   566
	case MUS_MP3:
megastep@7
   567
		SMPEG_stop(music_playing->data.mp3);
megastep@7
   568
		break;
megastep@7
   569
#endif
megastep@7
   570
	default:
megastep@7
   571
		/* Unknown music type?? */
megastep@7
   572
		return;
megastep@7
   573
	}
megastep@7
   574
	if(music_playing->fading != MIX_NO_FADING) /* Restore volume */
megastep@7
   575
		music_volume = music_playing->fade_volume;
megastep@7
   576
	music_playing->fading = MIX_NO_FADING;
megastep@7
   577
	music_playing = 0;
megastep@7
   578
	music_active = 0;
megastep@7
   579
	music_stopped = 0;
megastep@7
   580
}
megastep@7
   581
slouken@0
   582
/* Halt playing of music */
slouken@0
   583
int Mix_HaltMusic(void)
slouken@0
   584
{
megastep@7
   585
	if ( music_playing && !music_stopped ) {
megastep@7
   586
		/* Mark the music to be stopped from the sound thread */
megastep@7
   587
		music_stopped = 1;
megastep@7
   588
		/* Wait for it to be actually stopped */
megastep@7
   589
		while ( music_playing )
megastep@7
   590
			SDL_Delay(10);
slouken@0
   591
	}
slouken@0
   592
	return(0);
slouken@0
   593
}
slouken@0
   594
megastep@4
   595
/* Progressively stop the music */
megastep@4
   596
int Mix_FadeOutMusic(int ms)
megastep@4
   597
{
megastep@7
   598
	if ( music_playing && !music_stopped && music_playing->fading==MIX_NO_FADING ) {
megastep@4
   599
		if ( music_volume>0 ) {
megastep@4
   600
			music_playing->fading = MIX_FADING_OUT;
megastep@4
   601
			music_playing->fade_volume = music_volume;
megastep@4
   602
			music_playing->fade_length = ms;
megastep@4
   603
			music_playing->ticks_fade = SDL_GetTicks();
megastep@4
   604
			return(1);
megastep@4
   605
		}
megastep@4
   606
	}
megastep@4
   607
	return(0);
megastep@4
   608
}
megastep@4
   609
megastep@4
   610
Mix_Fading Mix_FadingMusic(void)
megastep@4
   611
{
megastep@7
   612
	if( music_playing && !music_stopped )
megastep@4
   613
		return music_playing->fading;
megastep@4
   614
	return MIX_NO_FADING;
megastep@4
   615
}
megastep@4
   616
slouken@0
   617
/* Pause/Resume the music stream */
slouken@0
   618
void Mix_PauseMusic(void)
slouken@0
   619
{
megastep@7
   620
	if ( music_playing && !music_stopped ) {
slouken@0
   621
		switch ( music_playing->type ) {
slouken@0
   622
#ifdef CMD_MUSIC
slouken@0
   623
		case MUS_CMD:
slouken@0
   624
			MusicCMD_Pause(music_playing->data.cmd);
slouken@0
   625
			break;
slouken@0
   626
#endif
slouken@0
   627
#ifdef MP3_MUSIC
slouken@0
   628
		case MUS_MP3:
slouken@0
   629
			SMPEG_pause(music_playing->data.mp3);
slouken@0
   630
			break;
slouken@0
   631
#endif
slouken@0
   632
		}
slouken@0
   633
	}
slouken@0
   634
	music_active = 0;
slouken@0
   635
}
megastep@4
   636
slouken@0
   637
void Mix_ResumeMusic(void)
slouken@0
   638
{
megastep@7
   639
	if ( music_playing && !music_stopped ) {
slouken@0
   640
		switch ( music_playing->type ) {
slouken@0
   641
#ifdef CMD_MUSIC
slouken@0
   642
		case MUS_CMD:
slouken@0
   643
			MusicCMD_Resume(music_playing->data.cmd);
slouken@0
   644
			break;
slouken@0
   645
#endif
slouken@0
   646
#ifdef MP3_MUSIC
slouken@0
   647
		case MUS_MP3:
slouken@0
   648
			SMPEG_pause(music_playing->data.mp3);
slouken@0
   649
			break;
slouken@0
   650
#endif
slouken@0
   651
		}
slouken@0
   652
	}
slouken@0
   653
	music_active = 1;
slouken@0
   654
}
slouken@0
   655
slouken@0
   656
void Mix_RewindMusic(void)
slouken@0
   657
{
megastep@7
   658
	if ( music_playing && !music_stopped ) {
slouken@0
   659
		switch ( music_playing->type ) {
slouken@0
   660
#ifdef MP3_MUSIC
slouken@0
   661
		case MUS_MP3:
slouken@0
   662
			SMPEG_rewind(music_playing->data.mp3);
slouken@0
   663
			break;
slouken@0
   664
#endif
megastep@7
   665
			/* TODO: Implement this for other music backends */
slouken@0
   666
		}
slouken@0
   667
	}
slouken@0
   668
}
slouken@0
   669
slouken@0
   670
/* Check the status of the music */
slouken@0
   671
int Mix_PlayingMusic(void)
slouken@0
   672
{
megastep@7
   673
	if ( music_playing && !music_stopped ) {
slouken@0
   674
		switch (music_playing->type) {
slouken@0
   675
#ifdef CMD_MUSIC
slouken@0
   676
			case MUS_CMD:
slouken@0
   677
				if (!MusicCMD_Active(music_playing->data.cmd)) {
slouken@0
   678
					music_playing = 0;
slouken@0
   679
				}
slouken@0
   680
				break;
slouken@0
   681
#endif
slouken@0
   682
#ifdef WAV_MUSIC
slouken@0
   683
			case MUS_WAV:
slouken@0
   684
				if ( ! WAVStream_Active() ) {
slouken@0
   685
					music_playing = 0;
slouken@0
   686
				}
slouken@0
   687
				break;
slouken@0
   688
#endif
slouken@0
   689
#ifdef MOD_MUSIC
slouken@0
   690
			case MUS_MOD:
slouken@0
   691
				if ( ! Player_Active() ) {
slouken@0
   692
					music_playing = 0;
slouken@0
   693
				}
slouken@0
   694
				break;
slouken@0
   695
#endif
slouken@0
   696
#ifdef MID_MUSIC
slouken@0
   697
			case MUS_MID:
slouken@0
   698
				if ( ! Timidity_Active() ) {
slouken@0
   699
					music_playing = 0;
slouken@0
   700
				}
slouken@0
   701
				break;
slouken@0
   702
#endif
slouken@0
   703
#ifdef MP3_MUSIC
slouken@0
   704
			case MUS_MP3:
slouken@0
   705
				if(SMPEG_status(music_playing->data.mp3)!=SMPEG_PLAYING)
slouken@0
   706
					music_playing = 0;
slouken@0
   707
				break;
slouken@0
   708
#endif
slouken@0
   709
		}
slouken@0
   710
	}
slouken@0
   711
	return(music_playing ? 1 : 0);
slouken@0
   712
}
slouken@0
   713
slouken@0
   714
/* Set the external music playback command */
slouken@0
   715
int Mix_SetMusicCMD(const char *command)
slouken@0
   716
{
slouken@0
   717
	Mix_HaltMusic();
slouken@0
   718
	if ( music_cmd ) {
slouken@0
   719
		free(music_cmd);
slouken@0
   720
		music_cmd = NULL;
slouken@0
   721
	}
slouken@0
   722
	if ( command ) {
slouken@0
   723
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
   724
		if ( music_cmd == NULL ) {
slouken@0
   725
			return(-1);
slouken@0
   726
		}
slouken@0
   727
		strcpy(music_cmd, command);
slouken@0
   728
	}
slouken@0
   729
	return(0);
slouken@0
   730
}
slouken@0
   731
slouken@0
   732
/* Uninitialize the music players */
slouken@0
   733
void close_music(void)
slouken@0
   734
{
slouken@0
   735
	Mix_HaltMusic();
slouken@0
   736
#ifdef CMD_MUSIC
slouken@0
   737
	Mix_SetMusicCMD(NULL);
slouken@0
   738
#endif
slouken@0
   739
#ifdef MOD_MUSIC
slouken@0
   740
	MikMod_Exit();
slouken@0
   741
#endif
slouken@0
   742
}
slouken@0
   743