music.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 26 Jun 2002 20:40:16 +0000
changeset 186 f98a7d07daca
parent 179 33596bb9f061
child 196 db9f7643b026
permissions -rw-r--r--
Mix_LoadMUS() now explicitly checks for an "MP3" file extension instead of
relying on the magic number.
slouken@0
     1
/*
slouken@138
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@138
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@138
     5
    This library is free software; you can redistribute it and/or
slouken@138
     6
    modify it under the terms of the GNU Library General Public
slouken@138
     7
    License as published by the Free Software Foundation; either
slouken@138
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@138
    10
    This library is distributed in the hope that it will be useful,
slouken@138
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@138
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@138
    13
    Library General Public License for more details.
slouken@0
    14
slouken@138
    15
    You should have received a copy of the GNU Library General Public
slouken@138
    16
    License along with this library; if not, write to the Free
slouken@138
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@138
    19
    Sam Lantinga
slouken@138
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@140
    23
/* $Id$ */
slouken@138
    24
slouken@3
    25
#include <stdlib.h>
slouken@3
    26
#include <string.h>
slouken@125
    27
#include <ctype.h>
slouken@24
    28
#include "SDL_endian.h"
slouken@24
    29
#include "SDL_audio.h"
slouken@29
    30
#include "SDL_timer.h"
slouken@0
    31
slouken@34
    32
#include "SDL_mixer.h"
slouken@0
    33
slouken@0
    34
/* The music command hack is UNIX specific */
slouken@0
    35
#ifndef unix
slouken@0
    36
#undef CMD_MUSIC
slouken@0
    37
#endif
slouken@0
    38
slouken@0
    39
#ifdef CMD_MUSIC
slouken@0
    40
#include "music_cmd.h"
slouken@0
    41
#endif
slouken@0
    42
#ifdef WAV_MUSIC
slouken@0
    43
#include "wavestream.h"
slouken@0
    44
#endif
slouken@0
    45
#ifdef MOD_MUSIC
slouken@29
    46
#  include "mikmod.h"
slouken@29
    47
#  if defined(LIBMIKMOD_VERSION)                /* libmikmod 3.1.8 */
slouken@29
    48
#    define UNIMOD			MODULE
slouken@29
    49
#    define MikMod_Init()		MikMod_Init(NULL)
slouken@29
    50
#    define MikMod_LoadSong(a,b)	Player_Load(a,b,0)
slouken@29
    51
#    define MikMod_FreeSong		Player_Free
slouken@29
    52
     extern int MikMod_errno;
slouken@29
    53
#  else                                        /* old MikMod 3.0.3 */
slouken@29
    54
#    define MikMod_strerror(x)		_mm_errmsg[x])
slouken@29
    55
#    define MikMod_errno		_mm_errno
slouken@29
    56
#  endif
slouken@0
    57
#endif
slouken@0
    58
#ifdef MID_MUSIC
slouken@106
    59
#  ifdef USE_TIMIDITY_MIDI
slouken@106
    60
#    include "timidity.h"
slouken@106
    61
#  endif
slouken@106
    62
#  ifdef USE_NATIVE_MIDI
slouken@106
    63
#    include "native_midi.h"
slouken@106
    64
#  endif
slouken@106
    65
#  if defined(USE_TIMIDITY_MIDI) && defined(USE_NATIVE_MIDI)
slouken@106
    66
#    define MIDI_ELSE	else
slouken@106
    67
#  else
slouken@106
    68
#    define MIDI_ELSE
slouken@106
    69
#  endif
slouken@0
    70
#endif
slouken@63
    71
#ifdef OGG_MUSIC
slouken@63
    72
#include "music_ogg.h"
slouken@63
    73
#endif
slouken@0
    74
#ifdef MP3_MUSIC
slouken@83
    75
#include "smpeg.h"
slouken@0
    76
slouken@0
    77
static SDL_AudioSpec used_mixer;
slouken@0
    78
#endif
slouken@0
    79
slouken@42
    80
int volatile music_active = 1;
slouken@42
    81
static int volatile music_stopped = 0;
slouken@0
    82
static int music_loops = 0;
slouken@0
    83
static char *music_cmd = NULL;
slouken@42
    84
static Mix_Music * volatile music_playing = NULL;
slouken@17
    85
static int music_volume = MIX_MAX_VOLUME;
slouken@0
    86
static int music_swap8;
slouken@0
    87
static int music_swap16;
megastep@4
    88
slouken@0
    89
struct _Mix_Music {
slouken@177
    90
	Mix_MusicType 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@106
   102
#ifdef USE_TIMIDITY_MIDI
slouken@0
   103
		MidiSong *midi;
slouken@106
   104
#endif
slouken@98
   105
#ifdef USE_NATIVE_MIDI
slouken@98
   106
		NativeMidiSong *nativemidi;
slouken@98
   107
#endif
slouken@0
   108
#endif
slouken@63
   109
#ifdef OGG_MUSIC
slouken@63
   110
		OGG_music *ogg;
slouken@63
   111
#endif
slouken@0
   112
#ifdef MP3_MUSIC
slouken@0
   113
		SMPEG *mp3;
slouken@0
   114
#endif
slouken@0
   115
	} data;
megastep@4
   116
	Mix_Fading fading;
slouken@17
   117
	int fade_step;
slouken@17
   118
	int fade_steps;
slouken@0
   119
	int error;
slouken@0
   120
};
slouken@29
   121
#ifdef MID_MUSIC
slouken@106
   122
#ifdef USE_TIMIDITY_MIDI
slouken@0
   123
static int timidity_ok;
slouken@142
   124
static int samplesize;
slouken@106
   125
#endif
slouken@98
   126
#ifdef USE_NATIVE_MIDI
slouken@98
   127
static int native_midi_ok;
slouken@98
   128
#endif
slouken@29
   129
#endif
slouken@0
   130
slouken@17
   131
/* Used to calculate fading steps */
slouken@17
   132
static int ms_per_step;
slouken@17
   133
megastep@10
   134
/* Local low-level functions prototypes */
slouken@173
   135
static void music_internal_volume(int volume);
slouken@173
   136
static int  music_internal_play(Mix_Music *music, double position);
slouken@173
   137
static int  music_internal_position(double position);
slouken@173
   138
static int  music_internal_playing();
slouken@173
   139
static void music_internal_halt(void);
megastep@7
   140
slouken@29
   141
slouken@29
   142
/* Support for hooking when the music has finished */
slouken@29
   143
