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