music.c
author Sam Lantinga <slouken@lokigames.com>
Sun, 26 Dec 1999 11:17:50 +0000
changeset 26 25edca541dc9
parent 24 a25bb2d59ce8
child 29 44b09d6e1170
permissions -rw-r--r--
Added a post-mix callback, and updated configure.in for Win32 cross-compile
slouken@0
     1
/*
slouken@26
     2
	MIXERLIB:  An audio mixer library based on the SDL library
slouken@26
     3
	Copyright (C) 1997-1999  Sam Lantinga
slouken@0
     4
slouken@26
     5
	This library is free software; you can redistribute it and/or
slouken@26
     6
	modify it under the terms of the GNU Library General Public
slouken@26
     7
	License as published by the Free Software Foundation; either
slouken@26
     8
	version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@26
    10
	This library is distributed in the hope that it will be useful,
slouken@26
    11
	but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@26
    12
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@26
    13
	Library General Public License for more details.
slouken@0
    14
slouken@26
    15
	You should have received a copy of the GNU Library General Public
slouken@26
    16
	License along with this library; if not, write to the Free
slouken@26
    17
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@26
    19
	Sam Lantinga
slouken@26
    20
	5635-34 Springhouse Dr.
slouken@26
    21
	Pleasanton, CA 94588 (USA)
slouken@26
    22
	slouken@devolution.com
slouken@0
    23
*/
slouken@0
    24
slouken@3
    25
#include <stdlib.h>
slouken@3
    26
#include <string.h>
slouken@24
    27
#include "SDL_endian.h"
slouken@24
    28
#include "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@17
    61
static int music_volume = MIX_MAX_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;
slouken@17
    92
	int fade_step;
slouken@17
    93
	int fade_steps;
slouken@0
    94
	int error;
slouken@0
    95
};
slouken@0
    96
static int timidity_ok;
slouken@0
    97
slouken@17
    98
/* Used to calculate fading steps */
slouken@17
    99
static int ms_per_step;
slouken@17
   100
megastep@10
   101
/* Local low-level functions prototypes */
megastep@7
   102
static void lowlevel_halt(void);
megastep@10
   103
static int  lowlevel_play(Mix_Music *music);
megastep@7
   104
slouken@0
   105
/* Mixing function */
slouken@0
   106
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   107
{
slouken@0
   108
	int i;
slouken@0
   109
slouken@0
   110
	if ( music_playing ) {
megastep@7
   111
		if ( music_stopped ) {
megastep@7
   112
			/* To avoid concurrency problems and the use of mutexes,
megastep@7
   113
			   the music is always stopped from the sound thread */
megastep@7
   114
			lowlevel_halt(); /* This function sets music_playing to NULL */
megastep@7
   115
			return;
megastep@7
   116
		}	
megastep@13
   117
		/* Handle fading */
slouken@17
   118
		if ( music_playing->fading != MIX_NO_FADING ) {
slouken@17
   119
			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
slouken@26
   120
				int fade_volume = music_playing->fade_volume;
slouken@26
   121
				int fade_step = music_playing->fade_step;
slouken@26
   122
				int fade_steps = music_playing->fade_steps;
slouken@17
   123
megastep@4
   124
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@17
   125
					Mix_VolumeMusic((fade_volume * (fade_steps-fade_step)) 
slouken@17
   126
									/ fade_steps);
slouken@17
   127
				} else { /* Fading in */
slouken@17
   128
					Mix_VolumeMusic((fade_volume * fade_step) / fade_steps);
slouken@17
   129
				}
slouken@17
   130
			} else {
slouken@17
   131
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@17
   132
					lowlevel_halt();
megastep@4
   133
					return;
megastep@4
   134
				}
megastep@4
   135
				music_playing->fading = MIX_NO_FADING;
megastep@4
   136
			}
megastep@4
   137
		}
megastep@13
   138
		/* Restart music if it has to loop */
