music.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 Aug 2001 23:00:30 +0000
changeset 100 110d1c8470fa
parent 98 f00eb57224a5
child 101 1b8c88a28df8
permissions -rw-r--r--
Default to timidity (so as not to break existing applications)
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@98
    59
#ifdef USE_NATIVE_MIDI
slouken@98
    60
#include "native_midi.h"
slouken@98
    61
#endif
slouken@0
    62
#endif
slouken@63
    63
#ifdef OGG_MUSIC
slouken@63
    64
#include "music_ogg.h"
slouken@63
    65
#endif
slouken@0
    66
#ifdef MP3_MUSIC
slouken@83
    67
#include "smpeg.h"
slouken@0
    68
slouken@0
    69
static SDL_AudioSpec used_mixer;
slouken@0
    70
#endif
slouken@0
    71
slouken@42
    72
int volatile music_active = 1;
slouken@42
    73
static int volatile music_stopped = 0;
slouken@0
    74
static int music_loops = 0;
slouken@0
    75
static char *music_cmd = NULL;
slouken@0
    76
static int samplesize;
slouken@42
    77
static Mix_Music * volatile music_playing = NULL;
slouken@17
    78
static int music_volume = MIX_MAX_VOLUME;
slouken@0
    79
static int music_swap8;
slouken@0
    80
static int music_swap16;
megastep@4
    81
slouken@0
    82
struct _Mix_Music {
slouken@0
    83
	enum {
slouken@0
    84
		MUS_CMD,
slouken@0
    85
		MUS_WAV,
slouken@0
    86
		MUS_MOD,
slouken@0
    87
		MUS_MID,
slouken@63
    88
		MUS_OGG,
slouken@0
    89
		MUS_MP3
slouken@0
    90
	} type;
slouken@0
    91
	union {
slouken@0
    92
#ifdef CMD_MUSIC
slouken@0
    93
		MusicCMD *cmd;
slouken@0
    94
#endif
slouken@0
    95
#ifdef WAV_MUSIC
slouken@0
    96
		WAVStream *wave;
slouken@0
    97
#endif
slouken@0
    98
#ifdef MOD_MUSIC
slouken@0
    99
		UNIMOD *module;
slouken@0
   100
#endif
slouken@0
   101
#ifdef MID_MUSIC
slouken@0
   102
		MidiSong *midi;
slouken@98
   103
#ifdef USE_NATIVE_MIDI
slouken@98
   104
		NativeMidiSong *nativemidi;
slouken@98
   105
#endif
slouken@0
   106
#endif
slouken@63
   107
#ifdef OGG_MUSIC
slouken@63
   108
		OGG_music *ogg;
slouken@63
   109
#endif
slouken@0
   110
#ifdef MP3_MUSIC
slouken@0
   111
		SMPEG *mp3;
slouken@0
   112
#endif
slouken@0
   113
	} data;
megastep@4
   114
	Mix_Fading fading;
megastep@4
   115
	int fade_volume;
slouken@17
   116
	int fade_step;
slouken@17
   117
	int fade_steps;
slouken@0
   118
	int error;
slouken@0
   119
};
slouken@29
   120
#ifdef MID_MUSIC
slouken@0
   121
static int timidity_ok;
slouken@98
   122
#ifdef USE_NATIVE_MIDI
slouken@98
   123
static int native_midi_ok;
slouken@98
   124
#endif
slouken@29
   125
#endif
slouken@0
   126
slouken@17
   127
/* Used to calculate fading steps */
slouken@17
   128
static int ms_per_step;
slouken@17
   129
megastep@10
   130
/* Local low-level functions prototypes */
megastep@7
   131
static void lowlevel_halt(void);
megastep@10
   132
static int  lowlevel_play(Mix_Music *music);
megastep@7
   133
slouken@29
   134
slouken@29
   135
/* Support for hooking when the music has finished */
slouken@29
   136
static void (*music_finished_hook)(void) = NULL;
slouken@29
   137
slouken@29
   138
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   139
{
slouken@29
   140
	SDL_LockAudio();
slouken@29
   141
	music_finished_hook = music_finished;
slouken@29
   142
	SDL_UnlockAudio();
slouken@29
   143
}
slouken@29
   144
slouken@29
   145
slouken@0
   146
/* Mixing function */
slouken@0
   147
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   148
{
slouken@0
   149
	if ( music_playing ) {
megastep@7
   150
		if ( music_stopped ) {
megastep@7
   151
			/* To avoid concurrency problems and the use of mutexes,
megastep@7
   152
			   the music is always stopped from the sound thread */
megastep@7
   153
			lowlevel_halt(); /* This function sets music_playing to NULL */
megastep@7
   154
			return;
slouken@29
   155
		}
megastep@13
   156
		/* Handle fading */
slouken@17
   157
		if ( music_playing->fading != MIX_NO_FADING ) {
slouken@17
   158
			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
slouken@26
   159
				int fade_volume = music_playing->fade_volume;
slouken@26
   160
				int fade_step = music_playing->fade_step;
slouken@26
   161
				int fade_steps = music_playing->fade_steps;
slouken@17
   162
megastep@4
   163
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@29
   164
					Mix_VolumeMusic((fade_volume * (fade_steps-fade_step))
slouken@17
   165
									/ fade_steps);
slouken@17
   166
				} else { /* Fading in */
slouken@17
   167
					Mix_VolumeMusic((fade_volume * fade_step) / fade_steps);
slouken@17
   168
				}
slouken@17
   169
			} else {
slouken@17
   170
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@17
   171
					lowlevel_halt();
megastep@4
   172
					return;
megastep@4
   173
				}
megastep@4
   174
				music_playing->fading = MIX_NO_FADING;
megastep@4
   175
			}
