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