megastep@13
   139
		if ( music_loops && !Mix_PlayingMusic() ) {
megastep@11
   140
			if ( -- music_loops ) {
megastep@11
   141
				Mix_RewindMusic();
megastep@11
   142
				if ( lowlevel_play(music_playing) < 0 ) {
megastep@11
   143
					fprintf(stderr,"Warning: Music restart failed.\n");
megastep@11
   144
					music_stopped = 1; /* Something went wrong */
megastep@11
   145
					music_playing->fading = MIX_NO_FADING;
megastep@11
   146
				}
megastep@11
   147
			}
megastep@11
   148
		}
megastep@16
   149
		if ( music_volume <= 0 ) { /* Don't mix if volume is null */
megastep@16
   150
			return;
megastep@16
   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@26
   193
			case MUS_MP3:
slouken@26
   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@26
   245
			Mix_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@26
   251
			Mix_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@26
   256
	samplesize	 = mixer->size/mixer->samples;
slouken@26
   257
	md_mixfreq	 = mixer->freq;
slouken@26
   258
	md_device	  = 0;
slouken@26
   259
	md_volume	  = 96;
slouken@0
   260
	md_musicvolume = 128;
slouken@0
   261
	md_sndfxvolume = 128;
slouken@26
   262
	md_pansep	  = 128;
slouken@26
   263
	md_reverb	  = 0;
slouken@0
   264
	MikMod_RegisterAllLoaders();
slouken@0
   265
	MikMod_RegisterAllDrivers();
slouken@0
   266
	if ( MikMod_Init() ) {
slouken@26
   267
		Mix_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;
slouken@0
   285
	if ( music_error ) {
slouken@0
   286
		return(-1);
slouken@0
   287
	}
slouken@0
   288
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   289
slouken@26
   290
	/* Calculate the number of ms for each callback */
slouken@26
   291
	ms_per_step = ((float)mixer->samples * 1000.0) / mixer->freq;
slouken@17
   292
slouken@0
   293
	return(0);
slouken@0
   294
}
slouken@0
   295
slouken@0
   296
/* Load a music file */
slouken@0
   297
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   298
{
slouken@0
   299
	FILE *fp;
slouken@0
   300
	unsigned char magic[5];
slouken@0
   301
	Mix_Music *music;
slouken@0
   302
slouken@0
   303
	/* Figure out what kind of file this is */
slouken@0
   304
	fp = fopen(file, "rb");
slouken@0
   305
	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
slouken@0
   306
		if ( fp != NULL ) {
slouken@0
   307
			fclose(fp);
slouken@0
   308
		}
slouken@26
   309
		Mix_SetError("Couldn't read from '%s'", file);
slouken@0
   310
		return(NULL);
slouken@0
   311
	}
slouken@0
   312
	magic[4] = '\0';
slouken@0
   313
	fclose(fp);
slouken@0
   314
slouken@0
   315
	/* Allocate memory for the music structure */
slouken@0
   316
	music = (Mix_Music *)malloc(sizeof(Mix_Music));
slouken@0
   317
	if ( music == NULL ) {
slouken@26
   318
		Mix_SetError("Out of memory");
slouken@0
   319
		return(NULL);
slouken@0
   320
	}
slouken@0
   321
	music->error = 0;
slouken@0
   322
slouken@0
   323
#ifdef CMD_MUSIC
slouken@0
   324
	if ( music_cmd ) {
slouken@0
   325
		music->type = MUS_CMD;
slouken@0
   326
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@0
   327
		if ( music->data.cmd == NULL ) {
slouken@0
   328
			music->error = 1;
slouken@0
   329
		}
slouken@0
   330
	} else
slouken@0
   331
#endif
slouken@0
   332
#ifdef WAV_MUSIC
slouken@0
   333
	/* WAVE files have the magic four bytes "RIFF"
slouken@0
   334
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@0
   335
	 */
slouken@0
   336
	if ( (strcmp(magic, "RIFF") == 0) || (strcmp(magic, "FORM") == 0) ) {
slouken@0
   337
		music->type = MUS_WAV;
slouken@0
   338
		music->data.wave = WAVStream_LoadSong(file, magic);
slouken@0
   339
		if ( music->data.wave == NULL ) {
slouken@0
   340
			music->error = 1;
slouken@0
   341
		}
slouken@0
   342
	} else
slouken@0
   343
#endif
slouken@0
   344
#ifdef MID_MUSIC
slouken@0
   345
	/* MIDI files have the magic four bytes "MThd" */
slouken@0
   346
	if ( strcmp(magic, "MThd") == 0 ) {
slouken@0
   347
		music->type = MUS_MID;
slouken@0
   348
		if ( timidity_ok ) {
slouken@0
   349
			music->data.midi = Timidity_LoadSong((char *)file);
slouken@0
   350
			if ( music->data.midi == NULL ) {
slouken@26
   351
				Mix_SetError("%s", Timidity_Error());
slouken@0
   352
				music->error = 1;
slouken@0
   353
			}
slouken@0
   354
		}
slouken@0
   355
		else {
slouken@26
   356
			Mix_SetError("%s", Timidity_Error());
slouken@0
   357
			music->error = 1;
slouken@0
   358
		}
slouken@0
   359
	} else
slouken@0
   360
#endif
slouken@0
   361
#ifdef MP3_MUSIC
slouken@0
   362
	if ( magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
slouken@0
   363
		SMPEG_Info info;
slouken@0
   364
		music->type = MUS_MP3;
slouken@0
   365
		music->data.mp3 = SMPEG_new(file, &info, 0);
slouken@0
   366
		if(!info.has_audio){
slouken@26
   367
			Mix_SetError("MPEG file does not have any audio stream.");
slouken@0
   368
			music->error = 1;
slouken@0
   369
		}else{
slouken@0
   370
			SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@0
   371
		}
slouken@0
   372
	} else
slouken@0
   373
#endif
slouken@0
   374
#ifdef MOD_MUSIC
slouken@0
   375
	if ( 1 ) {
slouken@0
   376
		music->type = MUS_MOD;
slouken@0
   377
		music->data.module = MikMod_LoadSong((char *)file, 64);
slouken@0
   378
		if ( music->data.module == NULL ) {
slouken@26
   379
			Mix_SetError("%s", _mm_errmsg[_mm_errno]);
slouken@0
   380
			music->error = 1;
slouken@0
   381
		}
slouken@0
   382
	} else
slouken@0
   383
#endif
slouken@0
   384
	{
slouken@26
   385
		Mix_SetError("Unrecognized music format");
slouken@0
   386
		music->error = 1;
slouken@0
   387
	}
slouken@0
   388
	if ( music->error ) {
slouken@0
   389
		free(music);
slouken@0
   390
		music = NULL;
slouken@0
   391
	}
slouken@0
   392
	return(music);
slouken@0
   393
}
slouken@0
   394