megastep@4
   176
		}
megastep@13
   177
		/* Restart music if it has to loop */
slouken@29
   178
		if ( !Mix_PlayingMusic() ) {
slouken@29
   179
			/* Restart music if it has to loop */
slouken@29
   180
			if ( music_loops && --music_loops ) {
megastep@11
   181
				Mix_RewindMusic();
megastep@11
   182
				if ( lowlevel_play(music_playing) < 0 ) {
megastep@11
   183
					fprintf(stderr,"Warning: Music restart failed.\n");
megastep@11
   184
					music_stopped = 1; /* Something went wrong */
megastep@11
   185
					music_playing->fading = MIX_NO_FADING;
megastep@11
   186
				}
megastep@11
   187
			}
slouken@29
   188
			else if (music_finished_hook) {
slouken@29
   189
			    lowlevel_halt();
slouken@29
   190
			    music_finished_hook();
slouken@29
   191
			    return;
slouken@29
   192
			}
megastep@11
   193
		}
megastep@16
   194
		if ( music_volume <= 0 ) { /* Don't mix if volume is null */
megastep@16
   195
			return;
megastep@16
   196
		}
slouken@0
   197
		switch (music_playing->type) {
slouken@0
   198
#ifdef CMD_MUSIC
slouken@0
   199
			case MUS_CMD:
slouken@0
   200
				/* The playing is done externally */
slouken@0
   201
				break;
slouken@0
   202
#endif
slouken@0
   203
#ifdef WAV_MUSIC
slouken@0
   204
			case MUS_WAV:
slouken@64
   205
				WAVStream_PlaySome(stream, len);
slouken@0
   206
				break;
slouken@0
   207
#endif
slouken@0
   208
#ifdef MOD_MUSIC
slouken@0
   209
			case MUS_MOD:
slouken@0
   210
				VC_WriteBytes((SBYTE *)stream, len);
slouken@0
   211
				if ( music_swap8 ) {
slouken@0
   212
					Uint8 *dst;
slouken@42
   213
					int i;
slouken@0
   214
slouken@0
   215
					dst = stream;
slouken@0
   216
					for ( i=len; i; --i ) {
slouken@0
   217
						*dst++ ^= 0x80;
slouken@0
   218
					}
slouken@0
   219
				} else
slouken@0
   220
				if ( music_swap16 ) {
slouken@0
   221
					Uint8 *dst, tmp;
slouken@42
   222
					int i;
slouken@0
   223
slouken@0
   224
					dst = stream;
slouken@0
   225
					for ( i=(len/2); i; --i ) {
slouken@0
   226
						tmp = dst[0];
slouken@0
   227
						dst[0] = dst[1];
slouken@0
   228
						dst[1] = tmp;
slouken@0
   229
						dst += 2;
slouken@0
   230
					}
slouken@0
   231
				}
slouken@0
   232
				break;
slouken@0
   233
#endif
slouken@0
   234
#ifdef MID_MUSIC
slouken@0
   235
			case MUS_MID:
slouken@98
   236
				if ( timidity_ok ) {
slouken@98
   237
					int samples = len / samplesize;
slouken@98
   238
  					Timidity_PlaySome(stream, samples);
slouken@98
   239
				}
slouken@0
   240
				break;
slouken@0
   241
#endif
slouken@63
   242
#ifdef OGG_MUSIC
slouken@63
   243
			case MUS_OGG:
slouken@63
   244
				OGG_playAudio(music_playing->data.ogg, stream, len);
slouken@63
   245
				break;
slouken@63
   246
#endif
slouken@0
   247
#ifdef MP3_MUSIC
slouken@26
   248
			case MUS_MP3:
slouken@26
   249
				SMPEG_playAudio(music_playing->data.mp3, stream, len);
slouken@0
   250
				break;
slouken@0
   251
#endif
slouken@0
   252
			default:
slouken@0
   253
				/* Unknown music type?? */
slouken@0
   254
				break;
slouken@0
   255
		}
slouken@0
   256
	}
slouken@0
   257
}
slouken@0
   258
slouken@0
   259
/* Initialize the music players with a certain desired audio format */
slouken@0
   260
