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