static void (*music_finished_hook)(void) = NULL;
slouken@29
   144
slouken@29
   145
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   146
{
slouken@29
   147
	SDL_LockAudio();
slouken@29
   148
	music_finished_hook = music_finished;
slouken@29
   149
	SDL_UnlockAudio();
slouken@29
   150
}
slouken@29
   151
slouken@29
   152
slouken@0
   153
/* Mixing function */
slouken@0
   154
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   155
{
slouken@173
   156
	if ( music_playing && music_active ) {
megastep@13
   157
		/* Handle fading */
slouken@17
   158
		if ( music_playing->fading != MIX_NO_FADING ) {
slouken@17
   159
			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
slouken@173
   160
				int volume;
slouken@26
   161
				int fade_step = music_playing->fade_step;
slouken@26
   162
				int fade_steps = music_playing->fade_steps;
slouken@17
   163
megastep@4
   164
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   165
					volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
slouken@17
   166
				} else { /* Fading in */
slouken@173
   167
					volume = (music_volume * fade_step) / fade_steps;
slouken@17
   168
				}
slouken@173
   169
				music_internal_volume(volume);
slouken@17
   170
			} else {
slouken@17
   171
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   172
					music_internal_halt();
slouken@173
   173
					if ( music_finished_hook ) {
slouken@173
   174
						music_finished_hook();
slouken@173
   175
					}
megastep@4
   176
					return;
megastep@4
   177
				}
megastep@4
   178
				music_playing->fading = MIX_NO_FADING;
megastep@4
   179
			}
megastep@4
   180
		}
megastep@13
   181
		/* Restart music if it has to loop */
slouken@173
   182
		if ( !music_internal_playing() ) {
slouken@173
   183
			/* Restart music if it has to loop at a high level */
slouken@29
   184
			if ( music_loops && --music_loops ) {
slouken@173
   185
				music_internal_play(music_playing, 0.0);
slouken@173
   186
			} else {
slouken@173
   187
				music_internal_halt();
slouken@173
   188
				if ( music_finished_hook ) {
slouken@173
   189
					music_finished_hook();
megastep@11
   190
				}
slouken@173
   191
				return;
megastep@11
   192
			}
megastep@16
   193
		}
slouken@0
   194
		switch (music_playing->type) {
slouken@0
   195
#ifdef CMD_MUSIC
slouken@0
   196
			case MUS_CMD:
slouken@0
   197
				/* The playing is done externally */
slouken@0
   198
				break;
slouken@0
   199
#endif
slouken@0
   200
#ifdef WAV_MUSIC
slouken@0
   201
			case MUS_WAV:
slouken@64
   202
				WAVStream_PlaySome(stream, len);
slouken@0
   203
				break;
slouken@0
   204
#endif
slouken@0
   205
#ifdef MOD_MUSIC
slouken@0
   206
			case MUS_MOD:
slouken@0
   207
				VC_WriteBytes((SBYTE *)stream, len);
slouken@0
   208
				if ( music_swap8 ) {
slouken@0
   209
					Uint8 *dst;
slouken@42
   210
					int i;
slouken@0
   211
slouken@0
   212
					dst = stream;
slouken@0
   213
					for ( i=len; i; --i ) {
slouken@0
   214
						*dst++ ^= 0x80;
slouken@0
   215
					}
slouken@0
   216
				} else
slouken@0
   217
				if ( music_swap16 ) {
slouken@0
   218
					Uint8 *dst, tmp;
slouken@42
   219
					int i;
slouken@0
   220
slouken@0
   221
					dst = stream;
slouken@0
   222
					for ( i=(len/2); i; --i ) {
slouken@0
   223
						tmp = dst[0];
slouken@0
   224
						dst[0] = dst[1];
slouken@0
   225
						dst[1] = tmp;
slouken@0
   226
						dst += 2;
slouken@0
   227
					}
slouken@0
   228
				}
slouken@0
   229
				break;
slouken@0
   230
#endif
slouken@0
   231
#ifdef MID_MUSIC
slouken@106
   232
#ifdef USE_TIMIDITY_MIDI
slouken@0
   233
			case MUS_MID:
slouken@98
   234
				if ( timidity_ok ) {
slouken@98
   235
					int samples = len / samplesize;
slouken@98
   236
  					Timidity_PlaySome(stream, samples);
slouken@98
   237
				}
slouken@0
   238
				break;
slouken@0
   239
#endif
slouken@106
   240
#endif
slouken@63
   241
#ifdef OGG_MUSIC
slouken@63
   242
			case MUS_OGG:
slouken@63
   243
				OGG_playAudio(music_playing->data.ogg, stream, len);
slouken@63
   244
				break;
slouken@63
   245
#endif
slouken@0
   246
#ifdef MP3_MUSIC
slouken@26
   247
			case MUS_MP3:
slouken@26
   248
				SMPEG_playAudio(music_playing->data.mp3, stream, len);
slouken@0
   249
				break;
slouken@0
   250
#endif
slouken@0
   251
			default:
slouken@0
   252
				/* Unknown music type?? */
slouken@0
   253
				break;
slouken@0
   254
		}
slouken@0
   255
	}
slouken@0
   256
}
slouken@0
   257
slouken@0
   258
/* Initialize the music players with a certain desired audio format */
slouken@0
   259