slouken@0
   395
/* Free a music chunk previously loaded */
slouken@0
   396
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   397
{
slouken@0
   398
	if ( music ) {
slouken@0
   399
		/* Caution: If music is playing, mixer will crash */
megastep@7
   400
		if ( music == music_playing && !music_stopped ) {
megastep@4
   401
			if ( music->fading == MIX_FADING_OUT ) {
megastep@4
   402
				/* Wait for the fade out to finish */
slouken@17
   403
				while ( music_playing && !music_stopped && (music_playing->fading == MIX_FADING_OUT) )
megastep@4
   404
					SDL_Delay(100);
megastep@4
   405
			} else {
megastep@4
   406
				Mix_HaltMusic(); /* Stop it immediately */
megastep@4
   407
			}
slouken@0
   408
		}
slouken@0
   409
		switch (music->type) {
slouken@0
   410
#ifdef CMD_MUSIC
slouken@0
   411
			case MUS_CMD:
slouken@0
   412
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   413
				break;
slouken@0
   414
#endif
slouken@0
   415
#ifdef WAV_MUSIC
slouken@0
   416
			case MUS_WAV:
slouken@0
   417
				WAVStream_FreeSong(music->data.wave);
slouken@0
   418
				break;
slouken@0
   419
#endif
slouken@0
   420
#ifdef MOD_MUSIC
slouken@0
   421
			case MUS_MOD:
slouken@0
   422
				MikMod_FreeSong(music->data.module);
slouken@0
   423
				break;
slouken@0
   424
#endif
slouken@0
   425
#ifdef MID_MUSIC
slouken@0
   426
			case MUS_MID:
slouken@0
   427
				Timidity_FreeSong(music->data.midi);
slouken@0
   428
				break;
slouken@0
   429
#endif
slouken@0
   430
#ifdef MP3_MUSIC
slouken@26
   431
			case MUS_MP3:
slouken@0
   432
				SMPEG_delete(music->data.mp3);
slouken@0
   433
				break;
slouken@0
   434
#endif
slouken@0
   435
			default:
slouken@0
   436
				/* Unknown music type?? */
slouken@0
   437
				break;
slouken@0
   438
		}
slouken@0
   439
		free(music);
slouken@0
   440
	}
slouken@0
   441
}
slouken@0
   442