int open_music(SDL_AudioSpec *mixer)
slouken@0
   261
{
slouken@0
   262
	int music_error;
slouken@0
   263
slouken@0
   264
	music_error = 0;
slouken@0
   265
#ifdef WAV_MUSIC
slouken@0
   266
	if ( WAVStream_Init(mixer) < 0 ) {
slouken@0
   267
		++music_error;
slouken@0
   268
	}
slouken@0
   269
#endif
slouken@0
   270
#ifdef MOD_MUSIC
slouken@0
   271
	/* Set the MikMod music format */
slouken@0
   272
	music_swap8 = 0;
slouken@0
   273
	music_swap16 = 0;
slouken@0
   274
	switch (mixer->format) {
slouken@0
   275
slouken@0
   276
		case AUDIO_U8:
slouken@0
   277
		case AUDIO_S8: {
slouken@0
   278
			if ( mixer->format == AUDIO_S8 ) {
slouken@0
   279
				music_swap8 = 1;
slouken@0
   280
			}
slouken@0
   281
			md_mode = 0;
slouken@0
   282
		}
slouken@0
   283
		break;
slouken@0
   284
slouken@0
   285
		case AUDIO_S16LSB:
slouken@0
   286
		case AUDIO_S16MSB: {
slouken@0
   287
			/* See if we need to correct MikMod mixing */
slouken@0
   288
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   289
			if ( mixer->format == AUDIO_S16MSB ) {
slouken@0
   290
#else
slouken@0
   291
			if ( mixer->format == AUDIO_S16LSB ) {
slouken@0
   292
#endif
slouken@0
   293
				music_swap16 = 1;
slouken@0
   294
			}
slouken@0
   295
			md_mode = DMODE_16BITS;
slouken@0
   296
		}
slouken@0
   297
		break;
slouken@0
   298
slouken@0
   299
		default: {
slouken@26
   300
			Mix_SetError("Unknown hardware audio format");
slouken@0
   301
			++music_error;
slouken@0
   302
		}
slouken@0
   303
	}
slouken@0
   304
	if ( mixer->channels > 1 ) {
slouken@0
   305
		if ( mixer->channels > 2 ) {
slouken@26
   306
			Mix_SetError("Hardware uses more channels than mixer");
slouken@0
   307
			++music_error;
slouken@0
   308
		}
slouken@0
   309
		md_mode |= DMODE_STEREO;
slouken@0
   310
	}
slouken@26
   311
	md_mixfreq	 = mixer->freq;
slouken@26
   312
	md_device	  = 0;
slouken@26
   313
	md_volume	  = 96;
slouken@0
   314
	md_musicvolume = 128;
slouken@0
   315
	md_sndfxvolume = 128;
slouken@26
   316
	md_pansep	  = 128;
slouken@26
   317
	md_reverb	  = 0;
slouken@0
   318
	MikMod_RegisterAllLoaders();
slouken@0
   319
	MikMod_RegisterAllDrivers();
slouken@0
   320
	if ( MikMod_Init() ) {
slouken@29
   321
		Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   322
		++music_error;
slouken@0
   323
	}
slouken@0
   324
#endif
slouken@0
   325
#ifdef MID_MUSIC
slouken@98
   326
	samplesize = mixer->size/mixer->samples;
slouken@100
   327
	if ( Timidity_Init(mixer->freq, mixer->format,
slouken@100
   328
	                    mixer->channels, mixer->samples) != 0 ) {
slouken@100
   329
		timidity_ok = 1;
slouken@0
   330
	} else {
slouken@0
   331
		timidity_ok = 0;
slouken@0
   332
	}
slouken@100
   333
#ifdef USE_NATIVE_MIDI
slouken@100
   334
	native_midi_ok = !timidity_ok;
slouken@100
   335
	if ( native_midi_ok ) {
slouken@100
   336
		native_midi_ok = native_midi_init();
slouken@100
   337
	}
slouken@100
   338
#endif
slouken@0
   339
#endif
slouken@63
   340
#ifdef OGG_MUSIC
slouken@63
   341
	if ( OGG_init(mixer) < 0 ) {
slouken@63
   342
		++music_error;
slouken@63
   343
	}
slouken@63
   344
#endif
slouken@0
   345
#ifdef MP3_MUSIC
slouken@0
   346
	/* Keep a copy of the mixer */
slouken@0
   347
	used_mixer = *mixer;
slouken@0
   348
#endif
slouken@42
   349
	music_playing = NULL;
megastep@7
   350
	music_stopped = 0;
slouken@0
   351
	if ( music_error ) {
slouken@0
   352
		return(-1);
slouken@0
   353
	}
slouken@0
   354
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   355
slouken@26
   356
	/* Calculate the number of ms for each callback */
slouken@44
   357
	ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
slouken@17
   358
slouken@0
   359
	return(0);
slouken@0
   360
}
slouken@0
   361
slouken@0
   362
/* Load a music file */
slouken@0
   363
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   364
{
slouken@0
   365
	FILE *fp;
slouken@52
   366
	Uint8 magic[5];
slouken@0
   367
	Mix_Music *music;
slouken@0
   368
slouken@0
   369
	/* Figure out what kind of file this is */
slouken@0
   370
	fp = fopen(file, "rb");
slouken@0
   371
	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
slouken@0
   372
		if ( fp != NULL ) {
slouken@0
   373
			fclose(fp);
slouken@0
   374
		}
slouken@26
   375
		Mix_SetError("Couldn't read from '%s'", file);
slouken@0
   376
		return(NULL);
slouken@0
   377
	}
slouken@0
   378
	magic[4] = '\0';
slouken@0
   379
	fclose(fp);
slouken@0
   380
slouken@0
   381
	/* Allocate memory for the music structure */
slouken@0
   382
	music = (Mix_Music *)malloc(sizeof(Mix_Music));
slouken@0
   383
	if ( music == NULL ) {
slouken@26
   384
		Mix_SetError("Out of memory");
slouken@0
   385
		return(NULL);
slouken@0
   386
	}
slouken@0
   387
	music->error = 0;
slouken@0
   388
slouken@0
   389
#ifdef CMD_MUSIC
slouken@0
   390
	if ( music_cmd ) {
slouken@0
   391
		music->type = MUS_CMD;
slouken@0
   392
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@0
   393
		if ( music->data.cmd == NULL ) {
slouken@0
   394
			music->error = 1;
slouken@0
   395
		}
slouken@0
   396
	} else
slouken@0
   397
#endif
slouken@0
   398
#ifdef WAV_MUSIC
slouken@0
   399
	/* WAVE files have the magic four bytes "RIFF"
slouken@0
   400
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@0
   401
	 */
slouken@57
   402
	if ( (strcmp((char *)magic, "RIFF") == 0) ||
slouken@57
   403
	     (strcmp((char *)magic, "FORM") == 0) ) {
slouken@0
   404
		music->type = MUS_WAV;
slouken@57
   405
		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
slouken@0
   406
		if ( music->data.wave == NULL ) {
slouken@0
   407
			music->error = 1;
slouken@0
   408
		}
slouken@0
   409
	} else
slouken@0
   410
#endif
slouken@0
   411
#ifdef MID_MUSIC
slouken@0
   412
	/* MIDI files have the magic four bytes "MThd" */
slouken@0
   413
	if ( strcmp(magic, "MThd") == 0 ) {
slouken@0
   414
		music->type = MUS_MID;
slouken@98
   415
#ifdef USE_NATIVE_MIDI
slouken@98
   416
  		if ( native_midi_ok ) {
slouken@98
   417
  			music->data.nativemidi = native_midi_loadsong((char *)file);
slouken@98
   418
	  		if ( music->data.nativemidi == NULL ) {
slouken@98
   419
		  		Mix_SetError("%s", native_midi_error());
slouken@98
   420
			  	music->error = 1;
slouken@98
   421
			}
slouken@98
   422
      		} else
slouken@98
   423
#endif
slouken@0
   424
		if ( timidity_ok ) {
slouken@0
   425
			music->data.midi = Timidity_LoadSong((char *)file);
slouken@0
   426
			if ( music->data.midi == NULL ) {
slouken@26
   427
				Mix_SetError("%s", Timidity_Error());
slouken@0
   428
				music->error = 1;
slouken@0
   429
			}
slouken@98
   430
		} else {
slouken@26
   431
			Mix_SetError("%s", Timidity_Error());
slouken@0
   432
			music->error = 1;
slouken@0
   433
		}
slouken@0
   434
	} else
slouken@0
   435
#endif
slouken@63
   436
#ifdef OGG_MUSIC
slouken@63
   437
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@63
   438
	if ( strcmp(magic, "OggS") == 0 ) {
slouken@63
   439
		music->type = MUS_OGG;
slouken@63
   440
		music->data.ogg = OGG_new(file);
slouken@63
   441
		if ( music->data.ogg == NULL ) {
slouken@63
   442
			music->error = 1;
slouken@63
   443
		}
slouken@63
   444
	} else
slouken@63
   445
#endif
slouken@0
   446
#ifdef MP3_MUSIC
slouken@0
   447
	if ( magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
slouken@0
   448
		SMPEG_Info info;
slouken@0
   449
		music->type = MUS_MP3;
slouken@0
   450
		music->data.mp3 = SMPEG_new(file, &info, 0);
slouken@0
   451
		if(!info.has_audio){
slouken@26
   452
			Mix_SetError("MPEG file does not have any audio stream.");
slouken@0
   453
			music->error = 1;
slouken@0
   454
		}else{
slouken@0
   455
			SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@0
   456
		}
slouken@0
   457
	} else
slouken@0
   458
#endif
slouken@0
   459
#ifdef MOD_MUSIC
slouken@0
   460
	if ( 1 ) {
slouken@0
   461
		music->type = MUS_MOD;
slouken@0
   462
		music->data.module = MikMod_LoadSong((char *)file, 64);
slouken@0
   463
		if ( music->data.module == NULL ) {
slouken@29
   464
			Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   465
			music->error = 1;
slouken@0
   466
		}
slouken@0
   467
	} else
slouken@0
   468
#endif
slouken@0
   469
	{
slouken@26
   470
		Mix_SetError("Unrecognized music format");
slouken@0
   471
		music->error = 1;
slouken@0
   472
	}
slouken@0
   473
	if ( music->error ) {
slouken@0
   474
		free(music);
slouken@0
   475
		music = NULL;
slouken@0
   476
	}
slouken@0
   477
	return(music);
slouken@0
   478
}
slouken@0
   479