int open_music(SDL_AudioSpec *mixer)
slouken@0
   260
{
slouken@0
   261
	int music_error;
slouken@0
   262
slouken@0
   263
	music_error = 0;
slouken@0
   264
#ifdef WAV_MUSIC
slouken@0
   265
	if ( WAVStream_Init(mixer) < 0 ) {
slouken@0
   266
		++music_error;
slouken@0
   267
	}
slouken@0
   268
#endif
slouken@0
   269
#ifdef MOD_MUSIC
slouken@0
   270
	/* Set the MikMod music format */
slouken@0
   271
	music_swap8 = 0;
slouken@0
   272
	music_swap16 = 0;
slouken@0
   273
	switch (mixer->format) {
slouken@0
   274
slouken@0
   275
		case AUDIO_U8:
slouken@0
   276
		case AUDIO_S8: {
slouken@0
   277
			if ( mixer->format == AUDIO_S8 ) {
slouken@0
   278
				music_swap8 = 1;
slouken@0
   279
			}
slouken@0
   280
			md_mode = 0;
slouken@0
   281
		}
slouken@0
   282
		break;
slouken@0
   283
slouken@0
   284
		case AUDIO_S16LSB:
slouken@0
   285
		case AUDIO_S16MSB: {
slouken@0
   286
			/* See if we need to correct MikMod mixing */
slouken@0
   287
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   288
			if ( mixer->format == AUDIO_S16MSB ) {
slouken@0
   289
#else
slouken@0
   290
			if ( mixer->format == AUDIO_S16LSB ) {
slouken@0
   291
#endif
slouken@0
   292
				music_swap16 = 1;
slouken@0
   293
			}
slouken@0
   294
			md_mode = DMODE_16BITS;
slouken@0
   295
		}
slouken@0
   296
		break;
slouken@0
   297
slouken@0
   298
		default: {
slouken@26
   299
			Mix_SetError("Unknown hardware audio format");
slouken@0
   300
			++music_error;
slouken@0
   301
		}
slouken@0
   302
	}
slouken@0
   303
	if ( mixer->channels > 1 ) {
slouken@0
   304
		if ( mixer->channels > 2 ) {
slouken@26
   305
			Mix_SetError("Hardware uses more channels than mixer");
slouken@0
   306
			++music_error;
slouken@0
   307
		}
slouken@0
   308
		md_mode |= DMODE_STEREO;
slouken@0
   309
	}
slouken@26
   310
	md_mixfreq	 = mixer->freq;
slouken@26
   311
	md_device	  = 0;
slouken@26
   312
	md_volume	  = 96;
slouken@0
   313
	md_musicvolume = 128;
slouken@0
   314
	md_sndfxvolume = 128;
slouken@26
   315
	md_pansep	  = 128;
slouken@26
   316
	md_reverb	  = 0;
slouken@0
   317
	MikMod_RegisterAllLoaders();
slouken@0
   318
	MikMod_RegisterAllDrivers();
slouken@0
   319
	if ( MikMod_Init() ) {
slouken@29
   320
		Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   321
		++music_error;
slouken@0
   322
	}
slouken@0
   323
#endif
slouken@0
   324
#ifdef MID_MUSIC
slouken@106
   325
#ifdef USE_TIMIDITY_MIDI
slouken@142
   326
	samplesize = mixer->size / mixer->samples;
slouken@100
   327
	if ( Timidity_Init(mixer->freq, mixer->format,
slouken@102
   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@106
   333
#endif
slouken@100
   334
#ifdef USE_NATIVE_MIDI
slouken@106
   335
#ifdef USE_TIMIDITY_MIDI
slouken@100
   336
	native_midi_ok = !timidity_ok;
slouken@106
   337
	if ( native_midi_ok )
slouken@106
   338
#endif
slouken@101
   339
		native_midi_ok = native_midi_detect();
slouken@100
   340
#endif
slouken@0
   341
#endif
slouken@63
   342
#ifdef OGG_MUSIC
slouken@63
   343
	if ( OGG_init(mixer) < 0 ) {
slouken@63
   344
		++music_error;
slouken@63
   345
	}
slouken@63
   346
#endif
slouken@0
   347
#ifdef MP3_MUSIC
slouken@0
   348
	/* Keep a copy of the mixer */
slouken@0
   349
	used_mixer = *mixer;
slouken@0
   350
#endif
slouken@42
   351
	music_playing = NULL;
megastep@7
   352
	music_stopped = 0;
slouken@0
   353
	if ( music_error ) {
slouken@0
   354
		return(-1);
slouken@0
   355
	}
slouken@0
   356
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   357
slouken@26
   358
	/* Calculate the number of ms for each callback */
slouken@44
   359
	ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
slouken@17
   360
slouken@0
   361
	return(0);
slouken@0
   362
}
slouken@0
   363
slouken@125
   364
/* Portable case-insensitive string compare function */
slouken@125
   365
int MIX_string_equals(const char *str1, const char *str2)
slouken@125
   366
{
slouken@125
   367
	while ( *str1 && *str2 ) {
slouken@125
   368
		if ( toupper((unsigned char)*str1) !=
slouken@125
   369
		     toupper((unsigned char)*str2) )
slouken@125
   370
			break;
slouken@125
   371
		++str1;
slouken@125
   372
		++str2;
slouken@125
   373
	}
slouken@125
   374
	return (!*str1 && !*str2);
slouken@125
   375
}
slouken@125
   376
slouken@0
   377
/* Load a music file */
slouken@0
   378
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   379
{
slouken@0
   380
	FILE *fp;
slouken@125
   381
	char *ext;
slouken@52
   382
	Uint8 magic[5];
slouken@0
   383
	Mix_Music *music;
slouken@0
   384
slouken@0
   385
	/* Figure out what kind of file this is */
slouken@0
   386
	fp = fopen(file, "rb");
slouken@0
   387
	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
slouken@0
   388
		if ( fp != NULL ) {
slouken@0
   389
			fclose(fp);
slouken@0
   390
		}
slouken@26
   391
		Mix_SetError("Couldn't read from '%s'", file);
slouken@0
   392
		return(NULL);
slouken@0
   393
	}
slouken@0
   394
	magic[4] = '\0';
slouken@0
   395
	fclose(fp);
slouken@0
   396
slouken@125
   397
	/* Figure out the file extension, so we can determine the type */
slouken@125
   398
	ext = strrchr(file, '.');
slouken@125
   399
	if ( ext ) ++ext; /* skip the dot in the extension */
slouken@125
   400
slouken@0
   401
	/* Allocate memory for the music structure */
slouken@0
   402
	music = (Mix_Music *)malloc(sizeof(Mix_Music));
slouken@0
   403
	if ( music == NULL ) {
slouken@26
   404
		Mix_SetError("Out of memory");
slouken@0
   405
		return(NULL);
slouken@0
   406
	}
slouken@0
   407
	music->error = 0;
slouken@0
   408
slouken@0
   409
#ifdef CMD_MUSIC
slouken@0
   410
	if ( music_cmd ) {
slouken@0
   411
		music->type = MUS_CMD;
slouken@0
   412
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@0
   413
		if ( music->data.cmd == NULL ) {
slouken@0
   414
			music->error = 1;
slouken@0
   415
		}
slouken@0
   416
	} else
slouken@0
   417
#endif
slouken@0
   418
#ifdef WAV_MUSIC
slouken@0
   419
	/* WAVE files have the magic four bytes "RIFF"
slouken@0
   420
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@0
   421
	 */
slouken@135
   422
	if ( (ext && MIX_string_equals(ext, "WAV")) ||
slouken@125
   423
	     (strcmp((char *)magic, "RIFF") == 0) ||
slouken@57
   424
	     (strcmp((char *)magic, "FORM") == 0) ) {
slouken@0
   425
		music->type = MUS_WAV;
slouken@57
   426
		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
slouken@0
   427
		if ( music->data.wave == NULL ) {
slouken@135
   428
		  	Mix_SetError("Unable to load WAV file");
slouken@0
   429
			music->error = 1;
slouken@0
   430
		}
slouken@0
   431
	} else
slouken@0
   432
#endif
slouken@0
   433
#ifdef MID_MUSIC
slouken@0
   434
	/* MIDI files have the magic four bytes "MThd" */
slouken@135
   435
	if ( (ext && MIX_string_equals(ext, "MID")) ||
slouken@135
   436
	     (ext && MIX_string_equals(ext, "MIDI")) ||
slouken@133
   437
	     strcmp((char *)magic, "MThd") == 0 ) {
slouken@0
   438
		music->type = MUS_MID;
slouken@98
   439
#ifdef USE_NATIVE_MIDI
slouken@98
   440
  		if ( native_midi_ok ) {
slouken@98
   441
  			music->data.nativemidi = native_midi_loadsong((char *)file);
slouken@98
   442
	  		if ( music->data.nativemidi == NULL ) {
slouken@98
   443
		  		Mix_SetError("%s", native_midi_error());
slouken@98
   444
			  	music->error = 1;
slouken@98
   445
			}
slouken@106
   446
	  	} MIDI_ELSE
slouken@98
   447
#endif
slouken@106
   448
#ifdef USE_TIMIDITY_MIDI
slouken@0
   449
		if ( timidity_ok ) {
slouken@0
   450
			music->data.midi = Timidity_LoadSong((char *)file);
slouken@0
   451
			if ( music->data.midi == NULL ) {
slouken@26
   452
				Mix_SetError("%s", Timidity_Error());
slouken@0
   453
				music->error = 1;
slouken@0
   454
			}
slouken@98
   455
		} else {
slouken@26
   456
			Mix_SetError("%s", Timidity_Error());
slouken@0
   457
			music->error = 1;
slouken@0
   458
		}
slouken@106
   459
#endif
slouken@0
   460
	} else
slouken@0
   461
#endif
slouken@63
   462
#ifdef OGG_MUSIC
slouken@63
   463
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@135
   464
	if ( (ext && MIX_string_equals(ext, "OGG")) ||
slouken@133
   465
	     strcmp((char *)magic, "OggS") == 0 ) {
slouken@63
   466
		music->type = MUS_OGG;
slouken@63
   467
		music->data.ogg = OGG_new(file);
slouken@63
   468
		if ( music->data.ogg == NULL ) {
slouken@63
   469
			music->error = 1;
slouken@63
   470
		}
slouken@63
   471
	} else
slouken@63
   472
#endif
slouken@0
   473
#ifdef MP3_MUSIC
slouken@135
   474
	if ( (ext && MIX_string_equals(ext, "MPG")) ||
icculus@186
   475
	     (ext && MIX_string_equals(ext, "MP3")) ||
slouken@135
   476
	     (ext && MIX_string_equals(ext, "MPEG")) ||
slouken@125
   477
	     magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
slouken@0
   478
		SMPEG_Info info;
slouken@0
   479
		music->type = MUS_MP3;
slouken@0
   480
		music->data.mp3 = SMPEG_new(file, &info, 0);
slouken@0
   481
		if(!info.has_audio){
slouken@26
   482
			Mix_SetError("MPEG file does not have any audio stream.");
slouken@0
   483
			music->error = 1;
slouken@0
   484
		}else{
slouken@0
   485
			SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@0
   486
		}
slouken@0
   487
	} else
slouken@0
   488
#endif
slouken@0
   489
#ifdef MOD_MUSIC
slouken@0
   490
	if ( 1 ) {
slouken@0
   491
		music->type = MUS_MOD;
slouken@0
   492
		music->data.module = MikMod_LoadSong((char *)file, 64);
slouken@0
   493
		if ( music->data.module == NULL ) {
slouken@29
   494
			Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   495
			music->error = 1;
slouken@126
   496
		} else {
slouken@126
   497
			/* Stop implicit looping, fade out and other flags. */
slouken@126
   498
			music->data.module->extspd  = 1;
slouken@126
   499
			music->data.module->panflag = 1;
slouken@126
   500
			music->data.module->wrap    = 0;
slouken@126
   501
			music->data.module->loop    = 0;
slouken@129
   502
#if 0 /* Don't set fade out by default - unfortunately there's no real way
slouken@129
   503
         to query the status of the song or set trigger actions.  Hum. */
slouken@126
   504
			music->data.module->fadeout = 1;
slouken@129
   505
#endif
slouken@0
   506
		}
slouken@0
   507
	} else
slouken@0
   508
#endif
slouken@0
   509
	{
slouken@26
   510
		Mix_SetError("Unrecognized music format");
slouken@0
   511
		music->error = 1;
slouken@0
   512
	}
slouken@0
   513
	if ( music->error ) {
slouken@0
   514
		free(music);
slouken@0
   515
		music = NULL;
slouken@0
   516
	}
slouken@0
   517
	return(music);
slouken@0
   518
}
slouken@0
   519
slouken@0
   520
/* Free a music chunk previously loaded */
slouken@0
   521
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   522
{
slouken@0
   523
	if ( music ) {
slouken@173
   524
		/* Stop the music if it's currently playing */
slouken@173
   525
		SDL_LockAudio();
slouken@173
   526
		if ( music == music_playing ) {
slouken@173
   527
			/* Wait for any fade out to finish */
slouken@173
   528
			while ( music->fading == MIX_FADING_OUT ) {
slouken@173
   529
				SDL_UnlockAudio();
slouken@173
   530
				SDL_Delay(100);
slouken@173
   531
				SDL_LockAudio();
slouken@173
   532
			}
slouken@173
   533
			if ( music == music_playing ) {
slouken@173
   534
				music_internal_halt();
megastep@4
   535
			}
slouken@0
   536
		}
slouken@173
   537
		SDL_UnlockAudio();
slouken@0
   538
		switch (music->type) {
slouken@0
   539
#ifdef CMD_MUSIC
slouken@0
   540
			case MUS_CMD:
slouken@0
   541
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   542
				break;
slouken@0
   543
#endif
slouken@0
   544
#ifdef WAV_MUSIC
slouken@0
   545
			case MUS_WAV:
slouken@0
   546
				WAVStream_FreeSong(music->data.wave);
slouken@0
   547
				break;
slouken@0
   548
#endif
slouken@0
   549
#ifdef MOD_MUSIC
slouken@0
   550
			case MUS_MOD:
slouken@0
   551
				MikMod_FreeSong(music->data.module);
slouken@0
   552
				break;
slouken@0
   553
#endif
slouken@0
   554
#ifdef MID_MUSIC
slouken@0
   555
			case MUS_MID:
slouken@98
   556
#ifdef USE_NATIVE_MIDI
slouken@98
   557
  				if ( native_midi_ok ) {
slouken@98
   558
					native_midi_freesong(music->data.nativemidi);
slouken@106
   559
				} MIDI_ELSE
slouken@98
   560
#endif
slouken@106
   561
#ifdef USE_TIMIDITY_MIDI
slouken@98
   562
				if ( timidity_ok ) {
slouken@98
   563
					Timidity_FreeSong(music->data.midi);
slouken@98
   564
				}
slouken@106
   565
#endif
slouken@0
   566
				break;
slouken@0
   567
#endif
slouken@63
   568
#ifdef OGG_MUSIC
slouken@63
   569
			case MUS_OGG:
slouken@63
   570
				OGG_delete(music->data.ogg);
slouken@63
   571
				break;
slouken@63
   572
#endif
slouken@0
   573
#ifdef MP3_MUSIC
slouken@26
   574
			case MUS_MP3:
slouken@0
   575
				SMPEG_delete(music->data.mp3);
slouken@0
   576
				break;
slouken@0
   577
#endif
slouken@0
   578
			default:
slouken@0
   579
				/* Unknown music type?? */
slouken@0
   580
				break;
slouken@0
   581
		}
slouken@0
   582
		free(music);
slouken@0
   583
	}
slouken@0
   584
}
slouken@0
   585
slouken@177
   586
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   587
   music, if 'music' is NULL.
slouken@177
   588
*/
slouken@177
   589
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   590
{
slouken@177
   591
	Mix_MusicType type = MUS_NONE;
slouken@177
   592
slouken@177
   593
	if ( music ) {
slouken@177
   594
		type = music->type;
slouken@177
   595
	} else {
slouken@177
   596
		SDL_LockAudio();
slouken@177
   597
		if ( music_playing ) {
slouken@177
   598
			type = music_playing->type;
slouken@177
   599
		}
slouken@177
   600
		SDL_UnlockAudio();
slouken@177
   601
	}
slouken@177
   602
	return(type);
slouken@177
   603
}
slouken@177
   604
slouken@173
   605
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   606
 */
slouken@173
   607
static int music_internal_play(Mix_Music *music, double position)
slouken@0
   608
{
slouken@173
   609
	int retval = 0;
megastep@10
   610
slouken@173
   611
	/* Note the music we're playing */
slouken@173
   612
	if ( music_playing ) {
slouken@173
   613
		music_internal_halt();
slouken@173
   614
	}
slouken@173
   615
	music_playing = music;
slouken@173
   616
slouken@173
   617
	/* Set the initial volume */
slouken@173
   618
	if ( music->fading == MIX_FADING_IN ) {
slouken@173
   619
		music_internal_volume(0);
slouken@173
   620
	} else {
slouken@173
   621
		music_internal_volume(music_volume);
slouken@173
   622
	}
slouken@173
   623
slouken@173
   624
	/* Set up for playback */
slouken@0
   625
	switch (music->type) {
slouken@0
   626
#ifdef CMD_MUSIC
slouken@173
   627
	    case MUS_CMD:
slouken@173
   628
		MusicCMD_Start(music->data.cmd);
slouken@173
   629
		break;
slouken@0
   630
#endif
slouken@0
   631
#ifdef WAV_MUSIC
slouken@173
   632
	    case MUS_WAV:
slouken@173
   633
		WAVStream_Start(music->data.wave);
slouken@173
   634
		break;
slouken@0
   635
#endif
slouken@0
   636
#ifdef MOD_MUSIC
slouken@173
   637
	    case MUS_MOD:
slouken@173
   638
		Player_Start(music->data.module);
slouken@173
   639
		break;
slouken@0
   640
#endif
slouken@0
   641
#ifdef MID_MUSIC
slouken@173
   642
	    case MUS_MID:
slouken@98
   643
#ifdef USE_NATIVE_MIDI
slouken@173
   644
		if ( native_midi_ok ) {
slouken@173
   645
			native_midi_start(music->data.nativemidi);
slouken@173
   646
		} MIDI_ELSE
slouken@98
   647
#endif
slouken@106
   648
#ifdef USE_TIMIDITY_MIDI
slouken@173
   649
		if ( timidity_ok ) {
slouken@173
   650
			Timidity_Start(music->data.midi);
slouken@173
   651
		}
slouken@106
   652
#endif
slouken@173
   653
		break;
slouken@0
   654
#endif
slouken@63
   655
#ifdef OGG_MUSIC
slouken@173
   656
	    case MUS_OGG:
slouken@173
   657
		OGG_play(music->data.ogg);
slouken@173
   658
		break;
slouken@63
   659
#endif
slouken@0
   660
#ifdef MP3_MUSIC
slouken@173
   661
	    case MUS_MP3:
slouken@173
   662
		SMPEG_enableaudio(music->data.mp3,1);
slouken@173
   663
		SMPEG_enablevideo(music->data.mp3,0);
slouken@179
   664
		SMPEG_play(music_playing->data.mp3);
slouken@173
   665
		break;
slouken@0
   666
#endif
slouken@173
   667
	    default:
slouken@173
   668
		Mix_SetError("Can't play unknown music type");
slouken@173
   669
		retval = -1;
slouken@173
   670
		break;
slouken@0
   671
	}
slouken@173
   672
slouken@173
   673
	/* Set the playback position, note any errors if an offset is used */
slouken@173
   674
	if ( retval == 0 ) {
slouken@173
   675
		if ( position > 0.0 ) {
slouken@173
   676
			if ( music_internal_position(position) < 0 ) {
slouken@173
   677
				Mix_SetError("Position not implemented for music type");
slouken@173
   678
				retval = -1;
slouken@173
   679
			}
slouken@173
   680
		} else {
slouken@173
   681
			music_internal_position(0.0);
slouken@173
   682
		}
slouken@173
   683
	}
slouken@173
   684
slouken@173
   685
	/* If the setup failed, we're not playing any music anymore */
slouken@173
   686
	if ( retval < 0 ) {
slouken@173
   687
		music_playing = NULL;
slouken@173
   688
	}
slouken@173
   689
	return(retval);
slouken@173
   690
}
slouken@173
   691
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   692
{
slouken@173
   693
	int retval;
slouken@173
   694
slouken@173
   695
	/* Don't play null pointers :-) */
slouken@173
   696
	if ( music == NULL ) {
slouken@173
   697
		Mix_SetError("music parameter was NULL");
slouken@173
   698
		return(-1);
slouken@173
   699
	}
slouken@173
   700
slouken@173
   701
	/* Setup the data */
slouken@173
   702
	if ( ms ) {
slouken@173
   703
		music->fading = MIX_FADING_IN;
slouken@173
   704
	} else {
slouken@173
   705
		music->fading = MIX_NO_FADING;
slouken@173
   706
	}
slouken@173
   707
	music->fade_step = 0;
slouken@173
   708
	music->fade_steps = ms/ms_per_step;
slouken@173
   709
slouken@173
   710
	/* Play the puppy */
slouken@173
   711
	SDL_LockAudio();
slouken@173
   712
	/* If the current music is fading out, wait for the fade to complete */
slouken@173
   713
	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
slouken@173
   714
		SDL_UnlockAudio();
slouken@173
   715
		SDL_Delay(100);
slouken@173
   716
		SDL_LockAudio();
slouken@173
   717
	}
slouken@173
   718
	music_active = 1;
slouken@173
   719
	music_loops = loops;
slouken@173
   720
	retval = music_internal_play(music, position);
slouken@173
   721
	SDL_UnlockAudio();
slouken@173
   722
slouken@173
   723
	return(retval);
slouken@173
   724
}
slouken@173
   725
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   726
{
slouken@173
   727
	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   728
}
slouken@173
   729
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   730
{
slouken@173
   731
	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   732
}
megastep@10
   733
slouken@173
   734
/* Set the playing music position */
slouken@173
   735
int music_internal_position(double position)
megastep@10
   736
{
slouken@173
   737
	int retval = 0;
slouken@173
   738
slouken@173
   739
	switch (music_playing->type) {
slouken@173
   740
#ifdef MOD_MUSIC
slouken@173
   741
	    case MUS_MOD:
slouken@173
   742
		Player_SetPosition((UWORD)position);
slouken@173
   743
		break;
slouken@173
   744
#endif
slouken@173
   745
#ifdef OGG_MUSIC
slouken@173
   746
	    case MUS_OGG:
slouken@173
   747
		OGG_jump_to_time(music_playing->data.ogg, position);
slouken@173
   748
		break;
slouken@173
   749
#endif
slouken@173
   750
#ifdef MP3_MUSIC
slouken@173
   751
	    case MUS_MP3:
slouken@176
   752
		if ( position > 0.0 ) {
slouken@173
   753
			SMPEG_skip(music_playing->data.mp3, position);
slouken@179
   754
		} else {
slouken@179
   755
			SMPEG_rewind(music_playing->data.mp3);
slouken@179
   756
			SMPEG_play(music_playing->data.mp3);
slouken@173
   757
		}
slouken@173
   758
		break;
slouken@173
   759
#endif
slouken@173
   760
	    default:
slouken@173
   761
		/* TODO: Implement this for other music backends */
slouken@173
   762
		retval = -1;
slouken@173
   763
		break;
megastep@10
   764
	}
slouken@173
   765
	return(retval);
megastep@4
   766
}
slouken@155
   767
int Mix_SetMusicPosition(double position)
slouken@154
   768
{
slouken@173
   769
	int retval;
slouken@173
   770
slouken@173
   771
	SDL_LockAudio();
slouken@173
   772
	if ( music_playing ) {
slouken@173
   773
		retval = music_internal_position(position);
slouken@173
   774
		if ( retval < 0 ) {
slouken@173
   775
			Mix_SetError("Position not implemented for music type");
slouken@154
   776
		}
slouken@173
   777
	} else {
slouken@173
   778
		Mix_SetError("Music isn't playing");
slouken@173
   779
		retval = -1;
slouken@154
   780
	}
slouken@173
   781
	SDL_UnlockAudio();
slouken@154
   782
slouken@173
   783
	return(retval);
slouken@154
   784
}
slouken@154
   785
slouken@0
   786
/* Set the music volume */
slouken@173
   787
static void music_internal_volume(int volume)
slouken@173
   788
{
slouken@173
   789
	switch (music_playing->type) {
slouken@173
   790
#ifdef CMD_MUSIC
slouken@173
   791
	    case MUS_CMD:
slouken@173
   792
		MusicCMD_SetVolume(volume);
slouken@173
   793
		break;
slouken@173
   794
#endif
slouken@173
   795
#ifdef WAV_MUSIC
slouken@173
   796
	    case MUS_WAV:
slouken@173
   797
		WAVStream_SetVolume(volume);
slouken@173
   798
		break;
slouken@173
   799
#endif
slouken@173
   800
#ifdef MOD_MUSIC
slouken@173
   801
	    case MUS_MOD:
slouken@173
   802
		Player_SetVolume((SWORD)volume);
slouken@173
   803
		break;
slouken@173
   804
#endif
slouken@173
   805
#ifdef MID_MUSIC
slouken@173
   806
	    case MUS_MID:
slouken@173
   807
#ifdef USE_NATIVE_MIDI
slouken@173
   808
		if ( native_midi_ok ) {
slouken@173
   809
			native_midi_setvolume(volume);
slouken@173
   810
		} MIDI_ELSE
slouken@173
   811
#endif
slouken@173
   812
#ifdef USE_TIMIDITY_MIDI
slouken@173
   813
		if ( timidity_ok ) {
slouken@173
   814
			Timidity_SetVolume(volume);
slouken@173
   815
		}
slouken@173
   816
#endif
slouken@173
   817
		break;
slouken@173
   818
#endif
slouken@173
   819
#ifdef OGG_MUSIC
slouken@173
   820
	    case MUS_OGG:
slouken@173
   821
		OGG_setvolume(music_playing->data.ogg, volume);
slouken@173
   822
		break;
slouken@173
   823
#endif
slouken@173
   824
#ifdef MP3_MUSIC
slouken@173
   825
	    case MUS_MP3:
slouken@173
   826
		SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
slouken@173
   827
		break;
slouken@173
   828
#endif
slouken@173
   829
	    default:
slouken@173
   830
		/* Unknown music type?? */
slouken@173
   831
		break;
slouken@173
   832
	}
slouken@173
   833
}
slouken@0
   834
int Mix_VolumeMusic(int volume)
slouken@0
   835
{
slouken@0
   836
	int prev_volume;
slouken@0
   837
slouken@0
   838
	prev_volume = music_volume;
megastep@4
   839
	if ( volume < 0 ) {
slouken@163
   840
		return prev_volume;
megastep@4
   841
	}
megastep@4
   842
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
   843
		volume = SDL_MIX_MAXVOLUME;
megastep@4
   844
	}
megastep@4
   845
	music_volume = volume;
slouken@173
   846
	SDL_LockAudio();
slouken@173
   847
	if ( music_playing ) {
slouken@173
   848
		music_internal_volume(music_volume);
slouken@0
   849
	}
slouken@173
   850
	SDL_UnlockAudio();
slouken@0
   851
	return(prev_volume);
slouken@0
   852
}
slouken@0
   853
