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