slouken@0
   480
/* Free a music chunk previously loaded */
slouken@0
   481
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   482
{
slouken@0
   483
	if ( music ) {
slouken@0
   484
		/* Caution: If music is playing, mixer will crash */
megastep@7
   485
		if ( music == music_playing && !music_stopped ) {
megastep@4
   486
			if ( music->fading == MIX_FADING_OUT ) {
megastep@4
   487
				/* Wait for the fade out to finish */
slouken@17
   488
				while ( music_playing && !music_stopped && (music_playing->fading == MIX_FADING_OUT) )
megastep@4
   489
					SDL_Delay(100);
megastep@4
   490
			} else {
megastep@4
   491
				Mix_HaltMusic(); /* Stop it immediately */
megastep@4
   492
			}
slouken@0
   493
		}
slouken@0
   494
		switch (music->type) {
slouken@0
   495
#ifdef CMD_MUSIC
slouken@0
   496
			case MUS_CMD:
slouken@0
   497
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   498
				break;
slouken@0
   499
#endif
slouken@0
   500
#ifdef WAV_MUSIC
slouken@0
   501
			case MUS_WAV:
slouken@0
   502
				WAVStream_FreeSong(music->data.wave);
slouken@0
   503
				break;
slouken@0
   504
#endif
slouken@0
   505
#ifdef MOD_MUSIC
slouken@0
   506
			case MUS_MOD:
slouken@0
   507
				MikMod_FreeSong(music->data.module);
slouken@0
   508
				break;
slouken@0
   509
#endif
slouken@0
   510
#ifdef MID_MUSIC
slouken@0
   511
			case MUS_MID:
slouken@98
   512
#ifdef USE_NATIVE_MIDI
slouken@98
   513
  				if ( native_midi_ok ) {
slouken@98
   514
					native_midi_freesong(music->data.nativemidi);
slouken@98
   515
				} else
slouken@98
   516
#endif
slouken@98
   517
				if ( timidity_ok ) {
slouken@98
   518
					Timidity_FreeSong(music->data.midi);
slouken@98
   519
				}
slouken@0
   520
				break;
slouken@0
   521
#endif
slouken@63
   522
#ifdef OGG_MUSIC
slouken@63
   523
			case MUS_OGG:
slouken@63
   524
				OGG_delete(music->data.ogg);
slouken@63
   525
				break;
slouken@63
   526
#endif
slouken@0
   527
#ifdef MP3_MUSIC
slouken@26
   528
			case MUS_MP3:
slouken@0
   529
				SMPEG_delete(music->data.mp3);
slouken@0
   530
				break;
slouken@0
   531
#endif
slouken@0
   532
			default:
slouken@0
   533
				/* Unknown music type?? */
slouken@0
   534
				break;
slouken@0
   535
		}
slouken@0
   536
		free(music);
slouken@0
   537
	}
slouken@0
   538
}
slouken@0
   539