slouken@173
   854
/* Halt playing of music */
slouken@173
   855
static void music_internal_halt(void)
megastep@7
   856
{
megastep@7
   857
	switch (music_playing->type) {
megastep@7
   858
#ifdef CMD_MUSIC
slouken@173
   859
	    case MUS_CMD:
megastep@7
   860
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
   861
		break;
megastep@7
   862
#endif
megastep@7
   863
#ifdef WAV_MUSIC
slouken@173
   864
	    case MUS_WAV:
megastep@7
   865
		WAVStream_Stop();
megastep@7
   866
		break;
megastep@7
   867
#endif
megastep@7
   868
#ifdef MOD_MUSIC
slouken@173
   869
	    case MUS_MOD:
megastep@7
   870
		Player_Stop();
megastep@7
   871
		break;
megastep@7
   872
#endif
megastep@7
   873
#ifdef MID_MUSIC
slouken@173
   874
	    case MUS_MID:
slouken@98
   875
#ifdef USE_NATIVE_MIDI
slouken@98
   876
		if ( native_midi_ok ) {
slouken@98
   877
			native_midi_stop();
slouken@106
   878
		} MIDI_ELSE
slouken@98
   879
#endif
slouken@106
   880
#ifdef USE_TIMIDITY_MIDI
slouken@98
   881
		if ( timidity_ok ) {
slouken@98
   882
			Timidity_Stop();
slouken@98
   883
		}
slouken@106
   884
#endif
megastep@7
   885
		break;
megastep@7
   886
#endif
slouken@63
   887
#ifdef OGG_MUSIC
slouken@173
   888
	    case MUS_OGG:
slouken@63
   889
		OGG_stop(music_playing->data.ogg);
slouken@63
   890
		break;
slouken@63
   891
#endif
megastep@7
   892
#ifdef MP3_MUSIC
slouken@173
   893
	    case MUS_MP3:
megastep@7
   894
		SMPEG_stop(music_playing->data.mp3);
megastep@7
   895
		break;
megastep@7
   896
#endif
slouken@173
   897
	    default:
megastep@7
   898
		/* Unknown music type?? */
megastep@7
   899
		return;
megastep@7
   900
	}
megastep@7
   901
	music_playing->fading = MIX_NO_FADING;
slouken@17
   902
	music_playing = NULL;
megastep@7
   903
}
slouken@0
   904
