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