megastep@10
   540
static int lowlevel_play(Mix_Music *music)
slouken@0
   541
{
megastep@10
   542
	if(!music)
slouken@0
   543
		return(-1);
megastep@10
   544
slouken@0
   545
	switch (music->type) {
slouken@0
   546
#ifdef CMD_MUSIC
slouken@0
   547
		case MUS_CMD:
slouken@0
   548
			MusicCMD_SetVolume(music_volume);
slouken@0
   549
			MusicCMD_Start(music->data.cmd);
slouken@0
   550
			break;
slouken@0
   551
#endif
slouken@0
   552
#ifdef WAV_MUSIC
slouken@0
   553
		case MUS_WAV:
slouken@0
   554
			WAVStream_SetVolume(music_volume);
slouken@0
   555
			WAVStream_Start(music->data.wave);
slouken@0
   556
			break;
slouken@0
   557
#endif
slouken@0
   558
#ifdef MOD_MUSIC
slouken@0
   559
		case MUS_MOD:
slouken@42
   560
			Player_SetVolume((SWORD)music_volume);
slouken@0
   561
			Player_Start(music->data.module);
slouken@0
   562
			Player_SetPosition(0);
slouken@0
   563
			break;
slouken@0
   564
#endif
slouken@0
   565
#ifdef MID_MUSIC
slouken@0
   566
		case MUS_MID:
slouken@98
   567
#ifdef USE_NATIVE_MIDI
slouken@98
   568
  			if ( native_midi_ok ) {
slouken@98
   569
				native_midi_setvolume(music_volume);
slouken@98
   570
				native_midi_start(music->data.nativemidi);
slouken@98
   571
			} else
slouken@98
   572
#endif
slouken@98
   573
			if ( timidity_ok ) {
slouken@98
   574
				Timidity_SetVolume(music_volume);
slouken@98
   575
				Timidity_Start(music->data.midi);
slouken@98
   576
			}
slouken@0
   577
			break;
slouken@0
   578
#endif
slouken@63
   579
#ifdef OGG_MUSIC
slouken@63
   580
		case MUS_OGG:
slouken@63
   581
			OGG_setvolume(music->data.ogg, music_volume);
slouken@63
   582
			OGG_play(music->data.ogg);
slouken@63
   583
			break;
slouken@63
   584
#endif
slouken@0
   585
#ifdef MP3_MUSIC
slouken@26
   586
		case MUS_MP3:
slouken@0
   587
			SMPEG_enableaudio(music->data.mp3,1);
slouken@0
   588
			SMPEG_enablevideo(music->data.mp3,0);
slouken@83
   589
			SMPEG_setvolume(music->data.mp3,(int)(((float)music_volume/(float)MIX_MAX_VOLUME)*100.0));
slouken@0
   590
			SMPEG_play(music->data.mp3);
slouken@0
   591
			break;
slouken@0
   592
#endif
slouken@0
   593
		default:
slouken@0
   594
			/* Unknown music type?? */
slouken@0
   595
			return(-1);
slouken@0
   596
	}
megastep@10
   597
	return(0);
megastep@10
   598
}
megastep@10
   599