int Mix_HaltMusic(void)
slouken@0
   905
{
slouken@173
   906
	SDL_LockAudio();
slouken@173
   907
	if ( music_playing ) {
slouken@173
   908
		music_internal_halt();
slouken@0
   909
	}
slouken@173
   910
	SDL_UnlockAudio();
slouken@173
   911
slouken@0
   912
	return(0);
slouken@0
   913
}
slouken@0
   914
megastep@4
   915
/* Progressively stop the music */
megastep@4
   916
int Mix_FadeOutMusic(int ms)
megastep@4
   917
{
slouken@173
   918
	int retval = 0;
slouken@173
   919
slouken@173
   920
	SDL_LockAudio();
slouken@173
   921
	if ( music_playing && (music_playing->fading == MIX_NO_FADING) ) {
slouken@173
   922
		music_playing->fading = MIX_FADING_OUT;
slouken@173
   923
		music_playing->fade_step = 0;
slouken@173
   924
		music_playing->fade_steps = ms/ms_per_step;
slouken@173
   925
		retval = 1;
megastep@4
   926
	}
slouken@173
   927
	SDL_UnlockAudio();
slouken@173
   928
slouken@173
   929
	return(retval);
megastep@4
   930
}
megastep@4
   931
megastep@4
   932
Mix_Fading Mix_FadingMusic(void)
megastep@4
   933
{
slouken@173
   934
	Mix_Fading fading = MIX_NO_FADING;
slouken@173
   935
slouken@173
   936
	SDL_LockAudio();
slouken@173
   937
	if ( music_playing ) {
slouken@173
   938
		fading = music_playing->fading;
slouken@173
   939
	}
slouken@173
   940
	SDL_UnlockAudio();
slouken@173
   941
slouken@173
   942
	return(fading);
megastep@4
   943
}
megastep@4
   944