megastep@10
   443
static int lowlevel_play(Mix_Music *music)
slouken@0
   444
{
megastep@10
   445
	if(!music)
slouken@0
   446
		return(-1);
megastep@10
   447
slouken@0
   448
	switch (music->type) {
slouken@0
   449
#ifdef CMD_MUSIC
slouken@0
   450
		case MUS_CMD:
slouken@0
   451
			MusicCMD_SetVolume(music_volume);
slouken@0
   452
			MusicCMD_Start(music->data.cmd);
slouken@0
   453
			break;
slouken@0
   454
#endif
slouken@0
   455
#ifdef WAV_MUSIC
slouken@0
   456
		case MUS_WAV:
slouken@0
   457
			WAVStream_SetVolume(music_volume);
slouken@0
   458
			WAVStream_Start(music->data.wave);
slouken@0
   459
			break;
slouken@0
   460
#endif
slouken@0
   461
#ifdef MOD_MUSIC
slouken@0
   462
		case MUS_MOD:
slouken@0
   463
			Player_SetVolume(music_volume);
slouken@0
   464
			Player_Start(music->data.module);
slouken@0
   465
			Player_SetPosition(0);
slouken@0
   466
			break;
slouken@0
   467
#endif
slouken@0
   468
#ifdef MID_MUSIC
slouken@0
   469
		case MUS_MID:
slouken@0
   470
			Timidity_SetVolume(music_volume);
slouken@0
   471
			Timidity_Start(music->data.midi);
slouken@0
   472
			break;
slouken@0
   473
#endif
slouken@0
   474
#ifdef MP3_MUSIC
slouken@26
   475
		case MUS_MP3:
slouken@0
   476
			SMPEG_enableaudio(music->data.mp3,1);
slouken@0
   477
			SMPEG_enablevideo(music->data.mp3,0);
slouken@0
   478
			SMPEG_setvolume(music->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
slouken@0
   479
			SMPEG_play(music->data.mp3);
slouken@0
   480
			break;
slouken@0
   481
#endif
slouken@0
   482
		default:
slouken@0
   483
			/* Unknown music type?? */
slouken@0
   484
			return(-1);
slouken@0
   485
	}
megastep@10
   486
	return(0);
megastep@10
   487
}
megastep@10
   488
megastep@10
   489
/* Play a music chunk.  Returns 0, or -1 if there was an error.
megastep@10
   490
*/
megastep@10
   491
int Mix_PlayMusic(Mix_Music *music, int loops)
megastep@10
   492
{
megastep@10
   493
	/* Don't play null pointers :-) */
megastep@10
   494
	if ( music == NULL ) {
megastep@10
   495
		return(-1);
megastep@10
   496
	}
megastep@10
   497
	/* If the current music is fading out, wait for the fade to complete */
megastep@10
   498
	while ( music_playing && !music_stopped && music_playing->fading==MIX_FADING_OUT ) {
megastep@10
   499
		SDL_Delay(100);
megastep@10
   500
	}
megastep@10
   501
slouken@17
   502
	if ( lowlevel_play(music) < 0 ) {
megastep@10
   503
		return(-1);
slouken@26
   504
	}
slouken@0
   505
	music_active = 1;
megastep@7
   506
	music_stopped = 0;
megastep@10
   507
	music_loops = loops;
slouken@0
   508
	music_playing = music;
megastep@4
   509
	music_playing->fading = MIX_NO_FADING;
megastep@4
   510
	return(0);
megastep@4
   511
}
megastep@4
   512
megastep@4
   513
/* Fade in a music over "ms" milliseconds */
megastep@4
   514
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
megastep@4
   515
{
megastep@4
   516
	if ( music && music_volume > 0 ) { /* No need to fade if we can't hear it */
megastep@4
   517
		music->fade_volume = music_volume;
megastep@4
   518
		music_volume = 0;
slouken@17
   519
		if ( Mix_PlayMusic(music, loops) < 0 ) {
megastep@4
   520
			return(-1);
slouken@26
   521
		}
slouken@17
   522
		music_playing->fade_step = 0;
slouken@17
   523
		music_playing->fade_steps = ms/ms_per_step;
megastep@4
   524
		music_playing->fading = MIX_FADING_IN;
megastep@4
   525
	}
slouken@0
   526
	return(0);
slouken@0
   527
}
slouken@0
   528
slouken@0
   529
/* Set the music volume */
slouken@0
   530
int Mix_VolumeMusic(int volume)
slouken@0
   531
{
slouken@0
   532
	int prev_volume;
slouken@0
   533
slouken@0
   534
	prev_volume = music_volume;
megastep@4
   535
	if ( volume < 0 ) {
megastep@4
   536
		volume = 0;
megastep@4
   537
	}
megastep@4
   538
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
   539
		volume = SDL_MIX_MAXVOLUME;
megastep@4
   540
	}
megastep@4
   541
	music_volume = volume;
megastep@7
   542
	if ( music_playing && !music_stopped ) {
megastep@4
   543
		switch (music_playing->type) {
slouken@0
   544
#ifdef CMD_MUSIC
megastep@4
   545
		case MUS_CMD:
megastep@4
   546
			MusicCMD_SetVolume(music_volume);
megastep@4
   547
			break;
slouken@0
   548
#endif
slouken@0
   549
#ifdef WAV_MUSIC
megastep@4
   550
		case MUS_WAV:
megastep@4
   551
			WAVStream_SetVolume(music_volume);
megastep@4
   552
			break;
slouken@0
   553
#endif
slouken@0
   554
#ifdef MOD_MUSIC
megastep@4
   555
		case MUS_MOD:
megastep@4
   556
			Player_SetVolume(music_volume);
megastep@4
   557
			break;
slouken@0
   558
#endif
slouken@0
   559
#ifdef MID_MUSIC
megastep@4
   560
		case MUS_MID:
megastep@4
   561
			Timidity_SetVolume(music_volume);
megastep@4
   562
			break;
slouken@0
   563
#endif
slouken@0
   564
#ifdef MP3_MUSIC
megastep@4
   565
		case MUS_MP3:
megastep@4
   566
			SMPEG_setvolume(music_playing->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
megastep@4
   567
			break;
slouken@0
   568
#endif
megastep@4
   569
		default:
megastep@4
   570
			/* Unknown music type?? */
megastep@4
   571
			break;
slouken@0
   572
		}
slouken@0
   573
	}
slouken@0
   574
	return(prev_volume);
slouken@0
   575
}
slouken@0
   576
megastep@7
   577
static void lowlevel_halt(void)
megastep@7
   578
{
megastep@7
   579
	switch (music_playing->type) {
megastep@7
   580
#ifdef CMD_MUSIC
megastep@7
   581
	case MUS_CMD:
megastep@7
   582
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
   583
		break;
megastep@7
   584
#endif
megastep@7
   585
#ifdef WAV_MUSIC
megastep@7
   586
	case MUS_WAV:
megastep@7
   587
		WAVStream_Stop();
megastep@7
   588
		break;
megastep@7
   589
#endif
megastep@7
   590
#ifdef MOD_MUSIC
megastep@7
   591
	case MUS_MOD:
megastep@7
   592
		Player_Stop();
megastep@7
   593
		break;
megastep@7
   594
#endif
megastep@7
   595
#ifdef MID_MUSIC
megastep@7
   596
	case MUS_MID:
megastep@7
   597
		Timidity_Stop();
megastep@7
   598
		break;
megastep@7
   599
#endif
megastep@7
   600
#ifdef MP3_MUSIC
megastep@7
   601
	case MUS_MP3:
megastep@7
   602
		SMPEG_stop(music_playing->data.mp3);
megastep@7
   603
		break;
megastep@7
   604
#endif
megastep@7
   605
	default:
megastep@7
   606
		/* Unknown music type?? */
megastep@7
   607
		return;
megastep@7
   608
	}
slouken@17
   609
	if ( music_playing->fading != MIX_NO_FADING ) /* Restore volume */
megastep@7
   610
		music_volume = music_playing->fade_volume;
megastep@7
   611
	music_playing->fading = MIX_NO_FADING;
slouken@17
   612
	music_playing = NULL;
megastep@7
   613
	music_active = 0;
megastep@11
   614
	music_loops = 0;
megastep@7
   615
	music_stopped = 0;
megastep@7
   616
}
megastep@7
   617
slouken@0
   618
/* Halt playing of music */
slouken@0
   619
int Mix_HaltMusic(void)
slouken@0
   620
{
megastep@7
   621
	if ( music_playing && !music_stopped ) {
megastep@7
   622
		/* Mark the music to be stopped from the sound thread */
megastep@7
   623
		music_stopped = 1;
megastep@7
   624
		/* Wait for it to be actually stopped */
megastep@7
   625
		while ( music_playing )
megastep@7
   626
			SDL_Delay(10);
slouken@0
   627
	}
slouken@0
   628
	return(0);
slouken@0
   629
}
slouken@0
   630
megastep@4
   631
/* Progressively stop the music */
megastep@4
   632
int Mix_FadeOutMusic(int ms)
megastep@4
   633
{
slouken@26
   634
	if ( music_playing && !music_stopped &&
slouken@26
   635
	     (music_playing->fading == MIX_NO_FADING) ) {
slouken@17
   636
		if ( music_volume > 0 ) {
megastep@4
   637
			music_playing->fading = MIX_FADING_OUT;
megastep@4
   638
			music_playing->fade_volume = music_volume;
slouken@26
   639
			music_playing->fade_step = 0;
slouken@26
   640
			music_playing->fade_steps = ms/ms_per_step;
megastep@4
   641
			return(1);
megastep@4
   642
		}
megastep@4
   643
	}
megastep@4
   644
	return(0);
megastep@4
   645
}
megastep@4
   646
megastep@4
   647
Mix_Fading Mix_FadingMusic(void)
megastep@4
   648
{
megastep@7
   649
	if( music_playing && !music_stopped )
megastep@4
   650
		return music_playing->fading;
megastep@4
   651
	return MIX_NO_FADING;
megastep@4
   652
}
megastep@4
   653
slouken@0
   654
/* Pause/Resume the music stream */
slouken@0
   655
void Mix_PauseMusic(void)
slouken@0
   656
{
slouken@17
   657
	if ( music_playing && !music_stopped ) {
megastep@13
   658
		music_active = 0;
slouken@0
   659
	}
slouken@0
   660
}
megastep@4
   661
slouken@0
   662
void Mix_ResumeMusic(void)
slouken@0
   663
{
slouken@17
   664
	if ( music_playing && !music_stopped ) {
megastep@13
   665
		music_active = 1;
slouken@0
   666
	}
slouken@0
   667
}
slouken@0
   668
slouken@0
   669
void Mix_RewindMusic(void)
slouken@0
   670
{
megastep@7
   671
	if ( music_playing && !music_stopped ) {
slouken@0
   672
		switch ( music_playing->type ) {
slouken@0
   673
#ifdef MP3_MUSIC
slouken@0
   674
		case MUS_MP3:
slouken@0
   675
			SMPEG_rewind(music_playing->data.mp3);
slouken@0
   676
			break;
slouken@0
   677
#endif
megastep@7
   678
			/* TODO: Implement this for other music backends */
slouken@0
   679
		}
slouken@0
   680
	}
slouken@0
   681
}
slouken@0
   682
megastep@13
   683
int Mix_PausedMusic(void)
megastep@13
   684
{
slouken@17
   685
	return (music_active == 0);
megastep@13
   686
}
megastep@13
   687
slouken@0
   688
/* Check the status of the music */
megastep@13
   689
int Mix_PlayingMusic(void)
slouken@0
   690
{
megastep@13
   691
	if ( music_playing && ! music_stopped ) {
slouken@0
   692
		switch (music_playing->type) {
slouken@0
   693
#ifdef CMD_MUSIC
slouken@0
   694
			case MUS_CMD:
slouken@0
   695
				if (!MusicCMD_Active(music_playing->data.cmd)) {
megastep@10
   696
					return(0);
slouken@0
   697
				}
slouken@0
   698
				break;
slouken@0
   699
#endif
slouken@0
   700
#ifdef WAV_MUSIC
slouken@0
   701
			case MUS_WAV:
slouken@0
   702
				if ( ! WAVStream_Active() ) {
megastep@10
   703
					return(0);
slouken@0
   704
				}
slouken@0
   705
				break;
slouken@0
   706
#endif
slouken@0
   707
#ifdef MOD_MUSIC
slouken@0
   708
			case MUS_MOD:
slouken@0
   709
				if ( ! Player_Active() ) {
megastep@10
   710
					return(0);
slouken@0
   711
				}
slouken@0
   712
				break;
slouken@0
   713
#endif
slouken@0
   714
#ifdef MID_MUSIC
slouken@0
   715
			case MUS_MID:
slouken@0
   716
				if ( ! Timidity_Active() ) {
megastep@10
   717
					return(0);
slouken@0
   718
				}
slouken@0
   719
				break;
slouken@0
   720
#endif
slouken@0
   721
#ifdef MP3_MUSIC
slouken@0
   722
			case MUS_MP3:
slouken@0
   723
				if(SMPEG_status(music_playing->data.mp3)!=SMPEG_PLAYING)
megastep@10
   724
					return(0);
slouken@0
   725
				break;
slouken@0
   726
#endif
slouken@0
   727
		}
megastep@13
   728
		return(1);
slouken@0
   729
	}
megastep@13
   730
	return(0);
slouken@0
   731
}
slouken@0
   732
slouken@0
   733
/* Set the external music playback command */
slouken@0
   734
int Mix_SetMusicCMD(const char *command)
slouken@0
   735
{
slouken@0
   736
	Mix_HaltMusic();
slouken@0
   737
	if ( music_cmd ) {
slouken@0
   738
		free(music_cmd);
slouken@0
   739
		music_cmd = NULL;
slouken@0
   740
	}
slouken@0
   741
	if ( command ) {
slouken@0
   742
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
   743
		if ( music_cmd == NULL ) {
slouken@0
   744
			return(-1);
slouken@0
   745
		}
slouken@0
   746
		strcpy(music_cmd, command);
slouken@0
   747
	}
slouken@0
   748
	return(0);
slouken@0
   749
}
slouken@0
   750
slouken@0
   751
/* Uninitialize the music players */
slouken@0
   752
void close_music(void)
slouken@0
   753
{
slouken@0
   754
	Mix_HaltMusic();
slouken@0
   755
#ifdef CMD_MUSIC
slouken@0
   756
	Mix_SetMusicCMD(NULL);
slouken@0
   757
#endif
slouken@0
   758
#ifdef MOD_MUSIC
slouken@0
   759
	MikMod_Exit();
slouken@0
   760
#endif
slouken@0
   761
}
slouken@0
   762