megastep@10
   600
/* Play a music chunk.  Returns 0, or -1 if there was an error.
megastep@10
   601
*/
megastep@10
   602
int Mix_PlayMusic(Mix_Music *music, int loops)
megastep@10
   603
{
megastep@10
   604
	/* Don't play null pointers :-) */
megastep@10
   605
	if ( music == NULL ) {
megastep@10
   606
		return(-1);
megastep@10
   607
	}
megastep@10
   608
	/* If the current music is fading out, wait for the fade to complete */
megastep@10
   609
	while ( music_playing && !music_stopped && music_playing->fading==MIX_FADING_OUT ) {
megastep@10
   610
		SDL_Delay(100);
megastep@10
   611
	}
megastep@10
   612
slouken@17
   613
	if ( lowlevel_play(music) < 0 ) {
megastep@10
   614
		return(-1);
slouken@26
   615
	}
slouken@0
   616
	music_active = 1;
megastep@7
   617
	music_stopped = 0;
megastep@10
   618
	music_loops = loops;
slouken@0
   619
	music_playing = music;
megastep@4
   620
	music_playing->fading = MIX_NO_FADING;
megastep@4
   621
	return(0);
megastep@4
   622
}
megastep@4
   623
megastep@4
   624
/* Fade in a music over "ms" milliseconds */
megastep@4
   625
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
megastep@4
   626
{
megastep@4
   627
	if ( music && music_volume > 0 ) { /* No need to fade if we can't hear it */
megastep@4
   628
		music->fade_volume = music_volume;
megastep@4
   629
		music_volume = 0;
slouken@17
   630
		if ( Mix_PlayMusic(music, loops) < 0 ) {
megastep@4
   631
			return(-1);
slouken@26
   632
		}
slouken@17
   633
		music_playing->fade_step = 0;
slouken@17
   634
		music_playing->fade_steps = ms/ms_per_step;
megastep@4
   635
		music_playing->fading = MIX_FADING_IN;
megastep@4
   636
	}
slouken@0
   637
	return(0);
slouken@0
   638
}
slouken@0
   639
slouken@0
   640
/* Set the music volume */
slouken@0
   641
int Mix_VolumeMusic(int volume)
slouken@0
   642
{
slouken@0
   643
	int prev_volume;
slouken@0
   644
slouken@0
   645
	prev_volume = music_volume;
megastep@4
   646
	if ( volume < 0 ) {
megastep@4
   647
		volume = 0;
megastep@4
   648
	}
megastep@4
   649
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
   650
		volume = SDL_MIX_MAXVOLUME;
megastep@4
   651
	}
megastep@4
   652
	music_volume = volume;
megastep@7
   653
	if ( music_playing && !music_stopped ) {
megastep@4
   654
		switch (music_playing->type) {
slouken@0
   655
#ifdef CMD_MUSIC
megastep@4
   656
		case MUS_CMD:
megastep@4
   657
			MusicCMD_SetVolume(music_volume);
megastep@4
   658
			break;
slouken@0
   659
#endif
slouken@0
   660
#ifdef WAV_MUSIC
megastep@4
   661
		case MUS_WAV:
megastep@4
   662
			WAVStream_SetVolume(music_volume);
megastep@4
   663
			break;
slouken@0
   664
#endif
slouken@0
   665
#ifdef MOD_MUSIC
megastep@4
   666
		case MUS_MOD:
slouken@42
   667
			Player_SetVolume((SWORD)music_volume);
megastep@4
   668
			break;
slouken@0
   669
#endif
slouken@0
   670
#ifdef MID_MUSIC
megastep@4
   671
		case MUS_MID:
slouken@98
   672
#ifdef USE_NATIVE_MIDI
slouken@98
   673
			if ( native_midi_ok ) {
slouken@98
   674
				native_midi_setvolume(music_volume);
slouken@98
   675
			} else
slouken@98
   676
#endif
slouken@98
   677
			if ( timidity_ok ) {
slouken@98
   678
				Timidity_SetVolume(music_volume);
slouken@98
   679
			}
megastep@4
   680
			break;
slouken@0
   681
#endif
slouken@63
   682
#ifdef OGG_MUSIC
slouken@63
   683
		case MUS_OGG:
slouken@63
   684
			OGG_setvolume(music_playing->data.ogg, music_volume);
slouken@63
   685
			break;
slouken@63
   686
#endif
slouken@0
   687
#ifdef MP3_MUSIC
megastep@4
   688
		case MUS_MP3:
slouken@83
   689
			SMPEG_setvolume(music_playing->data.mp3,(int)(((float)music_volume/(float)MIX_MAX_VOLUME)*100.0));
megastep@4
   690
			break;
slouken@0
   691
#endif
megastep@4
   692
		default:
megastep@4
   693
			/* Unknown music type?? */
megastep@4
   694
			break;
slouken@0
   695
		}
slouken@0
   696
	}
slouken@0
   697
	return(prev_volume);
slouken@0
   698
}
slouken@0
   699
megastep@7
   700