slouken@0
   945
/* Pause/Resume the music stream */
slouken@0
   946
void Mix_PauseMusic(void)
slouken@0
   947
{
slouken@173
   948
	music_active = 0;
slouken@0
   949
}
megastep@4
   950
slouken@0
   951
void Mix_ResumeMusic(void)
slouken@0
   952
{
slouken@173
   953
	music_active = 1;
slouken@0
   954
}
slouken@0
   955
slouken@0
   956
void Mix_RewindMusic(void)
slouken@0
   957
{
slouken@173
   958
	Mix_SetMusicPosition(0.0);
slouken@0
   959
}
slouken@0
   960
megastep@13
   961
int Mix_PausedMusic(void)
megastep@13
   962
{
slouken@17
   963
	return (music_active == 0);
megastep@13
   964
}
megastep@13
   965
slouken@0
   966
/* Check the status of the music */
slouken@173
   967
static int music_internal_playing()
slouken@173
   968
{
slouken@173
   969
	int playing = 1;
slouken@173
   970
	switch (music_playing->type) {
slouken@173
   971
#ifdef CMD_MUSIC
slouken@173
   972
	    case MUS_CMD:
slouken@173
   973
		if (!MusicCMD_Active(music_playing->data.cmd)) {
slouken@173
   974
			playing = 0;
slouken@173
   975
		}
slouken@173
   976
		break;
slouken@173
   977
#endif
slouken@173
   978
#ifdef WAV_MUSIC
slouken@173
   979
	    case MUS_WAV:
slouken@173
   980
		if ( ! WAVStream_Active() ) {
slouken@173
   981
			playing = 0;
slouken@173
   982
		}
slouken@173
   983
		break;
slouken@173
   984
#endif
slouken@173
   985
#ifdef MOD_MUSIC
slouken@173
   986
	    case MUS_MOD:
slouken@173
   987
		if ( ! Player_Active() ) {
slouken@173
   988
			playing = 0;
slouken@173
   989
		}
slouken@173
   990
		break;
slouken@173
   991
#endif
slouken@173
   992
#ifdef MID_MUSIC
slouken@173
   993
	    case MUS_MID:
slouken@173
   994
#ifdef USE_NATIVE_MIDI
slouken@173
   995
		if ( native_midi_ok ) {
slouken@173
   996
			if ( ! native_midi_active() )
slouken@173
   997
				playing = 0;
slouken@173
   998
		} MIDI_ELSE
slouken@173
   999
#endif
slouken@173
  1000
#ifdef USE_TIMIDITY_MIDI
slouken@173
  1001
		if ( timidity_ok ) {
slouken@173
  1002
			if ( ! Timidity_Active() )
slouken@173
  1003
				playing = 0;
slouken@173
  1004
		}
slouken@173
  1005
#endif
slouken@173
  1006
		break;
slouken@173
  1007
#endif
slouken@173
  1008
#ifdef OGG_MUSIC
slouken@173
  1009
	    case MUS_OGG:
slouken@173
  1010
		if ( ! OGG_playing(music_playing->data.ogg) ) {
slouken@173
  1011
			playing = 0;
slouken@173
  1012
		}
slouken@173
  1013
		break;
slouken@173
  1014
#endif
slouken@173
  1015
#ifdef MP3_MUSIC
slouken@173
  1016
	    case MUS_MP3:
slouken@173
  1017
		if ( SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
slouken@173
  1018
			playing = 0;
slouken@173
  1019
		break;
slouken@173
  1020
#endif
slouken@173
  1021
	    default:
slouken@173
  1022
		playing = 0;
slouken@173
  1023
		break;
slouken@173
  1024
	}
slouken@173
  1025
	return(playing);
slouken@173
  1026
}
megastep@13
  1027
int Mix_PlayingMusic(void)
slouken@0
  1028
{
slouken@173
  1029
	int playing = 0;
slouken@173
  1030
slouken@173
  1031
	SDL_LockAudio();
slouken@173
  1032
	if ( music_playing ) {
slouken@173
  1033
		playing = music_internal_playing();
slouken@0
  1034
	}
slouken@173
  1035
	SDL_UnlockAudio();
slouken@173
  1036
slouken@173
  1037
	return(playing);
slouken@0
  1038
}
slouken@0
  1039
slouken@0
  1040
/* Set the external music playback command */
slouken@0
  1041
int Mix_SetMusicCMD(const char *command)
slouken@0
  1042
{
slouken@0
  1043
	Mix_HaltMusic();
slouken@0
  1044
	if ( music_cmd ) {
slouken@0
  1045
		free(music_cmd);
slouken@0
  1046
		music_cmd = NULL;
slouken@0
  1047
	}
slouken@0
  1048
	if ( command ) {
slouken@0
  1049
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
  1050
		if ( music_cmd == NULL ) {
slouken@0
  1051
			return(-1);
slouken@0
  1052
		}
slouken@0
  1053
		strcpy(music_cmd, command);
slouken@0
  1054
	}
slouken@0
  1055
	return(0);
slouken@0
  1056
}
slouken@0
  1057
slouken@154
  1058
int Mix_SetSynchroValue(int i)
slouken@154
  1059
{
slouken@154
  1060
	if ( music_playing && ! music_stopped ) {
slouken@154
  1061
		switch (music_playing->type) {
slouken@154
  1062
#ifdef MOD_MUSIC
slouken@173
  1063
		    case MUS_MOD:
slouken@173
  1064
			if ( ! Player_Active() ) {
slouken@173
  1065
				return(-1);
slouken@173
  1066
			}
slouken@173
  1067
			Player_SetSynchroValue(i);
slouken@173
  1068
			return 0;
slouken@173
  1069
			break;
slouken@154
  1070
#endif
slouken@173
  1071
		    default:
slouken@173
  1072
			return(-1);
slouken@173
  1073
			break;
slouken@154
  1074
		}
slouken@154
  1075
		return(-1);
slouken@154
  1076
	}
slouken@154
  1077
	return(-1);
slouken@154
  1078
}
slouken@154
  1079
slouken@154
  1080
int Mix_GetSynchroValue(void)
slouken@154
  1081
{
slouken@154
  1082
	if ( music_playing && ! music_stopped ) {
slouken@154
  1083
		switch (music_playing->type) {
slouken@154
  1084
#ifdef MOD_MUSIC
slouken@173
  1085
		    case MUS_MOD:
slouken@173
  1086
			if ( ! Player_Active() ) {
slouken@173
  1087
				return(-1);
slouken@173
  1088
			}
slouken@173
  1089
			return Player_GetSynchroValue();
slouken@173
  1090
			break;
slouken@154
  1091
#endif
slouken@173
  1092
		    default:
slouken@173
  1093
			return(-1);
slouken@173
  1094
			break;
slouken@154
  1095
		}
slouken@154
  1096
		return(-1);
slouken@154
  1097
	}
slouken@154
  1098
	return(-1);
slouken@154
  1099
}
slouken@154
  1100
slouken@154
  1101
slouken@0
  1102
/* Uninitialize the music players */
slouken@0
  1103
void close_music(void)
slouken@0
  1104
{
slouken@0
  1105
	Mix_HaltMusic();
slouken@0
  1106
#ifdef CMD_MUSIC
slouken@0
  1107
	Mix_SetMusicCMD(NULL);
slouken@0
  1108
#endif
slouken@0
  1109
#ifdef MOD_MUSIC
slouken@0
  1110
	MikMod_Exit();
slouken@101
  1111
	MikMod_UnregisterAllLoaders();
slouken@101
  1112
	MikMod_UnregisterAllDrivers();
slouken@0
  1113
#endif
slouken@0
  1114
}
slouken@0
  1115