static void lowlevel_halt(void)
megastep@7
   701
{
megastep@7
   702
	switch (music_playing->type) {
megastep@7
   703
#ifdef CMD_MUSIC
megastep@7
   704
	case MUS_CMD:
megastep@7
   705
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
   706
		break;
megastep@7
   707
#endif
megastep@7
   708
#ifdef WAV_MUSIC
megastep@7
   709
	case MUS_WAV:
megastep@7
   710
		WAVStream_Stop();
megastep@7
   711
		break;
megastep@7
   712
#endif
megastep@7
   713
#ifdef MOD_MUSIC
megastep@7
   714
	case MUS_MOD:
megastep@7
   715
		Player_Stop();
megastep@7
   716
		break;
megastep@7
   717
#endif
megastep@7
   718
#ifdef MID_MUSIC
megastep@7
   719
	case MUS_MID:
slouken@98
   720
#ifdef USE_NATIVE_MIDI
slouken@98
   721
		if ( native_midi_ok ) {
slouken@98
   722
			native_midi_stop();
slouken@98
   723
		} else
slouken@98
   724
#endif
slouken@98
   725
		if ( timidity_ok ) {
slouken@98
   726
			Timidity_Stop();
slouken@98
   727
		}
megastep@7
   728
		break;
megastep@7
   729
#endif
slouken@63
   730
#ifdef OGG_MUSIC
slouken@63
   731
	case MUS_OGG:
slouken@63
   732
		OGG_stop(music_playing->data.ogg);
slouken@63
   733
		break;
slouken@63
   734
#endif
megastep@7
   735
#ifdef MP3_MUSIC
megastep@7
   736
	case MUS_MP3:
megastep@7
   737
		SMPEG_stop(music_playing->data.mp3);
megastep@7
   738
		break;
megastep@7
   739
#endif
megastep@7
   740
	default:
megastep@7
   741
		/* Unknown music type?? */
megastep@7
   742
		return;
megastep@7
   743
	}
slouken@17
   744
	if ( music_playing->fading != MIX_NO_FADING ) /* Restore volume */
megastep@7
   745
		music_volume = music_playing->fade_volume;
megastep@7
   746
	music_playing->fading = MIX_NO_FADING;
slouken@17
   747
	music_playing = NULL;
megastep@7
   748
	music_active = 0;
megastep@11
   749
	music_loops = 0;
megastep@7
   750
	music_stopped = 0;
megastep@7
   751
}
megastep@7
   752
slouken@0
   753
/* Halt playing of music */
slouken@0
   754
int Mix_HaltMusic(void)
slouken@0
   755
{
megastep@7
   756
	if ( music_playing && !music_stopped ) {
megastep@7
   757
		/* Mark the music to be stopped from the sound thread */
megastep@7
   758
		music_stopped = 1;
megastep@7
   759
		/* Wait for it to be actually stopped */
slouken@42
   760
		while ( music_playing && music_active )
megastep@7
   761
			SDL_Delay(10);
slouken@0
   762
	}
slouken@0
   763
	return(0);
slouken@0
   764
}
slouken@0
   765
megastep@4
   766
/* Progressively stop the music */
megastep@4
   767
int Mix_FadeOutMusic(int ms)
megastep@4
   768
{
slouken@26
   769
	if ( music_playing && !music_stopped &&
slouken@26
   770
	     (music_playing->fading == MIX_NO_FADING) ) {
slouken@17
   771
		if ( music_volume > 0 ) {
megastep@4
   772
			music_playing->fading = MIX_FADING_OUT;
megastep@4
   773
			music_playing->fade_volume = music_volume;
slouken@26
   774
			music_playing->fade_step = 0;
slouken@26
   775
			music_playing->fade_steps = ms/ms_per_step;
megastep@4
   776
			return(1);
megastep@4
   777
		}
megastep@4
   778
	}
megastep@4
   779
	return(0);
megastep@4
   780
}
megastep@4
   781
megastep@4
   782
Mix_Fading Mix_FadingMusic(void)
megastep@4
   783
{
megastep@7
   784
	if( music_playing && !music_stopped )
megastep@4
   785
		return music_playing->fading;
megastep@4
   786
	return MIX_NO_FADING;
megastep@4
   787
}
megastep@4
   788
slouken@0
   789
/* Pause/Resume the music stream */
slouken@0
   790
void Mix_PauseMusic(void)
slouken@0
   791
{
slouken@17
   792
	if ( music_playing && !music_stopped ) {
megastep@13
   793
		music_active = 0;
slouken@0
   794
	}
slouken@0
   795
}
megastep@4
   796
slouken@0
   797
void Mix_ResumeMusic(void)
slouken@0
   798
{
slouken@17
   799
	if ( music_playing && !music_stopped ) {
megastep@13
   800
		music_active = 1;
slouken@0
   801
	}
slouken@0
   802
}
slouken@0
   803
slouken@0
   804
void Mix_RewindMusic(void)
slouken@0
   805
{
megastep@7
   806
	if ( music_playing && !music_stopped ) {
slouken@0
   807
		switch ( music_playing->type ) {
slouken@29
   808
#ifdef MOD_MUSIC
slouken@29
   809
		case MUS_MOD:
slouken@29
   810
			Player_Start(music_playing->data.module);
slouken@29
   811
			Player_SetPosition(0);
slouken@29
   812
			break;
slouken@29
   813
#endif
slouken@0
   814
#ifdef MP3_MUSIC
slouken@0
   815
		case MUS_MP3:
slouken@0
   816
			SMPEG_rewind(music_playing->data.mp3);
slouken@0
   817
			break;
slouken@0
   818
#endif
slouken@98
   819
#ifdef MID_MUSIC
slouken@98
   820
		case MUS_MID:
slouken@98
   821
#ifdef USE_NATIVE_MIDI
slouken@98
   822
			if ( native_midi_ok ) {
slouken@98
   823
				native_midi_stop();
slouken@98
   824
			}
slouken@98
   825
#endif
slouken@98
   826
			break;
slouken@98
   827
#endif
slouken@29
   828
		default:
megastep@7
   829
			/* TODO: Implement this for other music backends */
slouken@29
   830
			break;
slouken@0
   831
		}
slouken@0
   832
	}
slouken@0
   833
}
slouken@0
   834
megastep@13
   835
int Mix_PausedMusic(void)
megastep@13
   836
{
slouken@17
   837
	return (music_active == 0);
megastep@13
   838
}
megastep@13
   839
slouken@0
   840
/* Check the status of the music */
megastep@13
   841
int Mix_PlayingMusic(void)
slouken@0
   842
{
megastep@13
   843
	if ( music_playing && ! music_stopped ) {
slouken@0
   844
		switch (music_playing->type) {
slouken@0
   845
#ifdef CMD_MUSIC
slouken@0
   846
			case MUS_CMD:
slouken@0
   847
				if (!MusicCMD_Active(music_playing->data.cmd)) {
megastep@10
   848
					return(0);
slouken@0
   849
				}
slouken@0
   850
				break;
slouken@0
   851
#endif
slouken@0
   852
#ifdef WAV_MUSIC
slouken@0
   853
			case MUS_WAV:
slouken@64
   854
				if ( ! WAVStream_Active() ) {
megastep@10
   855
					return(0);
slouken@0
   856
				}
slouken@0
   857
				break;
slouken@0
   858
#endif
slouken@0
   859
#ifdef MOD_MUSIC
slouken@0
   860
			case MUS_MOD:
slouken@0
   861
				if ( ! Player_Active() ) {
megastep@10
   862
					return(0);
slouken@0
   863
				}
slouken@0
   864
				break;
slouken@0
   865
#endif
slouken@0
   866
#ifdef MID_MUSIC
slouken@0
   867
			case MUS_MID:
slouken@98
   868
#ifdef USE_NATIVE_MIDI
slouken@98
   869
				if ( native_midi_ok ) {
slouken@98
   870
					if ( ! native_midi_active() )
slouken@98
   871
						return(0);
slouken@98
   872
				} else
slouken@98
   873
#endif
slouken@98
   874
				if ( timidity_ok ) {
slouken@98
   875
					if ( ! Timidity_Active() )
slouken@98
   876
						return(0);
slouken@0
   877
				}
slouken@0
   878
				break;
slouken@0
   879
#endif
slouken@63
   880
#ifdef OGG_MUSIC
slouken@63
   881
			case MUS_OGG:
slouken@63
   882
				if ( ! OGG_playing(music_playing->data.ogg) ) {
slouken@63
   883
					return(0);
slouken@63
   884
				}
slouken@63
   885
				break;
slouken@63
   886
#endif
slouken@0
   887
#ifdef MP3_MUSIC
slouken@0
   888
			case MUS_MP3:
slouken@98
   889
				if ( SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
megastep@10
   890
					return(0);
slouken@0
   891
				break;
slouken@0
   892
#endif
slouken@29
   893
			default:
slouken@29
   894
				break;
slouken@0
   895
		}
megastep@13
   896
		return(1);
slouken@0
   897
	}
megastep@13
   898
	return(0);
slouken@0
   899
}
slouken@0
   900
slouken@0
   901
/* Set the external music playback command */
slouken@0
   902
int Mix_SetMusicCMD(const char *command)
slouken@0
   903
{
slouken@0
   904
	Mix_HaltMusic();
slouken@0
   905
	if ( music_cmd ) {
slouken@0
   906
		free(music_cmd);
slouken@0
   907
		music_cmd = NULL;
slouken@0
   908
	}
slouken@0
   909
	if ( command ) {
slouken@0
   910
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
   911
		if ( music_cmd == NULL ) {
slouken@0
   912
			return(-1);
slouken@0
   913
		}
slouken@0
   914
		strcpy(music_cmd, command);
slouken@0
   915
	}
slouken@0
   916
	return(0);
slouken@0
   917
}
slouken@0
   918
slouken@0
   919
/* Uninitialize the music players */
slouken@0
   920
void close_music(void)
slouken@0
   921
{
slouken@0
   922
	Mix_HaltMusic();
slouken@0
   923
#ifdef CMD_MUSIC
slouken@0
   924
	Mix_SetMusicCMD(NULL);
slouken@0
   925
#endif
slouken@0
   926
#ifdef MOD_MUSIC
slouken@0
   927
	MikMod_Exit();
slouken@71
   928
    MikMod_UnregisterAllLoaders();
slouken@71
   929
    MikMod_UnregisterAllDrivers();
slouken@0
   930
#endif
slouken@0
   931
}
slouken@0
   932